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)的更多相关文章

  1. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  2. LeetCode 7 Reverse Integer int:2147483647-2147483648 难度:2

    https://leetcode.com/problems/reverse-integer/ class Solution { public: int inf = ~0u >> 1; in ...

  3. Reverse Integer - 反转一个int,溢出时返回0

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  4. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  5. LeetCode——Reverse Integer(逆置一个整数)

    问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   Ha ...

  6. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  7. Reverse Integer [LeetCode]

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  8. 【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 ...

  9. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

随机推荐

  1. 更加灵活的编写控制层的方法____结合eval函数

    结合EVAL函数,我们可以把API放到测试用例那边去,就可以使用一个定位元素,测试用例可以使用多个API 发现eval里面可以拼接str.那么写成这样更顺眼     eval("self.d ...

  2. 有意思的bug

    1. Xss攻击型的bug Xss攻击即跨站脚步攻击,通过插入恶意脚本 ,实现对用户浏览器的控制. Bug现象:新增物品时,物品名称输入一段JavaScript代码,在提交时此代码被执行.如:输入&l ...

  3. 自己写的 读写 ini 配置文件类

    /// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...

  4. etcd查看key-value

    get/set key-value etcdctl get/set /key-path etcdctl watch --recursive /test/sm/default/apps 查看所有key- ...

  5. 系统一般信息监控查看shell.磁盘,负载等达阀值告警机制,改进测试中.

    1 #!/bin/sh  2 #Create by Qrui  3 while [ "1"="1" ]  4 do  5 clear  6  7 echo &q ...

  6. python3.5过滤网址和图片的函数自己亲测可用

    def has_replace(tag): #过滤网址 real=re.sub(r'<a\shref=.+</a>', '',tag.decode(), count=0, flags ...

  7. 1.scrapyd部署相关问题

    部署scrapy爬虫项目到6800上 启动scrapyd 出现问题 1: scrapyd-deloy -l  未找到相关命令 scrapyd-deploy -l 可以看到当前部署的爬虫项目,但是当我输 ...

  8. 0_Simple__template

    简单的 CUDA 应用模板,白送的 Sample. ▶ 源代码 //template_cpu.cpp extern "C" void computeGold(float *, co ...

  9. 5. Java中序列化的serialVersionUID作用

    Java序列化是将一个对象编码成一个字节流,反序列化将字节流编码转换成一个对象. 序列化是Java中实现持久化存储的一种方法:为数据传输提供了线路级对象表示法. Java的序列化机制是通过在运行时判断 ...

  10. form表单提交参数封装

    function getFormValues(element,options) { var data = {}; if(element == null || element == undefined) ...