当前位置:   article > 正文

Leetcode刷题笔记,palindromic strings回文_dp[left][right] = dp[left + 1][right-1] and s[left

dp[left][right] = dp[left + 1][right-1] and s[left] == s[right]

516. Longest Palindromic Subsequence 

One possible longest palindromic subsequence is "bb".

  1. class Solution(object):
  2. def longestPalindromeSubseq(self, s):
  3. """
  4. :type s: str
  5. :rtype: int
  6. """
  7. length = len(s)
  8. dp = [[0]*length for i in s]
  9. for left in xrange(length-1, -1, -1):
  10. dp[left][left] = 1
  11. for right in xrange(left+1, length):
  12. if s[left] == s[right]:
  13. dp[left][right] = dp[left+1][right-1] + 2
  14. else:
  15. dp[left][right] = max(dp[left+1][right], dp[left][right-1])
  16. return dp[0][length-1]

 

9. Palindrome Number

  1. class Solution(object):
  2. def isPalindrome(self, x):
  3. if x < 0:
  4. return False
  5. ranger = 1
  6. while x / ranger >= 10:
  7. ranger *= 10
  8. while x:
  9. left = x / ranger
  10. right = x % 10
  11. if left != right:
  12. return False
  13. x = (x % ranger) / 10
  14. ranger /= 100
  15. 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:

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

Example 2:

  1. Input: "aaa"
  2. Output: 6
  3. Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
  1. class Solution(object):
  2. def countSubstrings(self, s):
  3. """
  4. :type s: str
  5. :rtype: int
  6. """
  7. n = len(s)
  8. res = 0
  9. for i in xrange(len(s)):
  10. # add itself
  11. res += 1
  12. # even
  13. left = i
  14. right = i + 1
  15. while left >= 0 and right <= n-1 and s[left] == s[right]:
  16. res += 1
  17. left -= 1
  18. right += 1
  19. # odd
  20. left = i-1
  21. right = i+1
  22. while left >= 0 and right <= n-1 and s[left] == s[right]:
  23. res += 1
  24. left -= 1
  25. right += 1
  26. 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:

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

Example 2:

  1. Input: "cbbd"
  2. Output: "bb"
  1. class Solution(object):
  2. def longestPalindrome(self, s):
  3. self.n = len(s)
  4. self.long = self.left = 0
  5. for i in xrange(self.n):
  6. # even
  7. self.helper(i, i+1, s)
  8. self.helper(i-1, i+1, s)
  9. return s[self.left: self.left+self.long]
  10. def helper(self, l, r, s):
  11. while 0 <= l and r < self.n and s[l] == s[r]:
  12. l -= 1
  13. r += 1
  14. l += 1
  15. r -= 1
  16. if r - l + 1 > self.long:
  17. self.long = r - l + 1
  18. self.left = l

131. Palindrome Partitioning

  1. class Solution(object):
  2. def partition(self, s):
  3. """
  4. :type s: str
  5. :rtype: List[List[str]]
  6. """
  7. res = []
  8. self.helper(s, [], res)
  9. return res
  10. def helper(self, s, cur, res):
  11. if not s:
  12. res.append(cur)
  13. return
  14. for i in xrange(1, len(s) + 1): # 这里要加1
  15. if self.isPal(s[:i]):
  16. self.helper(s[i:], cur + [s[:i]], res)
  17. def isPal(self, s):
  18. return s == s[::-1]

680. Valid Palindrome II 

  1. class Solution(object):
  2. def validPalindrome(self, s):
  3. """
  4. :type s: str
  5. :rtype: bool
  6. """
  7. left, right = 0, len(s)-1
  8. while left < right:
  9. if s[left] != s[right]:
  10. del_left = s[left+1:right+1]
  11. del_right = s[left:right]
  12. return del_left == del_left[::-1] or del_right == del_right[::-1]
  13. else:
  14. left += 1
  15. right -= 1
  16. return True

336. Palindrome Pairs

 思路:

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

  1. class Solution(object):
  2. def palindromePairs(self, words):
  3. """
  4. :type words: List[str]
  5. :rtype: List[List[int]]
  6. """
  7. def check(word):
  8. return word == word[::-1]
  9. word_idx = {word:i for i, word in enumerate(words)}
  10. re = []
  11. for word, i in word_idx.items():
  12. for j in xrange(len(word)+1):
  13. prefix = word[:j]
  14. suffix = word[j:]
  15. if check(prefix):
  16. back = suffix[::-1]
  17. if back != word and back in word_idx:
  18. re.append([word_idx[back], i])
  19. # 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.
  20. if j != len(word) and check(suffix):
  21. back = prefix[::-1]
  22. if back != word and back in word_idx:
  23. re.append([i, word_idx[back]])
  24. return re

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/115973
推荐阅读
  

闽ICP备14008679号