Reverse Integer——LeetCode进阶路⑦
原题链接https://leetcode.com/problems/reverse-integer/
- 题目描述
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321Example 2:
Input: -123
Output: -321Example 3:
Input: 120
Output: 21Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 思路分析:从高位开始对10求余,存下来乘10,依次进行……这道题实在是太温柔~
唯一需要注意的不能用粗暴法,用数组存起来再反转,会溢出的源码附录:
class Solution {
public int reverse(int x) {
if((x<=0 && x>-10)||(x<10&&x>0)){
return x;
} long result = 0;
while(x != 0 ){
result = result*10 + x%10;
x = x/10;
} if(result <= Integer.MIN_VALUE || result >= Integer.MAX_VALUE){
return 0;
} return (int)result;
}
}
Reverse Integer——LeetCode进阶路⑦的更多相关文章
- Reverse Integer LeetCode Java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public cl ...
- Reverse Integer [LeetCode]
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- Reverse Integer ---- LeetCode 007
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Solution ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
随机推荐
- redis - [07] 数据类型
redis是一个开源(BSD许可)的,内存中的数据结构存储系统,可以用作数据库.缓存和消息中间件MQ.它支持多种类型的数据结构,如字符串(String).散列(Hash).列表(List).集合( ...
- C# async/await使用举例
1.async/await几点总结 a.被async标记的方法,返回值类型只能为void.Task.Task<T>. b.被async标记的方法,内部可以有await修饰符,表明内部逻辑某 ...
- SQL Server 2025 AI相关能力初探
SQL Server 在2024年11月开始进行社区私有预览(链接),由于涉及AI能力,我也是第一时间申请了内侧资格,悲剧的是,直到2025年2月,才拿到预览版的测试资格-.-,此时已经是CTP1.3 ...
- 关于valueOf的一点思考
官方描述:返回值为该对象的原始值. 来源:Object.prototype,所以所有js对象都继承了此方法,根据犀牛书第六版的描述,对象转换为数字和字符串的时候的过程是不一样的. 对象 -> 字 ...
- Golang 入门 : 字符串及底层字符类型
字符串 基本使用 在 Go 语言中,字符串是一种基本类型,默认是通过 UTF-8 编码的字符序列,当字符为 ASCII 码时则占用 1 个字节,其它字符根据需要占用 2-4 个字节,比如中文编码通常需 ...
- NumPy学习5
今天学习了11, NumPy数组元素增删改查NumPy 数组元素的增删改查操作,主要有以下方法:数组元素操作方法函数名称 描述说明resize 返回指定形状的新数组.append 将元素值添加到数组的 ...
- chrome播放webRTC的H265视频方法
需求描述 最近有需求实现浏览器直接播放摄像头视频 鉴于Camera本身支持了rtsp流,本想web直接播放rtsp,但是还不行,搜了一下webRTC实现的效果和延迟会好一些.于是就使用了mediaMT ...
- 查看oracle数据库编码格式;ORACLE数据库NLS_CHARACTERSET和NLS_NCHAR_CHARACTERSET区别
查看Oracle数据库字符编码格式得方法,有以下两种,第二种方法有注释,第一种没有Select * from nls_database_parameter;Select * from sys.prop ...
- AIX操作系统基本命令
1,内核 bootinfo -k 2,硬件 bootinfo -r lscfg |grep proc lspv lscfg 3,操作系统 oslevel -r oslevel -s uname ...
- WIN2012域用户添加和批量添加工具
WIN2012域用户添加和批量添加,不需要进行复杂的进电脑管理去添加 直接在软件上就可单个用户添加,可批量添加,并把指定的用户加入组 可以自定义组织单位,使用起来比较简单方便. 链接:https:// ...