这是来刷成就的么 Reverse Integer

这题目虽然下面提示你考虑溢出,但是没有溢出的test case,也没有给出溢出时的处理要求,所以虽然不知道他在说什么,但是感觉和做题没关系
Python的字符串那么好用…真不知道这题是想干啥

Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

View Code PYTHON
1
2
3
4
5
6
7
8
class Solution:
    # @return an integer
    def reverse(self, x):
        x_str = str(x)
        if x_str[0] == '-':
            return int('-'+x_str[-1:0:-1])
        else:
            return int(x_str[::-1])

 

Share This: 

Leave a Reply

Your email address will not be published. Required fields are marked *