LeetCode刷题疑惑

HashMap的put方法在jdkAPI_1.8中是这样的:

put(K key, V value)

Associates the specified value with the specified key in this map.

​

但是在LeetCode的一道算法题里,调用put方法确实将value和key的位置互换了。

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        Map<Integer, Integer> hashMap = new HashMap<>(len -1);
        hashMap.put(nums[0] , 0);
        for(int i = 1; i < len; i++){
            int another = target-nums[i];
            if(hashMap.containsKey(another)){
               return new int[]{hashMap.get(another),i};
            }
            hashMap.put(nums[i],i);
        } 
        throw new IllegalArgumentException("No two sum soluton!");
    }
}

这是为啥呀,有会的朋友可以解答一下吗? 

猜你喜欢

转载自blog.csdn.net/weixin_51942352/article/details/127940104