题目:

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

分析:

乍看,好似是一个很简单的题目,只需要将整数从最低位起到最高位依次处理即可,但是,此题的关键在于如何处理溢出数据。我们知道,Integer类型数据的范围是:
#define       INT_MIN         (-2147483647 - 1)          /* minimum (signed) int value */

#define       INT_MAX       2147483647                   /* maximum (signed) int value */
而且,此处的溢出判断必须同时包括原始数据x 以及反转结果,均不越界。

溢出判断:

(1)2147483647 为十位数,首先,当x为9位数及以下时,原始数据与反转数据均不会越界;
(2)对反转做特殊判断,参考博客:http://blog.csdn.net/stephen_wong/article/details/28779481
溢出判断代码:
bool overflow(int x)
{
if (x / 1000000000 == 0) // x的绝对值小于1000000000, 不越界
{
return false;
} else if (x == INT_MIN) // INT_MIN反转后越界,也没法按下述方法取绝对值(需要特判),直接返回true
{
return true;
}
x = abs(x);
// x = d463847412 -> 2147483647. (参数x,本身没有越界,所以d肯定是1或2)
// or -d463847412 -> -2147483648.
for (int cmp = 463847412; cmp != 0; cmp/=10, x/=10)
{
if ( x%10 > cmp%10 )
{
return true;
} else if (x%10 < cmp%10)
{
return false;
}
} return false;
}

AC代码:

class Solution
{
public:
int reverse(int x)
{
if( overflow(x) == true)
{
return 0;
} int result = 0; while (x!=0)
{
result = 10*result + x%10;
x /= 10;
} return result;
}
private:
bool overflow(int x)
{
if (x / 1000000000 == 0) // x的绝对值小于1000000000, 不越界
{
return false;
} else if (x == INT_MIN) // INT_MIN反转后越界,也没法按下述方法取绝对值(需要特判),直接返回true
{
return true;
}
x = abs(x);
// x = d463847412 -> 2147483647. (参数x,本身没有越界,所以d肯定是1或2)
// or -d463847412 -> -2147483648.
for (int cmp = 463847412; cmp != 0; cmp/=10, x/=10)
{
if ( x%10 > cmp%10 )
{
return true;
} else if (x%10 < cmp%10)
{
return false;
}
} return false;
}
};

LeetCode07 AC代码下载


LeetCode(7)Reverse Integer的更多相关文章

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

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

  2. LeetCode(47)-Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  3. LeetCode(190) Reverse Bits

    题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  4. LeetCode(151) Reverse Words in a String

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  5. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  6. LeetCode(92) Reverse Linked List II

    题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...

  7. LeetCode(25)Reverse Nodes in k-Group

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  8. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. [poj 2104] K-th Number【主席树】

    传送门:http://poj.org/problem?id=2104 保存模版. #include <cstdio> #include <algorithm> #include ...

  2. Codeforces Round #396 (Div. 2) A

    While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common s ...

  3. Dwarves, Hats and Extrasensory Abilities Codeforces - 1063C

    https://codeforces.com/contest/1063/problem/C 首先可以想到一个简单做法:先钦定这个直线的斜率k=-1,然后设直线y=-x+b 设黑点放直线上方:如果已知( ...

  4. python_17(sql)

    第1章 MySQL安装 1.1 windows版下载地址 1.2 添加环境变量 1.3 初始化 1.4 启动mysql服务 1.5 链接mysql 1.6 制作mysql的windows服务 1.7 ...

  5. appcloud 加载第三方页面loading效果

    apiready = function() { var header = $api.byId('header'); $api.fixIos7Bar(header); var headerPos = $ ...

  6. 实现如下语法的功能:var a = (5).plus(3).minus(6);

    Number.prototype.plus= function(val){ return parseInt(this)+val; }; Number.prototype.minus= function ...

  7. APP弱网测试点

  8. [文章泛读] The varying faces of a program transformation systems (ACM Inroads, 2012)

    Beevi S. Nadera, D. Chitraprasad, and Vinod S. S. Chandra. 2012. The varying faces of a program tran ...

  9. js生成txt文件

    HTML CODE: <div class="modal-footer"> <a onfocus="this.blur();" id=&quo ...

  10. nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati

    root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-mod ...