-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aQua
authored and
aQua
committed
Jul 28, 2017
1 parent
2a1b615
commit 86ecb4f
Showing
3 changed files
with
76 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,53 @@ | ||
package Problem0015 | ||
|
||
import ( | ||
"sort" | ||
) | ||
import "sort" | ||
|
||
func threeSum(nums []int) [][]int { | ||
// 排序后,可以按规律查找 | ||
sort.Ints(nums) | ||
res := [][]int{} | ||
|
||
for i := 0; i < len(nums)-2; i++ { | ||
for j := i + 1; j < len(nums)-1; j++ { | ||
for k := j + 1; k < len(nums); k++ { | ||
if nums[i]+nums[j]+nums[k] == 0 { | ||
temp := []int{nums[i], nums[j], nums[k]} | ||
if yes := contain(res, temp); !yes { | ||
res = append(res, temp) | ||
for i := range nums { | ||
// 避免添加重复的结果 | ||
if i > 0 && nums[i] == nums[i-1] { | ||
continue | ||
} | ||
|
||
} | ||
} | ||
l, r := i+1, len(nums)-1 | ||
|
||
for l < r { | ||
s := nums[i] + nums[l] + nums[r] | ||
switch { | ||
case s < 0: | ||
// 较小的 l 需要变大 | ||
l++ | ||
case s > 0: | ||
// 较大的 r 需要变小 | ||
r-- | ||
default: | ||
res = append(res, []int{nums[i], nums[l], nums[r]}) | ||
// 为避免重复添加,l 和 r 都需要移动到不同的元素上。 | ||
l, r = next(nums, l, r) | ||
} | ||
} | ||
} | ||
|
||
return res | ||
} | ||
|
||
func contain(numss [][]int, nums []int) bool { | ||
sort.Ints(nums) | ||
|
||
for _, ns := range numss { | ||
sort.Ints(ns) | ||
if equal(ns, nums) { | ||
return true | ||
func next(nums []int, l, r int) (int, int) { | ||
for l < r { | ||
switch { | ||
case nums[l] == nums[l+1]: | ||
l++ | ||
case nums[r] == nums[r-1]: | ||
r-- | ||
default: | ||
l++ | ||
r-- | ||
return l, r | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func equal(a, b []int) bool { | ||
for i, v := range a { | ||
if v != b[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
return l, r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
# [15. 3Sum](https://leetcode.com/problems/3sum/) | ||
|
||
## 题目 | ||
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. | ||
|
||
Note: The solution set must not contain duplicate triplets. | ||
``` | ||
For example, given array S = [-1, 0, 1, 2, -1, -4], | ||
A solution set is: | ||
[ | ||
[-1, 0, 1], | ||
[-1, -1, 2] | ||
] | ||
``` | ||
## 解题思路 | ||
穷举法无法通过时间限制 | ||
|
||
|
||
## 总结 | ||
|
||
|
||
详见代码 |