【leetcode】Reverse Integer
2014-08-14 21:01
363 查看
题目:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
解析:将一个整形数字倒序输出,注意判断正负。这道题在华为面试的时候遇到过,很简单。
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
解析:将一个整形数字倒序输出,注意判断正负。这道题在华为面试的时候遇到过,很简单。
class Solution { public: int reverse(int x) { int result = 0; bool ifNegative = false; if(x<0){ ifNegative = true; x = -x; } while(x!=0){ int temp = x % 10; result = result * 10 + temp; x = x/10; } if(ifNegative){ result = -result; } return result; } };
相关文章推荐
- leetcode--Reverse Integer
- leetcode-- Reverse Integer
- leetcode-7-Reverse Integer
- [LeetCode] Reverse Integer
- [leetcode 7]Reverse Integer
- leetcode 007 —— Reverse Integer
- LeetCode:07: Reverse Integer
- [Leetcode] Reverse Integer
- LeetCode题解——Reverse Integer
- LeetCode (2). Reverse Integer
- LeetCode 7 Reverse Integer
- leetcode-Reverse Integer
- 【LeetCode】Reverse Integer
- [LeetCode]Reverse Integer
- Leetcode: Reverse Integer
- leetcode7 Reverse Integer
- LeetCode | #7 Reverse Integer
- leetcode-7. Reverse Integer
- [LeetCode] Reverse Integer
- LeetCode - 7 - Reverse Integer