当前位置:网站首页>LeetCoed18. Sum of four numbers
LeetCoed18. Sum of four numbers
2022-04-23 08:09:00 【Want to get into Ali's chicken】
Ideas
The same idea as the sum of three , It also uses a double pointer, which is to add an extra layer on the outside for Circulation is enough .
Code
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;
}
}
版权声明
本文为[Want to get into Ali's chicken]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230641067646.html
边栏推荐
- Implementation of new
- 利用sqlmap注入获取网址管理员账号密码
- C语言学习记录——삼십팔 字符串函数使用和剖析(2)
- Redis--为什么字符串emstr的字符串长度是44字节上限?
- BUUCTF [ACTF2020 新生赛]Include1
- Implementation principle of instanceof
- 巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」
- Cloud computing skills competition -- the first part of openstack private cloud environment
- 高精度焊接机械臂定位
- MySQL -- the secret of lock -- how to lock data
猜你喜欢
随机推荐
LeetCoed18. 四数之和
雲計算技能大賽 -- openstack私有雲環境 第一部分
干货!以点为形:可微分的泊松求解器
NIH降血脂指南《your guide to lowering your Cholesterol with TLC》笔记(持续更新中)
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface
Implementation of promise all
浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
面试学习路线
[problem solving] vs2019 solves the problem that the EXE file generated by compilation cannot be opened
Anti shake and throttling
【问题解决】VS2019解决编译生成的exe文件打不开的情况
云计算赛项--2020年赛题基础部分[任务3]
Upload labs range practice
Research on system and software security (I)
sentinel集成nacos动态更新数据原理
PHP high precision computing
thinkphp6+jwt 实现登录验证
Feign source code analysis
Move layout (Flex layout, viewport label)









