Skip to content

15. 3Sum #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- "1.11.x"
- "1.12.x"

go_import_path: leetcode

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ continually updating 😃.
* [1. Two Sum](src/0001_two_sum/twosum.go)   *`lookup table;`*  *`hash table`*
* [4. Median of Two Sorted Arrays](src/0004_median_of_two_sorted_arrays/motsa.go)   *`binary search;`*  *`divide and conquer`*
* [11. Container With Most Water](src/0011_container_with_most_water/container_with_most_water.go)   *`double index;`*  *`array`*
* [15. 3Sum](src/0015_3Sum/3sum.go)   *`double index;`*  *`array`*
* [26. Remove Duplicates from Sorted Array](src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
* [48. Rotate Image](src/0048_rotate_image/rotate_image.go)
Expand Down
44 changes: 44 additions & 0 deletions src/0015_3Sum/3sum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
15. 3Sum
https://leetcode.com/problems/3sum/
*/
// time: 2019-03-01

package threesum

import "sort"

// time complexity: O(n^2)
func threeSum(nums []int) [][]int {
sort.Ints(nums)
var res [][]int
for i := 0; i < len(nums)-2; i++ {
if nums[i] > 0 {
break
}
if i > 0 && nums[i] == nums[i-1] {
continue
}
for l, r := i+1, len(nums)-1; l < r; {
if l > i+1 && nums[l] == nums[l-1] {
l++
continue
}
if r < len(nums)-1 && nums[r] == nums[r+1] {
r--
continue
}
switch sum := nums[i] + nums[l] + nums[r]; {
case sum < 0:
l++
case sum > 0:
r--
default:
res = append(res, []int{nums[i], nums[l], nums[r]})
l++
r--
}
}
}
return res
}
23 changes: 23 additions & 0 deletions src/0015_3Sum/3sum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package threesum

import (
"reflect"
"testing"
)

func TestThreeSum(t *testing.T) {
testData := [][]int{
{-1, 0, 1, 2, -6, -4},
{-7, 3, -7, 3, 4, 5, 6, 6, 4},
}
expected := [][][]int{
{{-1, 0, 1}},
{{-7, 3, 4}},
}

for index, nums := range testData {
if res := threeSum(nums); !reflect.DeepEqual(res, expected[index]) {
t.Errorf("expected %v, got %v", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
|0011|[11. Container With Most Water](0011_container_with_most_water/container_with_most_water.go)|Medium|*`array;`* *`double index`*|
|0013|[13. Roman to Integer](0013_roman_to_integer/roman_to_integer.go)|Easy|*`math`*|
|0014|[14. Longest Common Prefix](0014_longest_common_prefix/lcp.go)|Easy||
|0015|[15. 3Sum](0015_3Sum/3sum.go)|Medium||
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
|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`*|
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
Expand Down