leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

第一题 Two Sum 不符合预期结果

Open johnsonzheng0824 opened this issue 7 years ago • 0 comments

算法本身没问题,改一下比较好

public static int[] twoSum(int[] numbers, int target)
{
        int[] result = new int[2];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++)
        {
            if (map.containsKey(target - numbers[i]))
            {
                result[1] = i;
                result[0] = map.get(target - numbers[i]);
                return result;
            }
            map.put(numbers[i], i);
        }
        return result;
}

johnsonzheng0824 avatar Apr 08 '18 01:04 johnsonzheng0824