首页 > 其他分享 >test目录

test目录

时间:2023-01-24 15:44:06浏览次数:61  
标签:map goal nums int num ints test 目录

两数之和

image-20230119205502517

解法利用HashMap(map.get(Key))实现数的存储和输出

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

标签:map,goal,nums,int,num,ints,test,目录
From: https://www.cnblogs.com/szwsama/p/17066130.html

相关文章