xiaoheiAh's blog
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;
}
快慢指针的运用
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;
}
快慢指针的运用
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;
}