首页 > 代码库 > leetcode:Reverse Integer【Python版】

leetcode:Reverse Integer【Python版】

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%1011             x = x/1012         return ret*flag

 

leetcode:Reverse Integer【Python版】