【Leetcode】【Easy】Reverse Integer
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- 【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 ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- 【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 ...
- 【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 ...
随机推荐
- UVA - 11795 状压DP
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- springboot(四)-项目部署
Springboot和我们之前学习的web应用程序不一样,其本质上是一个java应用程序.部署的方式有两种:打成jar包,或者打成war包. 打成jar包 切换到项目文件中 然后mvn install ...
- Oracle 11g 新特性 --SQL Plan Management 说明
Oracle 11g 新特性 --SQL Plan Management 说明 参见大神博主文章: http://blog.csdn.net/tianlesoftware/article/detail ...
- casper爬虫操作记录
phantom.outputEncoding = "gbk"; var fs=require("fs") //var data = fs.read(" ...
- datetime和date如何转json
import json from datetime import datetime, date class MyJson(json.JSONEncoder): def default(self, o) ...
- MySql——安装与配置与启动和停止
在Linux上安装mysql数据库,我们可以去其官网上下载mysql数据库的rpm包,http://dev.mysql.com/downloads/mysql/5.6.html#downloads,大 ...
- AttackEnemy人物攻击判断
AttackEnemy人物攻击判断 /// <param name="attackArea">攻击范围</param> /// <param name ...
- (转)裸奔的后果!一次ssh被篡改的入侵事件
裸奔的后果!一次ssh被篡改的入侵事件 原文:http://blog.51cto.com/phenixikki/1546669 通常服务器安全问题在规模较小的公司常常被忽略,没有负责安全的专员,尤其是 ...
- Go语言下载网络图片或文件
最近闲来无事, 于是就简单学习了下Go语言的基本的用法.由于实践才是最快的学习方法,所以这里就以下载网络图片或文件入手来学习Go语言 文件下载到本地,通常的思路就是先获得网络文件的 输入流 以及本地文 ...
- params传递任意参数
namespace 传递任意参数{ class Program { static void Main(string[] args) { //可传递任意数量参数 Test(1, 2, "sas ...