当前位置: 首页 > news >正文

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

打卡Day7

  • 1.454.四数相加II
  • 2.383. 赎金信
  • 3.15. 三数之和
  • 4.18. 四数之和

1.454.四数相加II

题目链接:四数相加II
文档讲解: 代码随想录

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

2.383. 赎金信

题目链接:赎金信
文档讲解: 代码随想录

class Solution {public boolean canConstruct(String ransomNote, String magazine) {int[] record = new int[26];if(ransomNote.length() > magazine.length()){return false;}for(int i = 0; i < magazine.length(); i++){record[magazine.charAt(i) - 'a']++;}for(int i = 0; i < ransomNote.length(); i++){record[ransomNote.charAt(i) - 'a']--;}for(int i = 0; i < ransomNote.length(); i++){//判断不等于0是不对的,没有考虑到magazine中存在该字母出现更多次的情况if(record[ransomNote.charAt(i) - 'a'] < 0){return false;}}return true;}
}
//使用增强for
class Solution {public boolean canConstruct(String ransomNote, String magazine) {int[] record = new int[26];if(ransomNote.length() > magazine.length()){return false;}for(char i: magazine.toCharArray()){record[i - 'a'] += 1;}for(char i: ransomNote.toCharArray()){record[i - 'a'] -= 1;}for(int i: record){if(i < 0){return false;}}return true;}
}

3.15. 三数之和

题目链接:三数之和
文档讲解: 代码随想录

class Solution {public List<List<Integer>> threeSum(int[] nums) {//哈希法List<List<Integer>> res = new ArrayList<>();//需要对nums进行排序Arrays.sort(nums);for(int i = 0; i < nums.length; i++){//如果第一个元素大于0,则不可能存在三元组if(nums[i] > 0){return res;}//a去重if(i > 0 && nums[i] == nums[i - 1]){continue;}HashSet<Integer> set = new HashSet<>();for(int j = i + 1; j < nums.length; j++){//b去重if(j > i + 2 && nums[j] == nums[j - 1] && nums[j] == nums[j - 2]){continue;}int c = - nums[i] - nums[j];if(set.contains(c)){res.add(Arrays.asList(nums[i], nums[j], c));set.remove(c);//c去重}else{set.add(nums[j]);}                        }}return res;}
}

注意点:
(1)a 的去重:在两种方法中纠结,一是判断nums[i] == nums[i+1],一种是判断nums[i] == nums[i-1]。如果前者,那么存在遗漏情况,例如{-1,-1,2}。
(2)b 的去重:可以和 a 一样判断nums[j] == nums[j-1],从而跳过相同的b。但是这样子可能会遗漏情况,例如{0,0,0}。因此只有当当前的 b 和前两个 b 都相同时才跳过当前的 b。这样可以保证至少有一个 b 被使用,并且不会出现重复。

class Solution {public List<List<Integer>> threeSum(int[] nums) {//双指针法List<List<Integer>> res = new ArrayList<>();//需要对nums进行排序Arrays.sort(nums);for(int i = 0; i < nums.length; i++){if(nums[i] > 0){return res;} //a去重if(i >= 1 && nums[i] == nums[i - 1]){continue;}int left = i + 1;int right = nums.length - 1;while(left < right){//left和right是两个指针,与二分查找不一样int sum = nums[i] + nums[left] + nums[right];if(sum > 0){right--;}else if(sum < 0){left++;}else{res.add(Arrays.asList(nums[i], nums[left], nums[right]));//去重while(left < right && nums[left] == nums[left + 1]){left++;}while(left < right && nums[right] == nums[right - 1]){right--;}right--;left++;}}}      return res;}
}

两数之和不能用双指针法的原因:因为双指针法需要排序,而两数之和需要返回索引下标。

4.18. 四数之和

题目链接:四数之和
文档讲解: 代码随想录

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++){//剪枝if(nums[i] > 0 && nums[i] > target){return res;}//去重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;}int left = j + 1;int right = nums.length - 1;while(left < right){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]));//去重while(left < right && nums[left] == nums[left + 1]){left++;}while(left < right && nums[right] == nums[right - 1]){right--;}left++;right--;}}}}return res;}
}
http://www.lryc.cn/news/385211.html

相关文章:

  • Python和tkinter实现的字母记忆配对游戏
  • Leetcode Hot100之链表
  • 5.9k!一款清新好用的后台管理系统!【送源码】
  • Vue-cli搭建项目----基础版
  • python之__call__函数介绍
  • 【AI】生成式AI服务器最低配置
  • 2.Android逆向协议-了解常用的逆向工具
  • 大数据------额外软件、插件及技术------Linux(完整知识点汇总)
  • iOS 其他应用的文件如何在分享中使用自己的应用打开
  • 【编译原理必考大题】 推导构建语法树,写出语法树的短语,简单短语和句柄
  • redis服务介绍
  • nodepad 中换行符、tab替换
  • 常见的字符串函数(包含头文件string.h)和字符函数(2)
  • Python | Leetcode Python题解之第187题重复的DNA序列
  • SpringCloud分布式微服务链路追踪方案:Skywalking
  • 首次线下联合亮相!灵途科技携手AEye、ATI亮相2024 EAC 易贸汽车产业大会
  • 一文入门CMake
  • 【LeetCode面试经典150题】117. 填充每个节点的下一个右侧节点指针 II
  • RTDETR更换优化器——Lion
  • Spring Boot中最佳实践:数据源配置详解
  • 第1章 物联网模式简介---独特要求和体系结构原则
  • 数据挖掘概览
  • 【学习】软件测试中常见的文档类型及其作用
  • electron的托盘Tray
  • Harmony OS UI框架探索笔记
  • transformers evaluate
  • 【ONLYOFFICE深度探索】:ONLYOFFICE桌面编辑器8.1震撼发布,打造高效办公新境界
  • C++系统相关操作4 - 获取CPU(指令集)架构类型
  • whisper 实现语音转文字
  • 使用VLLM部署llama3量化版