給定一個整數數組和一個目標值,找出數組中和爲目標值的兩個數。你可以假設每個輸入只對應一種答案,且同樣的元素不能
被重複利用。
給定 nums = [2, 7, 11, 15], target = 9 因爲 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
class Solution {
public int[] twoSum(int[] nums, int target) {
int [] key = new int [2];
key[0]=0;
key[1]=1;
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if((nums[i]+nums[j])==target){
key[0]=i;
key[1]=j;
return key;
}
}
}
return key;
}
}