LeetCode 第九题, 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.

题意解析:

即是判断一个数字是否是回文串.

水题~~~~~~~~~~~~~~~~~~~~~~~~~

代码如下:

solution 1:依次取出左右的数字,对比.

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (x < 0)
            return false;
        if (x == 0)
            return true;

        int base = 1;
        while(x / base >= 10)
            base *= 10;

        while(x)
        {
            int leftDigit = x / base;
            int rightDigit = x % 10;
            if (leftDigit != rightDigit)
                return false;

            x -= base * leftDigit;
            base /= 100;
            x /= 10;
        }

        return true;
    }
};

solution 2: 将数字取反....判断是否相等,不过,翻转的时候可能会溢出,但是,也是过了..............

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(x < 0) return false;

        long long target = x;
        //if(target < 0) target = -target;
        long long reverse = 0;
        long long curNum = target;
        while(curNum != 0)
        {
            reverse = 10*reverse+curNum%10;
            curNum /= 10;
        }

        return reverse == target;
    }
};
时间: 2024-08-02 08:19:11

LeetCode 第九题, Palindrome Number的相关文章

leetcode第九题--Palindrome Number

Problem: 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

LeetCode第九题—— 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 rig

Leetcode第九题_Palindrome Number

Palindrome Number 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 th

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

leetcode_9题——Palindrome Number (数学问题)

Palindrome Number Total Accepted: 57795 Total Submissions: 194286My Submissions Question Solution Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Hide Tags Math Have you met this question in a real i

LeetCode算法题-Single Number(Java实现)

这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两次. 找到那个只出现了一次的元素.例如: 输入:[2,2,1] 输出:1 输入:[4,1,2,1,2] 输出:4 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 因为已经限定传入的数组不为空,所以此题不需要

[LeetCode 题解]:Palindrome Number

前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 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 c

LeetCode解题报告--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

leetcode笔记: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 co