算法-数组

算法-数组

leetcode_1

public int[] twoSum(int[] nums, int target) {
    if (nums == null || nums.length < 2) return null;
    HashMap<Integer, Integer> cacheMap = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        if (cacheMap.containsKey(target - nums[i])) {
            return new int[]{cacheMap.get(target - nums[i]), i};
        }
        cacheMap.put(nums[i], i);
    }
    return null;
}
posted @ 2022-12-19 19:15  Ryu~  阅读(21)  评论(0编辑  收藏  举报