009.Palindrome Number

问题链接:https://leetcode.com/problems/palindrome-number/#/description

Question:Determine whether an integer is a palindrome. Do this without extra space.

题意:判断一个数是否是回文数,不使用额外的空间。

分析:直接算出数字的倒序与该数字比较即可。

代码:

C++版

class Solution {
public:
    bool isPalindrome(int x) {
        int newNumber = 0;
        int originalNumber = x;
        if(x < 0 ){
            return false;
        }
        while(originalNumber > 0){
            newNumber = originalNumber%10 + newNumber*10;
            originalNumber /= 10;
        }
        if(newNumber == x){
            return true;
        }else{
            return false;
        }

    }
};

Python版:

 class Solution:
    # @return a boolean
    def isPalindrome(self, x):
        return str(x) == str(x)[::-1]  
时间: 2024-12-14 21:22:35

009.Palindrome Number的相关文章

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】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] 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 题意: 判断一个数是否是回文数. 分析: 按自己想

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

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 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

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 --java的实现

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 spa