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是除数. 唯一不同 ...
随机推荐
- Hi3516EV200 编译环境配置及交叉编译软件包
基础信息 OS: Ubuntu 16.04 xenial SDK 版本: Hi3516EV200R001C01SPC012 - Hi3516EV200_SDK_V1.0.1.1 SDK 包路径:Hi3 ...
- es6 形参的陷阱
先看代码: var x = 1; function s (a,y = function (){ x = 2 }){ var x = 1; y(); console.log(x) ...
- 查看docker服务状态
root用户使用 #查看docker服务状态: systemctl status docker 非root用户使用 #查看docker服务: sudo systemctl status docker
- Ubuntu修改启动顺序以及系统时间同步问题
Ubuntu修改启动顺序以及系统时间同步问题 修改启动顺序 选择要优先启动的序号,从0开始计数 修改配置文件 sudo vim /etc/default/grub 使用这个命令刷新一下 sudo up ...
- 自制一个超级简单的 php 发邮件的轮子 simpleMailTool.php
simpleMailTool 程序链接 https://github.com/kohunglee/simpleMailTool/ 一个简单的 php 发邮件的轮子,跟其他著名大轮子相比(如 PHPMa ...
- 【MathJax】语法总结
基础语法 1.显示公式 在行中显示的 (inline mode),就用 $...$ 单独一行显示 (display mode),则用 $$...$$ 2.希腊字母 要显示希腊字母,可以用 \alpha ...
- 【Java】(机考常用)类集
类集框架(集合框架)是一个用来代表和操纵集合的统一架构.所有的类集框架都包含如下内容: 接口:是代表类集的抽象数据类型.之所以定义多个接口,是为了以不同的方式操作集合对象. 例如:Collection ...
- 【教程】Anaconda安装
零.Anaconda介绍 Anaconda个人版是一个免费.易于安装的包管理器.环境管理器和Python发行版(所以装了Anaconda就可以不用再另外装Python了),有提供非常多的开源包,用An ...
- ChatGTP获取的d读取excel通用程序。
procedure ReadExcelFile(const FileName: string; AMemtable: TFDmemtable); var ExcelApp: OleVariant; S ...
- SQLServer中事务处理
--将当前库存记录insert医废转移单中 --declare @Warehouse nvarchar(100); declare @Warehouse_JJRID nvarchar(100); de ...