当前位置:网站首页>[leetcode15] sum of three numbers
[leetcode15] sum of three numbers
2022-07-06 12:24:00 【Vigorous waist Nuo dance】
Personally to , For record review only
Double pointer
problem
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
-105 <= nums[i] <= 105
source : Power button (LeetCode)
link :https://leetcode.cn/problems/3sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
vector<vector<int>> threeSum(vector<int>& nums) {
/* * The title gives an array . Ask Mei to cite the combination of all three numbers that add up to zero . And the combination cannot be repeated . * If you use triple loop enumeration directly . Additional judgment is needed to determine whether the combinations are repeated . * If you're right first nums Sort . The three numbers required to be enumerated are a<=b<=c. Then you can avoid repetition . */
sort(nums.begin(), nums.end());
/* obtain nums size . About to start enumerating .*/
int size = nums.size();
// Store results
vector<vector<int>> ans;
for (int first = 0; first < size; first++) {
// stay first=n-1 When . Can't open the second one for loop .
/* In order to avoid the repetition of the combination of Meiju */
if (first > 0 && nums[first] == nums[first - 1])
continue;
/* The goal is that the sum of three numbers equals zero . Then, along with second This subscript is getting bigger .third It's getting smaller . * Start with two pointers . Initialize first third */
int third = size - 1;
for (int second = first + 1; second < size; second++) {
/* Or to avoid the repetition of the combination of Meiju */
if (second > first + 1 && nums[second] == nums[second - 1])
continue;
/* Or first avoid the repetition of the combination */
while (third > second && third < size - 1 && nums[third] == nums[third + 1])
third--;
/* If the conditions are not met , Continue to move left third*/
while (third > second && nums[third] + nums[second] > -nums[first])
third--;
/* When double pointers overlap .second If the pointer continues to move to the right . It won't satisfy the meaning of the topic anymore * however first You can also continue to move right . */
if (third == second)
break;
if (nums[third] + nums[second] == -nums[first])
/* initialization vector<int> An easy way to use {1,2,3}*/
ans.push_back({
nums[first],nums[second],nums[third] });
/* The case of less than does not need to be considered . Keep moving right second It can be solved */
}
}
return ans;
}
边栏推荐
- js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- level16
- Page performance optimization of video scene
- Pytoch implements simple linear regression demo
- JS function promotion and declaration promotion of VaR variable
- (5) Introduction to R language bioinformatics -- ORF and sequence analysis
- ESP8266连接onenet(旧版MQTT方式)
- Esp8266 connect onenet (old mqtt mode)
- 單片機藍牙無線燒錄
- Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
猜你喜欢

Esp8266 connect onenet (old mqtt mode)

MySQL time, time zone, auto fill 0

Redis cache update strategy, cache penetration, avalanche, breakdown problems

ES6 grammar summary -- Part 2 (advanced part es6~es11)

Types de variables JS et transformations de type communes

Whistle+switchyomega configure web proxy
![C language callback function [C language]](/img/7b/910016123738240e24549ddea8a162.png)
C language callback function [C language]

Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】

CUDA C programming authoritative guide Grossman Chapter 4 global memory

【ESP32学习-2】esp32地址映射
随机推荐
(1) Introduction Guide to R language - the first step of data analysis
By v$rman_ backup_ job_ Oracle "bug" caused by details
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
vim命令行笔记
单片机蓝牙无线烧录
Selective sorting and bubble sorting [C language]
Basic operations of databases and tables ----- creating data tables
JS regular expression basic knowledge learning
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Basic operations of databases and tables ----- modifying data tables
A possible cause and solution of "stuck" main thread of RT thread
基於Redis的分布式ID生成器
I2C bus timing explanation
JS 函数提升和var变量的声明提升
Walk into WPF's drawing Bing Dwen Dwen
Redis based distributed locks and ultra detailed improvement ideas
MySQL时间、时区、自动填充0的问题
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
【ESP32学习-2】esp32地址映射