naimul64

https://leetcode.com/problems/reverse-integer/

Oct 23rd, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int reverse(int x) {
  4.         int y = x;
  5.         bool neg = false;
  6.         if(x < 0) {
  7.             neg = true;
  8.             y = -x;
  9.         }
  10.  
  11.         char a[50];
  12.         int i = 0;
  13.         if(y==0){
  14.             a[0]='0';
  15.             a[1] = 0;
  16.         }
  17.         while (y){
  18.             a[i++] = y%10 + 48;
  19.             y=y/10;
  20.         }
  21.         a[i] = 0;
  22.  
  23.         int l = strlen(a);
  24.         int total = 0;
  25.         for(i = 0; i< l ; i++){
  26.             total+=a[i] - 48;
  27.             if(i != l-1){
  28.                 total = total * 10;
  29.             }
  30.         }
  31.  
  32.         if(neg){
  33.             total = 0 - total;
  34.         }
  35.  
  36.         return total;
  37.     }
  38. };
Add Comment
Please, Sign In to add comment