Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
在数组中找到两个数其相加和等于固定的值。返回这两个数的下标。
class Solution {
public int[] twoSum(int[] nums, int target) {
if(nums.length<=0)
return null;
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[j]==target-nums[i]){
int num[]={i,j};
return num;
}
}
}
return null;
}
}
时间复杂度为O(n^2)