当前位置:网站首页>[leetcode] 15. Sum of three numbers
[leetcode] 15. Sum of three numbers
2022-06-30 12:13:00 【Xiaoqu】
15、 Sum of three numbers
subject :
Give you one containing n Array of integers nums, Judge nums Are there three elements in a,b,c , bring a + b + c = 0 ? Please find out all and for 0 Triple without repetition .
Be careful : Answer cannot contain duplicate triples .
Example 1:
Input :nums = [-1,0,1,2,-1,-4]
Output :[[-1,-1,2],[-1,0,1]]
Example 2:
Input :nums = []
Output :[]
Example 3:
Input :nums = [0]
Output :[]
Tips :
0 <= nums.length <= 3000
-10^5 <= nums[i] <= 10 ^5
Their thinking :
Sort + Double pointer
The difficulty of this problem is how to get rid of repeated solutions .
Algorithm flow :
- Special judgement , For array length n, If the array is null Or the array length is less than 3, return [ ].
- Sort the array .
- Traversing the sorted array :
- if nums[i]>0nums[i]>0: Because it's sorted , So there can't be three numbers that add up to 00, Direct return .
- For repeating elements : skip , Avoid duplicate solutions
- Left pointer L=i+1, Right pointer R=n-1, When L<R when , Execute loop :
- When nums[i]+nums[L]+nums[R]==0, Execute loop , Judge whether the left and right bounds are repeated with the next position , Remove duplicate solutions . At the same time L,R Move to the next position , Looking for new solutions
- If sum is greater than 0, explain nums[R] Too big ,R Move left
- If sum is less than 0, explain nums[L] Too small ,L Move right
Reference code :
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> resList = new ArrayList<>();
// Special condition judgment
if (nums.length < 3) return resList;
else if (nums.length == 3){
if (nums[0] + nums[1] + nums[2] == 0) {
List<Integer> resSingle = new ArrayList<>();
resSingle.add(nums[0]);
resSingle.add(nums[1]);
resSingle.add(nums[2]);
resList.add(resSingle);
}
return resList;
} else {
// 1. Sort
Arrays.sort(nums);
// 2. Double pointer
for (int j = 0; j < nums.length - 2; j++) {
if (j > 0) {
if (nums[j - 1] == nums[j]) continue;
}
int target = - nums[j];
int l = j + 1;
int r = nums.length - 1;
while (l < r) {
int sum = nums[l] + nums[r];
if (sum < target) l++;
else if (sum > target) r--;
else {
List<Integer> resSingle = new ArrayList<>();
resSingle.add(nums[j]);
resSingle.add(nums[l]);
resSingle.add(nums[r]);
resList.add(resSingle);
l++;
r--;
while (nums[l] == nums[l-1] && nums[r] == nums[r+1] && l < r) {
l++;
r--;
}
}
}
}
return resList;
}
}
}
边栏推荐
- Set集合
- Flutter 从零开始 007 输入框
- MySQL索引和优化的理解学习
- Another miserable day by kotlin grammar
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
- STM32 移植 RT-Thread 标准版的 FinSH 组件
- ClipboardJS——开发学习总结1
- A high precision positioning approach for category support components with multiscale difference reading notes
- led背光板的作用是什么呢?
- 拆分电商系统为微服务
猜你喜欢
AUTOCAD——LEN命令
lvgl 小部件样式篇
实现多方数据安全共享,解决普惠金融信息不对称难题
After the node is installed in the NVM, the display is not internal or external when the NPM instruction is used
聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
Embedded SIG | 多 OS 混合部署框架
beego开发博客系统学习(二)
MySQL 表的内连和外连
Four Misunderstandings of Internet Marketing
[pattern recognition]
随机推荐
3D线光谱共焦传感器在半导体如何检测
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
Go 语言入门很简单:Go 处理 XML 文件
Yolov5 export the pit encountered by onnx
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置show_legend参数为FALSE配置不显示图例信息
695. maximum island area
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
wallys/600VX – 2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
21、wpf之绑定使用小记
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
MATLAB中polarplot函数使用
Another miserable day by kotlin grammar
【BUG解决】fiftyone报AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘错误解决方法
200. 岛屿数量
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
OpenMLDB Meetup No.4 会议纪要
服务器常用的一些硬件信息(不断更新)
Multiparty cardinality testing for threshold private set-2021: Interpretation
Subtrate 源码追新导读-5月上旬: XCM 正式启用
AutoCAD - len command