当前位置:网站首页>Force deduction brush question (2): sum of three numbers
Force deduction brush question (2): sum of three numbers
2022-07-29 02:02:00 【Just for fun】
Force button brush questions (2): Sum of three numbers
1 Problem description
Given an inclusion n Array of integers nums, Judge nums Whether there is 3 Elements $ a、b、c$, bring a + b + c = 0 a+b+c=0 a+b+c=0.
Find all triples that meet the conditions and are not repeated .
Be careful : Answer cannot contain duplicate triples .
for example , Given array n u m s = [ − 1 , 0 , 1 , 2 , − 1 , − 4 ] nums=[-1,0,1,2,-1,-4] nums=[−1,0,1,2,−1,−4], The set of triples satisfying the requirement is [ [ − 1 , 0 , 1 ] , [ − 1 , − 1 , 2 ] ] [[-1,0,1],[-1,-1,2]] [[−1,0,1],[−1,−1,2]].
The requirements of the topic are as follows .
- find 3 The sum of the numbers equals 0, instead of “ Sum of two numbers ” The indefinite value in target, Therefore, this topic is a special case .
- The title requires that the number itself be returned , Not the index value .
- There are multiple answers to this question , Different from the previous question , This question requires that all possible answers be returned .
- Answer cannot contain duplicate triples , So we need to consider weight removal .
2 Problem analysis
Because this problem requires no longer to return the index value , So sort first , Then the idea of using double pointers is feasible .
The specific algorithm is to sort the original array first , Then a layer of loop fixes an element , Inside the loop, use double pointers to find the remaining two elements , Special attention should be paid here to the weight removal .
3 python Realization
Remember what Qian Xuesen said , Everything should be grasped from the whole .
The solution to this problem , By fixing i i i, Go looking for l l l, r r r.
The length of the array is n n n, The position of the first element is 0 0 0, The last one is n − 1 n-1 n−1,
class Solution2:
def threeSum(self, nums):
n = len(nums)
nums.sort() # Yes nums Sort the array in ascending order
res = [] # Define a list of controls ( Array )
for i in range(n-2):
if i > 0 and nums[i] == nums[i-1]: # This part is to remove the duplicate part
continue
l = i + 1
r = n - 1
while (l < r):
if (nums[i] + nums[l] + nums[r] < 0):
l += 1
elif (nums[i] + nums[l] + nums[r] > 0):
r -= 1
else:
res.append([nums[i],nums[l],nums[r]])
while (l < r and nums[l] == nums[l + 1]): # To remove duplicate parts
l += 1 # Just repeat ,l The pointer moves all the way to the right
while (l < r and nums[r] == nums[r - 1]): # To remove duplicate parts
r -= 1 # Just repeat ,r The pointer keeps moving left
l += 1
r -= 1
return res
fu2 = Solution2()
nums2 = [-1,0,1,2,-1,-4]
print(fu2.threeSum(nums2))
nums2.sort()
print(nums2)




4 Step by step analysis


边栏推荐
- [WesternCTF2018]shrine
- [understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
- Where will Jinan win in hosting the first computing power conference?
- 数据平台数据接入实践
- Tda75610-i2c-determination of I2C address of analog power amplifier
- MySQL execution order
- 基于 ICA 与 DL 的语音信号盲分离
- 【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
- Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
- 【流放之路-第五章】
猜你喜欢

Anaconda environment installation problem

The basic concept of transaction and the implementation principle of MySQL transaction

Day01作业

Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system

使用本地缓存+全局缓存实现小型系统用户权限管理

Make logic an optimization example in sigma DSP - data distributor
![[UE4] replay game playback for ue4.26](/img/c3/1c7b30797f46dbd323cac4d158600f.png)
[UE4] replay game playback for ue4.26

数据平台数据接入实践

LeetCode 112:路径总和

动态内存与智能指针
随机推荐
What is the function of data parsing?
数据平台数据接入实践
Planning mathematics final simulation exam I
知道创宇上榜CCSIP 2022全景图多个领域
九天后我们一起,聚焦音视频、探秘技术新发展
FPGA实现10M多功能信号发生器
Planning mathematics final exam simulation II
DSP震动座椅
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen
Yocto project download and compilation
As long as I run fast enough, it won't catch me. How does a high school student achieve a 70% salary increase under the epidemic?
Tda75610-i2c-determination of I2C address of analog power amplifier
[golang] use select {}
Dynamic memory and smart pointer
Network security litigation risk: four issues that chief information security officers are most concerned about
[the road of Exile - Chapter 8]
How to find the right agent type? Multi angle analysis for you!
The basic concept of transaction and the implementation principle of MySQL transaction
Regular filtering data learning notes (①)
[public class preview]: application exploration of Kwai gpu/fpga/asic heterogeneous platform