Skip to content

Commit 82e88fd

Browse files
committed
feat: add the solution of Single Number(136) with kotlin.
1 parent e1134dd commit 82e88fd

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [Easy][E] | [][121-java] | | [][121-kotlin] |
8787
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [Easy][E] | [][122-java] | | [][122-kotlin] |
8888
| [125][125-question] | [Valid Palindrome][125-tips] | [Easy][E] | | | [][125-kotlin] |
89+
| [136][136-question] | [Single Number][136-tips] | [Easy][E] | | | [][136-kotlin] |
8990
| [226][226-question] | [Invert Binary Tree][226-tips] | [Easy][E] | [][226-java] | [][226-js] | [][226-kotlin] |
9091
| [504][504-question] | [Base 7][504-tips] | [Easy][E] | | | [][504-kotlin] |
9192
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [Easy][E] | [][543-java] | | [][543-kotlin] |
@@ -206,6 +207,7 @@
206207
[121-question]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
207208
[122-question]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
208209
[125-question]: https://leetcode.com/problems/valid-palindrome/
210+
[136-question]: https://leetcode.com/problems/single-number/
209211
[226-question]: https://leetcode.com/problems/invert-binary-tree/
210212
[504-question]: https://leetcode.com/problems/base-7/description/
211213
[543-question]: https://leetcode.com/problems/diameter-of-binary-tree/
@@ -307,6 +309,7 @@
307309
[121-tips]: ./tips/121/README.md
308310
[122-tips]: ./tips/122/README.md
309311
[125-tips]: ./tips/125/README.md
312+
[136-tips]: ./tips/136/README.md
310313
[226-tips]: ./tips/226/README.md
311314
[504-tips]: ./tips/504/README.md
312315
[543-tips]: ./tips/543/README.md
@@ -529,6 +532,7 @@
529532
[121-kotlin]: ./src/_121/kotlin/Solution.kt
530533
[122-kotlin]: ./src/_122/kotlin/Solution.kt
531534
[125-kotlin]: ./src/_125/kotlin/Solution.kt
535+
[136-kotlin]: ./src/_136/kotlin/Solution.kt
532536
[226-kotlin]: ./src/_226/kotlin/Solution.kt
533537
[504-kotlin]: ./src/_504/kotlin/Solution.kt
534538
[543-kotlin]: ./src/_543/kotlin/Solution.kt

src/_136/kotlin/Solution.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _136.kotlin
2+
3+
import java.util.*
4+
import kotlin.collections.HashMap
5+
6+
/**
7+
* @author relish
8+
* @since 2018/11/14
9+
*/
10+
class Solution {
11+
fun singleNumber1(nums: IntArray): Int {
12+
val c = HashSet<Int>()
13+
val r = HashSet<Int>()
14+
for (num in nums) {
15+
if (r.contains(num)) continue
16+
if (c.contains(num)) {
17+
c.remove(num)
18+
r.add(num)
19+
} else {
20+
c.add(num)
21+
}
22+
}
23+
return c.single()
24+
}
25+
26+
fun singleNumber(nums: IntArray): Int {
27+
var s = 0
28+
for (num in nums) {
29+
s = s.xor(num)
30+
}
31+
return s
32+
}
33+
}
34+
35+
fun main(args: Array<String>) {
36+
println(Solution().singleNumber(intArrayOf(2, 2, 1)))
37+
println(Solution().singleNumber(intArrayOf(4, 1, 2, 1, 2)))
38+
}

tips/136/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[Single Number][title]
2+
3+
## Description
4+
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
5+
6+
**Note:**
7+
8+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
9+
10+
**Example 1:**
11+
12+
```
13+
Input: [2,2,1]
14+
Output: 1
15+
```
16+
17+
**Example 1:**
18+
19+
```
20+
Input: [4,1,2,1,2]
21+
Output: 4
22+
```
23+
24+
25+
**Tags:**
26+
HashTable、 Bit Manipulation
27+
28+
29+
## 思路 1
30+
1 存在`r`内的属于已出现重复的数字
31+
2 存在`c`内的属于可能只出现一次的数字
32+
3 当当前数字存在r中时, 说明是重复数字, 直接跳过进入下个循环;当出现重复数字时, 在r中移除该数字, 在c中加入该数字
33+
4 最后`c`中就只剩下一个数字了
34+
从代码看上去是线性复杂度的, 其实并不是, 因为contains方法里执行了循环操作。因此此解法效率较低。但可以应对其他数字出现2次以上的情况。
35+
kotlin(272ms/62.96%):
36+
```kotlin
37+
fun singleNumber(nums: IntArray): Int {
38+
val c = HashSet<Int>()
39+
val r = HashSet<Int>()
40+
for (num in nums) {
41+
if (r.contains(num)) continue
42+
if (c.contains(num)) {
43+
c.remove(num)
44+
r.add(num)
45+
} else {
46+
c.add(num)
47+
}
48+
}
49+
return c.single()
50+
}
51+
```
52+
53+
## 思路 2
54+
异或。(一个数字与自己异或为0;任何数与0异或为这个数本身)
55+
kotlin(228ms/100.00%):
56+
```kotlin
57+
fun singleNumber(nums: IntArray): Int {
58+
var s = 0
59+
for (num in nums) {
60+
s = s.xor(num)
61+
}
62+
return s
63+
}
64+
```
65+
66+
## 结语
67+
68+
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
69+
70+
[title]: https://leetcode.com/problems/single-number/
71+
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)