作者: 负雪明烛
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++)的更多相关文章

  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. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  3. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

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

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

  5. LeetCode 976 Largest Perimeter Triangle 解题报告

    题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  6. 【LeetCode】266. Palindrome Permutation 解题报告(C++)

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

  7. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

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

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

  9. 479. Largest Palindrome Product

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

随机推荐

  1. 什么是GP、LP、PE、VC、FOF?

    GP GP是General Partner的缩写,意思是普通合伙人.投资者经常听到的一些基金.风投等投资公司采用的就是普通合伙人的制度,在美国等发达国家,普通合伙人很常见. 其实,说白了,GP最开始指 ...

  2. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  3. A Child's History of England.24

    Besides all these troubles, William the Conqueror was troubled by quarrels among his sons. He had th ...

  4. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...

  5. Docker学习(四)——Docker容器连接

    Docker容器连接     容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过-P或-p参数来指定端口映射. 下面我们来实现通过端口连接到一个docker容器. 1.网络端口映射    ...

  6. Maven pom.xml报错解决

    用Maven建了一个web工程,总是在pom.xml头的地方报错: 大概是: Original error: Could not transfer artifact org.hamcrest:hamc ...

  7. 【Java 8】Predicate详解

    一.java.util.function.Predicate 这里类是java自带主要广泛用在支持lambda表达式的API中. 1.接口源码 @FunctionalInterface public ...

  8. BigDecimal 中 关于RoundingMode介绍

    RoundingMode介绍 RoundingMode是一个枚举类,有以下几个常量:UP.DOWN.CEILING.FLOOR.HALF_UP.HALF_DOWN.HALF_EVEN.UNNECESS ...

  9. Synchronized和Lock接口

    关于synchronized字段,不管该关键字是修饰方法还是修饰同步代码块,synchronzed拿到的都是对象. 当synchronized修饰的是方法时,synchronized所拿到的是调用该方 ...

  10. Python matplotlib绘图设置坐标轴的标题

    一.语法简介 plt.xlabel("销售月份",fontsize=16,color='red',fontweight='bold',loc='center',background ...