LeetCode 9


### Description:

Determine whether an integer is a palindrome. Do this without extra space.

  • 题意:
    判断一个整数是不是回文数

  • 思路:
    注意负数不是回文数。
    设整数为x, 定义一个rev来保存右边的回文, 当数字长度是偶数时, 左边右边则回文, 当数字长度是奇数时, 左边右边/10 则回文, 具体看代码。


Example:


  1. Input: "-1"
    Output: false

  2. Input: "11"
    Output: true



代码


C

bool isPalindrome(int x) {
    if(x < 0)
        return false;
    if(x > 9 && x%10==0){
        return false;
    }
    int rev = 0;
    while(x > rev){
        rev *= 10;
        rev += x%10;
        x /= 10;
    }
    if(x == rev || rev/10 == x){
        return true;
    }
    return false;
}

posted @ 2018-03-14 19:21  shengwudiyi  阅读(93)  评论(0编辑  收藏  举报