【LeetCode】479. Largest Palindrome Product 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: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位数字乘积能构成的最大回文数字。
解题方法
题目本身不是难题,但是普通方法肯定会超时。。这个题我没认真做,抄了一个别人的答案。我觉得这个题没什么意思。
class Solution(object):
def largestPalindrome(self, n):
if n==1: return 9
if n==2: return 987
for a in xrange(2, 9*10**(n-1)):
hi=(10**n)-a
lo=int(str(hi)[::-1])
if a**2-4*lo < 0: continue
if (a**2-4*lo)**.5 == int((a**2-4*lo)**.5):
return (lo+10**n*(10**n-a))%1337
二刷,下面的python代码依然会超时,尴尬。思路是找出n位数字的上界和下界,然后我们把目标在这区间内进行搜索,看这个区间内的每个数字给构成回文,然后判断这个数字能否被区间内的数字整除。
class Solution(object):
def largestPalindrome(self, n):
"""
:type n: int
:rtype: int
"""
upper = 10 ** n - 1
lower = upper / 10
for i in xrange(upper, lower, -1):
l = int(str(i) + str(i)[::-1])
j = upper
while j * j > l:
if l % j == 0:
return l % 1337
j -= 1
return 9
C++代码效率比较高,能通过。
class Solution {
public:
int largestPalindrome(int n) {
int upper = pow(10, n) - 1;
int lower = upper / 10;
for (int i = upper; i > lower; i--){
string s = to_string(i);
long p = stol(s + string(s.rbegin(), s.rend()));
for (long j = upper; j * j > p; j--){
if (p % j == 0){
return p % 1337;
}
}
}
return 9;
}
};
日期
2018 年 2 月 28 日
2018 年 11 月 30 日 —— 又到了周末
【LeetCode】479. Largest Palindrome Product 解题报告(Python & C++)的更多相关文章
- 【easy】479. Largest Palindrome Product
Find the largest palindrome made from the product of two n-digit numbers Since the result could be v ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- LeetCode 812 Largest Triangle Area 解题报告
题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 【LeetCode】266. Palindrome Permutation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 479 Largest Palindrome Product 最大回文数乘积
你需要找到由两个 n 位数的乘积组成的最大回文数.由于结果会很大,你只需返回最大回文数 mod 1337得到的结果.示例:输入: 2输出: 987解释: 99 x 91 = 9009, 9009 % ...
- 479. Largest Palindrome Product
class Solution { public: int largestPalindrome(int n) { vector<,,,,,,,}; ]; } }; 这里的穷举法,你不得不服
随机推荐
- C#集合Dictionary中按值的排序
C#集合Dictionary中按值的降序排列 static void Main(string[] args) { Dictionary<string, int> d ...
- 使用 Skywalking 对 Kubernetes(K8s)中的微服务进行监控
1. 概述 老话说的好:任何成功都不是轻易得来的,是不断地坚持与面对的结果. 言归正传,之前我们聊了 SpringCloud 开发的微服务是如何部署在 Kubernetes(K8s)集群中的,今天我 ...
- [源码解析] PyTorch分布式优化器(1)----基石篇
[源码解析] PyTorch分布式优化器(1)----基石篇 目录 [源码解析] PyTorch分布式优化器(1)----基石篇 0x00 摘要 0x01 从问题出发 1.1 示例 1.2 问题点 0 ...
- clickhouse 输入输出格式
TabSeparated.TabSeparatedRaw.TabSeparatedWithNames和TabSeparatedWithNamesAndTypes TabSeparated 默认格式,缩 ...
- Spring.DM web开发环境搭建
作为一个初学者来说,搭建好Spring.DM 的web开发环境还是有些麻烦的.我就遇到了N多麻烦,走了很多弯路.本文介绍了2种比较简单的搭建Spring.DM OSGi web开发环境的搭建. 第 ...
- centos7.4 64位安装 git
参考博客:Linux Jenkins配置Git 1. git --version 查看有没有安装 过 git,没有则 继续 2. git 压缩包下载地址:https://mirrors.edge.ke ...
- maven管理本地jar包
maven作为包管理工具,好处不必多说.但是有些情况,比如需要引入第三方包,如快递鸟,支付宝,微信等jar包(当然有可能直接提供maven依赖),如果直接下载到本地之后,怎么整合到自己的maven工程 ...
- Python enumerate():使用计数器简化循环
摘要:当您需要计数和迭代中的值时,Pythonenumerate()允许您编写 Pythonicfor循环.最大的优点enumerate()是它返回一个带有计数器和值的元组,因此您不必自己增加计数器. ...
- Unity——WegGL打包问题
Rendering设置 Gamma和Linear颜色空间,两者有色差,Gamma有个2.25左右的修正值: WebGL2.0可用的情况,只支持Deferred Render延迟渲染,且只支持Linea ...
- Unity实现“笼中窥梦”的渲染效果
效果 思路 5个面用5个RenderTexture来接受5个摄像机分别获取的小场景图像: RenderTexture就当成屏幕来理解,MainCamera是把画面显示在屏幕上,屏幕就是最大的Rende ...