标签:负数 solution 回文数 while 问题 log 时间复杂度 public amp
判断一个数是否为回文数。
int
的问题。但这种做法只击败了37%的提交,考虑优化。class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
int tem = x;
long revx = 0;
while(tem)
{
revx = revx * 10;
revx += tem % 10;
tem /= 10;
}
if(revx == x)
return true;
else
return false;
}
};
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0 || (x % 10 == 0 && x != 0))
return false;
int revx = 0;
while(x > revx)
{
revx = revx * 10 + x % 10;
x /= 10;
}
return x == revx || x == revx / 10;
}
};
优化!优化!优化!!
标签:负数 solution 回文数 while 问题 log 时间复杂度 public amp
原文地址:https://www.cnblogs.com/songjy11611/p/12151057.html