Skip to content

136. Single Number🖖 #74

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 3 commits into from
Feb 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ continually updating 😃.
* [88. Merge Sorted Array](src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
* [122. Best Time to Buy and Sell Stock II](src/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)   *`greedy;`*  *`array`*
* [136. Single Number](src/0136_single_number/single_number.go)   *`hash table;`*  *`bit manipulation`*
* [167. Two Sum II - Input array is sorted](src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
* [179. Largest Number](src/0179_largest_number/ln.go)   *`sort`*
* [200. Number of Islands](src/0200_number_of_island/number_of_island.go)   *`dfs;`*  *`bfs`*
Expand Down
37 changes: 37 additions & 0 deletions src/0136_single_number/single_number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
136. Single Number
https://leetcode.com/problems/single-number/

Given a non-empty array of integers, every element appears twice except for one. Find that single one.
*/
// time: 2019-02-01

package sn

// time complexity: O(n)
// space complexity: O(n)
func singleNumber(nums []int) int {
record := make(map[int]int)
for _, num := range nums {
if _, ok := record[num]; ok {
delete(record, num)
} else {
record[num] = 1
}
}
var res int
for key := range record {
res = key
}
return res
}

// time complexity: O(n)
// space complexity: O(1)
func singleNumber1(nums []int) int {
res := 0
for _, num := range nums {
res ^= num
}
return res
}
22 changes: 22 additions & 0 deletions src/0136_single_number/single_number_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sn

import "testing"

func TestSingleNumber(t *testing.T) {
testCases := [][]int{
{2, 2, 1},
{4, 1, 2, 1, 2},
}
expected := []int{1, 4}
testFuncs := []func([]int) int{
singleNumber, singleNumber1,
}

for _, testFunc := range testFuncs {
for index, nums := range testCases {
if res := testFunc(nums); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
|0121|[121. Best Time to Buy and Sell Stock](0121_best_time_to_buy_and_sell_stock/maxprofit.go)|Easy||
|0122|[122. Best Time to Buy and Sell Stock II](0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)|Easy|*`greedy`*|
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
|0136|[136. Single Number](0136_single_number/single_number.go)|Easy|*`hash table;`* *`bit manipulation`*|
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|
Expand Down