7.Reverse Integer (INT; Overflow)
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:要注意溢出。用以下做法,会溢出。
class Solution {
public:
int reverse(int x) {
int ret = ;
while(x){
ret = ret * + x%;
x /= ;
}
return ret;
}
};
改进的做法:
class Solution {
public:
int reverse(int x) {
if (x == INT_MIN) return ;
int ret = ;
int digit;
bool pos = x>=?true:false;
x = abs(x); //现在对负数求模编译器不统一,所以转为正数操作
while(x){
digit = x%;
if (ret > (INT_MAX - digit) / ) //10*ret+digit > INT_MAX
return ;
ret = ret * + digit;
x /= ;
}
if(pos) return ret;
else return (-ret);
}
};
7.Reverse Integer (INT; Overflow)的更多相关文章
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- LeetCode 7 Reverse Integer int:2147483647-2147483648 难度:2
https://leetcode.com/problems/reverse-integer/ class Solution { public: int inf = ~0u >> 1; in ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- LeetCode——Reverse Integer(逆置一个整数)
问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321 Ha ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- Reverse Integer [LeetCode]
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
随机推荐
- jmeter监控服务器的方法
先下载Jmeter资源监控插件,我的百度云jmeter视频里面有说. 地址如下: JMeterPlugins-Standard-1.3.1.zip 下载 https://jmeter-plugins ...
- ApplicationEvent事件机制源码分析
<spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...
- http 各个状态返回值
code 定义在 org.apache.http.HttpStatus 转载来自于:http://desert3.iteye.com/blog/1136548 502 Bad Gateway:tomc ...
- ros建立ospf邻居的条件
Two routers do not become neighbors unless the following conditions are met. Two way communication b ...
- 第15章 高并发服务器编程(1)_非阻塞I/O模型
1. 高性能I/O (1)通常,recv函数没有数据可用时会阻塞等待.同样,当socket发送缓冲区没有足够多空间来发送消息时,函数send会阻塞. (2)当socket在非阻塞模式下,这些函数不会阻 ...
- 单例模式(Singleton)
单例模式 Singletonn Pattern Ensure a class has only one instance, and provide a global point of access ...
- Spring 配置 web.xml (防止spring 内存溢出)
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...
- sql 数据库修复
数据库修复 exec sp_dboption 'dbname1','single user',‘true’ dbcc checkdb('dbname1') dbcc checkdb('dbname1' ...
- libcurl 不支持https访问
项目中使用libcurl 访问https的时候会报错,比如:“Unsupported protocol” 或者:“Protocol https not supported or disabled in ...
- focusin 事件| focusout事件
focusin 定义和用法 当元素(或在其内的任意元素)获得焦点时发生 focusin 事件. 当在元素或在其内的任意元素上发生 focus 事件时,focusin() 方法添加要运行的函数. 与 f ...