?? 題目描述
?? leetcode鏈接:https://leetcode.cn/problems/valid-palindrome/
思路:
這道題只判斷字符串中的字母與數(shù)字是否是回文。雖然小寫大寫字母可以互相轉(zhuǎn)換,但是里面是含有數(shù)字字符的,所以先統(tǒng)一,把字符串中所有的字母都轉(zhuǎn)換成大寫或者小寫,然后一個下標(biāo)從左開始尋找一個下標(biāo)從右開始尋找匹配的字符,直到都滿足在判斷是否相等若一直是相等則是回文,否則不是回文。文章來源:http://www.zghlxwxcb.cn/news/detail-662299.html
代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-662299.html
class Solution {
public:
bool isPalindrome(string s) {
// 全部統(tǒng)一為小寫
for (auto& val : s) {
if (isupper(val)) {
val += 32;
}
}
int left = 0;
int right = s.size() - 1;
while (left < right) {
while (left < right && !isalnum(s[left])) {
left++;
}
while (left < right && !isalnum(s[right])) {
right--;
}
// 判斷是否相等
if (s[left] == s[right]) {
left++;
right--;
continue;
}
return false;
}
return true;
}
};
到了這里,關(guān)于leetcode 125.驗證回文串的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!