思路:

先判定符号,整型范围[-2^32,2^32]

取余除10操作,依次进行,越界返回0

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

#define IMAX numeric_limits<int>::max()
#define IMIN numeric_limits<int>::min()
class Solution {
public:
int reverse(int x) {
int sign= x>0?1:-1;
if(x==IMIN)return 0;
else x = abs(x);
int res=0,count=0;
for(;x;x/=10)
{
if(count>9)return (sign==1)?IMAX:IMIN;
res = res*10;
if(count==8){ //倒数第二位向后看会不会越界,越界返回0
if(sign==1&&res>IMAX/10) return 0;
if(sign==-1&&res*sign<IMIN/10) return 0;
}
if(count==9){
if(sign==1&&(IMAX-res<=x%10))return 0;//最后一位向后看不会越界,越界返回0
if(sign==-1&&(res*sign-IMIN<=x%10)) return 0;
}
count++;
res=x%10+res;
}
return res*sign;
}
};

14-Reverse Integer的更多相关文章

  1. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

  2. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  3. 65. Reverse Integer && Palindrome Number

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

  4. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  5. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  6. leetcode第七题Reverse Integer (java)

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

  7. Reverse Integer 2015年6月23日

    题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...

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

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

  9. LeetCode之Easy篇 ——(7)Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...

  10. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

随机推荐

  1. NOIP模拟84(多校17)

    T1 宝藏 解题思路 考场上一眼出 \(nlog^2\) 做法,然后没看见是 1s 3e5 的数据,我竟然以为自己切了?? 考完之后尝试着把二分改为指针的移动,然后就过了??或许是数据水吧,感觉自己的 ...

  2. 字符串匹配(kmp+trie+aho-corasic automaton+fail tree)

    目录 kmp 那么怎么快速求最长前缀后缀呢 trie aho-corasic automaton fail tree kmp 对于一个字符串\(s_{0\dots n}\),称\(s_{0\dots ...

  3. opencv学习(一)——图像入门

    图像入门 一.读取图像 在opencv中使用cv.imread(filename, flags)函数读取图像.filename参数表示读取图像的路径.读取图像的路径应完整给出,且不能含有中文,否则在调 ...

  4. Wedding DJ题解 (回归OI)

    写在前面 高考结束了, 很遗憾, 我是其中的失败者, zzu, 没有想过最后来到这个学校, 并且还是信息安全专业, 不过, 时间久了, 也慢慢适应了: 当我被这个学校的这个专业录取, 也就注定着, 我 ...

  5. cf2A Winner(implementation)

    题意: N个回合. 每个回合:name score[名为name的这个人得了score分(可负可正)]. 问最后谁的累积分数是最高的.设为M.如果有好几个都得了M,找出这几个人中哪个最早回合累积分数超 ...

  6. k8s入坑之路(11)kubernetes服务发现

    kubernetes访问场景 1.集群内部访问 2.集群内部访问外部 3.集群外部访问内部 1.集群内部访问 1.pod之间直接ip通讯(利用calico通过路由表经过三层将ip流量转发)由于容器之间 ...

  7. The 'stream().forEach()' chain can be replaced with 'forEach()' (may change semantics)

    对集合操作时,因不同的写法Idea经常会提示:The 'stream().forEach()' chain can be replaced with 'forEach()' (may change s ...

  8. 大一C语言学习笔记(9)---指针篇--从”内存的使用“和“流程控制”的角度来理解“指针变量的使用‘

    #深入理解指针变量 举个错误栗子: //以下代码的目的是输出100和1000,但输出结果只有一个100 #include<stdio.h> #include<malloc.h> ...

  9. Python基础(__slots__)

    class Point(object): __slots__ = ('name','point') p1 = Point() p1.name = 100 print(p1.name)#100 #p1. ...

  10. Python基础(获取对象信息)

    import types print(type('abc') == str)#True print(type(123) == int)#True def f1(): pass print(type(f ...