Skip to content

Commit 6bd6dfc

Browse files
committed
Base 7.
1 parent 440868f commit 6bd6dfc

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>476.Number Complement (Playground).xcscheme</key>
8+
<dict>
9+
<key>isShown</key>
10+
<false/>
11+
<key>orderHint</key>
12+
<integer>0</integer>
13+
</dict>
14+
</dict>
15+
</dict>
16+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>485.Max Consecutive Ones (Playground).xcscheme</key>
8+
<dict>
9+
<key>isShown</key>
10+
<false/>
11+
<key>orderHint</key>
12+
<integer>0</integer>
13+
</dict>
14+
</dict>
15+
</dict>
16+
</plist>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
Given an integer num, return a string of its base 7 representation.
3+
4+
 
5+
6+
Example 1:
7+
Input: num = 100
8+
Output: "202"
9+
10+
Example 2:
11+
Input: num = -7
12+
Output: "-10"
13+
 
14+
15+
Constraints:
16+
- -107 <= num <= 107
17+
18+
*/
19+
class Solution {
20+
func convertToBase7(_ num: Int) -> String {
21+
if num == 0 { return "0" }
22+
var x = 0
23+
while pow(7, x) <= abs(num) {
24+
x += 1
25+
}
26+
x -= 1
27+
let res = decimalToBinary(abs(num), maxBit: x)
28+
return num > 0 ? res.lazy.reversed().map { String($0) }.joined() : "-" + res.lazy.reversed().map { String($0) }.joined()
29+
}
30+
31+
private func pow(_ base: Int, _ power: Int) -> Int {
32+
if base == 0 { return 0 }
33+
var res = 1
34+
for _ in 0..<power {
35+
res *= base
36+
}
37+
return res
38+
}
39+
40+
private func decimalToBinary(_ num: Int, maxBit bit: Int) -> [Int] {
41+
var n = num
42+
var i = bit
43+
var arr = Array(repeating: 0, count: i + 1)
44+
while i >= 0 && n > 0 {
45+
var x = 1
46+
while x * pow(7, i) <= n {
47+
x += 1
48+
}
49+
x -= 1
50+
n -= x * pow(7, i)
51+
arr[i] = x
52+
i -= 1
53+
}
54+
return arr
55+
}
56+
}
57+
58+
let s = Solution()
59+
let r = s.convertToBase7(100)
60+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/504.Base 7.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
97. [Teemo Attacking](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/495.Teemo%20Attacking.playground/Contents.swift)
102102
98. [Next Greater Element I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/496.Next%20Greater%20Element%20I.playground/Contents.swift)
103103
99. [Keyboard Row](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/500.Keyboard%20Row.playground/Contents.swift)
104+
100. [Base 7](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/501.Base%207.playground/Contents.swift)
104105

105106
#### Medium
106107

0 commit comments

Comments
 (0)