leetcode简单题目两道(3)

本文介绍了两种实用的算法:一种用于将数组中所有零元素移动到末尾同时保持非零元素相对顺序的方法;另一种用于判断字符串是否遵循给定模式的算法。提供了详细的C++与Java实现代码。

    本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了。

Problem

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example:

Given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

You must do this in-place without making a copy of the array. Minimize the total number of operations.

Code:

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        if (nums.size() == 0 || nums.size() == 1) {
            return;
        }
        int i = 0, j,k;
        while(i < nums.size()) {
            while (nums[i] != 0) {
                i++;
            }
            if (i < nums.size()) {
                j = i + 1;
                while (j < nums.size() && nums[j] == 0) {
                    j++;
                }
                if (j < nums.size()) {
                    k = nums[i];
                    nums[i] = nums[j];
                    nums[j] = k;
                }
                i++;
            }
        }
    }
};
说明:

用两个下标,i保存碰到的0的位置,j表示从i之后第一个非0,交换之后i++,j继续。
Problem:

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples:

pattern = "abba", str = "dog cat cat dog" should return true. pattern = "abba", str = "dog cat cat fish" should return false. pattern = "aaaa", str = "dog cat cat dog" should return false. pattern = "abba", str = "dog dog dog dog" should return false.

Notes:

You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

Code:

public class Solution {
    public boolean wordPattern(String pattern, String str) {
        if (pattern == null && str == null) {
            return true;
        }
        if (pattern == null) {
            return false;
        }
        if (str == null) {
            return false;
        }
        String[] array = str.split(" ");
        if (pattern.length() != array.length) {
            return false;
        }
        Map<String, String> map = new HashMap<String, String>();
        Set<String> value = new HashSet<String>();
        for (int i = 0; i < pattern.length(); i++) {
            String tmp = pattern.substring(i, i + 1);
            if (map.containsKey(tmp)) {
                if (array[i].compareTo(map.get(tmp)) != 0) {
                    return false;
                }
            } else {
                if (value.contains(array[i])) {
                    return false;
                }
                map.put(tmp, array[i]);
                value.add(array[i]);
            }
        }
        return true;
    }
}
说明:

发现字符串的操作还是java的String类比较好用,此题的思路在于对待唯一,就是一个字符对应唯一的字符串,且一个串对应唯一的字符。

 

转载于:https://www.cnblogs.com/hawk-whu/p/6746361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值