Rubyr代码随想录算法训练营第七天|454.四数相加II 、383. 赎金信 、15. 三数之和 、18.四数之和

Leetcode 454.四数相加II 

1) 使用HashMap,key是两数之和(a + b / c + d), value是 两数之和出现的次数。

2) 遍历 A, B两个数组元素之和,和出现的次数,放到map中。

3) 遍历C, D 两个数组,统计 a+b+c+d = 0 出现的次数。根据 0 - ( c + d ) = a + b, 找到map中等于0 - ( c + d )的元素和,然后、获取把map中key对应的value,也就是出现次数统计出来。

4) 最后返回统计值 count。

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int res = 0;
        Map<Integer, Integer> map = new HashMap<>();

        // get the sum of the nums1 and nums2
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                map.put(nums1[i] + nums2[j], map.getOrDefault(nums1[i] + nums2[j], 0) + 1);
            }
        }

        // tranverae the  nums1 and nums2
        // 0 - (nums3 + nums4) = nums1 + nums2; than find  0 - (nums3 + nums4) from map

        for (int k = 0; k < nums3.length; k++) {
            for (int l = 0; l < nums4.length; l++) {
                if ( map.containsKey(0 - nums3[k] - nums4[l])) {
                    res += map.get(0 - nums3[k] - nums4[l]);
                }
            }
        }
        return res;

    }

    // time complexity is O(n^2)
}

Leetcode 383 赎金信  (Ransom Note)

1) 对ransomNote字符串for循环,获取的每一个字符都必须也存在于magzine中,所以ransomNote每一个字符在res数组中都不能为0

2) 当if 判断了每一个字符之后,进行res[index]--, 字符数量- 1,才能判断下一个循环的字符。

class Solution {

    public boolean canConstruct(String ransomNote, String magazine) {

        int[] res = new int[26];

        for (char ch : magazine.toCharArray()) {
            int index = ch - 'a';
            res[index]++;
        }

        for (char ch : ransomNote.toCharArray()) {
            int index = ch - 'a';
            if (res[index] == 0) {
                return false;
            }
            res[index]--;
        }
        return true;

        
    }
}

leetcode 15. 三数之和

T (n) = O(n^2)

1) 与上题不同的是,在同一个数组中找三数之和等于0的数,a + b + c = 0,  0 - (a + b) = c。

2) 在for循环中使用双指针,left = i + 1, right = array.length - 1, i 作为不断移动的第三个指针。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        //先排序,有序才能使用双指针
        Arrays.sort(nums);

        List<List<Integer>> res = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            //一开始arr[i] 就大于0,说明之和的数都大于0,不可能出现三数之和等于0的情况
            if (nums[i] > 0) {
                return res; //不能直接写return, missing return value
            }
            //  对a指针去重
            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 {
                    //刚好sum = 0时,使用Arrays.as()将数组转化成list
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    // b 和 c 指针去重
                    //不能写成if,这里a指针不变,继续向中间考虑寻找, 
                    //b.c指针去重放在找到一个三元组之后
                    while (right > left && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    while (right > left && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    
                    right--; 
                    left++;
                }
            }
        }
       return res;
    }
}

Leetcode 18.四数之和 

和上题一样,多了一个指针,多了一层循环j, j =  i + 1;

i和 j 不断在for循环移动

left指针和right指针也从左右两边向内移动

T (n) = O(n^3)

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res= new ArrayList<>();
        Arrays.sort(nums);
       
        for (int i = 0; i < nums.length; i++) {
		
            // 如果nums[i] > target 直接返回 res
            if (nums[i] > 0 && nums[i] > target) {
                return res;
            }
		    
            // 对a 指针(nums[i])去重
            if (i > 0 && nums[i - 1] == nums[i]) {    
                continue;
            }
            
            for (int j = i + 1; j < nums.length; j++) {
                //对b 指针(nums[j])去重
                if (j > i + 1 && nums[j - 1] == nums[j]) {  
                    continue;
                }

                int left = j + 1;
                int right = nums.length - 1;
                while (right > left) {
                    long sum = (long) 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]));
                        // 对 c, d指针, 也就是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;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值