英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论