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. WIN7中 HttpListener 拒绝访问 异常解决 C#

      WIN7中 HttpListener 拒绝访问 异常解决 C# http://www.cnblogs.com/cmdszh/archive/2012/08/16/httplistener.html ...

  2. python-%操作符

    1.打印字符串 print("His name is %s"%("Aviad")) His name is Aviad 2.打印整数print("He ...

  3. codeforces 1101F Trucks and Cities 区间dp+单调优化 好题

    题目传送门 题意简述:(来自洛谷) 有n个城市坐落在一条数轴上,第ii个城市位于位置ai​. 城市之间有m辆卡车穿行.每辆卡车有四个参数:si​为起点编号,fi​为终点编号,ci​表示每行驶1个单位长 ...

  4. 后台返回的值ajax接收不到

    原因有很多种可能,我遇到的是后台写的Controller忘记了加@ResponseBody,导致springMVC把返回的字符串当成view了

  5. mysql 查询 TOP N 问题

    Q:有一个学生成绩表,表名 stu(学生表),字段有:id(主键),name(学生姓名),subject(学科),score(分数) 1.查询该表中,所有科目都及格的学生 ; 说明:都及格的话,就是最 ...

  6. 给python解释器本身添加注册表

    import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.pr ...

  7. node+express搭建过程以及安装ejs模板引擎解决方案

    一.Node.js简介 1.Node.js是什么? Node.js 可以作为服务器向用户提供服务,与 PHP.Python.Ruby on Rails 相比,它跳过了 Apache.Nginx 等 H ...

  8. Sublime Text加上Eclipse

    打造属于自己的前端开发神器 -- 给Sublime Text加上Eclipse的光环     将Sublime Text打造成如Eclipse一般的前端开发IDE 1. 快捷键移植篇   从Java开 ...

  9. NPOI之C#下载Excel

    Java中这个类库叫POI,C#中叫NPOI,很多从Java一直到.Net平台的类库为了区别大部分都是在前面加个N,比如Hibernate和NHibernate. npoi下载地址 一.使用NPOI下 ...

  10. Sql server 0x80131904

    这是一个比较让人纠结的错误. 背景:添加了网站的分支,要拿一个现有的数据库做一个新的数据库的东东. 首先就是还原备份,然后做代码的修改.然而在登录的时候报了这个错误.0x80131904 什么插入的列 ...