04数组矩阵
顺时针打印二维数组
0所在行列清零
z字形打印二维数组
找出边界为1的最大子方针
返回子数组最大累加和
子矩阵最大累加和
矩阵运算
顺时针打印二维数组
int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};int r = 0;int c = 0;int lowr = 0,highr=arr.length-1;int lowc = 0,highc=arr[0].length-1;while(lowr<highr||lowc<highc){while(c<highc){System.out.println(arr[r][c++]);}while(r<highr){System.out.println(arr[r++][c]);}while(c>lowc){System.out.println(arr[r][c--]);}while(r>lowr){System.out.println(arr[r--][c]);}r++;c++;lowr++;lowc++;highr--;highc--;
0所在行列清零
int[][] arr = {{1,2,0,0},{5,6,7,8},{9,10,11,12},{13,14,15,16}};int m = arr.length;int n = arr[0].length;int[] row = new int[m];int[] col = new int[n];for(int i=0;i<m;i++){for(int j=0;j<n;j++){if(arr[i][j]==0){row[i]=1;col[j]=1;}}}for(int i=0;i<m;i++){if(row[i]==1){for(int j=0;j<n;j++){arr[i][j]=0;}}}for(int j=0;j<n;j++){if(col[j]==1){for(int i=0;i<m;i++){arr[i][j]=0;}}}
z字形打印二维数组
int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};int m = arr.length;int n = arr[0].length;int r=0,c=0;while(true){if(c==n-1){System.out.println(arr[r++][c]);while(c>0&&r<m-1&&c<n-1){System.out.println(arr[r++][c--]);}}else if(r==0){System.out.println(arr[r][c++]);while(c>0&&r<m-1&&c<n-1){System.out.println(arr[r++][c--]);}}if(r==m-1){System.out.println(arr[r][c++]);while(r>0&&r<m-1&&c<n-1){System.out.println(arr[r--][c++]);}}else if(c==0){System.out.println(arr[r++][c]);while(r>0&&r<m-1&&c<n-1){System.out.println(arr[r--][c++]);}}if(r>=m-1&&c>=n-1)break;}
找出边界为1的最大子方阵
int[][] arr = {{0,1,1,1,1,1},{0,1,0,0,1,1},{0,1,0,0,1,1},{0,1,1,1,1,1},{0,1,0,1,1,1}};int m = arr.length;int n = arr[0].length;int[][][] f = new int[m][n][3];f[0][0][0] = f[0][0][1] = arr[0][0];for(int i=1;i<m;i++){f[i][0][0] = arr[i][0];if(arr[i][0]==1){f[i][0][1] = f[i-1][0][1]+1;}}for(int j=1;j<n;j++){f[0][j][0] = arr[0][j];if(arr[0][j]==1){f[0][j][1] = f[0][j-1][1]+1;}}for(int i=1;i<m;i++){for(int j=1;j<n;j++){if(arr[i][j]==1){f[i][j][0] = f[i][j-1][0]+1;f[i][j][1] = f[i-1][j][1]+1;f[i][j][2] = Math.min(f[i][j][0],f[i][j][1]);}}}int max = 0;for(int i=0;i<m;i++){for(int j=0;j<n;j++){max = Math.max(f[i][j][2],max);}}System.out.println(max);
返回子数组最大累加和
int[] arr = {1,-2,3,5,-2,6,-1};int l=0,r=0;int sum = 0;int max = sum;while(r<arr.length){if(sum+arr[r]<arr[r]){l = r;sum = 0;}sum+=arr[r];max = Math.max(max,sum);r++;}System.out.println(max);
子矩阵最大累加和
矩阵运算