【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】Reverse Integer
- 【Leetcode】【Easy】Reverse Integer
- leetCode(62)-Reverse Integer
- leetcode 7 Reverse Integer
- 【Leetcode】Reverse Integer
- LeetCode-Reverse Integer
- [LeetCode]1 Reverse Integer
- LeetCode-easy-Reverse Integer
- [Leetcode]Reverse Integer
- LeetCode-Reverse Integer
- 【leetcode】 Reverse Integer
- LeetCode 7:《Reverse Integer》
- Leetcode练习- Reverse Integer
- 【leetcode with java】7 Reverse Integer
- [Leetcode]-Reverse Integer
- Leetcode007 Reverse Integer
- leetcode - Reverse Integer
- 【leetcode】Reverse Integer
- [LeetCode] Reverse Integer
- LeetCode 7 - Reverse Integer