File tree 3 files changed +83
-0
lines changed 3 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 60
60
| [ 121] [ 121-question ] | [ Best Time to Buy and Sell Stock] [ 121-tips ] | [ ✅] [ 121-java ] | | [ ✅] [ 121-kotlin ] |
61
61
| [ 122] [ 122-question ] | [ Best Time to Buy and Sell Stock II] [ 122-tips ] | [ ✅] [ 122-java ] | | [ ✅] [ 122-kotlin ] |
62
62
| [ 226] [ 226-question ] | [ Invert Binary Tree] [ 226-tips ] | [ ✅] [ 226-java ] | [ ✅] [ 226-js ] | [ ✅] [ 226-kotlin ] |
63
+ | [ 504] [ 504-question ] | [ Base 7] [ 504-tips ] | | | [ ✅] [ 504-kotlin ] |
63
64
| [ 543] [ 543-question ] | [ Diameter of Binary Tree] [ 543-tips ] | [ ✅] [ 543-java ] | | [ ✅] [ 543-kotlin ] |
64
65
| [ 561] [ 561-question ] | [ Array Partition I] [ 561-tips ] | [ ✅] [ 561-java ] | [ ✅] [ 561-js ] | |
65
66
| [ 643] [ 643-question ] | [ Maximum Average Subarray I] [ 643-tips ] | [ ✅] [ 643-java ] | [ ✅] [ 643-js ] | |
212
213
[ 121-question ] : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
213
214
[ 122-question ] : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
214
215
[ 226-question ] : https://leetcode.com/problems/invert-binary-tree/
216
+ [ 504-question ] : https://leetcode.com/problems/base-7/description/
215
217
[ 543-question ] : https://leetcode.com/problems/diameter-of-binary-tree/
216
218
[ 554-question ] : https://leetcode.com/problems/brick-wall/description/
217
219
[ 561-question ] : https://leetcode.com/problems/array-partition-i/
522
524
[ 121-kotlin ] : ./src/_121/kotlin/Solution.kt
523
525
[ 122-kotlin ] : ./src/_122/kotlin/Solution.kt
524
526
[ 226-kotlin ] : ./src/_226/kotlin/Solution.kt
527
+ [ 504-kotlin ] : ./src/_504/kotlin/Solution.kt
525
528
[ 543-kotlin ] : ./src/_543/kotlin/Solution.kt
526
529
[ 771-kotlin ] : ./src/_771/kotlin/Solution.kt
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments