leetcode125驗證迴文串
阿新 • • 發佈:2018-12-13
def isPalindrome(s): ''' 驗證迴文串很簡單 雙指標 但是字串查找回文串比較難 ''' strs = '' if s =='': ###定義空字元是迴文串 return True for i in s: if i.isdigit(): strs += i if i.isalpha(): strs += i.lower() else: continue print(strs) n = len(strs) l,r = 0,n-1 while l <= r: if strs[l] == strs[r]: l += 1 r -= 1 else: return False return True s = "A man, a plan, a canal: Panama" isPalindrome(s)