9-Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true
Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:

Coud you solve it without converting the integer to a string?

my code

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

int main()
{
    int x;
    cin>>x;
    if(x<0)
    {
        cout<= j)
            cout<

refer code



    //整数进行半逆置
    int revertNumber = 0;
    while(x>revertNumber)
    {
        //常用的整数逆置操作
        revertNumber = revertNumber * 10 + x % 10;
        x /= 10;
    }
    if(x == revertNumber || x == revertNumber / 10)
        cout<<"true";
    else
        cout<<"false";
    return 0;
}

总结

1.该题的思想是进行整数的半逆置来判断是否是回文整数,常用的整数逆置方法是不断将尾数加上低位权值,即
revertNum = revertNum * 10 + x%10
2.该题本人使用的是数组思想,定义两个游标,一个右移,一个左移,只要其指向的数值不等,则返回false,直到两个游标相遇,返回true

你可能感兴趣的:(9-Palindrome Number)