LeetCode刷题实战479:最大回文数乘积
共 1709字,需浏览 4分钟
·
2021-12-28 13:45
Given an integer n, return the largest palindromic integer that can be represented as the product of two n-digits
integers. Since the answer can be very large, return it modulo 1337.
示例
示例:
输入: 2
输出: 987
解释: 99 x 91 = 9009, 9009 % 1337 = 987
说明:
n 的取值范围为 [1,8]。
解题
class Solution {
public int largestPalindrome(int n) {
if(n == 1) return 9;
long max = (long)Math.pow(10,n) - 1;
for(long i = max - 1; i > max / 10; i--){
String s1 = String.valueOf(i);
long rev = Long.parseLong(s1 + new StringBuilder(s1).reverse().toString());
for(long x = max; x * x >= rev; x --){
if(rev % x == 0) return (int)(rev % 1337);
}
}
return -1;
}
}
LeetCode刷题实战462:最少移动次数使数组元素相等 II
LeetCode刷题实战470:用 Rand7() 实现 Rand10()