Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int reverse(int x) {
- int y = x;
- bool neg = false;
- if(x < 0) {
- neg = true;
- y = -x;
- }
- char a[50];
- int i = 0;
- if(y==0){
- a[0]='0';
- a[1] = 0;
- }
- while (y){
- a[i++] = y%10 + 48;
- y=y/10;
- }
- a[i] = 0;
- int l = strlen(a);
- int total = 0;
- for(i = 0; i< l ; i++){
- total+=a[i] - 48;
- if(i != l-1){
- total = total * 10;
- }
- }
- if(neg){
- total = 0 - total;
- }
- return total;
- }
- };
Add Comment
Please, Sign In to add comment