leetcode 479. 最大回文乘积 Largest Palindrome Product 解法 python

本文介绍了一种高效算法,用于寻找由两个n位数乘积组成的最大回文数,并通过模1337操作处理可能的极大结果。文章详细解析了解题思路,包括如何生成回文数以及精确因式分解范围,提供了完整的Python代码实现。

一.问题描述

你需要找到由两个 n 位数的乘积组成的最大回文数。

由于结果会很大,你只需返回最大回文数 mod 1337得到的结果。

示例:

输入: 2

输出: 987

解释: 99 x 91 = 9009, 9009 % 1337 = 987

说明:

n 的取值范围为 [1,8]。

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.

二.解题思路

首先由于range是[1,8],相乘后最大尾数可以到15位,因此穷举每个数,判断是不是回文数然后再穷举所有的因式分解肯定超时。

想到为何不手动生成所有的回文数然后穷举因式分解。

生成回文数的方法就是只要迭代位数的一半,然后剩下一半直接+上原来数字的逆序生成了一个回文数

这里要注意到一个规律,1个n位数乘n位数的最大回文数是2n位的数字

第一个版本正序找每个回文数的因式分解是否有,只过了4个test

想到从大的回文数到小的话,找到一个就可以马上返回,更改之后过了7个test,最后一个test过不了

之后把穷举因式分解的范围再精确一下,总算过了最后一个test

难点是确定因式分解的范围还有生成回文数的范围

更多leetcode算法题解法请关注我的专栏leetcode算法从零到结束或关注我

欢迎大家一起讨论一起刷题一起ac

三.源码

import math
class Solution:
    def largestPalindrome(self, n: int) -> int:
        start=int(math.pow(10,n-1)) # the start num to generate the palind
        end=int(math.pow(10,n)-1)   # maybe the end to generate, we should judge
       # max_num is used to judge whether the palind num exceeds the max_num
        max_num=int((math.pow(10,n)-1)*(math.pow(10,n)-1)) 
        try_start=int(math.pow(10,n-1)) # the start factor 
        try_end=int(math.pow(10,n)-1)   # the max factor
        res_now=0
        for i in range(end,start-1,-1):
            palind=self.generate_palind(i)
            if palind>max_num:
                continue
            res_now=self.find_num(palind,try_start,try_end)
            if res_now!=-1:
                return res_now
        if n==1:
            res_now=9
        return res_now
    
    def generate_palind(self,st):
        s=str(st)
        return int(s+s[::-1])  
    
    def find_num(self,palind,min_try,max_try):
        #pay attention to the boundary, otherwise you will meet TLS
        start=max(min_try,math.ceil(palind/max_try))
        # we only need to consider the mid from start to end
        # because 91*99  is the same to 99*91
        end=int((start+min(max_try,int(palind/min_try)))/2)
        for i in range(start,end):
            if palind%i==0:
                return palind%1337
        return -1

 

### LeetCode 第 5 题 '最长回文子串' 的 Python 解法 对于给定字符串 `s`,返回其中的最长回文子串是一个经典算法问题。一种高效的解决方案是利用中心扩展方法来寻找可能的最大长度回文。 #### 中心扩展法解析 该方法基于观察到的一个事实:一个回文串可以由中间向两端不断扩散而得。因此可以从每一个字符位置出发尝试构建尽可能大的回文序列[^1]。 具体来说: - 对于每个字符作为单个字符的中心点; - 或者两个相同相邻字符作为一个整体中心点; - 向两侧延伸直到遇到不匹配的情况为止; 记录下每次找到的有效回文串及其起始索引和结束索引,并更新全局最优解。 下面是具体的 Python 实现代码: ```python def longest_palindrome(s: str) -> str: if not s or len(s) == 0: return "" start, end = 0, 0 for i in range(len(s)): len1 = expand_around_center(s, i, i) len2 = expand_around_center(s, i, i + 1) max_len = max(len1, len2) if max_len > end - start: start = i - (max_len - 1) // 2 end = i + max_len // 2 return s[start:end + 1] def expand_around_center(s: str, left: int, right: int) -> int: L, R = left, right while L >= 0 and R < len(s) and s[L] == s[R]: L -= 1 R += 1 return R - L - 1 ``` 此函数通过遍历整个输入字符串并调用辅助函数 `expand_around_center()` 来计算以当前位置为中心能够形成的最长回文串长度。最终得到的结果即为所求的最大回文子串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值