您的位置:首页 > 其它

【LeetCode】Reverse Integer

2014-04-03 19:39 423 查看


题目描述


Reverse Integer

 

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.

题目分析

这个题目没什么难度,一个循环搞定


代码示例

#include
#include

using namespace std;

class Solution {
public:
int reverse(int x) {
int sign = 1;
if(x < 0)	x = -x,sign = -1;
int ret = 0;
while(x>0)
{
ret = ret * 10 + (x%10);
x /= 10;
}
return ret*sign;
}
};

void check(int x,int expected)
{
Solution so;

if(expected == (so.reverse(x)))
printf("---------------------passed%5d--%5d \n",expected,(so.reverse(x)));
else
printf("---------------------failed%5d--%5d \n",expected,(so.reverse(x)));
}

int main()
{
check(123,321);
check(0,0);
check(1,1);
check(-123,-321);
check(+123,321);
return 0;
}



推荐学习C++的资料

C++标准函数库
http://download.csdn.net/detail/chinasnowwolf/7108919

在线C++API查询
http://www.cplusplus.com/
map使用方法

http://www.cplusplus.com/reference/map/map/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode