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导出压缩包工具类

    /** * @Title: exportZip * @Description:TODO(导出建压缩文件) * @param delAdviceinfo 为项目实体类 * @date 2018年4月4日 ...

  2. Go之运算符

    逻辑运算符用于连接布尔型表达式.在Java中不同于数学的逻辑表达 3<X<5 ,java 中应该写成 x>3 & x<5 "&" 和&quo ...

  3. 二叉树最近公共祖先(LeetCode)

    给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x 的深 ...

  4. 搭建一个MP-demo(mybatis_plus)

    MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 搭建一个简单的MP-demo 1.配置pom.xml ...

  5. Windows Server 2012 蓝屏 Wpprecorder.sys 故障

    坑爹的园区昨天停电了,导致运行中的服务器中断,来电之后,其中有一台Windows 系统的服务无法运行了,接了个显示器,发现无法进入系统了,挂掉了,这下可完蛋了,虽然做了Radio 磁盘阵列,数据不会丢 ...

  6. P1438 无聊的数列 (差分+线段树)

    题目 P1438 无聊的数列 解析: 先考虑修改,用差分的基本思想,左端点加上首项\(k\),修改区间\((l,r]\)内每个数的差分数组都加上公差\(d\),最后的\(r+1\)再减去\(k+(r- ...

  7. Linux keepalived工作原理

    keepalived简介与工作原理 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他 ...

  8. windows系统下升级nodejs

    别整那些有的没得,直接Win+R 输入:npm config ls 找到nodejs安装路径 然后上nodejs官网,下载最新安装程序,指定旧版本目录,直接安装覆盖掉 啥用n模块啥得,不适合俺们微软体 ...

  9. 使用ubuntu做为dotnet core开发环境

    一.安装google浏览器 1.下载安装包(传送门:http://www.google.cn/intl/zh-CN/chrome/browser/desktop/index.html) 2.使用sud ...

  10. Docke--基础篇

    什么是Docker? Docker 最初是dotCloud公司创始人Solomon Hykes在法国期间发起的一个公司内部项目,它是基于dotCloud公司多年云服务技术的一次革新,并与2013年3月 ...