[LeetCode]68. Palindrome Number回文数字

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

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

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?

There is a more generic way of solving this problem.

Subscribe to see which companies asked this question

解法1:首先想到的就是将整数转换为字符串,然后从两头往中间对比即可。或者将每一位分别取出来后暂存再前后比较。但是这样都需要额外的空间。

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) return false;
        string s = to_string(x);
        int i = 0, j = s.size() - 1;
        while (i < j)
            if (s[i++] != s[j--]) return false;
        return true;
    }
};

解法2:可以想办法每次都取出整数的最高位和最低位进行比较,然后去掉这个最高位和最低位,取新的整数的最低位和最高位比较……

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) return false;
        int num = x, n = 0;
        while (num > 0) {
            ++n;
            num /= 10;
        }
        if (n == 1) return true;
        int i = 1, j = n - 1;
        while (i <= j) {
            int low = x % (int)pow(10, i) / (int)pow(10, i - 1);
            int high = x / (int)pow(10, j) % 10;
            if (low != high) return false;
            ++i;
            --j;
        }
        return true;
    }
};

一种更简单的写法,严格按照上述思路:

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) return false;
        int n = 1;
        while (x / n >= 10) n *= 10;
        while (x > 0) {
            int low = x % 10;
            int high = x / n;
            if (low != high) return false;
            x = (x % n) / 10; //取出中间剩余的数字
            n /= 100; //注意每次去掉两位,因此除数相应缩小100倍
        }
        return true;
    }
};
时间: 2024-09-30 14:24:53

[LeetCode]68. Palindrome Number回文数字的相关文章

LeetCode 9 Palindrome Number 回文数字

题目:Determine whether an integer is a palindrome. Do this without extra space. 翻译:判断一个数字是否是回文数,不要额外空间. 解题思路:因为数字既然传过去了,就不会有越界的问题.每次只需要取最前面和最后面的那一位数字进行比较,相同则继续,不同则返回. 首先要获取数字的位数,假设数字是12344321,一共有8位. 其次是要每次取前后各一位来进行比较,用数字除以1后面7个0得到第一位,用数字对10取余数得到最后一位. 此

LeetCode (30) Palindrome Number (回文数字)

题目描述 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) -- 负数不为回文 If you are thinking of converting the integer to string, note the restriction of using extra space.

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

leetcode 9 Palindrome Number 回文数

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

[LeetCode]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

Leetcode——3 Palindrome Number(回文数)

Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧. Solution: public class Solution { public boolean isPalindrome(int x) { int n=1; int copyx=x; if(x<0)return false; if(x<10)return true; w

LeetCode Valid Palindrome 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回

9. Palindrome Number 回文 my second leetcode 20170807

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

LeetCode Problem 9:Palindrome Number回文数

描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could a