获取矩阵两串对角线数字之和的差值

2024-09-05 10:52:50 浏览数 (1)

代码语言:txt复制
  /**
     * 获取矩阵两串对角线数字之和的差值
     *
     * 1 2 3
     * 4 5 6
     * 7 8 9
     *
     * 1 5 9=15;
     * 3 5 7=15;
     * 15-15=0;
     * @param arr
     * @return
     */
    public static Integer diagonalDiferrence(ArrayList<ArrayList<Integer>> arr){
        if(arr==null){
            return null;
        }
        if (arr.isEmpty()){
            return null;
        }
        for (ArrayList<Integer> integers : arr) {
            for (Integer integer : integers) {
                if (integer<-100 || integer > 100){
                    return null;
                }
            }
        }

        int count=0;
        ArrayList<Integer> integerArrayList = new ArrayList<>();
        for (int i = 0; i < arr.size(); i  ) {
            ArrayList<Integer> integerList = arr.get(i);
            for (int i1 = 0; i1 < integerList.size(); i1  ) {
                if(count==i1){
                    Integer i2 = integerList.get(i1);
                    integerArrayList.add(i2);
                    break;
                }else if (integerArrayList.size()==count){
                    if (i1==(count-1)){
                        Integer i2 = integerList.get(count);
                        integerArrayList.add(i2);
                        break;
                    }else {
                        continue;
                    }
                }else {
                    break;
                }
            }
            count  ;
        }

        System.out.println(integerArrayList);

        int count1=0;
        ArrayList<Integer> integerArrayList1 = new ArrayList<>();
        for (int i = 0; i < arr.size(); i  ) {
            List<Integer> integerList = arr.get(i);
            int reverseIndex=integerList.size()-1;
            for (int i1 = integerList.size()-1; i1 >=0 ; i1--) {
                if(count1==(integerList.size()-i1-1)){
                    Integer i2 = integerList.get(i1);
                    integerArrayList1.add(i2);
                    break;
                }else if (integerArrayList1.size()==count1){
                    if (i1==(integerList.size()-count1-1)){
                        Integer i2 = integerList.get(reverseIndex--);
                        integerArrayList1.add(i2);
                        break;
                    }else {
                        continue;
                    }
                }else {
                    break;
                }
            }
            count1  ;
        }

        System.out.println(integerArrayList1);

        int sum=0;
        for (Integer integer : integerArrayList) {
            sum =integer;
        }
        int sum1=0;
        for (Integer integer : integerArrayList1) {
            sum1 =integer;
        }

        int minersVal=sum-sum1;
        if (minersVal<0){
            int i = sum1 - sum;
            return i;
        }

        return minersVal;
    }

0 人点赞