【easy】479. Largest Palindrome Product
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的更多相关文章
- 【LeetCode】479. Largest Palindrome Product 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 479. Largest Palindrome Product
class Solution { public: int largestPalindrome(int n) { vector<,,,,,,,}; ]; } }; 这里的穷举法,你不得不服
- 479 Largest Palindrome Product 最大回文数乘积
你需要找到由两个 n 位数的乘积组成的最大回文数.由于结果会很大,你只需返回最大回文数 mod 1337得到的结果.示例:输入: 2输出: 987解释: 99 x 91 = 9009, 9009 % ...
- 【欧拉计划4】Largest palindrome product
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1371281760.html 原创:[欧 ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
- 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 ...
- 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 ...
随机推荐
- Selenium WebDriver原理(一):Selenium WebDriver 是怎么工作的?
首先我们来看一个经典的例子: 搭出租车 在出租车驾驶中,通常有3个角色: 乘客 : 他告诉出租车司机他想去哪里以及如何到达那里 对出租车司机说: 1.去阳光棕榈园东门 2.从这里转左 3.然后直行 2 ...
- Asp.net mvc 动作方法中 调用 另一个控制器的动作方法
public ActionResult 动作方法(){ var otherController = DependencyResolver.Current.GetService<另一个控制器的类名 ...
- php框架之phalcon
1.开发助手 1) 下载 git clone https://github.com/phalcon/cphalcon.git git clone https://github.com/phalcon/ ...
- flutter-开发总结
### 上拉加载下拉刷新 ``` import 'dart:async'; import 'package:flutter_easyrefresh/easy_refresh.dart'; import ...
- 解决win环境下访问本机虚拟机中centos7 ftp服务器的问题
inux搭建ftp服务器 1.安装软件: yum install vsftpd 2.修改配置文件vsftpd.conf: vim /etc/vsftpd/vsftpd.conf 把anonymous_ ...
- 【MySQL 读书笔记】RR(REPEATABLE-READ)事务隔离详解
这篇我觉得有点难度,我会更慢的更详细的分析一些 case . MySQL 的默认事务隔离级别和其他几个主流数据库隔离级别不同,他的事务隔离级别是 RR(REPEATABLE-READ) 其他的主流数据 ...
- 快速实现兼容的js复制方式。有点非正规,通过非正规的排版实现。
<div style="height:1px; width:1px; overflow:hidden; position:absolute;left:-2px;"> & ...
- ueditor 回显html样式会直接出来,无效
百度了好久,后面发现可以直接这样
- 【DeepLearning】优化算法:SGD、GD、mini-batch GD、Moment、RMSprob、Adam
优化算法 1 GD/SGD/mini-batch GD GD:Gradient Descent,就是传统意义上的梯度下降,也叫batch GD. SGD:随机梯度下降.一次只随机选择一个样本进行训练和 ...
- 2017年末大总结(by一个软件开发实习生)
时间本是一条连续不断的河流,我们却生造出了一些有头有尾的节点,不知道是不是为了增加一点仪式感呢. 今年最大的变化就是学会了java并找到一份了实习,很幸运能进入这个行业,做一份热爱的工作.从开始自学编 ...