Easy => 1252. Cells with Odd Values in a Matrix

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
    public int oddCells(int n, int m, int[][] indices) {
        boolean[] oddRows = new boolean[n];
        boolean[] oddCols = new boolean[m];
        
        for(int[] item : indices) {
            // 遍历 indices 获取每一列每一行的出现次数是否为奇数
            // 异或: 相同为0 不同为1 
            oddRows[item[0]] ^= true;
            oddCols[item[1]] ^= true;
        }
        
        int oddCnt = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                // 行列出现 奇数次 + 偶数次 才能是产生奇数
                oddCnt += oddRows[i] != oddCols[j] ? 1 : 0;
            }
        }
        // Time Complexity: O(m*n + indices.length)
        return oddCnt;
    }

EASY =>26. Remove Duplicates from Sorted Array

快慢指针的运用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public int removeDuplicates(int[] nums) {
    int slow = 0;
    int fast = 1;
    while(fast < nums.length) {
        if(nums[slow] != nums[fast]) {
            nums[++slow] = nums[fast++];
        } else {
            fast++;
        }
    }
    return slow + 1;
}

EASY => 27. Remove Element

快慢指针的运用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public int removeElement(int[] nums, int val) {
    int curr = 0;
    int p = 0;
    while(p < nums.length) {
        if(nums[p] == val ) {
            p++;
        } else {
            nums[curr++] = nums[p++];
        }
    }
    return curr;
}