LeetCode 1. 两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> hashmap = new HashMap<>();

        int n = nums.length;

        for(int i = 0; i < n; i++){
            if(hashmap.containsKey(target - nums[i])){
                return new int[]{hashmap.get(target-nums[i]),i};
            }
            hashmap.put(nums[i],i);
        }
        return new int[0];
    }
}

 

posted @ 2020-10-28 16:10  peanut_zh  阅读(60)  评论(0编辑  收藏  举报