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 645b75b

Browse files
authored
136. Single Number🖖 (#74)
* 136 solved🖖 * optimize code
1 parent 2e7042b commit 645b75b

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ continually updating 😃.
2222
* [88. Merge Sorted Array](src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
2323
* [121. Best Time to Buy and Sell Stock](src/0121_best_time_to_buy_and_sell_stock/maxprofit.go)   *`dynamic programming;`*  *`array`*
2424
* [122. Best Time to Buy and Sell Stock II](src/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)   *`greedy;`*  *`array`*
25+
* [136. Single Number](src/0136_single_number/single_number.go)   *`hash table;`*  *`bit manipulation`*
2526
* [167. Two Sum II - Input array is sorted](src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
2627
* [179. Largest Number](src/0179_largest_number/ln.go)   *`sort`*
2728
* [200. Number of Islands](src/0200_number_of_island/number_of_island.go)   *`dfs;`*  *`bfs`*

‎src/0136_single_number/single_number.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
136. Single Number
3+
https://leetcode.com/problems/single-number/
4+
5+
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
6+
*/
7+
// time: 2019年02月01日
8+
9+
package sn
10+
11+
// time complexity: O(n)
12+
// space complexity: O(n)
13+
func singleNumber(nums []int) int {
14+
record := make(map[int]int)
15+
for _, num := range nums {
16+
if _, ok := record[num]; ok {
17+
delete(record, num)
18+
} else {
19+
record[num] = 1
20+
}
21+
}
22+
var res int
23+
for key := range record {
24+
res = key
25+
}
26+
return res
27+
}
28+
29+
// time complexity: O(n)
30+
// space complexity: O(1)
31+
func singleNumber1(nums []int) int {
32+
res := 0
33+
for _, num := range nums {
34+
res ^= num
35+
}
36+
return res
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sn
2+
3+
import "testing"
4+
5+
func TestSingleNumber(t *testing.T) {
6+
testCases := [][]int{
7+
{2, 2, 1},
8+
{4, 1, 2, 1, 2},
9+
}
10+
expected := []int{1, 4}
11+
testFuncs := []func([]int) int{
12+
singleNumber, singleNumber1,
13+
}
14+
15+
for _, testFunc := range testFuncs {
16+
for index, nums := range testCases {
17+
if res := testFunc(nums); res != expected[index] {
18+
t.Errorf("expected %d, got %d", expected[index], res)
19+
}
20+
}
21+
}
22+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
|0121|[121. Best Time to Buy and Sell Stock](0121_best_time_to_buy_and_sell_stock/maxprofit.go)|Easy||
5858
|0122|[122. Best Time to Buy and Sell Stock II](0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)|Easy|*`greedy`*|
5959
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
60+
|0136|[136. Single Number](0136_single_number/single_number.go)|Easy|*`hash table;`* *`bit manipulation`*|
6061
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
6162
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
6263
|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|

0 commit comments

Comments
(0)

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