1 /** 2 * 获取方形矩阵两串对角线数字之和的差值 3 * 4 * 1 2 3 5 * 4 5 6 6 * 7 8 9 7 * 8 * 1+5+9=15; 9 * 3+5+7=15; 10 * 15-15=0; 11 * @param arr 12 * @return 13 */ 14 public static Integer diagonalDiferrence(ArrayList<ArrayList<Integer>> arr){ 15 if(arr==null){ 16 return null; 17 } 18 if (arr.isEmpty()){ 19 return null; 20 } 21 for (ArrayList<Integer> integers : arr) { 22 for (Integer integer : integers) { 23 if (integer<-100 || integer > 100){ 24 return null; 25 } 26 } 27 } 28 29 int count=0; 30 ArrayList<Integer> integerArrayList = new ArrayList<>(); 31 for (int i = 0; i < arr.size(); i++) { 32 ArrayList<Integer> integerList = arr.get(i); 33 for (int i1 = 0; i1 < integerList.size(); i1++) { 34 if(count==i1){ 35 Integer i2 = integerList.get(i1); 36 integerArrayList.add(i2); 37 break; 38 }else if (integerArrayList.size()==count){ 39 if (i1==(count-1)){ 40 Integer i2 = integerList.get(count); 41 integerArrayList.add(i2); 42 break; 43 }else { 44 continue; 45 } 46 }else { 47 break; 48 } 49 } 50 count++; 51 } 52 53 System.out.println(integerArrayList); 54 55 int count1=0; 56 ArrayList<Integer> integerArrayList1 = new ArrayList<>(); 57 for (int i = 0; i < arr.size(); i++) { 58 List<Integer> integerList = arr.get(i); 59 int reverseIndex=integerList.size()-1; 60 for (int i1 = integerList.size()-1; i1 >=0 ; i1--) { 61 if(count1==(integerList.size()-i1-1)){ 62 Integer i2 = integerList.get(i1); 63 integerArrayList1.add(i2); 64 break; 65 }else if (integerArrayList1.size()==count1){ 66 if (i1==(integerList.size()-count1-1)){ 67 Integer i2 = integerList.get(reverseIndex--); 68 integerArrayList1.add(i2); 69 break; 70 }else { 71 continue; 72 } 73 }else { 74 break; 75 } 76 } 77 count1++; 78 } 79 80 System.out.println(integerArrayList1); 81 82 int sum=0; 83 for (Integer integer : integerArrayList) { 84 sum+=integer; 85 } 86 int sum1=0; 87 for (Integer integer : integerArrayList1) { 88 sum1+=integer; 89 } 90 91 int minersVal=sum-sum1; 92 if (minersVal<0){ 93 int i = sum1 - sum; 94 return i; 95 } 96 97 return minersVal; 98 }
标签:arr,i1,integerList,int,差值,对角线,Integer,两串,size From: https://www.cnblogs.com/liaowanzhong/p/18398014