mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
add similar problems url
This commit is contained in:
parent
f32b33b7ea
commit
247f249042
@ -1,5 +1,5 @@
|
||||
# [15. 3Sum](https://leetcode.com/problems/3sum/)
|
||||
# 思路
|
||||
# 思路、双指针法
|
||||
找出数组中的所有和为0的三个数组合。
|
||||
先想一下如何求所有和为0的两个数的组合。可以这样考虑,先将数组从小到大排序,再设置两个指针low和high分别初始为数组两端,计算两个指针的和sum,
|
||||
根据sum与0的大小关系适当调整指针:
|
||||
@ -11,6 +11,8 @@
|
||||
注意跳过重复元素。
|
||||
时间复杂度O(n^2),空间复杂度O(1)
|
||||
|
||||
类似的题目还有[16. 3Sum Closest](https://leetcode.com/problems/3sum-closest/)和[18. 4Sum](https://leetcode.com/problems/4sum/),都是先排序再使用双指针。
|
||||
|
||||
|
||||
# C++
|
||||
``` C++
|
||||
|
Loading…
Reference in New Issue
Block a user