File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 8
8
| 9| [ 回文数] ( https://leetcode-cn.com/problems/palindrome-number/ ) | [ JavaScript] ( ./algorithms/palindrome-number.js ) | Easy|
9
9
| 14| [ 最长公共前缀] ( https://leetcode-cn.com/problems/longest-common-prefix/ ) | [ JavaScript] ( ./algorithms/longest-common-prefix.js ) | Easy|
10
10
| 21| [ 合并两个有序链表] ( https://leetcode-cn.com/problems/merge-two-sorted-lists/ ) | [ JavaScript] ( ./algorithms/merge-two-sorted-lists.js ) | Easy|
11
+ | 136| [ 只出现一次的数字] ( https://leetcode-cn.com/problems/single-number/ ) | [ JavaScript] ( ./algorithms/single-number.js ) | Easy|
11
12
| 189| [ 轮转数组] ( https://leetcode-cn.com/problems/rotate-array/ ) | [ JavaScript] ( ./algorithms/rotate-array.js ) | Medium|
12
13
| 344| [ 反转字符串] ( https://leetcode-cn.com/problems/reverse-string/ ) | [ JavaScript] ( ./algorithms/reverse-string.js ) | Easy|
13
14
| 349| [ 两个数组的交集] ( https://leetcode-cn.com/problems/intersection-of-two-arrays/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ // 方法一:映射
2
+
3
+ /**
4
+ * @param {number[] } nums
5
+ * @return {number }
6
+ */
7
+ var singleNumber = function ( nums ) {
8
+ const o = { } ;
9
+
10
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
11
+ const curr = nums [ i ] ;
12
+
13
+ // 如果已经出现过
14
+ if ( o [ curr ] ) {
15
+ delete o [ curr ] ;
16
+ } else {
17
+ o [ curr ] = 1 ;
18
+ }
19
+ }
20
+
21
+ const keys = Object . keys ( o ) ;
22
+
23
+ return keys [ 0 ] ;
24
+ } ;
25
+
26
+ // 方法二:位运算
27
+
28
+ /**
29
+ * @param {number[] } nums
30
+ * @return {number }
31
+ */
32
+ var singleNumber = function ( nums ) {
33
+ let result = 0
34
+
35
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
36
+ result ^= nums [ i ]
37
+ }
38
+
39
+ return result
40
+ } ;
You can’t perform that action at this time.
0 commit comments