原题:

Reverse digits of an integer.

=>反转一个整数的数字。例子如下:

Example1: x = 123, return 321
Example2: x = -123, return -321

Have you thought about this?

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.

=>假如最后一位是0,那么结果会是什么样的呢?比如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?

=>你有考虑过反转后的溢出问题嘛?假如是32bit的整数,1000000003反转后就会溢出。你怎么处理这样的情况?

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).

=>抛出一个异常?很好,假如不能抛出异常呢?其实可以重新定义这个函数(比如加一个参数)

class Solution {
public:
int reverse(int x) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case. };

晓东分析:

这个题目就反转本身而言是很简单的,晓东就不多分析了。所以,我主要来说一下溢出的问题,我个人的思路就是在得到反转的值的时候,先不乘上最高位,留着进行比较。

所以总的来说,还是不复杂的。

代码实现:

class Solution {
public:
int reverse(int x) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int max_first_bit = 2; //default for 4
int max_remain_num = 147483647; int num = 0;
int temp = abs(x); while(temp >= 10){
num = num * 10 + temp % 10;
temp /= 10;
} switch(sizeof(int)){
case 1:
max_first_bit = 1;
max_remain_num = 27;
break;
case 2:
max_first_bit = 3;
max_remain_num = 2767;
break;
case 4:
max_first_bit = 2;
max_remain_num = 147483647;
break;
case 8:
max_first_bit = 9;
max_remain_num = 223372036854775807;
break;
} if(x > 0){
if (temp < max_first_bit)
return num * 10 + temp % 10;
else if(num <= max_remain_num)
return num * 10 + temp % 10;
else
throw x;
}else{
if (temp < max_first_bit)
return 0 - (num * 10 + temp % 10);
else if(num <= max_remain_num + 1)
return 0 - (num * 10 + temp % 10);
else
throw x;
} }
};

执行结果:

1020 / 1020 test cases passed.
Status:

Accepted

Runtime:
28 ms

希望大家有更好的算法能够提出来,不甚感谢。

若您觉得该文章对您有帮助,请在下面用鼠标轻轻按一下“顶”,哈哈~~·

Reverse Integer--整数的反转的更多相关文章

  1. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  2. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  3. [LeetCode]7. Reverse Integer整数反转

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  4. Leetcode7 : Reverse Integer 整数反转问题

    问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...

  5. 【LeetCode】7. Reverse Integer 整型数反转

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不 ...

  6. 7. Reverse Integer (整数的溢出)

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

  7. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  8. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

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

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

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

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

随机推荐

  1. jQuery 最简化实现

    window.jQuery = (selector) => { let nodes = {}; if (typeof selector === 'string') { //是字符串 let te ...

  2. 【LOJ】 #2520. 「FJOI2018」所罗门王的宝藏

    题解 发现似乎相当于问一个2000个元的方程组有没有解-- 然而我懵逼啊-- 发现当成图论,两个点之间连一条边,开始BFS,每个点的值赋成边权减另一个点的点权 如果一个环不合法那么肯定无解 代码 #i ...

  3. Robot Framework Selenium学习博客

    http://blog.csdn.net/tulituqi/article/details/21888059

  4. mysql 笔记(一)

    mysql 笔记 预留 mysql> use mysql; mysql> grant all privileges  on *.* to root@'%' identified by &q ...

  5. 爱奇艺全国高校算法大赛初赛C

    区间$dp$. 倒着考虑这件事件,肯定有最后一个取走的数字,假设是$a[k]$,那么最后一次取走的价值肯定是$a[0]*a[k]*a[n+1]$,之前取走的价值和为$[1,k-1]$的价值加上$[k+ ...

  6. BNUOJ 52509 Borrow Classroom

    最近公共祖先. 如果$A$到$1$的时间小于$B$到$C$再到$1$的时间,那么一定可以拦截. 如果上述时间相等,需要在到达$1$之前,两者相遇才可以拦截. #include<bits/stdc ...

  7. HDU 3339 In Action【最短路+01背包】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...

  8. SPOJDRUIDEOI - Fata7y Ya Warda!【单调栈】

    题目链接[http://www.spoj.com/problems/DRUIDEOI/en/] 题意:给出n个数,从1到n围城一个环(1和n相连),求每个数左边第一个比他大的第一个下标,右边第一个比他 ...

  9. 洛谷P1149 火柴棒等式

    题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1.加号与等号 ...

  10. 斐波那契数列(python实现)

    描述 一个斐波那契序列,F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) (n>=2),根据n的值,计算斐波那契数F(n),其中0≤n≤1000. 输入 输入 ...