[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 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.

【分析】

将整数反转,然后与原来的数比较,如果相等则为Palindrome Number否则不是

【代码】

/*********************************
*   日期:2015-01-20
*   作者:SJF0115
*   题目: 9.Palindrome Number
*   网址:https://oj.leetcode.com/problems/palindrome-number/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
using namespace std;

class Solution {
public:
    bool isPalindrome(int x) {
        // negative integer
        if(x < 0){
            return false;
        }//if
        int tmp = x;
        int n = 0;
        // reverse an integer
        while(tmp){
            n = n * 10 + tmp % 10;
            tmp /= 10;
        }//while
        return (x == n);
    }
};

int main(){
    Solution solution;
    int num = 1234321;
    bool result = solution.isPalindrome(num);
    // 输出
    cout<<result<<endl;
    return 0;
}


【分析二】

上一种思路,将整数反转时有可能会溢出。

这里的思路是不断的取第一位和最后一位(10进制)进行比较,相等则取第二位和倒数第二位.......直到完成比较或者中途不相等退出。

【代码二】

class Solution {
public:
    bool isPalindrome(int x) {
        // negative integer
        if(x < 0){
            return false;
        }//if
        // 位数
        int divisor = 1;
        while(x / divisor >= 10){
            divisor *= 10;
        }//while
        int first,last;
        while(x){
            first = x / divisor;
            last = x % 10;
            // 高位和低位比较 是否相等
            if(first != last){
                return false;
            }//if
            // 去掉一个最高位和一个最低位
            x = x % divisor / 10;
            divisor /= 100;
        }//while
        return true;
    }
};

时间: 2024-10-31 12:43:22

[LeetCode]9.Palindrome Number的相关文章

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 (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

[LeetCode] Valid Palindrome II 验证回文字符串之二

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. N

[LeetCode] Largest Palindrome Product 最大回文串乘积

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example: Input: 2 Output: 987 Explanation: 99 x 91 = 9009, 9009 % 1337 = 987 Note: The

[LeetCode] Palindrome Number &amp;amp; Valid Palindrome - 回文系列问题

题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如121.656.3443 方法有很多,正着看和到着看两数相同:当然负数显然不是回文 我的方法: 第一种方法: 由于没有没有看到前面的without extra space.采用的方法是把数字转换为字符串,依次比较最前和最后两个字符是否相同,直到遍历完毕. /** * 判断一个数字是否是回文数字 Pal

【LeetCode】9. Palindrome Number

题目: Determine whether an integer is a palindrome. Do this without extra space. 思考过程: 题目很简单,要求是判断一个数是否是回文数,回文数的定义就是数字翻转之后与原先的数一样的话就是回文数,比如 101 , 22, 1 等,所以要处理这个问题的话,只需要将一个数的最高位换到最低位,次高位换到第二低位,依次全部换好之后,得到一个新的数,判断新的数是否与原先的数相等,如果相等,就是回文数,如果不等,就不是,所以解答代码如

【LeetCode从零单排】No.9 Palindrome Number

题目       这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意).Palindrome就是判断一个整数是否对称. 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

[LeetCode]202.Happy Number

题目 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until

[LeetCode]234.Palindrome Linked List

题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 思路 利用双指针法找到链表中点位置,链表中点以后的的元素(不包括中点元素)翻转,再跟链表中点位置以前的元素一一匹配. 代码 /*--------------------------------------- * 日期:2015-07-18 * 作者:SJF01