问题:https://leetcode.com/PRoblems/palindrome-number/?tab=Description Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是否是回文数。不能增加额外的空间。 分析: 因为不能增加额外的空间,所以每次比较头的数和末尾的数,如果不相等返回false。如果相等去掉首尾,再比较剩下数的首尾。 C++代码:
class Solution {public: bool isPalindrome(int x) { if(x<0) return false; int m=1; while(x/m>=10) m*=10; while(x !=0){ int left=x/m; int right=x%10; if(left != right) return false; x=(x%m)/10; m/=100; } return true; }};新闻热点
疑难解答