Leetcode: Reverse Integer
2013-12-17 23:00
169 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
class Solution {
public:
int reverse(int x) {
bool negative = false;
if (x < 0) {
x = -x;
negative = true;
}
int result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x = x / 10;
}
if (negative) {
result = -result;
}
return result;
}
};
Example1: x = 123, return 321
Example2: x = -123, return -321
class Solution {
public:
int reverse(int x) {
bool negative = false;
if (x < 0) {
x = -x;
negative = true;
}
int result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x = x / 10;
}
if (negative) {
result = -result;
}
return result;
}
};
相关文章推荐
- Leetcode 7 Reverse Integer
- LeetCode - Reverse Integer
- LeetCode:Reverse Integer
- LeetCode——Reverse Integer
- [LeetCode] Reverse Integer
- LeetCode:Reverse Integer
- leetcode 7 -- Reverse Integer
- [LeetCode]Reverse Integer
- leetcode [007] : Reverse Integer
- Leetcode 7 Reverse Integer
- leetcode_reverse Integer
- Leetcode: Reverse Integer
- Leetcode代码学习周记——Reverse Integer
- LeetCode:Reverse Integer
- LeetCode题解(7)--Reverse Integer
- Leetcode 7 Reverse Integer
- leetcode 7 Reverse Integer
- LeetCode 007 Reverse Integer
- LeetCode7——Reverse Integer
- LeetCode: Reverse Integer