leetcode:Reverse Integer【Python版】

it2022-05-18  66

1、在进入while之前,保证x是非负的;

2、符号还是专门用flag保存

===================

3、另一思路:将integer转换成string,然后首位swap,直至中间;

1 class Solution: 2 # @return an integer 3 def reverse(self, x): 4 ret = 0 5 flag = 1 6 if x < 0: 7 flag = -1 8 x *= -1 9 while(x!=0): 10 ret = ret*10+x 11 x = x/10 12 return ret*flag

 

转载于:https://www.cnblogs.com/CheeseZH/p/4033801.html

相关资源:数据结构—成绩单生成器

最新回复(0)