原题:

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. yum使用

    一.使用yum安装和卸载软件,有个前提是yum安装的软件包都是rpm格式的.安装的命令是,yum install ~,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲突 ...

  2. PHP原理之变量

    作者: Laruence(   ) 本文地址: http://www.laruence.com/2008/08/22/412.html 转载请注明出处 或许你知道,或许你不知道,PHP是一个弱类型,动 ...

  3. linux网络管理----网络基础

    1.1 ISO/OSI 七层模型 笔记: 打入ipconfig: mac地址:Media Access Control 也是物理地址,这是由网卡决定的.负责的是内网,也就是局域网通信. IPv4地址: ...

  4. PHP函数usort是咋回事?还能当后门?

    开始 详情看这:https://www.leavesongs.com/PHP/bypass-eval-length-restrict.html 原谅我见识短,没用过usort函数 上面连接的文章中,发 ...

  5. [HihoCoder1169]猜单词

    题目大意: 给你一个数列,问区间[l,r]内与k最接近的数与k的差是多少. 思路: 将数列中的数和询问的数先从小到大排序, 从小到大枚举每个数,如果是数列上的,就加到线段树中, 如果是询问中的,就在线 ...

  6. JBoss 7 配置成windows启动服务

    将Jboss7 server 配置成一个windows启动服务的两个文件,部署步骤如下: 1.    先检查是否配置java_home和jboss_home的环境变量,如没配置上先配置,如我的是JBO ...

  7. [bzoj1025][SCOI2009]游戏 (分组背包)

    Description windy学会了一种游戏.对于1到N这N个数字,都有唯一 且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们 ...

  8. noip2013 车站分级

    题目描述 一条单向的铁路线上,依次有编号为 1, 2, …, n1,2,…,n的 nn个火车站.每个火车站都有一个级别,最低为 11 级.现有若干趟车次在这条线路上行驶,每一趟都满足如下要求:如果这趟 ...

  9. python开发_filecmp

    filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单.python标准库还提供了difflib模块用于比较文件的内容.关于difflib模块,且听下回分解. filecmp ...

  10. Codeforces Beta Round #6 (Div. 2 Only) B. President's Office 水题

    B. President's Office 题目连接: http://codeforces.com/contest/6/problem/B Description President of Berla ...