当前位置:网站首页>LeetCoed18. 四数之和
LeetCoed18. 四数之和
2022-04-23 06:41:00 【想进阿里的小菜鸡】
思路
和三数之和一样的思路,也是用双指针就是在外面多加一层for循环即可。
代码
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
int temp = 0;
int left = 0;
int j = 0;
int right = 0;
List<List<Integer>> res = new ArrayList<>();
Set<List<Integer>> set = new HashSet<>();
for(int i =0;i<nums.length-2;i++){
for(j = i+1;j<nums.length-2;j++){
left = j+1;
right = nums.length-1;
while(right>left){
temp = nums[i] + nums[j]+nums[left]+nums[right];
if(temp > target){
right--;
}else if(temp < target){
left++;
}else{
set.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
left++;
right--;
}
}
}
}
for(List<Integer> a:set){
res.add(a);
}
return res;
}
}
版权声明
本文为[想进阿里的小菜鸡]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_56640241/article/details/124354830
边栏推荐
- Chapter VII asset impairment
- Redis -- why is the string length of string emstr the upper limit of 44 bytes?
- Essays (updated from time to time)
- 《内网安全攻防:渗透测试实战指南》读书笔记(六):域控制器安全
- Link to some good tutorials or notes about network security and record them
- 聊聊接口幂等与消费幂等的本质
- Research on system and software security (4)
- Houdini > rigid body, rigid body breaking RBD
- MySQL -- the secret of lock -- how to lock data
- Research on system and software security (3)
猜你喜欢
CTF-MISC总结
Intranet penetration series: pingtunnel of Intranet tunnel
Intranet penetration series: dnscat2 of Intranet tunnel
Intranet penetration series: dns2tcp of Intranet tunnel
《内网安全攻防:渗透测试实战指南》读书笔记(五):域内横向移动分析及防御
SAP GUI安全性
The displayed amount of ABAP ALV is inconsistent with the exported amount
BUUCTF [极客大挑战 2019]EasySQL1
Ctf-misc summary
CTF attack and defense world brush questions 51-
随机推荐
Redis transaction implements optimistic locking principle
Learning records of some shooting ranges: sqli labs, upload labs, XSS
攻防世界MISC刷题1-50
BUUCTF MISC刷题
1+x云计算中级--脚本搭建读写分离
面试学习路线
Analysis of Nacos source code
CTF-MISC学习之从开始到放弃
C read INI file and write data to INI file
KVM安装部署
Attack and defense world misc questions 1-50
TA notes of Zhuang understand (zero) < bedding and learning methods >
内网渗透系列:内网隧道之icmptunnel(jamesbarlow师傅的)
C # control the camera, rotate and drag the observation script (similar to scenes observation mode)
Post of experience in preparation for guarantee and research -- the 18th (2021) Central South planning department promoted the exemption to Zhejiang University Institute of Technology
Expression related to month, year and day in SVG
Link to some good tutorials or notes about network security and record them
How does Apache Hudi accelerate traditional batch mode?
LeetCode15. 三数之和
Go语学习笔记 - Slice、Map | 从零开始Go语言