File tree Expand file tree Collapse file tree 6 files changed +104
-0
lines changed
476.Number Complement.playground/xcuserdata/recherst.xcuserdatad/xcschemes
485.Max Consecutive Ones.playground/xcuserdata/recherst.xcuserdatad/xcschemes Expand file tree Collapse file tree 6 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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)
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change 101
101
97 . [ Teemo Attacking] ( https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/495.Teemo%20Attacking.playground/Contents.swift )
102
102
98 . [ Next Greater Element I] ( https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/496.Next%20Greater%20Element%20I.playground/Contents.swift )
103
103
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 )
104
105
105
106
#### Medium
106
107
You can’t perform that action at this time.
0 commit comments