知识点
字符串 双指针
思路
按照.
将字符串分成两半,将前后缀的0去掉之后,双指针判断是否是回文串即可。
AC Code(C++)
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x string字符串 * @return bool布尔型 */ bool isPalindromeNumber(string x) { int idx = x.find("."); if (idx == string::npos) return check(x); return check(x.substr(0, idx)) and check(x.substr(idx + 1)); } bool check(string s) { cout << s << endl; while (s.size() and s.back() == '0') s.pop_back(); reverse(s.begin(), s.end()); while (s.size() and s.back() == '0') s.pop_back(); for (int i = 0, j = (int)s.size() - 1; i < j; i ++, j --) { if (s[i] != s[j]) return false; } return true; } };