码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]9. Palindrome Number

时间:2019-05-04 18:44:01      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:result   text   put   number   sub   time   isp   pen   rev   

简单题,如果不转string要牺牲一些时间复杂度:

Submission Detail

11509 / 11509 test cases passed.
Status: 

Accepted

Runtime: 128 ms
Memory Usage: 13.2 MB
Submitted: 1 minute ago
class Solution:
    def isPalindrome(self, x: int) -> bool:
        # solve it without converting the integer to a string
        # deal 0
        if x==None:
            return False

        if x < 0:
            return False
        if x == 0:
            return True
        # noraml
        order = []
        while (True):
            remain = x % 10
            div = x // 10
            if div == 0 and remain ==0:
                break
            else:
                order.append(remain)
                x = x // 10

        # is Palindrome
        # notice lengthS >=
        lengthS = len(order)
        if lengthS % 2 == 1:
            # odd
            list1 = order[0:lengthS // 2 + 1]
            list2 = order[lengthS // 2:]
            list2.reverse()
            for index in range(len(list1)):
                if list1[index] == list2[index]:
                    if index == len(list1) -1:
                        return True
                else:
                    # no palindrome
                    return False
        else:
            # even
            list1 = order[0:lengthS // 2]
            list2 = order[lengthS // 2:]
            list2.reverse()
            for index in range(len(list1)):
                if list1[index] == list2[index]:
                    if index == len(list1)-1:
                        return True
                else:
                    # no palindrome
                    return False

 

[leetcode]9. Palindrome Number

标签:result   text   put   number   sub   time   isp   pen   rev   

原文地址:https://www.cnblogs.com/alfredsun/p/10809272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!