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. notepad++配置编译运行java

    点击插件->Plugin Manager->show plugin manager : 选择NppExec,选择install,就将这个插件下载下来了. 这个时候会重启notepad++: ...

  2. HDU - 1024 M子段最大和 简单DP

    如何确保每个段至少一个数是关键(尤其注意负数情况) #include<iostream> #include<algorithm> #include<cstdio> ...

  3. BZOJ - 1003 DP+最短路

    这道题被马老板毒瘤了一下,TLE到怀疑人生 //然而BZOJ上妥妥地过了(5500ms+ -> 400ms+) 要么SPFA太玄学要么是初始化block被卡到O(n^4) 不管了,不改了 另外D ...

  4. 为什么Kafka那么快,明显领先其他mq?

    经常看到有很多Kafka的测试文章,测试结果通常都是“吊打”其他MQ.感慨它的牛B之余我觉得必要仔细分析一下它如此快速的原因.这篇文章不同于其他介绍Kafka使用或者技术实现的文章,此处我会重点解释— ...

  5. jsonProperty

    //说明:界面参数name需要为: employeeName,Json格式的话需要传入:employee_name @JsonProperty("employee_name") p ...

  6. django学习笔记——搭建博客网站

    1. 配置环境,创建django工程 虚拟环境下建立Django工程,即创建一个包含python脚本文件和django配置文件的目录或者文件夹,其中manage.py是django的工程管理助手.(可 ...

  7. python-几种快速了解函数及模块功能的方式

    背景 在进行编程的时候经常要导入各种包的各种函数,但是很多包一下又不知道为什么要导入这个模块,所以想总结下有哪些方法可以让我们快速熟悉其中函数的作用. import numpy as np impor ...

  8. filter get乱码 全站编码解决 包装模式

    包装模式简介: package com.itheima.test; import java.io.BufferedReader; import java.io.IOException; import ...

  9. 关于eclipse添加自动查找文件以及svn的插件

    1.添加自动查找当前文件位置的插件(如下图) 在百度搜索下载 OpenExplorer_1.5.0.v201108051313.jar,下载之后放入eclipse下面的plugin文件夹下面既可以 2 ...

  10. bootstrap框架的使用

    1.默认修改input输入框激活的颜色(充电桩) .form-control:focus, .ms-choice:focus, input[type=text]:focus, input[type=p ...