LeetCode记录之7——Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
反转数字的整数。
示例1:x = 123,返回321
示例2:x = -123,返回-321
注意:
假定输入为32位有符号整数。 当反转的整数溢出时,你的函数应该返回0。
做LeetCode我个人最大的缺点就是上来就干,因为英语毕竟没有中文看着方便,导致好多提示根本没有注意。如本题溢出时返回0。
class Solution {
public int reverse(int x) {
boolean isAbove=true;
StringBuffer sBuffer=new StringBuffer();
int newX=x;
if (x < 0) {
isAbove = false;
x = Math.abs(x);
}
while (x > 0) {
sBuffer.append(x % 10);
x = x / 10;
}
if (newX!=0) {
try {
if (isAbove)
return Integer.parseInt(sBuffer.toString());
else
return -Integer.parseInt(sBuffer.toString());
} catch (Exception e) {
return 0;
} }
else
return 0;
}
}
顺道来复习下JAVA基本数据类型的数值范围:
byte的取值范围为-128~127,占用1个字节(-2的7次方到2的7次方-1)
short的取值范围为-32768~32767,占用2个字节(-2的15次方到2的15次方-1)
int的取值范围为(-2147483648~2147483647),占用4个字节(-2的31次方到2的31次方-1)
long的取值范围为(-9223372036854774808~9223372036854774807),占用8个字节(-2的63次方到2的63次方-1)
LeetCode记录之7——Reverse Integer的更多相关文章
- 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之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
- leetCode练题——7. Reverse Integer
1.题目: 7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...
- leetcode第七题--Reverse Integer
Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- 【LeetCode算法-7】Reverse Integer
LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- LeetCode(7)Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...
随机推荐
- C. Ray Tracing——披着搜索外衣的扩展欧几里得
[题目大意] 给你一个n*m的矩形,光线从(0,0)出发,沿右上方向以每秒根号2米的速度运动,碰到矩形边界就会反弹(符合物理规律的反弹),询问k个点,这些点都在矩形内部且不在矩形边界上,求光经过这些点 ...
- opennebula 镜像池
{ "IMAGE_POOL": { "IMAGE": [ { ", ", ", "TEMPLATE": { & ...
- SQL 数据排重,去掉重复数据 有用
.最大的错误: 在对数据排重的时候,首先想到的就是Distinct,虽然这很管用,但多数场合下不适用,因为通常排重后还要做进一步处理,比如对编号排重后要按日期统计等. 无法排重的Group by ...
- Luogu 4069 [SDOI2016]游戏
BZOJ 4515 树链剖分 + 李超线段树 要求支持区间插入一条线段,然后查询一个区间内的最小值.可以使用李超线段树解决,因为要维护一个区间内的最小值,所以每一个结点再维护一个$res$表示这个区间 ...
- App测试从入门到精通之功能测试
App的功能测试指的是针对软件需求以及用户要求针对APP功能进行测试.简单点理解就是保证App功能的正确性,不要系统出现Bug.让用户用户的舒服,用的爽!好了,我们看下关于App的功能测试要点有哪些? ...
- css总结19:HTML5 Canvas(画布)
1 <canvas> 标签定义图形,比如图表和其他图像. 例1:简单使用: <canvas id="Canva" width="200" h ...
- 三分题两道:lightoj1146 Closest Distance、lightoj1240 Point Segment Distance (3D)
lightoj1146 Two men are moving concurrently, one man is moving from A to B and other man is moving f ...
- JQuery.validator插件使用
首先给变量validator赋值 var validator =$('#test').validate({validate构造 }); 接着调用 $('#test').valid() 会使用上面的验证 ...
- .net core webapi 文件上传在 Swagger 文档中的有好提示处理
前提: 需要nuget Swashbuckle.AspNetCore 我暂时用的是 4.01 最新版本: 描述:解决 .net core webapi 上传文件使用的是 IFormFile,在S ...
- SQL 全角半角转换-(摘抄)
/****** SQL转换全角/半角函数 开始******/ CREATE FUNCTION ConvertWordAngle ( @str NVARCHAR(4000), --要转换的字符串 @fl ...