题目:

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. Codeforces Round #546 (Div. 2) B. Nastya Is Playing Computer Games

    链接:https://codeforces.com/contest/1136/problem/B 题意: 有n个井盖,每个井盖上有一个小石头. 给出n和k,k表示刚开始在第k个井盖上方. 有三种操作, ...

  2. jQuery position() 源码解读

    position的代码比较简单... position: function() { if ( !this[ 0 ] ) { return; } var offsetParent, offset, el ...

  3. 渣渣菜鸡为什么要看 ElasticSearch 源码?

    前提 人工智能.大数据快速发展的今天,对于 TB 甚至 PB 级大数据的快速检索已然成为刚需,大型企业早已淹没在系统生成的浩瀚数据流当中.大数据技术业已集中在如何存储和处理这些海量的数据上.Elast ...

  4. [android][avd位置修改][需自己手动]

    1.找到avd目录(C:\的那个)并打开,移动.avd文件到需要的位置. 2.修改ini文件path,完成.

  5. Sass基本特性

    Sass扩展/继承@extend 代码的继承,声明方式:.class;调用方式:@extend 如: .btn { border: 1px solid #ccc; padding: 6px 10px; ...

  6. 安卓ListView基础应用

    listview简单描述 主页面: package com.example.listview; import com.lidroid.xutils.ViewUtils; import com.lidr ...

  7. iOS 画圆图片的几种方法

    方法一: self.cycleImv= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)]; [self.view addS ...

  8. moment算本月开始日期和结束日期

    moment算本月开始日期和结束日期 1.引入moment.js var vStartDate=new moment().add('month',addMonth).format("YYYY ...

  9. Windwos下安装和使用MongoDB

    1)下载安装包下载路径:https://www.mongodb.com/download-center#community包名称:mongodb-win32-x86_64-3.4.10-signed. ...

  10. cookie安全

    www.baidu.com host 文件 定义 a.baidu.com 为127.0.01 本地编写php程序 读取浏览器发送给 a.baidu.com的cookie 会把 .baidu.com域下 ...