原题链接在这里: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的更多相关文章

  1. [LeetCode] Largest Palindrome Product 最大回文串乘积

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

  2. 【欧拉计划4】Largest palindrome product

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1371281760.html 原创:[欧 ...

  3. 【LeetCode】479. Largest Palindrome Product 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [Swift]LeetCode479. 最大回文数乘积 | Largest Palindrome Product

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

  5. Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  6. 欧拉计划之Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  7. (Problem 4)Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  8. 【easy】479. Largest Palindrome Product

    Find the largest palindrome made from the product of two n-digit numbers Since the result could be v ...

  9. Problem 4: Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

随机推荐

  1. Django 项目补充知识(JSONP,前端瀑布流布局,组合搜索,多级评论)

    一.JSONP 1浏览器同源策略 通过Ajax,如果在当前域名去访问其他域名时,浏览器会出现同源策略,从而阻止请求的返回 由于浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一 ...

  2. libhdfs的配置和使用

    测试环境:centos6.10,hadoop2.7.3,jdk1.8 测试代码:HDFSCSample.c #include "hdfs.h" #include <strin ...

  3. 【Tech】SQL Server2008使用笔记

    1.64位win7系统报错“未在本地计算机上注册 Microsoft.ACE.OLEDB.12.0 ”. 解决方法:从这个网址下载和安装一个驱动http://www.microsoft.com/zh- ...

  4. CentOS 7卸载mariadb安装mysql

    CentOS 7已经将默认集成mariadb而不是mysql,这对于多数还是依赖于mysql的应用来说,需要手动的进行更新. 可能会遇到这样错误,换成MySQL就好了. error 2002 (hy0 ...

  5. Kubernetes lxcfs

    容器实现的基础是NameSpace和Cgroups. NameSpace实现了对容器(进程)的隔离,NameSpace技术实际上修改了应用进程看待整个计算机“视图”,也就是作用域,即它的“视线”被操作 ...

  6. Python 字典Dict概念和操作

    # 字典概念:无序的, 可变的键值对集合 # 定义 # 方式1 # {key: value, key: value...} # 例如 # {"name": "xin&qu ...

  7. 2015年蓝桥杯C/C++ B组题目题解

    1. 输入一个字符串,求它包含多少个单词.单词间以一个或者多个空格分开. 第一个单词前,最后一个单词后也可能有0到多个空格.比如:" abc xyz" 包含两个单词,"a ...

  8. HttpClient技术

    1.什么是HttpClient? 2.HttpClient特点? 特点: 2.1. 基于标准.纯净的Java语言.实现了Http1.0和Http1.1 2.2. 以可扩展的面向对象的结构实现了Http ...

  9. BZOJ2764 [JLOI2011]基因补全

    Description 在 生物课中我们学过,碱基组成了DNA(脱氧核糖核酸),他们分别可以用大写字母A,C,T,G表示,其中A总与T配对,C总与G配对.两个碱基序列能相互 匹配,当且仅当它们等长,并 ...

  10. hdu 5663 Hillan and the girl 莫比乌斯反演

    Hillan and the girl Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/O ...