当前位置:网站首页>2. Sum of three numbers
2. Sum of three numbers
2022-07-19 13:42:00 【_ ady】

Their thinking
Sort + Double pointer
- Sort
- Set three pointers ,i,lp,rp.i Responsible for traversing the array ,lp and rp Be responsible for finding the remaining two numbers .
- if nums[i]>0, Because it's sorted , So there can't be three numbers that add up to 0, Return directly and jump out of the loop .
- For repeating elements : skip , Avoid duplicate solutions
- Set the left pointer to i+1, The right pointer is 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,RL,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
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
int n = nums.length;
// 1. Sort
Arrays.sort(nums);
// 2. Traverse all data index
for (int i = 0; i < n; i++) {
if(nums[i]>0)
break;
// duplicate removal
if(i>0 && nums[i]==nums[i-1])
continue;
int lp = i + 1;
int rp = n - 1;
while (lp < rp){
int sum = nums[i] + nums[lp] + nums[rp];
if(sum < 0) {
lp++;
}else if(sum > 0){
rp--;
}else{
result.add(Arrays.asList(nums[i],nums[lp],nums[rp]));
lp++;
rp--;
// duplicate removal
while (lp < rp && nums[lp]==nums[lp-1]) lp++;
while (lp < rp && nums[rp]==nums[rp+1]) rp--;
}
}
}
return result;
}
边栏推荐
- onvif协议相关:2.1.1 none方式获取token
- onvif协议相关:3.1.2 Digest方式获取token列表
- 如何在MFC中添加一个线程
- Simulation of overvoltage protection (OVP) circuit based on PMOS
- Responsive dream weaving template wine cellar website
- [code hoof set novice village question 600] formatted input and output, using 0 to replace the completed space
- 模块7(王者荣耀商城异地多活架构设计)
- Codeforce:a. doremy's IQ [reverse greed]
- "Technology podcast month" day 10: meta podcast: talk about Podcasting
- Acwing game 60
猜你喜欢

Responsive Zhimeng template logistics and freight service website

Google Earth Engine——1992—至今混合坐标海洋模型、水温和盐度(全球海洋数据集HYCOM)

ModuleNotFoundError: No module named ‘_distutils_hack‘

onvif协议相关:3.1.1 Digest方式获取Authorization

每周小结(*65):有计划的输出

Onvif protocol related: 4.1.3 WS username token method to obtain screenshot URL

onvif协议相关:4.1.4 WS-Username token方式获取流地址
![[pumpkin Book ml] (task2) mathematical derivation of linear model (least squares estimation, generalized Rayleigh quotient, maximum likelihood estimation, etc.)](/img/ab/aaf4c01144364e7999582f326f16af.png)
[pumpkin Book ml] (task2) mathematical derivation of linear model (least squares estimation, generalized Rayleigh quotient, maximum likelihood estimation, etc.)

【腾讯蓝鲸】第七届 7·24 运维日节日祝福送上~ 快来许愿~

如何优雅的升级 Flink Job?
随机推荐
Realize automatic logging
【码蹄集新手村 600 题】如何使整数逆序
如何优雅的升级 Flink Job?
ONVIF Protocol Related: 4.1.3 WS - username token Method get capture d'écran URL
ssh无密钥登录
Helloword and led: a quick start to Hongmeng device development -- Huawei cloud 14 day Hongmeng device development practical learning notes Chapter 2
codeforce:A. Doremy‘s IQ【反向贪心】
【6.15】Codeforces Round #798 (Div. 2)
codeforce:A. Difference Operations【数学思维】
Onvif protocol related: 4.1.3 WS username token method to obtain screenshot URL
S32K148_ Can drive (bare metal development)
Onvif protocol related: 3.1.2 get the token list in digest mode
Flutter uses animatedswitcher to switch scenes
Codeforce:a. difference operations [mathematical thinking]
Onvif protocol related: 2.1.3 get the stream address in none mode
【码蹄集新手村 600 题】输出时的左对齐,右对齐
perl 命令批量替换文件中的一些内容
(PC+WAP)织梦模板服装礼服类网站
基于MOS管的防反接电路设计仿真
Transphorm's surface mount packaging product series adds industry standard to-263 (D2Pak) packaging products to expand the advantages of supergan platform