leetcode 479. Largest Palindrome Product 最大的回文数字 + 直接暴力真好
2017-12-13 09:40
591 查看
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].
想了好久才明白直接遍历所有的可能即可
建议和leetcode 680. Valid Palindrome II 去除一个字符的回文字符串判断 + 双指针 一起学习
代码如下:
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].
想了好久才明白直接遍历所有的可能即可
建议和leetcode 680. Valid Palindrome II 去除一个字符的回文字符串判断 + 双指针 一起学习
代码如下:
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; class Solution { public: int largestPalindrome(int n) { if (n == 1) return 9; int maxNum = pow(10, n), minNum = pow(10, n - 1); for (int i = maxNum; i >= minNum; i--) { string a = to_string(i), b = a; reverse(b.begin(), b.end()); long long now = stoll(a + b); for (int j = maxNum; j >= minNum; j--) { if (now / j >= maxNum) break; else if (now%j == 0) return now % 1337; } } return -1; } };
相关文章推荐
- leetcode 479. Largest Palindrome Product最大的回文乘积
- 9. Palindrome Number(判断整型数字是否是回文,直接暴力即可)
- [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积
- leetcode 363. Max Sum of Rectangle No Larger Than K 子矩形和小于K的最大值 + 动态规划DP + 暴力循环真好
- LeetCode Maximum Product Subarray_DP_最大子数组
- LeetCode 9 Palindrome Number(回文数字判断)
- 【LeetCode】152. Maximum Product Subarray最大连续子数组乘积
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
- LeetCode—Palindrome Number 数字是否是回文数字
- leetcode 9 给一个int类型的数字,判断是否是回文数
- LeetCode | Palindrome Number(回文数字)
- leetCode 9.Palindrome Number (回文数字) 解题思路和方法
- Leetcode 07 Reverse Integer(数字反转) && 09 Palindrome number(回文判断)
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
- [LeetCode] 回文数字 Palindrome Number
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
- Leetcode 9. Palindrome Number(判断回文数字)
- leetcode 327. Count of Range Sum 字段和问题 + 动态规划DP解决 + 字段数 + 暴力求解真好
- LeetCode-670:Maximum Swap (交换数字得最大整数) -- medium
- 蓝桥杯 回文数字 (暴力枚举)