Reverse Digits Of An Integer In Python
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Assume we are de
Solution 1:
Try converting the int to string
, then reverse
and then convert it to int
.
Ex:
a = 1534236469
print(int(str(a)[::-1]))
Output:
9646324351
To handle the negative number:
if str(a).startswith("-"):
a = a[1:]
print("-{0}".format(int(str(a)[::-1])))
else:
print(int(str(a)[::-1]))
Post a Comment for "Reverse Digits Of An Integer In Python"