自刷代码随想录Day07

lc1: 两数之和(简单)
题目描述:

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
在这里插入图片描述

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] indexs = new int[2];
        // 建立k-v,一一对应哈希表
        HashMap<Integer,Integer> hash = new HashMap<Integer, Integer>();
        // value为下标值,key为对应下标里面存储的值离target还差多少的值
        for (int i = 0; i < nums.length; i++) {
            if (hash.containsKey(nums[i])) {
                indexs[0]  = i;
                indexs[1] = hash.get(nums[i]);
                return indexs;
            }
            // 将数据存入 key为补数, value为下标
            hash.put(target - nums[i], i);
        }
        return indexs;
    }
}

题解:
之前我们使用了数组和set的数据结构来实现哈希,这道题采用了map来实现。
1)数组的长度受限,如果不能确定数组的长度我们就无法使用数组的数据结构,同时,如果数组长度很长(哈希值大),而元素很少,浪费空间严重,不建议使用数组。
2)set是一个集合,里面只能存放一个元素key值,这道题我们不仅需要y的值,还需要y值在nums数组对应的下标值,所以需要key-value键值对来存放。
注意:这道题的题解还是比较新颖的,它在map中存放的key值是这个key对应的value的值在数组中的值还差多少到target,如果找到这个差值,我们就找到了第二个数。


lc454:四数相加 II(中等)
题目描述:

给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
    在这里插入图片描述
class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        int res = 0;
        int temp;
        for (int i : nums1) {
            for (int j : nums2) {
                temp = i + j;
                if (map.containsKey(temp)) {
                    map.put(temp, map.get(temp) + 1);
                }else {
                    map.put(temp, 1);
                }
            }
        }
        // 统计剩余两个元素的和,如果和为map中存在的key的相反数,则和为0的个数+value
        for (int i : nums3) {
            for (int j : nums4) {
                temp = 0 - (i + j);
                if (map.containsKey(temp)) {
                    res += map.get(temp);
                }
            }
        }
        return res;
    }
}

该题的思路其实和两数和类似,只是我们在map中放的是两数之和,然后去比较的时候,需要用剩余两数之和来比较,比较重要的点是存储的value值不一样,本题的value的值为该key出现的次数,意思是没有两个数相加为该key值,对应的value值要+1。


lc15:三数之和(中等)
题目描述:

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
你返回所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
在这里插入图片描述
在这里插入图片描述

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                return res;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                if (nums[i] + nums[left] + nums[right] > 0) {
                right--;
                }
                else if (nums[i] + nums[left] + nums[right] < 0) {
                    left++;
                }
                else {
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[left]);
                    list.add(nums[right]);
                    res.add(list);
                   while (right > left && nums[right] == nums[right - 1]){
                       right--;
                   } 
                    while (right > left && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    left++;
                    right--;
                }
            }
        }
        return res;
    }
}

我的答案太垃圾了,一样的思路,这里贴出评论区题解:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                return result;
            }

            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }

            int left = i + 1;
            int right = nums.length - 1;
            while (right > left) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    while (right > left && nums[right] == nums[right - 1]) right--;
                    while (right > left && nums[left] == nums[left + 1]) left++;
                    
                    right--; 
                    left++;
                }
            }
        }
        return result;
    }
}

这道题的双指针问题还是比较巧妙的,如果是三个数,我们就固定一个来遍历,其它两个数使用双指针。
再看看,我们的Java对于数组是有一些操作的,比如可以直接排序,再来解题。然后就是我们的List集合的创建。
在这里插入图片描述

二刷的时候补充哈希法


lc18.:四数之和

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • a、b、c 和 d 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。
在这里插入图片描述

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList();
        int left;
        int right;
        for (int i = 0; i < nums.length; i++) {
            if (i != 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j < nums.length; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                left = j + 1;
                right = nums.length - 1;
                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    
                    if (sum > target) {
                        right--;
                    }else if (sum < target) {
                        left++;
                    }else {
                        res.add(Arrays.asList(nums[i],nums[j], nums[left], nums[right]));
                        while (right > left && nums[right] == nums[right - 1]){
                            right--;
                        }

                        while (right > left && nums[left] == nums[left + 1]){
                            left++;
                        }
                        left++;
                        right--;
                    }
                }
            }
        }
        return res;
    }
}

本题其实是上两道题的变形,求四数和和求五数、六数和都是一个道理。
本体解法使用的是双指针法,还可以尝试使用HashMap。
但是本题还有个用例没通过,就是int型溢出问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值