LeetCode Largest Palindrome Product
原题链接在这里:https://leetcode.com/problems/largest-palindrome-product/description/
题目:
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:
Input: 2
Output: 987
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
Note:
The range of n is [1,8].
题解:
当n=2时,数字可选区间就是(9, 99]. 最大的product 就是 99*99 = 9801. 最大的位数是2n 肯定是个偶数.
取前一半firstHalf '98', 再拼接'98'的reverse '89'组成新的palindrome '9889'. 然后i在(9,99]之间挨个试palindrome % i == 0.
试不出来firstHalf--, 变'97'重来.
Time Complexity: O((upperBound*upperBound)/10^n * (upperBound*lowerBound)), upperBound = 10^n, lowerBound = upperBound/10.
Space: O(1).
AC Java:
class Solution {
public int largestPalindrome(int n) {
if(n == 1){
return 9;
}
int upperBound = (int)Math.pow(10, n) - 1;
int lowerBound = upperBound/10;
long max = (long)upperBound * (long)upperBound;
int firstHalf = (int)(max/(long)Math.pow(10,n));
boolean palFound = false;
long pal = 0;
while(!palFound){
pal = createPal(firstHalf);
for(long i = upperBound;; i--){
if(pal/i>max || i*i<pal){
break;
}
if(pal%i == 0){
palFound = true;
break;
}
}
firstHalf--;
}
return (int)(pal%1337);
}
private long createPal(int n){
String s = n + new StringBuilder().append(n).reverse().toString();
return Long.valueOf(s);
}
}
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 ...
- 【欧拉计划4】Largest palindrome product
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1371281760.html 原创:[欧 ...
- 【LeetCode】479. Largest Palindrome Product 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [Swift]LeetCode479. 最大回文数乘积 | Largest Palindrome Product
Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...
- Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...
- 欧拉计划之Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...
- (Problem 4)Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...
- 【easy】479. Largest Palindrome Product
Find the largest palindrome made from the product of two n-digit numbers Since the result could be v ...
- Problem 4: Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...
随机推荐
- 41和为S的连续正数序列
题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- Loadrunder常见问题汇总(持续更新)
1.LR 脚本为空的解决方法: 1)如果安装了IE以外的浏览器,并且IE不是默认浏览器,则无法生成录制脚本 2)如果录制脚本时IE不能打开,则需要将浏览器的IE工具高级选项中,将“启用第三方浏览器扩展 ...
- c# 单例模式(Single);单例模式的5种写法
单例模式(Singleton Pattern): 在平时的开发中,可能会用到单例模式,许多java的笔试题中也会叫笔试者写出单例模式的那几种写法并且分析.那么下面就来轻轻地探讨一下,最简单的设计模式, ...
- SpringBoot整合集成redis
Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven. ...
- Springboot文件下载
https://blog.csdn.net/stubbornness1219/article/details/72356632 Springboot对资源的描述提供了相应的接口,其主要实现类有Clas ...
- 建议10:numpy使用基础
# -*- coding: utf-8 -*- import numpy as np #---------------------------------------- #-- 定义 ndarray ...
- JS 操作复制剪切粘贴
测试了很多次之后,虽然有点细碎的突破,但还是想说,麻辣隔壁... 众所周知使用 oncut/oncopy/onpaste 监听剪切板,采用 window.clipboardData 并不是适用于大多浏 ...
- I.mx6s上移植wm8960驱动(基于linux3.0.101版本)
I.mx6s上移植wm8960驱动 此篇博文只记录移植的步骤,其他不做分析.首先上一张wm8960的硬件连接图: 1 上电操作 配置wm8960的上电脚,文件位置:arch/arm/mach ...
- [NOI2008]设计路线
题目 洛谷 BZOJ 做法 神仙题 显然这是棵树 个节点相东仅连接一个结点 不同于剖分,还能存在\("V"\)字型,一个节点最多与另外节点连两条边 \(dp[i][j][k]\)表 ...
- 键盘keyCode
字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 ...