题目描述:

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].

要完成的函数:

int largestPalindrome(int n)

说明:

1、给定一个数字n,我们可以形成一个n位的数字(十进制),比如n=2,那么我们可以形成99或者98或者12这些两位的数字。

要求从两个n位的数字的积中找到最大的回文数,比如n=2,那么我们可以形成99/99这两个2位的数字,然后积是9801,不是回文数,那么我们就要继续往下找,99*98=9702,也不是……一直往下找,直到99*91=9009这个回文数。

由于回文数数值比较大,所以我们返回回文数%1337的结果就好。

2、这道题传统解法是找到n位数字的最大可能值和最小可能值,比如n=2,那么上限就是99,下限就是10,然后在上下限之间的数字彼此相乘,逐个判断是否为回文数。

这种方法也能解出来,不过就是很慢。

你得找出所有数字相乘得到的积,然后一个个判断是否是回文数。

但找出所有数字相乘得到的积,不能像下面这样写:

    bool ishuiwen(long t)
{
long result=0,t1=t;
while(t!=0)//得到反转之后的数,存储在result中
{
result=result*10+t%10;
t/=10;
}
return result==t1;
}
int largestPalindrome(int n)
{
if(n==1)
return 9;
int uplim=pow(10,n)-1,lowlim=pow(10,n-1);
long t;
for(long i=uplim;i>=lowlim;i--)//双重循环
{
for (long j=i;j>=lowlim;j--)
{
t=i*j;
if(ishuiwen(t))
return t%1337;
}
}
}

上面这样写会出错的,因为双重循环从最开始的 i = 99,然后 j 一直减小,直到 i 和 j 相乘的结果是一个回文数,假设是99*55。但可能还有一个更大的回文数,假设是98*89这种,而98*89这个结果根本不会被计算出来,我们算到99*55就return了。

我们用双重循环的话,得计算出所有相乘的结果,然后一个个判断是否是回文数,最后返回最大的那个。

这样做太慢了。

我们尝试一下生成法,生成所有可能的回文数,然后逐个判断是否是上下限之间的数相乘的结果。这样能大大减少需要判断的个数,而且判断的过程就是判断能不能整除,非常快速。

代码如下:(附详解)

    long buildhuiwen(int n)//建立回文数
{
long n1=n,result=0;
while(n!=0)
{
result=result*10+n%10;
n/=10;
n1*=10;
}
return n1+result; }
int largestPalindrome(int n)
{
if(n==1)
return 9;
int uplim=pow(10,n)-1,lowlim=pow(10,n-1);//得到上限是uplim,下限是lowlim
for(int i=uplim;i>=lowlim;i--)
{
long cand=buildhuiwen(i);//建立2n位的回文数,比如i=99,那么建立的回文数是9999,比如i=98,建立的回文数9889,构造回文序列。
for (long j=uplim;j*j>=cand;j--)//判断得到的回文数是否能整除n位的数字
{
if(cand%j==0)
return cand%1337;
}
}
}

上述代码中的外层循环,构造了一个回文序列,如果n=2,那么构造的回文序列是9999,9889,9779,9669,9559,9449……

接着在内层循环中,判断构造的每一个回文数是否能整除n位数。

这样做快了很多,减少了要判断的个数,而且判断过程也简化了,只需要判断是否整除。

细心的同学可能会发现,内层循环中的long j=uplim;j*j>=cand;j--,c++支持大数乘法了……

之前c++11没出的时候,大数乘法和加法都是要手工写代码实现的,现在不用啦。

上述代码实测366ms,beats 86.19% of cpp submissions。

leetcode-479-Largest Palindrome Product(找到两个乘数相乘得到的最大的回文数)的更多相关文章

  1. 【easy】479. Largest Palindrome Product

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

  2. 479 Largest Palindrome Product 最大回文数乘积

    你需要找到由两个 n 位数的乘积组成的最大回文数.由于结果会很大,你只需返回最大回文数 mod 1337得到的结果.示例:输入: 2输出: 987解释: 99 x 91 = 9009, 9009 % ...

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

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

  4. 479. Largest Palindrome Product

    class Solution { public: int largestPalindrome(int n) { vector<,,,,,,,}; ]; } }; 这里的穷举法,你不得不服

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

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

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

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

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

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

  8. Largest palindrome product

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

  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. C#中释放数据库连接资源

    1.确保释放数据库连接资源的两种方式如下:   a.使用try...catch...finally语句块,在finally块中关闭连接:   b.使用using语句块,无论如何退出,都会自动关闭连接: ...

  2. Eclipse自动补全修改

    一.前言 之前敲代码用的是文本工具sublime,转到Eclipse之后发现补全功能特别不方便,所以想根据自己的情况进行调整,具体有两点: 输入某些语句的前几个字母就能自动提示相关的完整语句 用tab ...

  3. Basic4android v3.20 发布

    这次主要是可视化设计器的增强. 具体新功能如下: This version includes many important improvements: Visual designer Anchors ...

  4. spark使用idea以client模式提交应用到standalone集群

    使用idea以client方式提交代码到standalone集群非常简单. 1.首先有一个部署好且已经启动的standalone集群 --------------------------------- ...

  5. 优秀前端工程师必备: 我要一个新窗口: js开新窗的2种姿势

    1.<a href="https://www.cnblogs.com/" title="博客园">当前页面打开博客园</a> js代码等 ...

  6. UVaLive 3126 Taxi Cab Scheme (最小路径覆盖)

    题意:有 n 个客人,要从 si 到 ti,每个人有一个出发时间,现在让你安排最少和出租车去接,在接客人时至少要提前一分钟到达客人的出发地点. 析:把每个客人看成一个结点,然后如果用同一个出租车接的话 ...

  7. Layout布局源码浅析之——FrameLayout

    一直想研究下安卓各种布局控件,FrameLayout是安卓最简单的界面布局,所以就从FrameLayout讲起. 1.属性.frameLayout继承ViewGroup,除了拥有ViewGroup的属 ...

  8. Oracle EBS 初始化用户密码

    ---修改密码,并且将限制用户下次登录的时候(第一次登录),强制要换一个新的口令: ---此过程可以完全模拟我们在标准用户的Form里面初始化用户的密码的动作!   ---最后要说明的是,这个处理过程 ...

  9. DELPHI移动端支付宝支付

    Delphi XE7 Android 应用接入支付宝SDK的方法 1      应用场景和准备工作: 采用XE系列开发的android apps. apps中需要集成支付宝的支付能力. 支付到指定的商 ...

  10. 在Team Foundation Server (TFS)的代码库或配置库中查找文件或代码

    [update 2017.2.11] 最新版本的TFS 2017已经增加了代码搜索功能,可以参考这个链接 https://blogs.msdn.microsoft.com/visualstudioal ...