蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目
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.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
翻译
很简单的整数倒置 需要注意的是符号和范围
利用Python则很简单
Hints
Related Topics: Math
代码
Java
class Solution {
public int reverse(int x) {
long result = 0;
while(x!=0){
result = result*10+x%10;
if(result>Integer.MAX_VALUE||result<Integer.MIN_VALUE)
return 0;
x = x/10;
}
return (int)result;
}
}
Python
class Solution(object):
def reverse(self, x):
sign = 1 if x>=0 else -1
x = x*sign
xs = str(x)
ys = xs[::-1]
y = int(ys)*sign
if y>2**31-1 or y<-2**31:
return 0
return y
蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]的更多相关文章
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]
题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...
随机推荐
- 6 [面向对象]-property
1.特性(property) 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 例一:BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性 ...
- 25-[jQuery]-事件
重点:jQuery事件绑定on().bind()与delegate() 方法详解 1.jquery的事件 <!DOCTYPE html> <html lang="en&qu ...
- CF 700 E. Cool Slogans
E. Cool Slogans 链接 题意: 给定一个字符串S,从中选出k个子串a[1],a[2]...a[k],满足a[i]在a[i+1]中出现了两次(可以重叠),求最大的k. 分析: 建出SAM, ...
- Redis学习之路(二)之Redis入门基础
一.Redis基本介绍 (1)Redis介绍 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(string ...
- Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)
题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...
- SQL Server 任务调度
SQL Server 内部集成了一个专用的操作系统,叫做SQLOS,处于SQL Server和Windows的中间层.SQLOS是一个协同式的多任务调度系统,使用非抢占式争用资源,用于管理线程调度.I ...
- 轻量级企业私有云 JimV 分享
当前云市场分析 云分两种,公有云.私有云.目前市面上的云产品,对于中小规模的企业来讲,痛点有如下几点: 私有云: 1.VMware ESXi 类: a) 授权费用昂贵: b) 创建虚拟机费时费力: 2 ...
- 查找linux镜像源中的软件版本并进行安装
输入以下代码进行软件查找 sudo apt-cache search YourSoftwareName 根据所得到的结果进行安装 sudo apt-get install YourSoftwareNa ...
- mysql的安装教程-【linux】
先卸载系统自带的mysql,停止mysql:service mysql stop 1.查找以前是否装有mysql命令:rpm -qa|grep -i mysql可以看到mysql的几个包:qt-mys ...
- 启动docker 端口映射时IPV4无法使用
CentOS7 Docker启动一个web服务,使用端口映射报错: WARNING: IPv4 forwarding is disabled. Networking will not work. 查找 ...