Skip to content

Commit bae60f9

Browse files
committed
feat: add the solution of Base 7(504) with kotlin.
1 parent 5daf9ee commit bae60f9

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [][121-java] | | [][121-kotlin] |
6161
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [][122-java] | | [][122-kotlin] |
6262
| [226][226-question] | [Invert Binary Tree][226-tips] | [][226-java] | [][226-js] | [][226-kotlin] |
63+
| [504][504-question] | [Base 7][504-tips] | | | [][504-kotlin] |
6364
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [][543-java] | | [][543-kotlin] |
6465
| [561][561-question] | [Array Partition I][561-tips] | [][561-java] | [][561-js] | |
6566
| [643][643-question] | [Maximum Average Subarray I][643-tips] | [][643-java] | [][643-js] | |
@@ -212,6 +213,7 @@
212213
[121-question]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
213214
[122-question]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
214215
[226-question]: https://leetcode.com/problems/invert-binary-tree/
216+
[504-question]: https://leetcode.com/problems/base-7/description/
215217
[543-question]: https://leetcode.com/problems/diameter-of-binary-tree/
216218
[554-question]: https://leetcode.com/problems/brick-wall/description/
217219
[561-question]: https://leetcode.com/problems/array-partition-i/
@@ -522,5 +524,6 @@
522524
[121-kotlin]: ./src/_121/kotlin/Solution.kt
523525
[122-kotlin]: ./src/_122/kotlin/Solution.kt
524526
[226-kotlin]: ./src/_226/kotlin/Solution.kt
527+
[504-kotlin]: ./src/_504/kotlin/Solution.kt
525528
[543-kotlin]: ./src/_543/kotlin/Solution.kt
526529
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_504/kotlin/Solution.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package _504.kotlin
2+
3+
import java.util.*
4+
5+
/**
6+
* @author relish
7+
* @since 2018/10/19
8+
*/
9+
class Solution {
10+
fun convertToBase7(num: Int): String {
11+
if (num == 0) return "0"
12+
val isPositive = num >= 0
13+
var n = Math.abs(num)
14+
val sb = StringBuilder()
15+
while (n > 0) {
16+
sb.append(n % 7)
17+
n /= 7
18+
}
19+
sb.append(if (isPositive) "" else "-")
20+
return sb.reverse().toString()
21+
}
22+
}
23+
24+
fun main(args: Array<String>) {
25+
val scanner = Scanner(System.`in`)
26+
val input = scanner.nextInt()
27+
println(_504.kotlin.Solution().convertToBase7(input))
28+
}

tips/504/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[Base 7][title]
2+
3+
## Description
4+
5+
Given an integer, return its base 7 string representation.
6+
7+
8+
**Example 1:**
9+
10+
```
11+
Input: 100
12+
Output: "202"
13+
```
14+
15+
**Example 2:**
16+
17+
```Input: -7
18+
Output: "-10"
19+
```
20+
21+
**Note:**
22+
The input will be in range of [-1e7, 1e7].
23+
24+
**Tags:** [no tags]
25+
26+
27+
## 思路 1
28+
判断一下0的情况和负数的符号
29+
kotlin(192ms/100.00%):
30+
```kotlin
31+
class Solution {
32+
fun convertToBase7(num: Int): String {
33+
if (num == 0) return "0"
34+
val isPositive = num >= 0
35+
var n = Math.abs(num)
36+
val sb = StringBuilder()
37+
while (n > 0) {
38+
sb.append(n % 7)
39+
n /= 7
40+
}
41+
sb.append(if (isPositive) "" else "-")
42+
return sb.reverse().toString()
43+
}
44+
}
45+
```
46+
47+
## 结语
48+
49+
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
50+
51+
[title]: https://leetcode.com/problems/base-7/description/
52+
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)