My son...# Joke - 肚皮舞运动
n*2
1 楼
用map解的,逻辑上有问题。
import java.util.HashMap;
public class Solution {
public static int[] twoSum(int[] numbers, int target) {
// Start typing your Java solution below
// DO NOT write main() function
HashMap map = new HashMap();
int[] result = new int[2];
for (int i = 0; i < numbers.length; i++)
{
map.put(numbers[i], i); // 这里为啥不行呐?一定要放到if加个
else之后才行,求大牛指导。
if (map.containsKey(target - numbers[i]))
{
result[0] = map.get(target-numbers[i]) + 1;
result[1] = i + 1;
break;
}
}
return result;
}
}
import java.util.HashMap;
public class Solution {
public static int[] twoSum(int[] numbers, int target) {
// Start typing your Java solution below
// DO NOT write main() function
HashMap
int[] result = new int[2];
for (int i = 0; i < numbers.length; i++)
{
map.put(numbers[i], i); // 这里为啥不行呐?一定要放到if加个
else之后才行,求大牛指导。
if (map.containsKey(target - numbers[i]))
{
result[0] = map.get(target-numbers[i]) + 1;
result[1] = i + 1;
break;
}
}
return result;
}
}