机缘
[https://leetcode.cn/problems/valid-palindrome/]
leetcode125. 验证回文串
如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。
收获
class Solution {
public:
bool isPalindrome(string s) {
string sgood;
for (char ch: s) {
if (isalnum(ch)) {
sgood += tolower(ch);
}
}
string sgood_rev(sgood.rbegin(), sgood.rend());
return sgood == sgood_rev;
}
};
C 库函数 void isalnum(int c) 检查所传的字符是否是字母和数字。如果 c 是一个数字或一个字母,则该函数返回非零值,否则返回 0。
C 库函数 int tolower(int c) 把给定的字母转换为小写字母。如果 c 有相对应的小写字母,则该函数返回 c 的小写字母,否则 c 保持不变。返回值是一个可被隐式转换为 char 类型的 int 值。
C 库函数 int toupper(int c) 把小写字母转换为大写字母。如果 c 有相对应的大写字母,则该函数返回 c 的大写字母,否则 c 保持不变。返回值是一个可被隐式转换为 char 类型的 int 值。
string sgood_rev(sgood.rbegin(), sgood.rend());利用迭代器颠倒sgood储存到sgood_rev