This is a solution for solving LeetCode problem #9 Palindrome Number. I'm thinking about the time complexity of the solution. I try to understand the time complexity with examples.
Say for a 3-digit number, it goes through the while loop twice.
For a 6-digit number, it goes through the while loop 3 times.
I conclude the number increases 1000 times, while loop increases by 1.
So what's the time complexity?
Could anyone please explain to me? Thank you so much.
def isPalindrome(self, x):
if x < 0 or (x > 0 and x % 10 == 0): return False
half = 0
while x > half:
half, x = half * 10 + x % 10, x / 10
return x in (half, half / 10)
x in (half, half/10)
returns True if x
is one of the two values in the tuple. It's equivalent to
x == half or x == half/10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.