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) ...
最新文章
- 百度网盘javascript加速视频播放速度
- 联想台式计算机设置双层密码,联想电脑怎样设密码?联想电脑设置密码方法步骤【图文】...
- mac插网线不能上网_实现两个无线路由器之间的连接,最稳定的方式是使用网线连接...
- 整合Tomcat和Nginx实现动静态负载均衡
- bazel 链接第三方动态库_惠州权威信息系统安全检测第三方
- nagios监控安装及设置案例
- java多态/重载方法——一个疑难代码引发的讨论
- dedecms读取多个类别信息
- python 直方图每个bin中的值_python – 如何获取直方图bin中的数据
- 2019第五届美亚杯全国电子数据取证大赛团队赛wp
- esp32-Arduino开发学习
- 【不三不四的脑洞】大龄程序猿 “奇葩” 相亲记 | 年中总“劫”
- python+opencv实现相似图片的搜索
- Java获取WiFi ssid_如何在Flutter中获取当前连接的wifi的wifi名称(SSID)
- 以极地号为例认识科考船上的各种设备
- [XJTU计算机网络安全与管理]——第十三讲 攻击与病毒
- 使用unity的Microphone接口进行录音时遇到的相关问题
- Apple补丁解决iOS、iPadOS和macOS中的0day漏洞
- Matlab游戏制作代码
- 网卡驱动程序不正常上不了网的修复方法
热门文章
- Java解析富文本rtf中文乱码
- win7 计算机登录用户密码,Win7电脑无需知道密码绕过密码登录的操作方法?
- html英文读法,classin读音发音 classln英文怎么读?
- RUBi: Reducing Unimodal Biases in Visual Question Answering
- 软件工程——2021软科中国大学专业排名
- enum ordinal java_Spring 3.x自动将枚举的ordinal值转换为枚举类型
- 技术博客对找工作有帮助吗?
- 王可欣作业一 统计软件简介与数据操作
- 统计软件简介与数据操作
- 华三路由器虚拟服务器设置,H3C vLNS系列虚拟L2TP网络服务器 配置指导-E0324-5W100...