<div class="question-title" style="box-sizing: border-box; padding-bottom: 10px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><h3 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 20px; margin-bottom: 10px; font-size: 24px; display: inline;">Palindrome Number</h3></div><div class="question-title" style="box-sizing: border-box; padding-bottom: 10px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span style="line-height: 30px;">Determine whether an integer is a palindrome. Do this without extra space.</span></div><div class="row" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><div class="col-md-12" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 1170px;"><div class="question-content" style="box-sizing: border-box; margin-left: 20px; margin-top: 20px; margin-bottom: 5px; line-height: 30px; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(221, 221, 221); padding-left: 20px; padding-bottom: 2px;"><div class="spoilers" style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">Some hints:</span><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">Could negative integers be palindromes? (ie, -1)</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">If you are thinking of converting the integer to string, note the restriction of using extra space.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">There is a more generic way of solving this problem.</p></div></div></div></div><pre name="code" class="cpp">class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
if(x<10) return true;
int t=x;
int d=0;//位数
while(t!=0){
t/=10;
d++;
}
int left = pow(10, d - 1);
int right = 1;
while( left >= right)
{
if (x / left % 10 != x / right % 10)
return false;
left /= 10;
right *= 10;
}
return true;
}
};
/***********************************************/
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
class Solution {
public:
bool isPalindrome(string s) {
int n = s.size();
if( n == 0) return true;
string ss;
for(int i = 0; i < n; i++){
if(s[i] >= 'a'&& s[i] <= 'z')
ss += s[i];
else if(s[i] >= 'A'&& s[i] <= 'Z')
ss += (s[i] + 32);
else if(s[i] >= '0'&& s[i] <= '9')
ss += (s[i]);
else continue;
}
if(ss.size() < 2) return true;
string s_rev = ss;
reverse(s_rev.begin(), s_rev.end());
if(ss == s_rev) return true;
else return false;
}
};