题解 | #牛的回文编号II#
牛的回文编号II
https://www.nowcoder.com/practice/0b576fd673834425878b99c736bb6c34
#include <cstddef> #include <iterator> #include <string> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x string字符串 * @return bool布尔型 */ bool isPalindromeNumber(string x) { std::size_t dotPos = x.find('.'); std::string integerPart = x.substr(0, dotPos); std::string decimalPart = (dotPos != std::string::npos) ? x.substr( dotPos + 1) : ""; decimalPart.erase(std::find_if(std::rbegin(decimalPart), std::rend(decimalPart), [](const auto & ch) -> bool{ return ch != '0'; }).base(), std::end(decimalPart)); bool integerIsPalindrome = isPalindrome(integerPart); bool decimalIsPalindrome = decimalPart.empty() ? true : isPalindrome( decimalPart); return integerIsPalindrome && decimalIsPalindrome; } bool isPalindrome(std::string str) { int left{}; int right = str.size() - 1; for (; left <= right; left++, right--) { if (str[right] != str[left]) { return false; } } return true; } };