【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<,,,,,,,}; ]; } }; 这里的穷举法,你不得不服
随机推荐
- oracle 将电话号码中间4位数以星号*代替
select replace('17665312355',substr('17665312355',4,4),'****') as phone, #类似E ...
- 脱离Editor、VS等IDE如何编译UE4工程
在Windows平台下,我们从.uproject文件生成VS解决方案.sln文件 .uproject文件用于打开Editor .sln文件用于打开VS工程 对于有增加C++代码的工程,Editor中和 ...
- 使用SpringBoot实现文件的上传
使用SpringBoot实现文件的上传 springboot可以直接使用 org.springframework.web.multipart.MultipartFile 所以非常容易实现 一.首先是简 ...
- nodejs-Path模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js Path模块 GitHub TOP Path模块 来自<JavaScript 标准参考教程(alpha)>,by ...
- SqlSession与SqlSessionFactory到底是什么关系?
1. SqlSession和SqlSessionFactory的接口定义 SqlSession: public interface SqlSession extends Closeable { ...
- Flask + Nginx + uwsgi 部署过程
一.安装Flask 1.itsdangerous tar xvf itsdangerous-0.23.tar.gz cd itsdangerous-0.23/ python setup.py inst ...
- Ruby Gems更换淘宝源方法
官方的 Rubygems 源由于有些资源放在 Amazon S3 上面,所以有时会抽风,在 Linux 下我用 proxychains gem install xxx 实现了指定程序实行 Shadow ...
- 对于HTML和XML的理解
1.什么是HTML??? HTML就是 超文本标记语言(超文本含义:超过文本 --图片 .视频.音频. 超链接) 2.HTML作用 把网页的信息格式化的展现,对网页信息进行规范化展示 连接(https ...
- lucene中创建索引库
package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...
- 如何使用table布局静态网页
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...