LeetCode——回文数
题目地址:https://leetcode-cn.com/problems/palindrome-number/
解题思路: 栈操作。
class Solution { public: int getLength(int x) { int len = 0; if (x == 0) return 1; while (x) { len++; x /= 10; } return len; } bool isPalindrome(int x) { stack<int> q; int index;//位数中间位置 int len = getLength(x); if (x < 0) return false; if (len == 1) return true; index = len / 2 + 1; int tmp;//位数 q.push(x % 10); tmp = 1; x /= 10; while (x) { if (len & 1 && tmp + 1 == index) {//如果是奇数,不压入最中间那个数 x /= 10; tmp++; continue; } if (tmp+1 >=index && q.top() == x % 10) q.pop(); else q.push(x % 10); x /= 10; tmp++; } if (q.empty()) return true; else return false; } };