美文网首页
letcode 字符串-day

letcode 字符串-day

作者: hehehehe | 来源:发表于2023-01-29 16:28 被阅读0次

    516. 最长回文子序列

    class Solution:
        def longestPalindromeSubseq(self, s: str) -> int:
            n = len(s)
            dp = [[0] * n for _ in range(n)]
            for i in range(n - 1, -1, -1):
                dp[i][i] = 1
                for j in range(i + 1, n):
                    if s[i] == s[j]:
                        dp[i][j] = dp[i + 1][j - 1] + 2
                    else:
                        dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
            return dp[0][n - 1]
    
    
    
    class Solution:
        def palindrome(self, s, left, right):
            while left >= 0 and right < len(s) and s[left] == s[right]:
                left -= 1
                right += 1
            return s[left + 1: right]
    
        def longestPalindrome(self, s: str) -> str:
            res = ''
            for i in range(len(s)):
                s1 = self.palindrome(s, i, i)
                s2 = self.palindrome(s, i, i + 1)
                res = res if len(res) > len(s1) else s1
                res = res if len(res) > len(s2) else s2
            return res
    

    522. 最长特殊序列 II

    class Solution:
        def findLUSlength(self, strs: List[str]) -> int:
            def is_subseq(s: str, t: str) -> bool:
                pt_s = pt_t = 0
                while pt_s < len(s) and pt_t < len(t):
                    if s[pt_s] == t[pt_t]:
                        pt_s += 1
                    pt_t += 1
                return pt_s == len(s)
            
            ans = -1
            for i, s in enumerate(strs):
                check = True
                for j, t in enumerate(strs):
                    if i != j and is_subseq(s, t):
                        check = False
                        break
                if check:
                    ans = max(ans, len(s))
            
            return ans
    

    12. 整数转罗马数字

    class Solution:
        VALUE_SYMBOLS = [
            (1000, "M"),
            (900, "CM"),
            (500, "D"),
            (400, "CD"),
            (100, "C"),
            (90, "XC"),
            (50, "L"),
            (40, "XL"),
            (10, "X"),
            (9, "IX"),
            (5, "V"),
            (4, "IV"),
            (1, "I"),
        ]
    
        def intToRoman(self, num: int) -> str:
            roman = list()
            for value, symbol in Solution.VALUE_SYMBOLS:
                while num >= value:
                    num -= value
                    roman.append(symbol)
                if num == 0:
                    break
            return "".join(roman)
    
    

    890. 查找和替换模式

    class Solution:
        def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
            def match(word: str, pattern: str) -> bool:
                mp = {}
                for x, y in zip(word, pattern):
                    if x not in mp:
                        mp[x] = y
                    elif mp[x] != y:  # word 中的同一字母必须映射到 pattern 中的同一字母上
                        return False
                return True
    
            return [word for word in words if match(word, pattern) and match(pattern, word)]
    
    

    22. 括号生成

    class Solution:
        def generateParenthesis(self, n: int) -> List[str]:
            if n <= 0:
                return []
            res = []
    
            def dfs(paths, left, right):
                if left > n or right > left:
                    return
                if len(paths) == n * 2:  # 因为括号都是成对出现的
                    res.append(paths)
                    return
    
                dfs(paths + '(', left + 1, right)  # 生成一个就加一个
                dfs(paths + ')', left, right + 1)
    
            dfs('', 0, 0)
            return res
    
    

    32. 最长有效括号

    class Solution:
        def longestValidParentheses(self, s: str) -> int:
            if not s:
                return 0
            stack = []
            ans = 0
            for i in range(len(s)):
                # 入栈条件
                if not stack or s[i] == '(' or s[stack[-1]] == ')':
                    stack.append(i)  # 下标入栈
                else:
                    # 计算结果
                    stack.pop()
                    ans = max(ans, i - (stack[-1] if stack else -1))
            return ans
    
    

    344. 反转字符串

    class Solution:
        def reverseString(self, s: List[str]) -> None:
            """
            Do not return anything, modify s in-place instead.
            """
            # n = len(s)
            # left = 0
            # right = n-1
            # while left < right:
            #     tmp = s[left]
            #     s[left] = s[right]
            #     s[right] = tmp
            #     left+=1
            #     right-=1
            
            def dfs(s,left,right):
                if left>=right:
                    return
                tmp = s[left]
                s[left] = s[right]
                s[right]=tmp
                dfs(s,left+1,right-1)
    
            dfs(s,0,len(s)-1)
    
            
    

    相关文章

      网友评论

          本文标题:letcode 字符串-day

          本文链接:https://www.haomeiwen.com/subject/yrrphdtx.html