1. Mobile zero

The number of non-zero elements (nums[index]) is the number of non-zero elements (nums[index]). And then you just fill in the extra zeros

class Solution {
    public void moveZeroes(int[] nums) {
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] ! =0) { nums[index++] = nums[i]; }}while (index < nums.length) {
            nums[index++] = 0; }}}Copy the code
  1. The word law

Use Hashmap to store the corresponding patterns between words and characters. Key is the character in pattern and value is the corresponding word. If the current word appears in the Hashmap but the corresponding character in pattern does not appear, there are two different characters corresponding to the same word. Return false. If the current word does not appear in the Hash table and the corresponding character in pattern does not appear, the corresponding relationship is added to the Hashmap. If a character in the current pattern is present, return false if the value is different from the current word

class Solution {
    public boolean wordPattern(String pattern, String s) {
        String a[] = s.split("");
        HashMap<Character, String> map = new HashMap<>();
        if(a.length! =pattern.length()){return false;
        for (int i = 0; i < pattern.length(); i++) {
            if(! map.containsKey(pattern.charAt(i)) && map.containsValue(a[i])) {return false;
            } else if(! map.containsKey(pattern.charAt(i))) { map.put(pattern.charAt(i), a[i]); }else if (map.containsKey(pattern.charAt(i))) {
                if(! map.get(pattern.charAt(i)).equals(a[i])) {return false; }}}return true; }}Copy the code