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 fc8254d

Browse files
committed
commit solution 136
1 parent 1fef7f4 commit fc8254d

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

‎index-tags.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
5353
| --- | --- | --- | --- | --- |
5454
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
55+
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
5556
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
5657
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
5758

@@ -103,6 +104,7 @@
103104

104105
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
105106
| --- | --- | --- | --- | --- |
107+
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
106108
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
107109
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
108110

‎index-type.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> |
4141
| [122](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii) | [买卖股票的最佳时机 ii](/solution/100-199/0122.best-time-to-buy-and-sell-stock-ii/) | `贪心算法`,`数组` | <font color=green>简单</font> |
4242
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
43-
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> |
43+
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
4444
| [141](https://leetcode-cn.com/problems/linked-list-cycle) | [环形链表](/solution/100-199/0141.linked-list-cycle/) | `链表`,`双指针` | <font color=green>简单</font> |
4545
| [155](https://leetcode-cn.com/problems/min-stack) | [最小栈](/solution/100-199/0155.min-stack/) | ``,`设计` | <font color=green>简单</font> |
4646
| [157](https://leetcode-cn.com/problems/read-n-characters-given-read4) | [用 read4 读取 n 个字符](/solution/100-199/0157.read-n-characters-given-read4/) | `字符串` | <font color=green>简单</font> |
@@ -127,7 +127,7 @@
127127
| [441](https://leetcode-cn.com/problems/arranging-coins) | [排列硬币](/solution/400-499/0441.arranging-coins/) | `数学`,`二分查找` | <font color=green>简单</font> |
128128
| [443](https://leetcode-cn.com/problems/string-compression) | [压缩字符串](/solution/400-499/0443.string-compression/) | `字符串` | <font color=green>简单</font> |
129129
| [447](https://leetcode-cn.com/problems/number-of-boomerangs) | [回旋镖的数量](/solution/400-499/0447.number-of-boomerangs/) | `哈希表` | <font color=green>简单</font> |
130-
| [448](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array) | [找到所有数组中消失的数字](/solution/400-499/0448.find-all-numbers-disappeared-in-an-array/) | `数组` | <font color=green>简单</font> |
130+
| [448](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array) | [找到所有数组中消失的数字](/solution/400-499/0448.find-all-numbers-disappeared-in-an-array/) | `数组` | <font color=green>简单</font> ||
131131
| [453](https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements) | [最小移动次数使数组元素相等](/solution/400-499/0453.minimum-moves-to-equal-array-elements/) | `数学` | <font color=green>简单</font> |
132132
| [455](https://leetcode-cn.com/problems/assign-cookies) | [分发饼干](/solution/400-499/0455.assign-cookies/) | `贪心算法` | <font color=green>简单</font> |
133133
| [459](https://leetcode-cn.com/problems/repeated-substring-pattern) | [重复的子字符串](/solution/400-499/0459.repeated-substring-pattern/) | `字符串` | <font color=green>简单</font> |
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number/description/)
2+
3+
### 题目描述
4+
5+
<p>给定一个<strong>非空</strong>整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。</p>
6+
7+
<p><strong>说明:</strong></p>
8+
9+
<p>你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?</p>
10+
11+
<p><strong>示例 1:</strong></p>
12+
13+
<pre><strong>输入:</strong> [2,2,1]
14+
<strong>输出:</strong> 1
15+
</pre>
16+
17+
<p><strong>示例&nbsp;2:</strong></p>
18+
19+
<pre><strong>输入:</strong> [4,1,2,1,2]
20+
<strong>输出:</strong> 4</pre>
21+
22+
### 解题思路
23+
24+
1. hash table
25+
26+
### 具体解法
27+
28+
#### **Golang**
29+
```go
30+
func singleNumber(nums []int) int {
31+
numMap := make(map[int]bool)
32+
for _, v := range nums {
33+
if _, ok := numMap[v]; ok {
34+
numMap[v] = false
35+
} else {
36+
numMap[v] = true
37+
}
38+
}
39+
for _, v := range nums {
40+
if numMap[v] == true {
41+
return v
42+
}
43+
}
44+
return 0
45+
}
46+
```
47+
48+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=136 lang=golang
5+
*
6+
* [136] 只出现一次的数字
7+
*/
8+
9+
// @lc code=start
10+
func singleNumber(nums []int) int {
11+
numMap := make(map[int]bool)
12+
for _, v := range nums {
13+
if _, ok := numMap[v]; ok {
14+
numMap[v] = false
15+
} else {
16+
numMap[v] = true
17+
}
18+
}
19+
for _, v := range nums {
20+
if numMap[v] == true {
21+
return v
22+
}
23+
}
24+
return 0
25+
}
26+
27+
// @lc code=end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSingleNumber(t *testing.T) {
8+
var ret int
9+
var nums []int
10+
ret = 1
11+
nums = []int{2, 2, 1}
12+
if ret != singleNumber(nums) {
13+
t.Fatalf("case fails %v\n", ret)
14+
}
15+
16+
ret = 4
17+
nums = []int{4, 1, 2, 1, 2}
18+
if ret != singleNumber(nums) {
19+
t.Fatalf("case fails %v\n", ret)
20+
}
21+
}

‎solution/100-199/_sidebar.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
- [133. 克隆图](solution/100-199/0133.clone-graph/)
4343
- [134. 加油站](solution/100-199/0134.gas-station/)
4444
- [135. 分发糖果](solution/100-199/0135.candy/)
45-
- [136. 只出现一次的数字](solution/100-199/0136.single-number/)
45+
- [136. 只出现一次的数字](solution/100-199/0136.single-number/)
4646
- [137. 只出现一次的数字 ii](solution/100-199/0137.single-number-ii/)
4747
- [138. 复制带随机指针的链表](solution/100-199/0138.copy-list-with-random-pointer/)
4848
- [139. 单词拆分](solution/100-199/0139.word-break/)

0 commit comments

Comments
(0)

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