Check String Palindrom
Question
Given a string S and check if S is the palindrome
判断字符串是否是回文字符串。
Solution 1:
可以直接想到i 跟 n - i -1 进行比较。时间复杂度O(N)
class Solution:
# @param s, a string
# @return a list of lists of string
def isPalindrome(self, s):
for i in range(len(s)):
if s[i] != s[len(s)-1-i]: return False
return TrueSolution 2:
使用左右指针解题思路更清晰 而且便于扩展。
class Solution:
# @param s, a string
# @return a list of lists of string
def isPalind(self, s):
start, end = 0, len(s) - 1
while start < end:
if s[start] != s[end]:
return False
start += 1
end -= 1
return True变型题:忽略空格特殊字符,然后比较
Last updated
Was this helpful?