查找缺失的正整数: 遍历调整后的数组,若某个位置的值不等于其索引加1,则说明 i+1 是最小的缺失正整数。若所有位置均满足 nums[i] = i+1,则说明数组包含1到 len 的所有正整数,此时最小缺失值为 len+1。
Java代码:
publicclassSolution{publicintfirstMissingPositive(int[] nums){int len = nums.length;for(int i =0; i < len; i++){while(nums[i]>0&& nums[i]<= len && nums[nums[i]-1]!= nums[i]){swap(nums, nums[i]-1, i);}}for(int i =0; i < len; i++){if(nums[i]!= i +1){return i +1;}}return len +1;}privatevoidswap(int[] nums,int index1,int index2){int temp = nums[index1];
nums[index1]= nums[index2];
nums[index2]= temp;}}
classSolution{publicvoidsetZeroes(int[][] matrix){int m = matrix.length, n = matrix[0].length;boolean[] row =newboolean[m];boolean[] col =newboolean[n];for(int i =0; i < m; i++){for(int j =0; j < n; j++){if(matrix[i][j]==0){
row[i]= col[j]=true;}}}for(int i =0; i < m; i++){for(int j =0; j < n; j++){if(row[i]|| col[j]){
matrix[i][j]=0;}}}}}