leetcode1332

 1 class Solution:
 2     def isPalindrome(self,s):
 3         n = len(s)
 4         i,j = 0,n-1
 5         while i < j:
 6             if s[i] != s[j]:
 7                 return False
 8             i += 1
 9             j -= 1
10         return True
11 
12     def removePalindromeSub(self, s: str) -> int:
13         n = len(s)
14         if n == 0:
15             return 0
16         if self.isPalindrome(s):
17             return 1
18         else:
19             return 2

算法思路:判断字符串是否是回文。

如果是空字符串,返回0;

如果是回文字符串,返回1;

否则,返回2。解释:第一次将所有的a都移除,第二次将所有的b都移除。因此,对于非回文字符串,最少2次就可以将其变成空串。

猜你喜欢

转载自www.cnblogs.com/asenyang/p/12234156.html
今日推荐