9. 回文数(LeetCode)的问题及正确结果。

huangapple go评论65阅读模式
英文:

9. Palindrome Number(leetcode) issues with correct results

问题

我是新手编程者。最近我在回文数字的测试输入中遇到了问题。以下是我的代码:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        str_x = str(x)
        list_x = []
        for i in str_x:
            list_x.append(i)
        new_x = (''.join(map(str, list_x[::-1])))
        if str(x) == str(new_x):
            return('true')
        elif str(x) != (new_x):
            return('false')

如果我手动在版本控制系统中输入所有值,我会得到10个中的10个正确结果。实际上,我错过了哪些细节?

英文:

I'm newbie at coding. So, recently I faced with problems with test inputs in Palendrome number. Here's my code

class Solution:
    def isPalindrome(self, x: int) -> bool:
        str_x = str(x)
        list_x = []
        for i in str_x:
            list_x.append(i)
        new_x = ((''.join(map(str, list_x[::-1]))))
        if str(x) == str(new_x):
            return('true')
        elif str(x) != (new_x) :
            return('false')

If I input all values manually in VCS, 10 of 10 i will get correct result.
Actually, what details have i missed?

答案1

得分: 0

The question has already been answered. You need to use booleans, instead of strings, in your return.

But, I wanted to add some code that might make things easier to understand:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        str_x = str(x) # This is fine, but can be added in the list comprehension below
        
        list_x = [char for char in str_x] # Nice to use clear names
        new_x = ''.join(map(str, list_x[::-1]))

        return str_x == new_x # Cleaner way return true / false
英文:

The question has already been answered. You need to use booleans, instead of strings, in your return.

But, I wanted to add some code that might make things easier to understand:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        str_x = str(x) # This is fine, but can be added in the list comprehension below
        
        list_x = [char for char in str_x] # Nice to use clear names
        new_x = ''.join(map(str, list_x[::-1]))

        return str_x == new_x # Cleaner way return true / false

huangapple
  • 本文由 发表于 2023年2月16日 05:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定