Leetcode刷题笔记,palindromic strings回文

3472. Longest Palindromic Subsequence After at Most K Operations

class Solution:
    def longestPalindromicSubsequence(self, s: str, k: int) -> int:
        def cost(a, b):
            return min(abs(ord(a) - ord(b)), 26 - abs(ord(a) - ord(b)))
        n = len(s)
        # 题目找的是subsequence不是substring
        # dp[i][j][c] stores the length of the longest palindromic subsequence for the substring s[i..j] using at most c operations.
        dp = [[[0] *(k+1) for _ in range(n)] for _ in range(n)]
        # base case, only single char
        for i in range(n):
            for j in range(k+1):
                dp[i][i][j] = 1
        # start with size 2
        for sz in range(2, n+1):
            # i is from 0 to n-size, included 
            for i in range(n-sz+1):
                j = i + sz - 1
                for c in range(k+1):
                    if s[i] == s[j]:
                        dp[i][j][c] = 2 + dp[i+1][j-1][c]
                    else:
                        option1 = dp[i+1][j][c]
                        option2 = dp[i][j-1][c]
                        option3 = 0
                        if c >= cost(s[i], s[j]):
                            option3 = 2 + dp[i+1][j-1][c-cost(s[i], s[j])]
                        dp[i][j][c] = max(option1, option2, option3)
    
        return dp[0][n-1][k]

516. Longest Palindromic Subsequence 

One possible longest palindromic subsequence is "bb".

class Solution(object):
    def longestPalindromeSubseq(self, s):
        """
        :type s: str
        :rtype: int
        """
        length = len(s)
        dp = [[0]*length for i in s]
        
        for left in xrange(length-1, -1, -1):
            dp[left][left] = 1
            for right in xrange(left+1, length):
                if s[left] == s[right]:
                    dp[left][right] = dp[left+1][right-1] + 2
                else:
                    dp[left][right] = max(dp[left+1][right], dp[left][right-1])
        return dp[0][length-1]
    

9. Palindrome Number

class Solution(object):
    def isPalindrome(self, x):
        if x < 0:
            return False
        
        ranger = 1
        while x / ranger >= 10:
            ranger *= 10
        
        while x:
            left = x / ranger
            right = x % 10
            
            if left != right:
                return False
            
            x = (x % ranger) / 10
            ranger /= 100
            
        return True

647. Palindromic Substrings 求回文,感觉有点难不知道为什么放在DP,不是用DP解

Given a string, your task is to count how many palindromic substrings in this string.

The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

Example 1:

Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".

Example 2:

Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
class Solution(object):
    def countSubstrings(self, s):
        """
        :type s: str
        :rtype: int
        """
       
        n = len(s)
        res = 0
        for i in xrange(len(s)):
            # add itself
            res += 1
            
            # even
            left = i
            right = i + 1
            while left >= 0 and right <= n-1 and s[left] == s[right]:
                res += 1
                left -= 1
                right += 1
                
            # odd
            left = i-1
            right = i+1
            while left >= 0 and right <= n-1 and s[left] == s[right]:
                res += 1
                left -= 1
                right += 1
        
        return res

 5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"
class Solution(object):
    def longestPalindrome(self, s):
        self.n = len(s)
        self.long = self.left = 0
        for i in xrange(self.n):
            # even
            self.helper(i, i+1, s)
            self.helper(i-1, i+1, s)
        return s[self.left: self.left+self.long]
    
    def helper(self, l, r, s):
        while 0 <= l and r < self.n and s[l] == s[r]:
            l -= 1
            r += 1
        l += 1
        r -= 1
        if r - l + 1 > self.long:
            self.long = r - l + 1
            self.left = l

131. Palindrome Partitioning

class Solution(object):
    def partition(self, s):
        """
        :type s: str
        :rtype: List[List[str]]
        """
        res = []
        self.helper(s, [], res)
        return res

    def helper(self, s, cur, res):
        if not s:
            res.append(cur)
            return

        for i in xrange(1, len(s) + 1):  # 这里要加1
            if self.isPal(s[:i]):
                self.helper(s[i:], cur + [s[:i]], res)

    def isPal(self, s):
        return s == s[::-1]

680. Valid Palindrome II 

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

336. Palindrome Pairs

 思路:

bot: 前缀 b 是回文, 检查对应后缀ot 的颠倒 to, 如果有to to+bot就是回文。 反过来,如果后缀t是回文, 对应前缀bo 颠倒 ob如果存在 bot+ob  就是回文。

class Solution(object):
    def palindromePairs(self, words):
        """
        :type words: List[str]
        :rtype: List[List[int]]
        """
        def check(word):
            return word == word[::-1]
        
        word_idx = {word:i for i, word in enumerate(words)}
        re = []
        for word, i in word_idx.items():
            for j in xrange(len(word)+1):
                prefix = word[:j]
                suffix = word[j:]
                if check(prefix):
                    back = suffix[::-1]
                    if back != word and back in word_idx:
                        re.append([word_idx[back], i])
                # if a palindrome can be created by appending an entire other word to the current word, then we will already consider                         # such a palindrome when considering the empty string as prefix for the other word.
                if j != len(word) and check(suffix):  
                    back = prefix[::-1]
                    if back != word and back in word_idx:
                        re.append([i, word_idx[back]])
        return re

C语言Leetcode笔记涵盖多方面内容: - **意义**:是锻炼解决问能力的好方式,能接触新的解思路和数据结构,可将过的目、笔记、思路及数据结构进行整理分享 [^2]。 - **具体笔记**: - **C语言leetcode笔记2**:有关于移动零的目,可建立新数组把非0元素先放入,剩下位置赋0;也可优化为直接在原数组操作。如`moveZeroes`函数实现: ```c void moveZeroes(int* nums, int numsSize) { int count = 0; for(int i = 0; i < numsSize; i++) { if(nums[i] != 0) { nums[count++] = nums[i]; } } for(int i = count; i < numsSize; i++) { nums[count++] = 0; } } ``` 还涉及122.买卖股票的最佳时机Ⅱ等目 [^3]。 - **C语言leetcode笔记3**:包含876.链表的中间结点(可通过遍历数节点个数、快慢指针方法求解)、874.比较含退格的字符串、155.最小栈(有getMin内部实现查找和getmin直接返回值两种方法)等目,同时有栈的使用例子及相关优化,如传递指针、初始化、动态分配、释放等 [^1]。 - **通用注意事项**: - 力扣若返回数组指针,数组首元素不能为空,否则系统认为数组全为空。 - 输出数组末尾需写上`'\0'`,否则会报地址错误,可使用`msmset`或结束时加`'\0'`。 - 使用`memset`函数。 - 函数返回线性表结果时,要用`malloc`申请线性表内存,不能直接申请数组,因为函数结束数组会被销毁。 - 使用`strlen`函数。 - `int`型变量使用负值可能报错,类似地址溢出错误。 - 计算任意类型数组大小可用`sizeof(arr) / sizeof(arr[0])` 。 - `break`不能和其他代码放一行。 - 写少用循环尤其是嵌套循环,可用多个标志位处理。 - `sizeof(数组nums)`会计算`'\0'`大小,`sizeof`计算变量占用字节数;`strlen`不计算`'\0'`大小,计算以`'\0'`结尾的字符串个数;`malloc`申请内存返回指针,做`sizeof`参数返回指针大小,此时用`strlen`。 - `malloc`申请的内存即使不返回使用,末尾也需加`'\0'`,否则报错。 - `a[10]`定义有十个元素的数组,元素从0 - 9 [^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值