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.

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?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

注意:

此题在2014年11月10日前,对结果没有加入整数溢出的测试。加上后,下面代码存在Bug(1027 / 1032 test cases passed)。

 class Solution {
public:
int reverse(int x) {
int res = ; while (x) {
res = * res + x % ;
x /= ;
} return res;
}
}; 

原因:int占用4个字节,能表示的范围是:-2,147,483,648 ~ 2,147,483,647。当反转时会遇到“10 * res”的过程,也就是此整数正着可能不越界,但是倒过来就会越界。

解题思路1:

在“10 * res”前加上判定语句,防止越界。

注意:

1、对2^31/10进行判定,不要对2^31进行判定,也就是不要和准确的边界值比大小。因为当整数已经越界时,它的值自动变化,再拿它比较永远不会越界;

2、注意多个&& || 混合时的优先级问题;

AC代码:

 class Solution {
public:
int reverse(int x) {
int res = ; while (x) {
if ((res < INT_MIN/10) || (res > INT_MAX/10)) {
res = ;
break;
} res = * res + x % ;
x /= ;
} return res;
}
};

解题思路2:

直接声明res为long(未适应32位和64位,应该是long long),这样就不会在计算中对long越界,返回结果时再判断是否对int越界;

 class Solution {
public:
int reverse(int x) {
long res = ; while (x) {
res = * res + x % ;
x /= ;
} if ((res>INT_MAX) || (res < INT_MIN))
res = ; return res;
}
};

【Leetcode】【Easy】Reverse Integer的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. C# 写 LeetCode easy #7 Reverse Integer

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

  6. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  7. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

  8. 【leetcode刷题笔记】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...

  9. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  10. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

随机推荐

  1. Codeforces - 570D 离散DFS序 特殊的子树统计 (暴力出奇迹)

    题意:给定一棵树,树上每个节点有对应的字符,多次询问在\(u\)子树的深度为\(d\)的所有节点上的字符任意组合能否凑成一个回文串 把dfs序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度 ...

  2. HDU - 1085 母函数

    年轻人的第一道母函数入门题 #include<bits/stdc++.h> using namespace std; const int maxn = 1000+2000+5000+1; ...

  3. maven tomcat eclipse 配置 debug

    1.单击Eclipse菜单"Run"中的"Run Configurations". 2.在弹出的对话框中的左侧树中找"到Maven Build&quo ...

  4. 转 Monitoring Restore/Recovery Progress

    ora-279 是可以忽略的报错 In general, a restore should take approximately the same time as a backup, if not l ...

  5. spring初始化bean的目的

    初始化bean就是为了将所有需要的bean全部装载到容器里面,等我们需要用到哪个bean就将哪个bean从容器里面拿出来

  6. vue父子组件生命周期函数执行顺序

    vue父组件加载和销毁执行最后一个钩子函数之前先执行一遍子组件的钩子: 1.加载 父:beforecreate-created-beforeMount-(子:beforecreate-created- ...

  7. Fedora中安装VLC播放器

    需要在机器上安装VLC,无奈不能直接通过yum安装,网上搜了一下,直接安装成功,其实挺简单的: 我的机器是Fedora15,其他的类似: ------------------------- 首先:su ...

  8. (转) 来自: http://man.linuxde.net/tee

    tee命令文件过滤分割与合并 tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin.简单的说就是把数据重定向到给定文件和屏幕上. 存在缓存机制,每1024个 ...

  9. 1、java线程模型

    要了解多线程,先需要把java线程模型搞清楚,否则有时候很难理清楚一个问题. 硬件多线程: 物理机硬件的并发问题跟jvm中的情况有不少相似之处,物理机的并发处理方案对于虚拟机也有相当大的参考意义.在买 ...

  10. core核心模块

    5. core核心模块 核心模块会通过compiler模块提供的调用compiler的功能, 将用户的输入转为VM直接的输入 编译模块用来编译, 而核心模块用来执行 在core.h文件中 // 不需要 ...