当前位置:网站首页>Leetcode (question 15) - sum of three numbers
Leetcode (question 15) - sum of three numbers
2022-06-13 09:40:00 【Python's path to becoming a God】
One 、 Title Description
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 .
Two 、 Example
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 :[]
3、 ... and 、 Ideas 1、 First, let's determine the conditions that are not met , If the length of the array is less than three , It returns an empty array directly .2、 If the length of the array is greater than or equal to three , Then sort the array , In order from small to large .3、 At this point, we judge the first element of the array , If the size of the first element is greater than 0, It returns an empty array directly .4、 We determine the position of three numbers , Suppose the three numbers are a,b,c, also a <= b <= c.5、 At this point, we will traverse the elements of the array , From the first , Traverse to the penultimate , At this point, the element traversed is assumed to be a Elements .6、 In the case of each element traversed above , hypothesis b Elements by a The next element of the element ,c Elements For the last element of the array .7、 And then determine a + b + c Is it 0, If 0, Then put it into the array , If it is greater than 0, Will c Elements Move one bit to the left , If it is less than 0, Will b Elements Move one bit to the right .8、 There is a condition in the title , Answer cannot contain duplicate triples . So we should de duplicate it . There are three situations (1): If a Element is equal to its previous element , Should be directly continue,(2): If b Element is equal to its next element , Then its subscript should be shifted one bit to the right .(3): If c Element is equal to its previous element , The subscript should be shifted one bit to the left .
Four 、 Code
/** * @param {number[]} nums * @return {number[][]} */
var threeSum = function(nums) {
let result = []
if(nums.length < nums) return result
let length = nums.length
nums.sort((a, b) => a -b)
for(let i = 0; i < length - 2; i++) {
let cur = nums[i]
if(cur > 0) return result
if(i > 0 && cur === nums[i - 1]) continue
let left = i + 1
let right = length - 1
while(left < right) {
let sum = cur + nums[left] + nums[right]
if(sum === 0) {
result.push([cur, nums[left], nums[right]])
while(nums[left] === nums[left + 1]) left++
while(nums[right] === nums[right] - 1) right--
left++
right--
}else if(sum > 0) {
right--
}else {
left++
}
}
}
return result
};
5、 ... and 、 summary 
边栏推荐
- Pxxx local socket communication
- LeetCode 6095. Strong password checker II
- Classes and objects -- polymorphic
- (dfs+ tree DP) acwing 846 Center of gravity of tree
- [51nod p2527] or and sum [bit operation]
- JS【中高级】部分的知识点我帮你们总结好了
- Jenkins接入Openldap用戶認證
- [51nod p2106] an odd number of times [bit operation]
- Overloading of typical operators
- LeetCode 5289. 公平分发饼干(DFS)
猜你喜欢
![[51nod p2102] or subtraction and [bit operation]](/img/49/8cc722e5fb18de5ce30d58adb805db.jpg)
[51nod p2102] or subtraction and [bit operation]

(dp+ memory) acwing 901 skiing

【20220526】UE5.0.2 release d11782b
![[implementation of depth first search]](/img/10/4f150e4fa0d4edf01483a72b881afe.jpg)
[implementation of depth first search]

GCC compilation process
![[51nod p2527] or and sum [bit operation]](/img/50/75ee9ee40ef0ca9b99e6900018b61a.jpg)
[51nod p2527] or and sum [bit operation]
![[51nod p3111] xiaoming'ai intercepts [Las]](/img/39/2d75a289c715fd010bf400d6eace71.jpg)
[51nod p3111] xiaoming'ai intercepts [Las]

VGA common resolution and calculation method

C language: five custom types

隐私计算FATE-核心概念与单机部署
随机推荐
Classes and objects -- Inheritance
LeetCode 6095. Strong password checker II
[ssl1271] sort I [heap]
go-zero微服务实战系列(三、API定义和表结构设计)
UNIX Environment advanced programming --3-file io---3.10 file sharing
Simple use of spiel expressions
[51nod p3395] n-bit gray code [bit operation]
I set up a blog
UNIX Environment advanced programming --8- process control ---8.5 function exit-8.6 function wait and waitpid
VDD,DVDD,AVDD,VCC,AFVDD,DOVDD,IOVDD
MOOC week 8 programming exercise 1
英国出台粮食安全计划抵御粮食供应危机
LeetCode 5259. 计算应缴税款总额
(tree DP) acwing 285 A dance without a boss
Haproxy + keepalived for high availability load balancing of MySQL
说说MySQL索引机制
Britain introduces food security plan to resist food supply crisis
C language structure
虚拟机内存结构简述
[51nod 2493] sum of binary distances [bit operation]