算法分析与设计作业题
Two Sum
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.
Example:
public static int[] twoSum(int[] nums, int target) {
int[] answer = new int[2];
A:for (int i = 0; i < nums.length; ++i){
answer[0] = i;
int b = target - nums[i];
for (int j = i + 1; j < nums.length; ++j){
if (nums[j] == b){
answer[1] = j;
break A;
}
}
}
return answer;
}