标签:leetcode reverse integer
Reverse digits of an integer.
Example1: x = 123, return 321 Example2: x = -123, return -321
click to show spoilers.
public class Solution { public long reverse(int x) { String str = String.valueOf(x); StringBuilder sb=new StringBuilder(); boolean flag=true; for( int i=str.length()-1; i>0; --i){ if (flag==true && str.charAt(i) == '0' ){ continue; }else{ flag=false; } sb.append(str.charAt(i)); } if(str.charAt(0)== '-'){ sb.insert(0, '-'); }else{ sb.append(str.charAt(0)); } return Long.valueOf(sb.toString()); } }
class Solution { public: int reverse(int x) { int r = 0; for( ; x ; x/=10){ r= r*10 + x%10; } return r; } };
class Solution: # @return an integer def reverse(self, x): s='' ix = 0 x = str(x) if x[0] == '-': s += x[0] ix = 1 beg=len(x)-1 while (x[beg] == '0' and beg > 0): beg -= 1 if ix == 0 : s += x[beg::-1] else: s += x[beg:0:-1] return int(s)
LeetCode--Reverse Integer,布布扣,bubuko.com
LeetCode--Reverse Integer
原文地址:http://blog.csdn.net/acema/article/details/37535231