首页 > 其他分享 >Study Plan For Algorithms - Part3

Study Plan For Algorithms - Part3

时间:2024-08-17 23:40:25浏览次数:9  
标签:index right res Study len numRows Part3 Algorithms left

1.最长回文子串
题目链接:https://leetcode.cn/problems/longest-palindromic-substringm/
给定一个字符串 s,找到 s 中最长的 回文 子串

class Solution:
    def longestPalindrome(self, s: str) -> str:
        def expand_around_center(left, right):
            while left >= 0 and right < len(s) and s[left] == s[right]:
                left -= 1
                right += 1
            return s[left + 1:right]        
            
        res = ""
        for i in range(len(s)):
            palindrome1 = expand_around_center(i, i)
            palindrome2 = expand_around_center(i, i + 1)
            if len(palindrome1) > len(res):
                res = palindrome1
            if len(palindrome2) > len(res):
                res = palindrome2
        return res

2.Z 字形变换
题目链接:https://leetcode.cn/problems/zigzag-conversion/
将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1 or numRows >= len(s):
            return s

        rows = ['' for _ in range(numRows)]
        index, step = 0, 1

        for char in s:
            rows[index] += char
            if index == 0:
                step = 1
            elif index == numRows - 1:
                step = -1
            index += step

        return ''.join(rows)

标签:index,right,res,Study,len,numRows,Part3,Algorithms,left
From: https://www.cnblogs.com/stephenxiong001/p/18365161

相关文章

  • Study Plan For Algorithms - Part2
    1.无重复字符的最长子串题目链接:https://leetcode.cn/problems/longest-substring-without-repeating-characters/给定一个字符串s,请找出其中不含有重复字符的最长子串的长度。classSolution:deflengthOfLongestSubstring(self,s:str)->int:char_dic......
  • python系列&deep_study系列:一文讲清chatGPT的发展历程、能力来源和复现它的关键之处
    这里写目录标题一文讲清chatGPT的发展历程、能力来源和复现它的关键之处1.ChatGPT是什么2.ChatGPT以及GPT系列模型2.1GPT-1/2/32.2GPT-3.5(InstructGPT)3.复现chatGPT的难点3.1海量的数据3.2超大规模的模型架构3.3深度人员参与(SFT,FeedME,PPO)3.4长期技术积累3......
  • python系列&deep_study系列:TOCH_npu不适配报错packages/torchaudio/lib/libtorchaudio
    TOCH_npu不适配报错packages/torchaudio/lib/libtorchaudio.so:undefinedsymbol:_ZNK5torch8autograd4Node4nTOCH_npu不适配报错packages/torchaudio/lib/libtorchaudio.so:undefinedsymbol:_ZNK5torch8autograd4Node4n报错:背景:解决办法:TOCH_npu不......
  • 159.302 The 8-Puzzle: Search Algorithms
    159.302ArtificialIntelligenceAssignment#1The8-Puzzle:SearchAlgorithmsMaximumnumberofmemberspergroup:3studentsDeadlineforsubmission:9thofSeptemberInstructionsYourtaskistowriteaC++programthatwillsolvethe8-puzzleprob......
  • AI Python for Beginners-Andrew吴恩达-study notes(2)
    1Introduction    itisbelievedthatwiththehelpofAIchatbotwecanlearnpythonmoreeasilyanditwillbeamazingtoautomatetasksusingPython2 CompletingatasklistwithAI2.1List①listisasinglevariableoftype thatholdsm......
  • VDI/VDE 2643 Part3 2008:10
    [!NOTE]原始PDF链接:https://www.doc88.com/p-50359701027029.htmlOptical3D-measuringsystemsMultipleviewsystemsbasedonareascanningPreliminarynoteThecontentofthisguidelinehasbeendevelopedinstrictaccordancewiththerequirementsandrecomme......
  • COMPSCI 753 Algorithms for Massive Data
    COMPSCI753AlgorithmsforMassiveDataAssignment1/Semester2,2024GraphMiningGeneralinstructionsanddataThisassignmentaimsatexploringthePageRankalgorithmonbigreal-worldnetworkdata.Byworkingonthisassignment,youwilllearn......
  • 在PhpStudy中安装joomla5.1
    joomla5.1对环境要求是PHP8.1以上;mysql8.0.13以上本地使用的phpstudy安装环境,phpstudyphp版本可以到8.2,但mysql版本只有8.0.12,刚好离要求的版本低一点,经过一番折腾终于安装成功,下面记录下安装过程中出现的问题。一、安装PHP8.21、如果PhpStudy没有安装PHP8.2,需要在控制......
  • ImportError:无法从“jwt.algorithms”导入名称“RSAAlgorithm”
    RSAAlgorithmPyJWT算法方法无法导入,但我确实安装了PyJWT错误:ImportError:cannotimportname'RSAAlgorithm'from'jwt.algorithms'我通过运行以下命令检查了该包是否可用:poetryshow|grep-ipyjwtpyjwt2.9.0J......
  • Studying-代码随想录训练营day62| Floyd 算法精讲、A*算法精讲(A star算法)、最短路算法
    第62天,完结撒花*★,°*:.☆( ̄▽ ̄)/$:*.°★*,最后的两个算法学习,编程语言C++目录Floyd算法精讲A*算法精讲(Astar算法) A*算法 复杂度分析 A*算法的缺点最短路算法总结篇 图论总结深搜和广搜并查集最小生成树 拓扑排序 最短路算法 总结 Floyd算法精讲......