479. Largest Palindrome Product

来源:互联网 发布:淘宝网书城首页 编辑:程序博客网 时间:2024/05/21 14:44

题目:

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

思路:

本题属于简单题,求最大回文,直接的思路是构造回文并比较,但是感觉这样做不简单,于是思考,回文特征,发现两数成绩构造的最大回文是有限个数:9, 9009, 906609, 99000099, 9966006699, 999000000999,99956644665999, 9999000000009999,所以本题使用枚举法:

代码:

class Solution {public:    int largestPalindrome(int n) {        vector<long> res = {9, 9009, 906609, 99000099, 9966006699, 999000000999, \                    99956644665999, 9999000000009999};        return int(res[n - 1] % 1337);        }};


阅读全文
0 0
原创粉丝点击