当前位置:网站首页>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
边栏推荐
- Positioning and decoration style
- Principle of sentinel integrating Nacos to update data dynamically
- Three minutes to teach you to use Houdini fluid > > to solve particle fluid droplets
- RAID0和RAID5的创建和模拟RAID5工作原理
- Cloud computing skills competition -- the first part of openstack private cloud environment
- Summary of facial classics
- 聊聊接口幂等与消费幂等的本质
- 简述CPU
- Go语学习笔记 - Slice、Map | 从零开始Go语言
- 云计算赛项--2020年赛题基础部分[任务3]
猜你喜欢

CTF attack and defense world brush questions 51-

DVWA靶场练习

【编程实践/嵌入式比赛】嵌入式比赛学习记录(一):TCP服务器和web界面的建立

数据库之MySQL——基础篇

浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda

Dvwa 靶场练习记录

Intranet penetration series: icmptunnel of Intranet tunnel (Master James Barlow's)

Ctf-misc summary

Ubuntu安装Mysql并查询平均成绩

Chapter IV intangible assets
随机推荐
Jetson Xavier NX(3)Bazel Mediapipe 安装
Hierarchical output binary tree
C 输出一种二维数组,特点如下。
Chapter VII asset impairment
A series of articles, a summary of common vulnerabilities of Web penetration (continuously updated)
智能名片小程序名片详情页功能实现关键代码
Ubuntu安装Mysql并查询平均成绩
PHP high precision computing
MYSQL——第一章节(数据类型2)
Redis transaction implements optimistic locking principle
yum源仓库本地搭建的两种方法
巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」
Essays (updated from time to time)
thinkphp6+jwt 实现登录验证
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
Cloud computing skills competition -- the first part of openstack private cloud environment
Intranet penetration series: pingtunnel of Intranet tunnel
面试学习路线
Buuctf misc brush questions
Mobile terminal layout (3D conversion, animation)