|
16 | 16 | The range of n is [1,8].
|
17 | 17 | */
|
18 | 18 | 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 | + } |
25 | 28 |
|
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; |
30 | 33 |
|
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)); |
34 | 37 |
|
35 |
| - boolean palindromFound = false; |
36 |
| - long palindrom = 0; |
| 38 | + boolean palindromFound = false; |
| 39 | + long palindrom = 0; |
37 | 40 |
|
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); |
42 | 45 |
|
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 | + } |
49 | 52 |
|
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 | + } |
54 | 58 | }
|
55 |
| - } |
56 | 59 |
|
57 |
| - firstHalf--; |
| 60 | + firstHalf--; |
| 61 | + } |
| 62 | + return (int) (palindrom % 1337); |
58 | 63 | }
|
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 |
| - } |
66 | 64 |
|
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 | + } |
72 | 69 | }
|
73 | 70 | }
|
0 commit comments