吴师兄学算法day07 双指针 680. 验证回文串 II

题目:680. 验证回文串 II

易错点:

  • s[1:3] 是左闭右开

我的第一次代码:

class Solution(object):
    def validPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        isPalindrome = lambda x: x == x[::-1]
        left, right = 0, len(s) - 1
        while left <= right:
            if s[left] == s[right]:
                left += 1
                right -= 1
            else:   # 如果不相等 就要移除1位
                # 当指针来到 位置[1:3] 只包括1,2,3
                # 如果移除左侧的1,对应的s[2:4],包含2,3 # python是左开右闭的
                # 如果移除右边的3,对应的s[1:3],包含1,2 # python是取前不取后
                return isPalindrome(s[left + 1: right + 1]) or isPalindrome(s[left: right])
        return True

我的第二次代码:

class Solution:
    def validPalindrome(self, s: str) -> bool:
        ishelp = lambda x : x==x[::-1]
        
        left = 0    
        right = len(s) -1
        while left < right:
            if s[left] == s[right]:
                left +=1
                right -=1
            else:
                # 去掉左边 | 去掉右边 # 因为本身s[1:3]就是左闭右开,本身就没包含右侧
                ans = ishelp(s[left+1:right+1]) or ishelp(s[left:right])
                return ans
        return True

大佬的代码:

class Solution(object):
    def validPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        isPalindrome = lambda x : x == x[::-1]
        left, right = 0, len(s) - 1
        while left <= right:
            if s[left] == s[right]:
                left += 1
                right -= 1
            else:
                return isPalindrome(s[left + 1 : right + 1]) or isPalindrome(s[left: right])
        return True

作者:负雪明烛
链接:https://leetcode.cn/problems/valid-palindrome-ii/solutions/252740/cong-liang-ce-xiang-zhong-jian-zhao-dao-bu-deng-de/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

老师的代码:

class Solution:
    def validPalindrome(self, s: str) -> bool:

        # 判断 [ low , hight ] 这个区间的字符串是否是回文串
        def isPalindrome(low, high):

            # 左边索引的位置在 low 开始 
            left = low

            # 右边索引的位置在 high
            right = high

            # 两个索引向内移动
            # left 向右移动
            # right 向左移动
            while left < right:
                
                # 判断这两个元素值是否相同
                if s[left] != s[right]:
                    # 如果不同,直接返回 False
                    return False
                    
                # 否则,left 向右移动
                left += 1
                # right 向左移动
                right -= 1

            # 返回结果
            return True

        # 左边索引的位置在 0 开始 
        low = 0
        
        # 右边索引的位置在 len(s) - 1
        high = len(s) - 1

        # 两个索引向内移动
        # left 向右移动
        # right 向左移动
        while low < high:

            # 1、判断这两个元素值是否相同
            # 如果相同
            if s[low] == s[high]: 

                # 两个索引向内移动
                low += 1

                high -= 1

            # 2、如果不相同
            else:
                # 3、删除 low 所在这个元素,判断 [ low + 1 , high ] 是否是回文串
                # 或者
                # 4、删除 high 所在这个元素,判断 [ low  , high - 1 ] 是否是回文串
                return isPalindrome(low + 1, high) or isPalindrome(low, high - 1)
        
        # 返回结果
        return True

总结:

  • 老师的代码,手写的,更符合直觉。没有借用s[left: right] 本身的左闭右开的性质。
  • 大佬写的更简洁,需要有一定理解,以及lambda x : 写法, 还有注意有s[1:3]左闭右开的属性。
  • 无他,唯熟尔。多练习就好了。
  • 累了就歇一会,只要在路上就好。

参考:

https://r07na4yqwor.feishu.cn/docx/XoOfd8yZ3o6pSuxS2dTck2lgnjf

posted @ 2024-01-15 20:53  o蹲蹲o  阅读(3)  评论(0编辑  收藏  举报