LeetCode 9 Palindrome Number (回文数)

翻译

确定一个整数是否是回文数。不能使用额外的空间。

一些提示:

负数能不能是回文数呢?(比如,-1)

如果你想将整数转换成字符串,但要注意限制使用额外的空间。

你也可以考虑翻转一个整数。
然而,如果你已经解决了问题“翻转整数(译者注:LeetCode 第七题),
那么你应该知道翻转的整数可能会造成溢出。
你将如何处理这种情况?

这是一个解决该问题更通用的方法。

原文

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

一开始的想法,大不了不新建一个字符串,直接在循环里用就好了,结果居然也可以accept。

public class Solution
{
    public bool IsPalindrome(int x)
    {
        for (int i = 0; i < x.ToString().Length / 2; i++)
        {
            if ((x.ToString())[i] != x.ToString()[x.ToString().Length - i - 1])
            {
                return false;
            }
        }
        return true;
    }
}

可是叻:

11506 / 11506 test cases passed.
Status: Accepted
Runtime: 244 ms
Your runtime beats 0.97% of csharp submissions.

然后修改了一下:

public class Solution
{
    public bool IsPalindrome(int x)
    {
        if (x < 0)
            return false;
        // 判断x的长度,比如x=232,div就等于100
        int div = 1;
        while (x / div >= 10)
            div *= 10;
        while (x != 0)
        {
            // 左边开始计数
            int left = x / div;
            // 右边开始计数
            int right = x % 10;
            if (left != right)
                return false;
            x = (x % div) / 10;
            div /= 100;
        }
        return true;
    }
}

性能还是不尽如人意呀,不过同样的代码放在Java上,貌似C#要快一些。


11506 / 11506 test cases passed.
Status: Accepted
Runtime: 196 ms
Your runtime beats 21.36% of csharp submissions.

下面这份代码是网上找到的,性能和上面的一模一样:

public class Solution
{
    public bool IsPalindrome(int x)
    {
        if (x < 0)
            return false;
        else if (x == 0)
            return true;
        else
        {
            int tmp = x;
            int y = 0;
            while (x != 0)
            {
                y = y * 10 + x % 10;
                x = x / 10;
            }
            if (y == tmp)
                return true;
            else
                return false;
        }
    }
}

版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp

时间: 2024-08-02 15:12:17

LeetCode 9 Palindrome Number (回文数)的相关文章

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——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]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 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

LeetCode 9 Palindrome Number 回文数字

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

Palindrome Number 回文数

判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if(x<0) return false; 4 5 int temp = x, reverseX = 0; 6 while (temp > 0) { 7 reverseX

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

Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4MB 效果:还行 class Solution: def isPalindrome(self, x: int) -> bool: s=str(x) if s==s[::-1]: return True else: return False 第二种方法:反转一半数字 执行用时:156 ms: 内存消耗