Skip to content

Commit 3c0b592

Browse files
refactor 483
1 parent dc9bac1 commit 3c0b592

File tree

1 file changed

+28
-25
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-25
lines changed

src/main/java/com/fishercoder/solutions/_483.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,39 @@
3131
*/
3232
public class _483 {
3333

34-
/**credit: https://discuss.leetcode.com/topic/82130/java-solution-with-hand-writing-explain*/
35-
public String smallestGoodBase(String n) {
36-
long nn = Long.parseLong(n);
37-
long res = 0;
38-
for (int k = 60; k >= 2; k--) {
39-
long start = 2;
40-
long end = nn;
41-
while (start < end) {
42-
long m = start + (end - start) / 2;
34+
public static class Solution1 {
35+
/**
36+
* credit: https://discuss.leetcode.com/topic/82130/java-solution-with-hand-writing-explain
37+
*/
38+
public String smallestGoodBase(String n) {
39+
long nn = Long.parseLong(n);
40+
long res = 0;
41+
for (int k = 60; k >= 2; k--) {
42+
long start = 2;
43+
long end = nn;
44+
while (start < end) {
45+
long m = start + (end - start) / 2;
4346

44-
BigInteger left = BigInteger.valueOf(m);
45-
left = left.pow(k).subtract(BigInteger.ONE);
46-
BigInteger right = BigInteger.valueOf(nn).multiply(BigInteger.valueOf(m).subtract(BigInteger.ONE));
47-
int cmr = left.compareTo(right);
48-
if (cmr == 0) {
49-
res = m;
50-
break;
51-
} else if (cmr < 0) {
52-
start = m + 1;
53-
} else {
54-
end = m;
47+
BigInteger left = BigInteger.valueOf(m);
48+
left = left.pow(k).subtract(BigInteger.ONE);
49+
BigInteger right = BigInteger.valueOf(nn).multiply(BigInteger.valueOf(m).subtract(BigInteger.ONE));
50+
int cmr = left.compareTo(right);
51+
if (cmr == 0) {
52+
res = m;
53+
break;
54+
} else if (cmr < 0) {
55+
start = m + 1;
56+
} else {
57+
end = m;
58+
}
5559
}
56-
}
5760

58-
if (res != 0) {
59-
break;
61+
if (res != 0) {
62+
break;
63+
}
6064
}
65+
return "" + res;
6166
}
62-
63-
return "" + res;
6467
}
6568

6669
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy