[leetcode]Reverse Integer 代码(C++)
2017-10-11 11:51
435 查看
题目:
Reverse digits of an integer.Example1: x = 123, return 321
Example2: x = -123, return -321
click to
show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
解题:
这道题很简单,但是我们也不能大意。写代码的时候要注意区分正负号,题目给出的是32-bit,注意溢出问题
代码如下:
class Solution {public:
int reverse(int x) { //注意:1.区分正负数 2.因为条件是32-bit,判断溢出 INT_MIN and INT_MAX
int i;
long sum=0;
int sign=1;
if(x<0) {x=-x;sign=-1;}
while(x){
sum=sum*10+x%10;
x/=10;
}
return (sum<INT_MIN||sum>INT_MAX)?0:sum*sign;
}
};
end
相关文章推荐
- LeetCode上Roman to Integer的C++代码
- leetcode上reverse integer的C语言代码
- LeetCode : 7 Reverse Integer C++
- [C++]LeetCode 7:Reverse Integer(翻转整数)
- 【leetcode】Reverse Integer整数反转----Java代码实现
- LeetCode 之 Reverse Integer — C++ 实现
- 【LeetCode】【Python】【C++】7. Reverse Integer代码实现
- leetcode 7 Reverse Integer C++
- LeetCode Reverse Integer C++
- LeetCode刷题(C++)——Reverse Integer(Easy)
- LeetCode每日一练(7) Reverse Integer C++版
- LeetCode 7 Reverse Integer ( C++ )
- LeetCode 7 — Reverse Integer(C++ Java Python)
- leetcode之92. Reverse Linked List II(C++读错题版本,交换一个链表中指定的两个位置上的元素)
- leetcode-Reverse Integer-7
- [leetcode-150]Evaluate Reverse Polish Notation(c++)
- leetcode — reverse-integer
- leetcode Reverse Integer & Reverse a string----重点
- Leetcode之Reverse Integer
- [LeetCode][7]Reverse Integer解析与位运算实现 -Java实现