2021暑假Leetcode刷题——Two Pointers(2)
5. 3Sum Closest (16)
思路和3Sum很类似。排序后declare一个difference变量,然后照旧外指针遍历从 0 到 length-2 ,外指针右侧左右指针相向移动。内部循环中计算出每次左右指针移动时所指值的和,与target计算差值与difference比较(注意用绝对值)。如果和小于target左指针右移,大于则右指针左移。注意点:外侧i循环终止的条件上可以加一个difference != 0,这样若sum等于target则直接跳出循环。时间复杂度 O(N^2),空间复杂度 O(1)。
Java的码:
public int threeSumClosest(int[] nums, int target) {if (nums == null || nums.length < 3) {return 0;}Arrays.sort(nums);int diff = Integer.MAX_VALUE;int length = nums.length;for (int i = 0; i < length - 2 && diff != 0; ++i) {if (i > 0 && nums[i] == nums[i - 1]) continue;int left = i + 1;int right = length - 1;while (left < right) {int sum = nums[left] + nums[right] + nums[i];diff = Math.abs(sum - target) < Math.abs(diff) ? sum - target : diff;if (sum < target) {++left;} else {--right;} }}return target + diff;}
6. 4Sum (18)
思路:外侧两个指针从左开始循环,在右侧剩下的地方进行2Sum,分两个函数进行。
外侧总函数:时间复杂度O(N^3)。
public List<List<Integer>> fourSum(int[] nums, int target) {List<List<Integer>> result = new ArrayList<>();Arrays.sort(nums); //先排序方便去重//异常检查,包括target过大或过小if (nums == null || nums.length < 4 || nums[0] * 4 > target || target > nums[nums.length - 1] * 4) {return result;}//最外侧i去重,与3Sum相似,注意i的范围for (int i = 0; i < nums.length - 3; ++i) {if (i > 0 && nums[i] == nums[i - 1]) continue;//j去重,与3Sum相似for (int j = i + 1; j < nums.length - 2; ++j) {if (j != i + 1 && nums[j] == nums[j - 1]) continue;//从2Sum返回的结果中每个List加入nums[i]和nums[j]for (var set : twoSum(nums, target - nums[i] - nums[j], j + 1)){result.add(new ArrayList<>(Arrays.asList(nums[i], nums[j])));result.get(result.size() - 1).addAll(set);}}}return result;}
2Sum函数做法:
6.1. HashSet:与Two Sum里第三种方法类似。遍历nums,先检测HashSet中是否存有 target - nums[i],再将其存入set。注意点:在非空的情况下,每次循环检查List里最后加的一组是不是和nums[i]相同。如此,可以避免重复的两个值被记下,也可以让防止程序忽略自身加和满足target的情况(如target=4,[2,2,2,2,2])。空间复杂度O(N)。
Java的码:
public List<List<Integer>> twoSum(int[] nums, int target, int start) {List<List<Integer>> result = new ArrayList<>();Set<Integer> set = new HashSet<>();for (int i = start; i < nums.length; ++i) {if (!result.isEmpty() && result.get(result.size() - 1).get(1) == nums[i]) continue;int remain = target - nums[i];if (set.contains(remain)) {result.add(Arrays.asList(remain, nums[i]));}set.add(nums[i]);}return result;
}
6.2. Two Pointer:思路和3Sum的思路一模一样。左右指针夹逼直到相遇。空间复杂度O(1)。
public List<List<Integer>> twoSum(int[] nums, int target, int start) {List<List<Integer>> result = new ArrayList<>();int right = nums.length - 1;int left = start;while (left < right) {int sum = nums[left] + nums[right];if ((left != start && nums[left] == nums[left - 1]) || (sum < target)) {++left;} else if ((right < nums.length - 1 && nums[right] == nums[right + 1]) || (sum > target)) {--right;} else {result.add(Arrays.asList(nums[left++], nums[right--]));}}return result;}
7. kSum
思路和4Sum一样,分出2Sum函数,然后递归解决前k-2个数字,每次递归往Listli加一个数字。
Java的码:时间复杂度O(N^{k-1})
public List<List<Integer>> kSum(int[] nums, int target, int start, int k) {List<List<Integer>> result = new ArrayList<>();if (start == nums.length || nums[start] * k > target || target > nums[nums.length - 1] * k){return result;}if (k == 2) return twoSum(nums, target, start);for (int i = start; i < nums.length; ++i) {if (i == start || nums[i - 1] != nums[i]) { //一样的去重方法for (var set : kSum(nums, target - nums[i], i + 1, k - 1)) {res.add(new ArrayList<>(Arrays.asList(nums[i])));res.get(res.size() - 1).addAll(set);}}} return res;
}
2021暑假Leetcode刷题——Two Pointers(2)相关推荐
- Leetcode刷题 2021.01.22
Leetcode刷题 2021.01.22 Leetcode1042 不邻接植花 Leetcode1010 总持续时间可被 60 整除的歌曲 Leetcode1091 二进制矩阵中的最短路径 Leet ...
- Leetcode刷题 2021.02.26
Leetcode刷题 2021.02.26 Leetcode1178 猜字谜 Leetcode869 重新排序得到 2 的幂 Leetcode1676 二叉树的最近公共祖先 IV Leetcode11 ...
- c语言贪心算法合并箭,LeetCode刷题题库:贪心算法
LeetCode刷题笔记:贪心算法 自大学开始,我便陆陆续续的学习一些 算法和数据结构 方面的内容,同时也开始在一些平台刷题,也会参加一些大大小小的算法竞赛.但是平时刷题缺少目的性.系统性,最终导致算 ...
- LeetCode刷题指南
CSDN话题挑战赛第1期 活动详情地址:https://marketing.csdn.net/p/bb5081d88a77db8d6ef45bb7b6ef3d7f 参赛话题:Leetcode刷题指南 ...
- 一个算法笨蛋的12月leetCode刷题日记
类似文章 一个算法笨蛋的2021年11月leetCode刷题日记 一个算法笨蛋的2021年12月leetCode刷题日记 一个算法笨蛋的2022年1月leetCode刷题日记 一个算法笨蛋的2022年 ...
- Leetcode刷题
刷题 leetcode 1.两数之和 #哈希表 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int ...
- LeetCode刷题模板(1):《我要打10个》之二分法
Author : 叨陪鲤 Email : vip_13031075266@163.com Date : 2021.01.23 Copyright : 未 ...
- Leetcode-How-What 力扣Leetcode刷题指南
Leetcode-How-What 力扣Leetcode刷题指南 About the way how to use Leetcode wisely for preparing the intervie ...
- 面试算法LeetCode刷题班—BAT面试官带你刷真题、过笔试
课程名称: <面试算法LeetCode刷题班> --BAT面试官带你刷真题.过笔试 主讲老师: 林老师 BAT资深研发工程师(T7/P8级),致力于搜索引擎及其子系统的研发.迭代与优化,数 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
最新文章
- poj 2104: K-th Number 【主席树】
- 前台更新进度条js-4
- CONNECT_NODES 中的SET HANDLER
- Equivalent Strings
- 数学发展重在人才,丘成桐鼓励年轻学子“无法无天”
- 成为Linux内核高手的四个方法
- 强悍的命令 —— cp
- 使用 python 完成 IP 存活检测
- hdu 1269 迷宫城堡(trajan判环)
- ajax下载Excel文件
- “企业级零代码黑客马拉松大赛”决赛名单公布
- VUCA时代下的敏捷
- hive会产生大量的tmp文件
- [QQ机器人]nonebot每日一言插件
- 企业如何做好员工关怀,减少人才流失
- 实现国标GB/T28181流媒体服务解决方案安防4G摄像头互联网直播方案EasyGBS之水库管理无线视频监控解决方案
- html5锥形,Three.js HTML5 3D开发实例 - 彩色圆锥体
- 国际网页短信系统平台搭建后台功能篇|移讯云短信系统
- android 刷机 zip,安卓自制zip刷机包 zip包刷机脚本函数详细例举教程
- 相比微信、支付宝支付,apple pay支付优缺点是什么呢?
热门文章
- 南阳oj The Triangle
- 南阳oj 题目72 Financial Management
- 计算机设备与驱动器空白图标,这个方法帮你删掉win10设备和驱动器里无效图标...
- 计算机丢失OX0000007B,win10系统应用程序无法正常启动0x000007b的解决办法
- 轻松与劳氏Lowe‘s 对接 EDI 要准备什么?
- Java编程思想 - 并发
- 计算机专业,25岁成985高校博导,13篇顶会!入职半年发ICML,网友:万点暴击...
- Android仿人人客户端(v5.7.1)——个人主页(三)
- 5G 与 WIFI6 的对比
- 【kaggle比赛记录】SHOPPE商品分类多模态分析