• leetcode简单题目两道(3)


        本来打算写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类比较好用,此题的思路在于对待唯一,就是一个字符对应唯一的字符串,且一个串对应唯一的字符。
  • 相关阅读:
    在其他博客里看到的比较好的map用法,进行储存啦啦~ x
    codevs 2597 团伙x
    codevs 1009 产生数x
    格子游戏x(并查集)
    codevs 5929 亲戚x
    [HDOJ2389]Rain on your Parade(二分图最大匹配,HK算法)
    [HDOJ2819]Swap(二分图最大匹配, 匈牙利算法)
    [HDOJ1281]棋盘游戏(二分图最大匹配,匈牙利算法)
    [HDOJ1083]Courses(二分图最大匹配,匈牙利算法)
    [HDOJ2444]The Accomodation of Students(二分图染色判定,最大匹配,匈牙利算法)
  • 原文地址:https://www.cnblogs.com/hawk-whu/p/6746361.html
Copyright © 2020-2023  润新知