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%10 11 x = x/10 12 return ret*flag
时间: 2024-09-29 17:28:04