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

Study Plan For Algorithms - Part5

时间:2024-08-19 13:15:28浏览次数:7  
标签:匹配 Study range Algorithms Plan str 字符串 dp 回文

1. 回文数
题目链接:https://leetcode.cn/problems/palindrome-number/
给定一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

class Solution:
    def isPalindrome(self, x: int) -> bool:
        str_x = str(x)
        return str_x == str_x[::-1]

2. 正则表达式匹配
题目链接:https://leetcode.cn/problems/regular-expression-matching/
给定一个字符串 s 和一个字符规律 p,请实现一个支持 '.' 和 '*' 的正则表达式匹配。

'.' 匹配任意单个字符
'*' 匹配零个或多个前面的那一个元素
所谓匹配,是要涵盖 整个 字符串 s 的,而不是部分字符串。

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        m, n = len(s), len(p)
        dp = [[False] * (n + 1) for _ in range(m + 1)]
        dp[0][0] = True

        for j in range(1, n + 1):
            if p[j - 1] == '*':
                dp[0][j] = dp[0][j - 2]

        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if p[j - 1] == '.' or p[j - 1] == s[i - 1]:
                    dp[i][j] = dp[i - 1][j - 1]
                elif p[j - 1] == '*':
                    if p[j - 2] == '.' or p[j - 2] == s[i - 1]:
                        dp[i][j] = dp[i][j - 2] or dp[i - 1][j]
                    else:
                        dp[i][j] = dp[i][j - 2]
        return dp[m][n]

标签:匹配,Study,range,Algorithms,Plan,str,字符串,dp,回文
From: https://www.cnblogs.com/stephenxiong001/p/18367113

相关文章

  • Study Plan For Algorithms - Part4
    1.整数反转题目链接:https://leetcode.cn/problems/reverse-integer/给定一个32位的有符号整数x,返回将x中的数字部分反转后的结果。如果反转后整数超过32位的有符号整数的范围[−2^31,2^31−1],就返回0。classSolution:defreverse(self,x:int)->int:......
  • Study Plan For Algorithms - Part3
    1.最长回文子串题目链接:https://leetcode.cn/problems/longest-palindromic-substringm/给定一个字符串s,找到s中最长的回文子串classSolution:deflongestPalindrome(self,s:str)->str:defexpand_around_center(left,right):whileleft......
  • plantUML介绍和使用
    讯飞星火plantUML是一种开源工具,用于绘制UML图和其他类型的图表。使用plantUML的方法包括安装环境、编写语法、生成预览等。plantUML介绍:plantUML是一种开源工具,用于绘制UML图和其他类型的图表,如类图、时序图、用例图等。它使用简单的文本语言描述图形,然后通过特定的工具生......
  • vtkPlaneSource 显示的大些,仅用作显示
    //注意:这里的中心是0,0,0要在ACTOR中设置位置。vtkNew<vtkPlaneSource>plane1;plane1->SetCenter(0,0,0);plane1->SetNormal(normal);plane1->SetResolution(1,1);plane1->Update();//将plane里面的点每个都乘以10vtkPoints*points=plane1->GetOutput()->GetP......
  • 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不......
  • Travel Plan
    注意类似题目这种建树的方式,建出来可能是树,也可能是堆,而前者不一定是连续的编号,后者一定是连续的编号,这就导致了后者左右子树中一个是完全二叉树,另一个不是完全二叉树(这里就要利用这个性质优化时间复杂度);自己做的时候就是没有抓住这个性质导致没有做出来显然考虑贡献,设\(s_{i,j}=......
  • 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......