本文主要是介绍力扣周赛387,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一题
代码
package Competition.The387Competitioin;public class Demo1 {public static void main(String[] args) {}public int[] resultArray(int[] nums) {int ans[]=new int[nums.length];int arr1[]=new int[nums.length];int arr2[]=new int[nums.length];if(nums.length<=2)return nums;int i=0;int j=0;int k=0;arr1[i++]=nums[k++];System.out.println(arr1[i-1]);arr2[j++]=nums[k++];System.out.println(arr1[j-1]);while (k<nums.length){if(arr1[i-1]>arr2[j-1]){arr1[i++]=nums[k++];System.out.println(arr1[i-1]);}else{arr2[j++]=nums[k++];}}int index_i=0;for(int c=0;c<i;c++){ans[index_i++]=arr1[c];}for(int c=0;c<j;c++){ans[index_i++]=arr2[c];}return ans;}
}
评价
敲代码的时候,我在这里放了一个小错误,
if(arr1[i-1]>arr2[j-1]){这个,我写成了arr1[i-1]>arr1[j-1],手写的太快了,debug了,所以写的不快,我真该死唉
第二题
代码
package Competition.The387Competitioin;public class Demo2 {public static void main(String[] args) {}public int countSubmatrices(int[][] grid, int k) {int m = grid.length;int n = grid[0].length;int ans = 0;int[][] prefixSum = new int[m + 1][n + 1];for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {prefixSum[i][j] = prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1] + grid[i - 1][j - 1];if(prefixSum[i][j]<=k)ans++;}System.out.println();}return ans;}}
评价:这个感觉很好,直接无脑写
第三题
代码
package Competition.The387Competitioin;public class Demo3 {public static void main(String[] args) {}public int minimumOperationsToWriteY(int[][] grid) {int n = grid.length;int m = grid[0].length;int ynumber[]=new int[3];int otherNumber[]=new int[3];int sumy=0;int sumOther=0;for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(i<n/2&&i==j){ynumber[grid[i][j]]++;sumy++;continue;}if(i< n/2&&j==n-i-1){ynumber[grid[i][j]]++;sumy++;continue;}if(i>n/2&&j==n/2){ynumber[grid[i][j]]++;sumy++;continue;}otherNumber[grid[i][j]]++;sumOther++;}}sumOther--;otherNumber[grid[n/2][n/2]]--;sumy++;ynumber[grid[n/2][n/2]]++;System.out.println("sumy:"+sumy);System.out.println("sunOther:"+sumOther);for(int i=0;i<3;i++){System.out.println(i);System.out.println("y:"+ynumber[i]);System.out.println("o:"+otherNumber[i]);}int ans=Integer.MAX_VALUE;for(int i=0;i<3;i++){for(int j=0;j<3;j++){if(i==j)continue;System.out.println(ans);ans=Math.min(ans,sumy-ynumber[i]+sumOther-otherNumber[j]);}}return ans;}
}
评价
还是第一题的毛病,首先题目写的太快了,刚开始没有想好就动代码,本来写成一段一段的,后面重新整个放在一起了,接下来就是发现我grid[n/2][n/2]多次利用了,我没有发现,导致我值不对账,最后通过调试发现了bug
总评
好久没有打周赛了,最近无聊的时候会力扣复检,都在背八股文,加油!,剑指和top100同时进行中ing
这篇关于力扣周赛387的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!