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]

/*可以先找到最大的回文数,再检查能否被分解,如果不能就再找仅次于这个回文数的小回文数:*/
class Solution {
public:
int largestPalindrome(int n) {
if(n==) return ;
int upper = pow(, n) - ;
int lower = pow(, n - );
for (int i = upper; i > lower; --i) {
long long cand = creatPalindrome(i);
for (int j = upper; cand / j < j; --j) {
if (cand % j == ) return cand % ;
}
}
return -;
} private:
long long creatPalindrome(int n) {
string lastHalf = to_string(n);
reverse(lastHalf.begin(), lastHalf.end());
return stoll(to_string(n) + lastHalf);
}
};

【easy】479. Largest Palindrome Product的更多相关文章

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

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

  2. 479. Largest Palindrome Product

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

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

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

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

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

  5. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  6. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  7. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

  8. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  9. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

随机推荐

  1. JAVA的三个版本,JSE,JEE,JME三者之间的区别

    JAVA是一种面向对象语言由SUN公司出品 J针对不同的使用方向规划出JSE,JEE,JME三个版本 1.JSE 指标准版一般用于用户学习JAVA语言的基础也是使用其他两个版本的基础主要用于编写C/S ...

  2. springboot中访问jsp文件方式

    首先,添加加载jsp文件的依赖包: <!--jsp依赖 对应springboot版本为2.1.4--><dependency> <groupId>org.apach ...

  3. [转帖]Windows Server 2016各种版本介绍

    Windows Server 2016各种版本介绍 http://www.5sharing.com/js/zx/872.html windows server的版本 时间:2018-10-06 10: ...

  4. npm install报错

    npm ERR!    C:\Users\xxxxxx\AppData\Roaming\npm-cache\_logs\2019-02-27T07_48_47_283Z-debug.log " ...

  5. 内存溢出OOM

    如何避免OOM 异常? 想要避免OOM 异常首先我们要知道什么情况下会导致OOM 异常. 1.图片过大导致OOM Android 中用bitmap 时很容易内存溢出,比如报如下错误:Java.lang ...

  6. Retrofit原理

    Retrofit原理解析最简洁的思路 Retrofit 工作原理总结 从架构角度看Retrofit的作用.原理和启示 Retrofit主要是在create方法中采用动态代理模式实现接口方法:这个过程构 ...

  7. VBScript 学习笔记

    创建一个变量 VBScript 变量名称的规则: 必须以字母开头 不能包含点号(.) 不能超过 255 个字符 在 VBScript 的缩写中,所有的变量都与类型 variant 相关,可存储不同类型 ...

  8. 文件上传XSS引发的安全问题

    文件上传xss,一般都是上传html文件导致存储或者反射xss 一般后缀是html,之前疏忽了,没怎么考虑文件上传xss 如果没有 验证文件内容,却验证了后缀的情况下,使用: htm后缀: 测试代码: ...

  9. Evaluation of Forwarding Efficiency in NFV-Nodes Toward Predictable Service Chain Performance

    文章名称:Evaluation of Forwarding Efficiency in NFV-Nodes Toward Predictable Service Chain Performance 发 ...

  10. thrift使用

    一.什么是thrift Thrift是一种接口描述语言和二进制通讯协议,它被用来定义和创建跨语言的服务.它被当作一个远程过程调用(RPC)框架来使用,是由FaceBook为“大规模跨语言服务开发”而开 ...