[Leetcode] reverse integer 反转整数
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
题意:将一个整数反转。
思路:这题有几个要注意的地方:一、正负号;二、反转后的结果是否溢出。第二点很重要,关于这点题目中有详细的说明。这里有两种解法方法:
方法一:改变返回结果的变量类型为long long ,这样,若是最终计算的结果超过INT_MAX,就返回0;否则,结构符号输出即可。
代码如下:
class Solution {
public:
int reverse(int x)
{
long long res=;
int cur=abs(x); while(cur>)
{
res=res*+cur%;
cur/=;
}
if(res>INT_MAX) return ;
return x>=?res:-res;
}
};
方法二:不用改变返回的变量类型,在while循环中,在计算res之前,若res>INT_MAX/10,就返回0,因为若是大于,则后续的计算中乘以10 了,还是会大于,所以提前返回。代码如下:
class Solution {
public:
int reverse(int x)
{
int res=;
int cur=abs(x); while(cur>)
{
if(res>INT_MAX/) return ;
res=res*+cur%;
cur/=;
} return x>=?res:-res;
}
};
在LeetCode上测试时,第二种方法,明显好些。
[Leetcode] reverse integer 反转整数的更多相关文章
- [leetcode]7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- leetcode 7 reverse integer 反转整数
描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 7. Reverse Integer 反转整数
[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 7. Reverse Integer[E]整数反转
题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...
随机推荐
- 【JUC源码解析】AQS
简介 AQS,也即AbstractQueuedSynchronizer,抽象队列同步器,提供了一个框架,可以依赖它实现阻塞锁和相关同步器.有两种类型,独占式(Exclusive)和共享式(Share) ...
- JavaWeb(三)——Tomcat服务器(二)
一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:
- 为什么请求时,需要使用URLEncode做encode转码操作(转)
什么要对url进行encode 发现现在几乎所有的网站都对url中的汉字和特殊的字符,进行了urlencode操作,也就是: http://hi.baidu.com/%BE%B2%D0%C4%C0%C ...
- POJ 2540 Hotter Colder(半平面交)
Description The children's game Hotter Colder is played as follows. Player A leaves the room while p ...
- 自测之Lesson16:并发通信
知识点:三个多路并发模型(select .poll .epoll) 题目:以epoll模型,编写一个可供多个客户端访问的服务器程序. 实现代码: #include <netinet/in.h&g ...
- 从电梯问题,看c和c++之间的区别(有点懂了)错觉错觉
磕磕碰碰的也相继用c和c++构造了不少的电梯了.虽然对自我的表现不满意,但是总体来说还是有一定的收获的,对于c和c++之间的区别感觉也摸到了一点点门道了... 用c语言构造电梯的步骤: 第一步: 分析 ...
- iOS开发UIApplication用法
1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplication对象,而且是单例的,如果 ...
- HDU 2117 Just a Numble
http://acm.hdu.edu.cn/showproblem.php?pid=2117 Problem Description Now give you two integers n m, yo ...
- pycharm/webstorm创建react项目
1.安装nodejs 2.安装reactapp依赖:npm install -g create-react-app 在pycharm/webstorm中选择react
- SQL SERVER技术内幕之8 分组集
分组集就是分组(GROUP BY子句)使用的一组属性,在传统的SQL中,一个聚合查询只能定义一个分组集: 假设现在不想生成4个单独的结果集,而是希望生成一个统一的结果集,其中包含所有4个分组集的聚合 ...