您的位置:首页 > 其它

[LeetCode]Reverse Integer

2014-09-29 22:56 337 查看
题目描述:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

解题方案:

该题比较简单,直接贴代码:

class Solution {
public:
int reverse(int x) {
//char word[15];
int flag;
int result = 0;
int count = 1;
if( x >= 0){
flag = 1;
}else{
flag = -1;
x = -x;
}
while(true){
int temp = x % 10;
//cout<<temp<<endl;
result = result * count + temp;
count = 10;
x /= 10;
//cout << x<<endl;
if(x == 0){
break;
}
}

return flag * result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: