LeetCode: Largest Palindrome Product

LeetCode: Largest Palindrome Product

Find the largest palindrome made from the product of two n-digit numbers.

Since the result could be very large, you should return the largest palindrome mod 1337.

Example:

1
2
3
Input: 2
Output: 987
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987

Note:

The range of n is [1,8].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Solution {
public int largestPalindrome(int n) {
if (n == 1) return 9;

int max = (int) Math.pow(10, n) - 1;
int min = (int) Math.pow(10, n - 1);
for (int i = max; i >= min; i--) {
long p = getPalindrome(i);
for (int j = max; j >= min; j--) {
if (p / j > max || p / j < min) break;
if (p % j == 0) {
return (int) (p % 1337);
}
}
}
return -1;
}

private long getPalindrome(int x) {
String value = x + new StringBuilder(x + "").reverse().toString();
return Long.valueOf(value);
}
}