Skip to content

Commit

Permalink
added Problem 15
Browse files Browse the repository at this point in the history
  • Loading branch information
aQua authored and aQua committed Jul 28, 2017
1 parent 2a1b615 commit 86ecb4f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 34 deletions.
64 changes: 35 additions & 29 deletions Algorithms/0015.3sum/3sum.go
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
}
30 changes: 29 additions & 1 deletion Algorithms/0015.3sum/3sum_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Problem0015

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -29,10 +30,36 @@ func Test_Problem0015(t *testing.T) {
qs := []question{

question{
para{[]int{-1, 0, 1, 2, -1, -4}},
para{[]int{1, -1, -1, 0}},
ans{[][]int{
[]int{-1, 0, 1},
}},
},

question{
para{[]int{-1, 0, 1, 2, 2, 2, 2, -1, -4}},
ans{[][]int{
[]int{-4, 2, 2},
[]int{-1, -1, 2},
[]int{-1, 0, 1},
}},
},
question{
para{[]int{0, 0, 0, 0, 0}},
ans{[][]int{
[]int{0, 0, 0},
}},
},
question{
para{[]int{1, 1, -2}},
ans{[][]int{
[]int{-2, 1, 1},
}},
},
question{
para{[]int{0, 0, 0}},
ans{[][]int{
[]int{0, 0, 0},
}},
},

Expand All @@ -43,5 +70,6 @@ func Test_Problem0015(t *testing.T) {
a, p := q.ans, q.para

ast.Equal(a.one, threeSum(p.one), "输入:%v", p)
fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
}
}
16 changes: 12 additions & 4 deletions Algorithms/0015.3sum/README.md
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]
]
```
## 解题思路
穷举法无法通过时间限制


## 总结


详见代码

0 comments on commit 86ecb4f

Please sign in to comment.