C++ Leetcode初級演算法之驗證迴文字串
阿新 • • 發佈:2018-11-28
給定一個字串,驗證它是否是迴文串,只考慮字母和數字字元,可以忽略字母的大小寫。
說明:本題中,我們將空字串定義為有效的迴文串。
示例 1:
輸入: “A man, a plan, a canal: Panama”
輸出: true
示例 2:
輸入: “race a car”
輸出: false
class Solution { public: bool isPalindrome(string s) { int start = 0; int end = s.length()-1; while(start < end) { if(!isalnum(s[start])) start++; else if(!isalnum(s[end])) end--; else if(tolower(s[start++]) != tolower(s[end--])) return false; } return true; } };