题目描述:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
解题方案:
该题比较简单,直接贴代码:
1 class Solution { 2 public: 3 int reverse(int x) { 4 //char word[15]; 5 int flag; 6 int result = 0; 7 int count = 1; 8 if( x >= 0){ 9 flag = 1; 10 }else{ 11 flag = -1; 12 x = -x; 13 } 14 while(true){ 15 int temp = x % 10; 16 //cout<<temp<<endl; 17 result = result * count + temp; 18 count = 10; 19 x /= 10; 20 //cout << x<<endl; 21 if(x == 0){ 22 break; 23 } 24 } 25 26 return flag * result; 27 } 28 };