Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 13, 2017 21:57
Show Gist options
  • Save cixuuz/22f2c810ec39f0a10db7848b0801c3d8 to your computer and use it in GitHub Desktop.
Save cixuuz/22f2c810ec39f0a10db7848b0801c3d8 to your computer and use it in GitHub Desktop.
[479. Largest Palindrome Product] #leetcode
class Solution {
// O(n^2) O(1)
public int largestPalindrome(int n) {
if (n == 1) return 9;
int max = (int) Math.pow(10, n) - 1;
for (int v = max-1; v > max/10; v--) {
long u=Long.valueOf(v+new StringBuilder().append(v).reverse().toString());
for (long x = max; x*x > u; x--) {
if (u % x == 0) return (int) (u%1337);
}
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment