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 分析: ...
随机推荐
- maven学习总结-eclipse开发
一.创建Web项目 1.1 选择建立Maven Project 选择File -> New ->Project,如下图所示:
- box-shadow 内阴影
1.盒子阴影样式单词:box-shadow 2.语法 代表边框间距靠左0 靠上0 和1px阴影范围 阴影颜色为黑色(#000),有inset 代表框内阴影 ,不带inset 代表框外阴影. 注意:bo ...
- c语言实践 给三个数输出最大的那个数
我是怎么想的,我前面学过两个数比大小,比如有三个数,a b c,先比较a和b的大小,然后用那个较大的和c比较就得出最大的那个了.这个求三个数比大小的问题最后变化成 了两个数比大小了. int main ...
- Luogu 4213 【模板】杜教筛(Sum)
当作杜教筛的笔记吧. 杜教筛 要求一个积性函数$f(i)$的前缀和,现在这个东西并不是很好算,那么我们考虑让它卷上另外一个积性函数$g(i)$,使$(f * g)$的前缀和变得方便计算,然后再反推出这 ...
- 高级软件测试技术(测试管理工具实践day3)
昨天在晚上由于安装bugzilla中有一些小问题,并且需要手工安装很多perl模块 ppm install XXX(模块名称).一直到过了十二点就没有继续更博了.所以由今天更. 继昨天的安装问题之后 ...
- YDNJS(上卷):this 的绑定对象
函数中的 this 是在调用时被绑定的,this 指向谁完全取决于函数的调用位置. 确定 this 的绑定对象的方式有 4 种. 默认绑定 默认绑定就是将函数中的 this 绑定给了全局对象 wind ...
- MySQL事务隔离级别测试实例
https://www.cnblogs.com/huanongying/p/7021555.html MySQL事务隔离级别 事务隔离级别 脏读 不可重复读 幻读 读未提交(read-uncommit ...
- [Lua快速了解一下]Lua的控制语句
-Lua中没有++或者--的骚操作 -while loop sum = num = do sum = sum + num num = num + end print("sum =" ...
- HTML 5与CSS 3权威指南(第2版·上册) 中文pdf扫描版
HTML5与CSS3权威指南(第2版·上册)已经成为HTML 5与CSS 3图书领域的一个标杆,被读者誉为“系统学习HTML 5与CSS 3技术的最佳指导参考书之一”和“Web前端工程师案头必备图书之 ...
- 关于CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary AS
1.添加用户"Network Service” 和 “IIS_IUSERS” 读下面目录的读写权限 a) C:\Windows\Temp b) C:\Windows\Microsoft.NE ...