[LeetCode] 009. Palindrome Number (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


009.Palindrome_Number (Easy)

链接

题目:https://oj.leetcode.com/problems/palindrome-number/

代码(github):https://github.com/illuz/leetcode

题意

判断一个数是否是回文数。

分析

按自己想的去写就行了。

  1. 可以先转为字符串,再判断。(这种解法用 Python 可以一句话完成 =w=)
  2. 更好的方法是直接算出回文的数再直接比较。

代码

C++:(可以先转为字符串)

class Solution {
public:
    bool isPalindrome(int x) {
		if (x < 0) return false;
		int bit[10];
		int cnt = 0;
		while (x) {
			bit[cnt++] = x % 10;
			x /= 10;
		}
		for (int i = 0; i < cnt; i++)
			if (bit[i] != bit[cnt - i - 1])
				return false;
		return true;
    }
};

Python:

class Solution:
    # @return a boolean
    def isPalindrome(self, x):
        return str(x) == str(x)[::-1]

C++:(算出回文)

class Solution {
public:
    bool isPalindrome(int x) {
		long long xx = x;
		long long new_xx = 0;

		while (xx > 0) {
			new_xx = new_xx * 10 + xx % 10;
			xx /= 10;
		}

		return new_xx == (long long)x;
    }
};

Java:

public class Solution {

    public boolean isPalindrome(int x) {
        long xx = x;
        long new_xx = 0;
        while (xx > 0) {
            new_xx = new_xx * 10 + xx % 10;
            xx /= 10;
        }
        return new_xx == x;
    }
}

Python:

class Solution:
    # @return a boolean
    def isPalindrome(self, x):
        xx = x
        new_xx = 0
        while xx > 0:
            new_xx = new_xx * 10 + xx % 10
            xx /= 10

        return new_xx == x

时间: 2024-12-26 19:35:10

[LeetCode] 009. Palindrome Number (Easy) (C++/Java/Python)的相关文章

LeetCode 009 Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. [题意] 题意判断一个整数是否是回文数 注意一下几点: 1. 不能用额外的空间 2. 负数不是回文数 [思路1] 依次比较首位值,需要注意处理类似1002001这样的数,当比较完首位值之后余下的数变成200, 2之前的两个0会自动清除. [代码] class Solution { public: int _size(int x){ int

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 006.ZigZag_Conversion (Easy) 链接: 题目:https://oj.leetcode.com/problems/zigzag-conversion/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个字符串按横写的折线排列. 分析: 直

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 020.Valid_Parentheses (Easy) 链接: 题目:https://oj.leetcode.com/problems/valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个括号字符串是否是有效的. 分析:

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

Java for LeetCode 009 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. 解题思路: 由于题目中给定不能使用额外空间开销,因此不能转为String类型,我们需要一位一位处理. Java代码如下: static public boolean isPalindrome(int x) { if (x < 0) return false; int temp = x; int beginIndex = 0, endIndex =

LeetCode 009 Palindrome Number - Java

Determine whether an integer is a palindrome. Do this without extra space. 定位:简单题 题目要求判断给出的数字是否是回文数,并且要求不适用额外空间.我们不能使用其他数据类型过度处理,那从个位开始计算,每提高计算一位将原先的值*10+新值,结束后判断是否与原值相同即可. Java实现: 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 in

【leetcode】Palindrome Number (easy)

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

LeetCode——009 Palindrome Number

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

【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;