LeetCode 9. Palindrome Number

来源:互联网 发布:热水器选购知乎 编辑:程序博客网 时间:2024/06/06 08:43

题目描述:

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


算法分析:

题目要求我们对输入的数字,判断一下是不是回文数。


大体的算法:

1. 开一个数组。首先对数字每次都模10,然后把数字保存在数组里面

2. 扫一遍数组,看看是不是对称


注意的点:

负数要返回false


class Solution {public:    bool isPalindrome(int x) {        if(x<0)            return false;        char list[100];        int size=0;        while(x!=0){            list[size]=(x%10)+'0';            x/=10;            ++size;        }                for(int i=0;i<size/2;++i){            if(list[i]!=list[size-i-1])                return false;        }        return true;    }};






0 0