leetCode练题——9. Palindrome Number
1、题目
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 left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
2、我的解法
1 # -*- coding: utf-8 -*- 2 # @Time : 2020/1/27 17:28 3 # @Author : SmartCat0929 4 # @Email : 1027699719@qq.com 5 # @Link : https://github.com/SmartCat0929 6 # @Site : 7 # @File : 9. Palindrome Number.py 8 9 class Solution: 10 def isPalindrome(self, x: int) -> bool: 11 if x > 0: 12 r = 0 13 y = str(x) 14 n = len(y) 15 d = [] 16 for i in (y): 17 d.append(i) 18 for j in range(n): 19 c = d.pop() 20 r = r + c 21 v = int(r) 22 if v == x: 23 return v 24 else: 25 return 0 26 elif x== 0: 27 return True 28 else: 29 return 0 30 31 32 print(Solution().isPalindrome(12321))
    既能朝九晚五,又能浪迹天涯!

    
                
            
        
浙公网安备 33010602011771号