Skip to content

Latest commit

 

History

History
18 lines (17 loc) · 589 Bytes

1.两数之和.md

File metadata and controls

18 lines (17 loc) · 589 Bytes
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int length = nums.length;
        Map<Integer,Integer> hashTable = new HashMap<>();
        for(int i=0;i<length;i++){
            if(hashTable.containsKey(target - nums[i])){
                return new int[]{i,hashTable.get(target-nums[i])};
            }
            hashTable.put(nums[i],i);
        }
        return new int[0];
    }
}

思路:

使用哈希表,哈希表的key为nums数组中的值,value为nums数组的下标,使用containsKey函数来寻找是否存在target-nums[i]的值