QUESTION:
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:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
EXPLANATION:
这个不用讲太多了,就是两个for循环,我倒是发现,之前的题目真的有点太简单了。反倒是那些最新的标为easy的反而是越来越难的,有很多奇怪的想法。
同时也发现最快的1ms的解答和这个答案是一样的,难道leetcode的判断逻辑还会和自己本地或者他服务端当时的性能有关?
SOLUTION:
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
o:
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (target-nums[i]==nums[j]){
result[0] = i;
result[1] = j;
break o;
}
}
}
return result;
}
}