LeetCodeTest算法测试 传递一个数组和一个特定的目标整型数字,返回的两个数组元素相加之后和目标整型数字相等的数组的下标组合集合。

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 
  4 public class LeetCodeTest {
  5     public static void main(String[] args) {
  6 
  7         int[] intArr=new int[]{2,7,11,15};
  8         List<CustomerIntIndex> customerIntIndexList=getArrIndex(intArr,9);
  9         System.out.println(customerIntIndexList);
 10     }
 11 
 12     public static List<CustomerIntIndex> getArrIndex(int[] intArr, int targetNum){
 13         int length = intArr.length;
 14         int resultVal=1;
 15         for (int i = 0; i < 4; i++) {
 16             resultVal*=10;
 17         }
 18 
 19         if(length<2 || length>resultVal){
 20             return null;
 21         }
 22 
 23         int resultVal1=1;
 24         for (int i = 0; i < 9; i++) {
 25             resultVal1*=10;
 26         }
 27         resultVal1=resultVal1*(-1);
 28         int resultVal2=1;
 29         for (int i = 0; i < 9; i++) {
 30             resultVal2*=10;
 31         }
 32         if(targetNum<resultVal1 || targetNum>resultVal2){
 33             return null;
 34         }
 35 
 36         for (int i = 0; i < intArr.length; i++) {
 37             int intElement=intArr[i];
 38             if(intElement<resultVal1 || intElement>resultVal2){
 39                 return null;
 40             }
 41         }
 42 
 43         ArrayList<CustomerIntIndex> arrayList = new ArrayList<CustomerIntIndex>();
 44         for (int i = 0; i < intArr.length-1; i++) {
 45             int intArrayEle = intArr[i];
 46             CustomerIntEle customerIntEle = new CustomerIntEle();
 47             customerIntEle.setIdIndex(i);
 48             customerIntEle.setIntArrEle(intArrayEle);
 49 
 50             for (int j = i+1; j < intArr.length; j++) {
 51                 int intArrayEle2 = intArr[j];
 52                 CustomerIntEle customerIntEle2 = new CustomerIntEle();
 53                 customerIntEle2.setIdIndex(j);
 54                 customerIntEle2.setIntArrEle(intArrayEle2);
 55 
 56                 int tempSum=intArrayEle+intArrayEle2;
 57                 if(tempSum==targetNum){
 58                     CustomerIntIndex customerIntIndex = new CustomerIntIndex();
 59                     customerIntIndex.setIdIndex1(i);
 60                     customerIntIndex.setIdIndex2(j);
 61                     arrayList.add(customerIntIndex);
 62                 }
 63             }
 64 
 65         }
 66 
 67         return arrayList;
 68     }
 69 }
 70 
 71 class CustomerIntIndex{
 72     private Integer idIndex1;
 73     private Integer idIndex2;
 74 
 75     public Integer getIdIndex1() {
 76         return idIndex1;
 77     }
 78     public void setIdIndex1(Integer idIndex1) {
 79         this.idIndex1 = idIndex1;
 80     }
 81     public Integer getIdIndex2() {
 82         return idIndex2;
 83     }
 84     public void setIdIndex2(Integer idIndex2) {
 85         this.idIndex2 = idIndex2;
 86     }
 87     @Override
 88     public String toString() {
 89         return "["+idIndex1+","+idIndex2+"]";
 90     }
 91 
 92 
 93 }
 94 
 95 class CustomerIntEle{
 96     private Integer idIndex;
 97     private Integer intArrEle;
 98     public int getIdIndex() {
 99         return idIndex;
100     }
101     public void setIdIndex(int idIndex) {
102         this.idIndex = idIndex;
103     }
104     public int getIntArrEle() {
105         return intArrEle;
106     }
107     public void setIntArrEle(int intArrEle) {
108         this.intArrEle = intArrEle;
109     }
110 
111 }

 

posted @ 2024-09-07 15:43  coderlwz  阅读(3)  评论(0编辑  收藏  举报