【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 ...
随机推荐
- 【算法笔记】B1047 编程团体赛
1047 编程团体赛 (20 分) 编程团体赛的规则为:每个参赛队由若干队员组成:所有队员独立比赛:参赛队的成绩为所有队员的成绩和:成绩最高的队获胜. 现给定所有队员的比赛成绩,请你编写程序找出冠军队 ...
- 洛谷 P3182 [HAOI2016]放棋子(错排问题)
题面 luogu 题解 裸的错排问题 错排问题 百度百科:\(n\)个有序的元素应有\(n!\)个不同的排列,如若一个排列使得所有的元素不在原来的位置上,则称这个排列为错排:有的叫重排.如,1 2的错 ...
- BT网站--Python开发爬虫代替.NET
BT网站-奥修磁力-Python开发爬虫代替.NET写的爬虫,主要演示访问速度和在一千万左右的HASH记录中索引效率. IBMID 磁力下载- WWW.IBMID.COM 现在用的是Python + ...
- properties 资源文件读取
1. 在source中添加资源文件 resource.properties #FTP 相关配置 #FTP 的ip地址 FTP_ADDRESS=192.168.88.142 FTP_PORT=21 ...
- srping 事物管理
1. 准备工作 1> 添加接口 BookShopDao package com.tx; public interface BookShopDao { //根据书号获取书的单价 public in ...
- 【记录】drozer与adb工具的安装与使用
drozer:链接:https://pan.baidu.com/s/1o8QOIF4 密码:a7yv adb:链接:https://pan.baidu.com/s/1o865VSm 密码:zq9t d ...
- Linux mmap 要主动释放共享内存
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sta ...
- 踩坑--springboot打war包
springboot需要jdk1.8+tomcat8.0 第一步:从IDEA中通过maven install将项目打成war包 第二步:将war包放在tomcat的webApp目录下,启动tomcat ...
- (转)CentOS/Linux 解决 SSH 连接慢
CentOS/Linux 解决 SSH 连接慢 原文:http://blog.csdn.net/doiido/article/details/43793391 现在连接linux服务器一般都是使用SS ...
- C#DataTable与Model互转
/// <summary> /// 实体转换辅助类 /// </summary> public class ModelConvertHelper<T> where ...