Skip to content

Commit 72ae5fd

Browse files
refactor 479
1 parent 9791cad commit 72ae5fd

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

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

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,55 @@
1616
The range of n is [1,8].
1717
*/
1818
public class _479 {
19-
/**reference: https://discuss.leetcode.com/topic/74125/java-solution-using-assumed-max-palindrom*/
20-
public int largestPalindrome(int n) {
21-
// if input is 1 then max is 9
22-
if (n == 1) {
23-
return 9;
24-
}
19+
public static class Solution1 {
20+
/**
21+
* credit: https://discuss.leetcode.com/topic/74125/java-solution-using-assumed-max-palindrom
22+
*/
23+
public int largestPalindrome(int n) {
24+
// if input is 1 then max is 9
25+
if (n == 1) {
26+
return 9;
27+
}
2528

26-
// if n = 3 then upperBound = 999 and lowerBound = 99
27-
int upperBound = (int) Math.pow(10, n) - 1;
28-
int lowerBound = upperBound / 10;
29-
long maxNumber = (long) upperBound * (long) upperBound;
29+
// if n = 3 then upperBound = 999 and lowerBound = 99
30+
int upperBound = (int) Math.pow(10, n) - 1;
31+
int lowerBound = upperBound / 10;
32+
long maxNumber = (long) upperBound * (long) upperBound;
3033

31-
// represents the first half of the maximum assumed palindrom.
32-
// e.g. if n = 3 then maxNumber = 999 x 999 = 998001 so firstHalf = 998
33-
int firstHalf = (int) (maxNumber / (long) Math.pow(10, n));
34+
// represents the first half of the maximum assumed palindrom.
35+
// e.g. if n = 3 then maxNumber = 999 x 999 = 998001 so firstHalf = 998
36+
int firstHalf = (int) (maxNumber / (long) Math.pow(10, n));
3437

35-
boolean palindromFound = false;
36-
long palindrom = 0;
38+
boolean palindromFound = false;
39+
long palindrom = 0;
3740

38-
while (!palindromFound) {
39-
// creates maximum assumed palindrom
40-
// e.g. if n = 3 first time the maximum assumed palindrom will be 998 899
41-
palindrom = createPalindrom(firstHalf);
41+
while (!palindromFound) {
42+
// creates maximum assumed palindrom
43+
// e.g. if n = 3 first time the maximum assumed palindrom will be 998 899
44+
palindrom = createPalindrom(firstHalf);
4245

43-
// here i and palindrom/i forms the two factor of assumed palindrom
44-
for (long i = upperBound; upperBound > lowerBound; i--) {
45-
// if n= 3 none of the factor of palindrom can be more than 999 or less than square root of assumed palindrom
46-
if (palindrom / i > maxNumber || i * i < palindrom) {
47-
break;
48-
}
46+
// here i and palindrom/i forms the two factor of assumed palindrom
47+
for (long i = upperBound; upperBound > lowerBound; i--) {
48+
// if n= 3 none of the factor of palindrom can be more than 999 or less than square root of assumed palindrom
49+
if (palindrom / i > maxNumber || i * i < palindrom) {
50+
break;
51+
}
4952

50-
// if two factors found, where both of them are n-digits,
51-
if (palindrom % i == 0) {
52-
palindromFound = true;
53-
break;
53+
// if two factors found, where both of them are n-digits,
54+
if (palindrom % i == 0) {
55+
palindromFound = true;
56+
break;
57+
}
5458
}
55-
}
5659

57-
firstHalf--;
60+
firstHalf--;
61+
}
62+
return (int) (palindrom % 1337);
5863
}
59-
return (int) (palindrom % 1337);
60-
}
61-
62-
private long createPalindrom(long num) {
63-
String str = num + new StringBuilder().append(num).reverse().toString();
64-
return Long.parseLong(str);
65-
}
6664

67-
public static void main(String... args) {
68-
System.out.println(Long.MAX_VALUE);
69-
System.out.println(Math.pow(99999999, 2) < Long.MAX_VALUE);
70-
_479 test = new _479();
71-
System.out.println(test.largestPalindrome(3));
65+
private long createPalindrom(long num) {
66+
String str = num + new StringBuilder().append(num).reverse().toString();
67+
return Long.parseLong(str);
68+
}
7269
}
7370
}

src/test/java/com/fishercoder/_479Test.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 7/19/17.
11-
*/
129
public class _479Test {
13-
private static _479 test;
10+
private static _479.Solution1 solution1;
1411

1512
@BeforeClass
1613
public static void setup() {
17-
test = new _479();
14+
solution1 = new _479.Solution1();
1815
}
1916

2017
@Test
2118
public void test1() {
22-
assertEquals(123, test.largestPalindrome(3));
19+
assertEquals(123, solution1.largestPalindrome(3));
2320
}
2421
}

0 commit comments

Comments
 (0)