File tree 3 files changed +51
-0
lines changed
lcci/05.02.Binary Number to String
3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,24 @@ func printBin(num float64) string {
108
108
}
109
109
```
110
110
111
+ ``` swift
112
+ class Solution {
113
+ func printBin (_ num : Double ) -> String {
114
+ var num = num
115
+ var ans = " 0."
116
+
117
+ while ans.count < 32 && num != 0 {
118
+ num *= 2
119
+ let x = Int (num)
120
+ ans.append (" \( x ) " )
121
+ num -= Double (x)
122
+ }
123
+
124
+ return num != 0 ? " ERROR" : ans
125
+ }
126
+ }
127
+ ```
128
+
111
129
<!-- tabs: end -->
112
130
113
131
<!-- end -->
Original file line number Diff line number Diff line change @@ -115,6 +115,24 @@ func printBin(num float64) string {
115
115
}
116
116
```
117
117
118
+ ``` swift
119
+ class Solution {
120
+ func printBin (_ num : Double ) -> String {
121
+ var num = num
122
+ var ans = " 0."
123
+
124
+ while ans.count < 32 && num != 0 {
125
+ num *= 2
126
+ let x = Int (num)
127
+ ans.append (" \( x ) " )
128
+ num -= Double (x)
129
+ }
130
+
131
+ return num != 0 ? " ERROR" : ans
132
+ }
133
+ }
134
+ ```
135
+
118
136
<!-- tabs: end -->
119
137
120
138
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func printBin( _ num: Double ) -> String {
3
+ var num = num
4
+ var ans = " 0. "
5
+
6
+ while ans. count < 32 && num != 0 {
7
+ num *= 2
8
+ let x = Int ( num)
9
+ ans. append ( " \( x) " )
10
+ num -= Double ( x)
11
+ }
12
+
13
+ return num != 0 ? " ERROR " : ans
14
+ }
15
+ }
You can’t perform that action at this time.
0 commit comments