代码随想录 算法训练营d7 哈希表 Leetcode454 四数相加2 Leetcode383 赎金信 Leetcode15 三数之和 Leetcode18 四数之和

Leetcode454 四数相加2 

题目链接

简单理解 四个数组的数 构成元组  相加为0

思想:参考力扣第一题 两数之和  才用哈希表解决问题

通过将ab数组之和存储到哈希表中,并记录次数

再通过 计算-(c+d)去匹配哈希表 如果存在 那么count+= 次数 即可

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int count = 0;
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i:nums1){
            for(int j:nums2){
                int tmp = i +j;
                map.put(tmp, map.getOrDefault(tmp, 0) + 1);
            }
        }
        for(int i:nums3){
            for(int j:nums4){    
                count += map.getOrDefault(0 - i - j, 0);
            }
        }
        return count;
    }
}

注意 哈希map的一些函数用法 如 getOrDefault等

Leetcode383 赎金信

题目链接

有效字母异位词的扩展提 判断一个字符串是否能由另一个字符串构成

但是只能用一次   通过数组的方式 统计次数即可  最后大于等于0即可

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if(ransomNote.length()>magazine.length()){
            return false;
        }
        int[] rec = new int[26];
        for(char c:magazine.toCharArray()){
            rec[c-'a']++;
        }
        for(char c:ransomNote.toCharArray()){
            rec[c-'a']--;
        }
        for(int i= 0;i<rec.length;i++){
            if(rec[i]<0){
                return false;
            }
        }
        return true;
    }
}

Leetcode15 三数之和

题目链接

此题可以用哈希表 但是非常麻烦 要对元组去重

应该采用双指针做法 更方便

讲解

首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。

依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i],b = nums[left],c = nums[right]。

接下来如何移动left 和right呢, 如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。

如果 nums[i] + nums[left] + nums[right] < 0 说明 此时 三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。

注意如何去重 i的去重  找到三元组后如何去重

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
	// 找出a + b + c = 0
        // a = nums[i], b = nums[left], c = nums[right]
        for (int i = 0; i < nums.length; i++) {
	    // 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了
            if (nums[i] > 0) { 
                return result;
            }

            if (i > 0 && nums[i] == nums[i - 1]) {  // 去重a
                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]));
		    // 去重逻辑应该放在找到一个三元组之后,对b 和 c去重
                    while (right > left && nums[right] == nums[right - 1]) right--;
                    while (right > left && nums[left] == nums[left + 1]) left++;
                    
                    right--; 
                    left++;
                }
            }
        }
        return result;
    }
}

Leetcode18 四数之和

题目链接

讲解

此题与上一题 三数之和 基本差不多 都是采用双指针思想  

此题 更多一个指针 

 

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            // nums[i] > target 直接返回, 剪枝操作
            if (nums[i] > 0 && nums[i] > target) {
                return result;
            }	
            if (i > 0 && nums[i - 1] == nums[i]) {    // 对nums[i]去重
                continue;
            }
            for (int j = i + 1; j < nums.length; j++) {
                if (j > i + 1 && nums[j - 1] == nums[j]) {  // 对nums[j]去重
                    continue;
                }
                int left = j + 1;
                int right = nums.length - 1;
                while (right > left) {
		    // nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        // 对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 result;
    }
}

双指针的一些题目

双指针法将时间复杂度:O(n^2)的解法优化为 O(n)的解法。也就是降一个数量级,题目如下:

链表相关双指针题目:

posted @ 2024-06-12 22:44  lorange  阅读(15)  评论(0)    收藏  举报