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 分析: ...
随机推荐
- Centos7.2 下搭建LNMP环境(终极版)Yum安装
PHP7.1+Nginx+MySQL5.7 安装PHP //安装源只要遇到选择的全是Y rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-rele ...
- javascript使用技巧总结,不断更新...
1.使用a标签来获得当前页面相对地址的绝对地址 function getAbsoluteUrl(url){ var a; if(!a) a = document.createElement('a'); ...
- WebFlux02 SpringBoot WebFlux项目骨架搭建
1 环境搭建 1.1 版本说明 jdk -> 1.8 maven -3.5 springboot -> 2.0.3 开发工具 -> IDEA 1.2 创建项目 利用 IDEA 或者 ...
- Java 基于quartz实现定时 之一(注解方式配置)
需要在项目里,导入quartz.jar package cn.zr.pringmvctest.trigger; import org.springframework.context.annotatio ...
- 面试题:MySQL性能调优——索引详解与索引的优化 没用
——索引优化,可以说是数据库相关优化.理解尤其是查询优化中最常用的优化手段之一.所以,只有深入索引的实现原理.存储方式.不同索引间区别,才能设计或使用最优的索引,最大幅度的提升查询效率! 一.BTre ...
- Python开发 第一篇 python的前世今生
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- 线程同步synchronized,wait,notifyAll 测试示例
https://www.cnblogs.com/LipeiNet/p/6475851.html 一 synchronized synchronized中文解释是同步,那么什么是同步呢,解释就是程序中 ...
- linq to sql 实现左(右)连接,那个方法是对的,该怎么处理
linq to sql 实现左(右)连接,那个方法是对的var query2 = from tb0 in db.table_0 join tb1 in db.table_1 on table_0.关 ...
- springboot调用微信的jscode2session报JSONObject异常
问题背景: 服务器为Centos6.5 JDK:OpenJDK1.7 tomcat7 项目为微信小程序api的后端代码,解密用户信息,代码是采用springboot写的 异常信息: 代码: json异 ...
- MongoDB整理笔记の进程控制
查看活动进程 > db.currentOp(); > // 等同于: db.$cmd.sys.inprog.findOne() { inprog: [ { "opid" ...