You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a (decimal - e.g. 3.72) number that is passed in as a string, return the binary representation that is passed in as a string. If the fractional part of the number can not be represented accurately in binary with at most 32 characters, return ERROR.
3
15
@@ -10,6 +22,10 @@ Given a (decimal - e.g. 3.72) number that is passed in as a string, return the b
10
22
Tags Expand
11
23
String Cracking The Coding Interview Bit Manipulation
12
24
25
+
*/
26
+
27
+
/*
28
+
13
29
Thoughts:
14
30
Expan the value for binary representation:
15
31
8, 4, 2, 1, 0.5, 0.25 ... etc
@@ -23,65 +39,76 @@ Given a (decimal - e.g. 3.72) number that is passed in as a string, return the b
23
39
Checkfloat first: if float part is '0' or "", can just move on the integer part.
24
40
25
41
Note: str.split("\\.")
26
-
Note2: use a set to prevent infinite loop on float: for example: 2x - 1 = x -> x = 1. that will cause infinite loop.
42
+
Note2: use a set to prevent infinite loop on float:
43
+
for example: 2x - 1 = x -> x = 1. that will cause infinite loop.
44
+
27
45
*/
28
46
publicclassSolution {
29
47
/**
30
48
*@param n: Given a decimal number that is passed in as a string
31
49
*@return: A string
32
50
*/
33
51
publicStringbinaryRepresentation(Stringn) {
34
-
if (n.length() == 0 || n.equals("0")) {
35
-
return"0";
36
-
}
37
-
if (n.indexOf(".") == -1) {
38
-
returnparseInteger(n);
39
-
}
40
-
String[] strs = n.split("\\.");
41
-
Stringdecimal = parseDecimal(strs[1]);
42
-
if (decimal.equals("ERROR")) {
43
-
returndecimal;
44
-
}
45
-
if (decimal.length() == 0 || decimal.equals("0")) {
46
-
returnparseInteger(strs[0]);
47
-
} else {
48
-
returnparseInteger(strs[0]) + "." + decimal;
49
-
}
52
+
if (n.length() == 0 || n.equals("0")) {
53
+
return"0";
54
+
}
55
+
//If no '.', no decimal, just parseInteger
56
+
if (n.indexOf(".") == -1) {
57
+
returnparseInteger(n);
58
+
}
59
+
//Split the string by '.'
60
+
String[] strs = n.split("\\.");
61
+
//Deal with decimal first.
62
+
Stringdecimal = parseDecimal(strs[1]);
63
+
//If not applicable, it won't work, don't need to calculate integer part. Just return ERROR.
64
+
if (decimal.equals("ERROR")) {
65
+
returndecimal;
66
+
}
67
+
//Deal with integer part
68
+
if (decimal.length() == 0 || decimal.equals("0")) {
69
+
returnparseInteger(strs[0]);
70
+
} else {
71
+
returnparseInteger(strs[0]) + "." + decimal;
72
+
}
50
73
}
51
74
52
75
publicStringparseInteger(Stringn) {
53
-
if (n.length() == 0 || n.equals("0")) {
54
-
returnn;
55
-
}
56
-
intnum = Integer.parseInt(n);
57
-
Stringrst = "";
58
-
while (num != 0) {
59
-
rst = num % 2 + rst;
60
-
num = num / 2;
61
-
}
62
-
returnrst;
76
+
if (n.length() == 0 || n.equals("0")) {
77
+
returnn;
78
+
}
79
+
intnum = Integer.parseInt(n);
80
+
Stringrst = "";
81
+
while (num != 0) {
82
+
rst = num % 2 + rst;//mod(2) -> binary representation
83
+
num = num / 2;//小时候转换二进制也是这样。
84
+
}
85
+
returnrst;
63
86
}
64
-
87
+
// A little bit math, but implemtable.
65
88
publicStringparseDecimal(Stringn) {
66
-
if (n.length() == 0 || n.equals("0")) {
67
-
return"";
68
-
}
69
-
doublenum = Double.parseDouble("0." + n);
70
-
HashSet<Double> set = newHashSet<Double>();
71
-
Stringrst = "";
72
-
while (num > 0) {
73
-
if (rst.length() > 32 || set.contains(num)) {
74
-
return"ERROR";
75
-
}
76
-
set.add(num);
77
-
if (num * 2 >= 1) {
78
-
rst = rst + "1";
79
-
num = num * 2 - 1;
80
-
} else {
81
-
rst = rst + "0";
82
-
num = num * 2;
83
-
}
84
-
}
85
-
returnrst;
89
+
if (n.length() == 0 || n.equals("0")) {
90
+
return"";
91
+
}
92
+
//A doublem must be able to catch it. If not, that is way bigger than 32 bit.
93
+
doublenum = Double.parseDouble("0." + n);
94
+
//Check existance
95
+
HashSet<Double> set = newHashSet<Double>();
96
+
Stringrst = "";
97
+
while (num > 0) {
98
+
if (rst.length() > 32 || set.contains(num)) {
99
+
return"ERROR";
100
+
}
101
+
set.add(num);
102
+
//For decimal: binary code on one spot == 1, means: num * 2 - 1 > 0
0 commit comments