LeetCode 1. 两数之和

题目连接:https://leetcode-cn.com/problems/two-sum/
Java解法:
   public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> used = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int com = target - nums[i];
            if (used.containsKey(com) && used.get(com) != i) {
                return new int[]{used.get(com), i};
            }
            used.put(nums[i], i);
        }
        return new int[0];
    }

  

 

posted @ 2020-10-02 15:35  Risfeng  阅读(161)  评论(0编辑  收藏  举报