算法小白第7天算法之旅 哈希表题目 454四数之和2 ● 383. 赎金信 ● 15. 三数之和 ● 18. 四数之和

标题## 454四数之和

https://leetcode.cn/problems/4sum-ii/

解题思路
因为题目规定的是四个不同的数组 所以首先不用考虑去重的问题 用哈希表的办法更快 只需要 他们加起来是0 即可 那么就统计前两个数之和 还有出现的频率 然后统计后两个数组的和 相加等于0 的次数
注意 最后都是在一个新的map集合中执行的操作 在map集合中 找相加为0的次数。

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
           Map<Integer, Integer>   Map =     new  HashMap<>();
           int temp;
           int res = 0;
           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);

                   }
               }
           }

              for(int i : nums3){
                  for(int j :nums4 ){
                      temp = i+j;
                      if(Map.containsKey(0 - temp)){
                         res = res + Map.get( 0 -temp);
                      }
                  }
              } 
              return res ;
           }
    }

383赎金信

https://leetcode.cn/problems/ransom-note/

在这里插入图片描述
解题思路

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        // 定义一个哈希映射数组
        int[] record = new int[26];

        // 遍历
        for(char c : magazine.toCharArray()){
            record[c - 'a'] += 1;
        }

        for(char c : ransomNote.toCharArray()){
            record[c - 'a'] -= 1;
        }
        
        // 如果数组中存在负数,说明ransomNote字符串总存在magazine中没有的字符
        for(int i : record){
            if(i < 0){
                return false;
            }
        }

        return true;
    }
}

15三数之和

解题思路
定义要给集合用来放 三元数组 先进行排序 然后遍历 元素 遍历元素时去重 a 注意去重时要比较前一个是不是 相等 如果 相等就去重

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值