1.两数之和

利用HashMap,Key存数字,Value存下表。

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> Hashtable = new HashMap<Integer,Integer>();
        for(int i = 0; i < nums.length ; i++){
            int y = target - nums[i];
            if(Hashtable.containsKey(y)){
                return new int[]{i+1,Hashtable.get(y)+1};
            }
            Hashtable.put(nums[i],i);
        }
    return new int[0];
    }
}

 

posted @ 2023-05-27 01:09  Lee最好好好吃饭  阅读(14)  评论(0)    收藏  举报