【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 ...
随机推荐
- c++中map的基本函数
c++中map的一些方法 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空则返回 ...
- C# Win7下隐藏手势提示
点击这里是原版答案 Stylus.IsFlicksEnabled="False" 手势是什么样子的
- 江西理工大学南昌校区排名赛 A: 单身狗的卡片游戏
题目描述 萌樱花是一只单身狗. 萌樱花今天在桌子上摆出了N张卡片,每张卡片i写着一个数字Ai 他不喜欢卡片上的数字多个出现,于是他想搞点事情拆分它们. 从桌子上选出三张卡,去掉一张数字最大,一张数字最 ...
- bugzilla部署问题
2018-09-25 1.部署环境 kvm虚拟机内 centos 7 系统 httpd+mariadb+bugzilla 关闭系统selinux.防火墙 setenforce 临时关闭se ...
- PIE SDK频率域滤波
1.算法功能简介 频率域滤波的基本工作流程为:空间域图像的傅里叶变换→频率域图像→设计滤波器→傅里叶逆变换→其他应用. 低通滤波,对频率域的图像通过滤波器削弱或抑制高频部分而保留低频部分的滤波方法,可 ...
- C#中Using里使用单例的问题
又给自己挖了一个坑跳进去. KafkaManager使用单例模型获取到一个producer,然而自己代码里用的时候加了一个using using (var producer = KafkaManage ...
- 问题记录——java.lang.IllegalArgumentException: Illegal character in scheme name at index 0
以下http请求报错是因为,请求的地址前面有个空格.... 2019-01-09 03:30:23,154 ERROR [business.modules.merchantreportresult.s ...
- 如何给MySql创建连接用户并授权
一般在为MySql创建用户时建议使用GRANT前台命令,当然如果对我们开发者而言,方法还有很多种,比如使用INSERT命令,甚至是直接修改mysql user数据表,但仍然建议按照MySQL规范去授权 ...
- Python远程连接Windows,并调用Windows命令(类似于paramiko)
import winrm win2012 = winrm.Session(')) r = win2012.run_cmd('D: &' ' cd python &' ' type s. ...
- !function()是干什么的?
叹号后面跟函数!function和加号后面跟函数+function都是跟(function(){})();这个函数是一个意思,都是告诉浏览器自动运行这个匿名函数的,因为!+()这些符号的运算符是最高的 ...