leetCode:reverseInteger 反向整数 【JAVA实现】
反向整数
给定一个 32 位有符号整数,将整数中的数字进行反转,如果超出整数的最大或者最小范围返回0
更多文章查看个人博客 个人博客地址:反向整数
方法一
利用StringBuilder的reverse方法,将数字转换成字符反转然后再转换回整数
public int reverseInteger(int num) {
if (num == 0 || (num < 10 && num > -10)) {
return num;
}
// 获得绝对值 去掉正负号
int temp = Math.abs(num);
StringBuilder st = new StringBuilder(String.valueOf(temp));
StringBuilder reverse = st.reverse();
long result = Long.valueOf(reverse.toString());
if (num > 0) {
return result > Integer.MAX_VALUE ? 0 : (int) result;
} else {
return result < Integer.MIN_VALUE ? 0 : -(int) result;
}
}
- StringBuilder reverse方法也是通过一个for循环把字符反转,时间复杂度是O(n),空间复杂度O(n)
名称 | 结果 |
---|---|
时间复杂度 | O(n) |
空间复杂度 | O(n) |
方法二
利用一个循环取余的方法将整数反转,效率要比上边方法好
public int reverseInteger(int num) {
if (num == 0 || (num < 10 && num > -10)) {
return num;
}
long result = 0;
for (; num != 0; num /= 10) {
result = result * 10 + num % 10;
}
return result > Integer.MAX_VALUE || result < Integer.MIN_VALUE ? 0 : (int) result;
}
名称 | 结果 |
---|---|
时间复杂度 | O(n) |
空间复杂度 | O(1) |
更多文章查看个人博客 个人博客地址:反向整数
leetCode:reverseInteger 反向整数 【JAVA实现】的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Spring JdbcTemplate 查询结果集Map反向生成Java实体(转)
原文地址:Spring JdbcTemplate 查询结果集Map反向生成Java实体 以前写过一篇文章吐槽过Spring JdbcTemplate的queryForList方法(参见:http:// ...
- C通过JNI反向调用JAVA程序方法
JNI反向调用JAVA程序 引述:上文讲过java线程---OS线程的关系,然后C怎样反向调用JAVA程序方法是我们这篇讲的重点 1.ThreadTest中添加run()方法 2.编译ThreadTe ...
- leetcode python两整数之和
# Leetcode 371 两整数之和***### 题目描述 **不使用**运算符 `+` 和 `-` ,计算两整数 `a `.`b` 之和. **示例1: ...
- leetcode.字符串.12整数转罗马数字-Java
1. 具体题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 ...
- Java实现 LeetCode 371 两整数之和
371. 两整数之和 不使用运算符 + 和 - ,计算两整数 a .b 之和. 示例 1: 输入: a = 1, b = 2 输出: 3 示例 2: 输入: ...
随机推荐
- CF837D Round Subset 动态规划
开始的时候数据范围算错了~ 我以为整个序列 2 和 5 的个数都不超过 70 ~ 一个非常水的 dp code: #include <bits/stdc++.h> #define M 75 ...
- unbuntu16.04安装geoserver运行环境
1.下载并上传 在windows下载geoserver 2.15.1Platform Independent Binary版本, 是zip文件,然后使用xfile将zip上传到/usr/geoserv ...
- Python中一些高效的数据操作
列表统计 chars = ["a", "b", "a", "c", "a", "d&quo ...
- python3删除mysql上月分区数据(脚本)
import datetime import pymysql import pymysql.cursors tables_schdule=["talbe1","table ...
- Java 面向对象(六)
抽象类和抽象方法 抽象方法 在方法前面添加了一个关键字 abstract 抽象方法的特点 (1)抽象方法是没有方法体的. (2)抽象方法必须得要定义在抽象类 或 接口当中 (在类前面添加上了一个abs ...
- Hibernate 基本使用
Hibernate框架概述 一.什么是框架 软件的一个半成品,已经帮你完成了部分功能. 把一些不确定的东西,按照框架要求,达到相应的功能 Hibernate是JavaEE技术三层架构所用到的技术 二. ...
- qt sql 数据库操作
1. 连接数据库 mysql连接: QSqlDatabase mysql_db=QSqlDatabase::addDatabase("QMYSQL","mysql_co ...
- TynSerial基本数据类型序列(还原)
TynSerial基本数据类型序列(还原) procedure TForm1.ToolButton17Click(Sender: TObject); var serial: TynSerial; be ...
- Go -- IP to int ip字符串和十进制相互转化
package main import ( "fmt" "net" "reflect" "github.com/thinkerid ...
- git notes的使用
1. 获取notes git fetch origin refs/notes/*:refs/notes/* 2. 设置notes 2.1 git config --add core.notesRef ...