• 算法----矩阵


    -1 -1 -1

    -1   1  1

    -1   1  1 

    最大子矩阵和是

    1 1

    1 1

    class Main{
        public static void main(String[] args) throws Exception{
            Scanner in = new Scanner(System.in);
    //        int[][] num = new int[3][3];
    //        int a = in.nextInt();
    //        int b = in.nextInt();
    //        num = new int[a][b];
    //        for (int i = 0; i < a; i++) {
    //            for (int j = 0; j < b; j++) {
    //                num[i][j]=in.nextInt();
    //            }
    //        }
    
            int[][] num  =  {{-1, -1, -1},
                    {-1, 1, 1},
                    {-1,  1, 1}};
            if(num==null||num.length==0||num[0].length==0){
                System.out.println(0);
                return;
            }
            int max = Integer.MIN_VALUE;
            for(int i=0;i<3;i++){ //一共有多少列
                int[] tmp = new int[num[0].length];
                for(int j=i;j<3;j++){ //一共有多少列
                    int cur = 0;
                    for(int k=0;k<3;k++){ //每一列有多少条数据
                        tmp[k] += num[j][k];  //
                        cur += tmp[k];
                        max = Math.max(cur,max);
                        cur = cur <= 0 ? 0:cur;//舍去
                    }
                }
            }
            System.out.println(max);
        }
    }
    
  • 相关阅读:
    数组_leetcode283
    数组_leetcode438
    数组_leetcode215
    数组_leetcode167
    数组_leetcode209
    数组_leetcode88
    数组_leetcode80
    数组_leetcode76
    数组_leetcode75
    数组_leetcode27
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/14450996.html
Copyright © 2020-2023  润新知