Created
September 13, 2017 21:57
-
-
Save cixuuz/22f2c810ec39f0a10db7848b0801c3d8 to your computer and use it in GitHub Desktop.
[479. Largest Palindrome Product] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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