https://oj.leetcode.com/problems/reverse-integer/
一个整数,给反过来,比如123输出321.注意12300的情况,应该输出321,还有-123,是-321.
class Solution { public: int reverse(int x) { if(x == 0) return 0; bool isNegative = false; if(x<0) { isNegative = true; x = -x; } vector<int> num; while(x!=0) { num.push_back( x%10); x = x/10; } int ans = 0; for(int i = 0;i<num.size();i++) { ans = ans * 10; ans += num[i]; } if(isNegative) ans = ans*(-1); return ans; } };