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.

—— 将数字转换为字符串,我们做过判断字符串是否为回文的题目的,但是这里会用到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?

—— 将数字逆转,判断逆转前后是否相同,但是这会出现overflow的问题(没有做过”Reverse Integer”的题目,也不是很清楚为什么会溢出。)

本题要求判断数字是否为回文,并要求不能利用额外的内存空间。本题可以利用类似字符串的回文判断使用两个指针进行判断。当然对一个数字我们无法从前后移动两个指针,指向不同的数字为,这里我们可以通过数学运算,每次获得数字的最高位和最低位的数值。

首先获得数字的位数,依次通过除以 10*ditalNumber 和 对10取余分别获得最高位和最低位的数字,判断两数字是否相等。需要注意的是每次得到最高位数需要除去的数字大小的变化。总体来说,代码比较简单。

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) return false;
        if (x < 10) return true;

        int count = 0;
        int t = x;

        while (t)
        {
            count++;
            t = t / 10;
        }

        int highNum = pow(10, count - 1);
        while (highNum > 1)
        {
            int high = x / highNum;
            int low = x % 10;
            if (high != low)    return false;

            x = x - highNum * high;
            highNum /= 100;
            x = x / 10;
        }

        return true;
    }
};
时间: 2024-12-15 16:16:56

LeetCode (30) 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]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 ext

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 //测试字符串里是否有字母或数字,若没有,则立刻返回

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

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