leetcode编程题1----两数之和twoSum
public class Twoindices {
int[] sum=new int[2];
Map<Integer,Integer> tr=new HashMap<>();
public int[] twoSum(int[] nums, int target) {
int number=nums.length;
for(int i=0;i<number;i++){
tr.put(nums[i],i);
}
int temp=0;
int indice=0;
for(int j=0;j<number;j++){
temp=nums[j];
if(tr.containsKey(target-temp) && j!=tr.get(target-temp) ){
indice=tr.get(target-temp);
sum[0]=j+1;
sum[1]=indice+1;
break;
}
}
return sum;
}
public static void main(String[] args){
int[] nums = {0,4,3,0};
int target = 0;
int[] result;
Twoindices test=new Twoindices();
result=test.twoSum(nums,target);
for(int i:result){
System.out.println(i);
}
}
}