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 ...
随机推荐
- range开始节点和结束节点
sc = range.startContainer, so = range.startOffsest ec = range.endContainer, eo=range.endOffset sta ...
- Linux下运行java项目
最近初步接触了linux,感觉很有新鲜感.之前在windows下干过的事情也便想到在linux环境下实现一下.正好手头在编java,就想既然java可以在windows的DOS操作下运行,是不是也可以 ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
- mongodb php 支持
http://bbs.csdn.net/topics/391931404?page=1 windows下为php7.0.4安装目前官方版本对应的最新的php_mongodb.dll扩展,该扩展版本为1 ...
- PHP辅攻_[学习资料收集]PHP连接SQLServer2005方法
PHP连接SQLServer2005 1.修改php.ini将extension=php_mssql.dll的注释删除保存. 修改php.in将mssql.secure_connection = Of ...
- PHP的垃圾回收机制以及大概实现
垃圾回收,简称gc.顾名思义,就是废物重利用的意思.再说这个之前先接触一下内存泄露,大概意思就是申请了一块地儿拉了会儿屎,拉完之后不收拾,那么这块地就算糟蹋了,地越用越少,最后一地全是屎.说到底一句, ...
- 12个提问频率最高的php面试题
你是否正在准备寻找一份PHP开发的工作,并且也在寻找一些关于PHP的面试题及答案?本文为大家分享了一些被提问频率最高的11个PHP面试题,以及对应的常规回答,每个公司都有自己的面试标准,面试和问题是完 ...
- MIPI协议中文详解【转】
本文转载自:http://www.voidcn.com/blog/michaelcao1980/article/p-6254588.html 一.MIPI MIPI(移动行业处理器接口)是Mobile ...
- oracle中检索结果汉字首字母排序详解
今天写需求,要求将结果按照成本中心首字母排序,平且空放在最前面. 进入正题: 1.使用oracle自带的函数: 按照首字母排序:nlssort(xxx,'NLS_SORT=SCHINESE_PINYI ...
- vue-router scrollBehavior无效的问题及解决方案
在使用vue做单页面应用开发时候 使用vue-router作为路由控制器 在使用过程中发现每个页面打开都在原来的位置 不能返回到页面顶部位置 ,然后查看api文档 滚动行为 发现如下代码: con ...