Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7823ef

Browse files
authored
15. 3Sum (#78)
* 15 solved. * fix bug * fix bug * fix bug
1 parent fc5d125 commit b7823ef

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

‎.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: go
22

33
go:
4-
- "1.11.x"
4+
- "1.12.x"
55

66
go_import_path: leetcode
77

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ continually updating 😃.
1313
* [1. Two Sum](src/0001_two_sum/twosum.go)   *`lookup table;`*  *`hash table`*
1414
* [4. Median of Two Sorted Arrays](src/0004_median_of_two_sorted_arrays/motsa.go)   *`binary search;`*  *`divide and conquer`*
1515
* [11. Container With Most Water](src/0011_container_with_most_water/container_with_most_water.go)   *`double index;`*  *`array`*
16+
* [15. 3Sum](src/0015_3Sum/3sum.go)   *`double index;`*  *`array`*
1617
* [26. Remove Duplicates from Sorted Array](src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
1718
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
1819
* [48. Rotate Image](src/0048_rotate_image/rotate_image.go)

‎src/0015_3Sum/3sum.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
15. 3Sum
3+
https://leetcode.com/problems/3sum/
4+
*/
5+
// time: 2019年03月01日
6+
7+
package threesum
8+
9+
import "sort"
10+
11+
// time complexity: O(n^2)
12+
func threeSum(nums []int) [][]int {
13+
sort.Ints(nums)
14+
var res [][]int
15+
for i := 0; i < len(nums)-2; i++ {
16+
if nums[i] > 0 {
17+
break
18+
}
19+
if i > 0 && nums[i] == nums[i-1] {
20+
continue
21+
}
22+
for l, r := i+1, len(nums)-1; l < r; {
23+
if l > i+1 && nums[l] == nums[l-1] {
24+
l++
25+
continue
26+
}
27+
if r < len(nums)-1 && nums[r] == nums[r+1] {
28+
r--
29+
continue
30+
}
31+
switch sum := nums[i] + nums[l] + nums[r]; {
32+
case sum < 0:
33+
l++
34+
case sum > 0:
35+
r--
36+
default:
37+
res = append(res, []int{nums[i], nums[l], nums[r]})
38+
l++
39+
r--
40+
}
41+
}
42+
}
43+
return res
44+
}

‎src/0015_3Sum/3sum_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package threesum
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestThreeSum(t *testing.T) {
9+
testData := [][]int{
10+
{-1, 0, 1, 2, -6, -4},
11+
{-7, 3, -7, 3, 4, 5, 6, 6, 4},
12+
}
13+
expected := [][][]int{
14+
{{-1, 0, 1}},
15+
{{-7, 3, 4}},
16+
}
17+
18+
for index, nums := range testData {
19+
if res := threeSum(nums); !reflect.DeepEqual(res, expected[index]) {
20+
t.Errorf("expected %v, got %v", expected[index], res)
21+
}
22+
}
23+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
|0011|[11. Container With Most Water](0011_container_with_most_water/container_with_most_water.go)|Medium|*`array;`* *`double index`*|
1212
|0013|[13. Roman to Integer](0013_roman_to_integer/roman_to_integer.go)|Easy|*`math`*|
1313
|0014|[14. Longest Common Prefix](0014_longest_common_prefix/lcp.go)|Easy||
14+
|0015|[15. 3Sum](0015_3Sum/3sum.go)|Medium||
1415
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
1516
|0019|[19. Remove Nth Node From End of List](0019_remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list.go)|Medium|*`linked list`*|
1617
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /