Two Sum

  • Post author:
  • Post category:其他


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



版权声明:本文为sjphiChina原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。