Given an array of integers, find two numbers such that they add up to a specific target number.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
代码:
public class Solution {
public int[] twoSum(int[] nums, int target) {
int []a = {0,0};
for(int i=0; i<nums.length ;i++){
for(int j=i+1; j<nums.length ;j++){
if(nums[i]+nums[j]==target){
a[0]=i+1;
a[1]=j+1;
break ;
}
}
}
return a;
}
}