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: 输入: ...
随机推荐
- 【原创】go语言学习(十三)struct介绍2
目录: 方法的定义 函数和方法的区别 值类型和指针类型 面向对象和继承 结构体和json序列化 方法的定义 1.和其他语言不一样,Go的方法采⽤用另外一种方式实现. package main impo ...
- 【原创】go语言学习(五)函数详解1
目录 1.函数介绍 2.多返回值和可变参数 3.defer语句 4.内置函数介绍 1.函数介绍 1.1定义: 有输⼊入.有输出,⽤用来执⾏行行⼀一个指定任务的代码块. func functionnam ...
- python快捷键的使用【摘抄】
接触python有些快捷键还不熟悉,搜索到下面这个文章很好的转发和摘抄了,感谢作者的用心分析 摘抄来源:https://www.cnblogs.com/haiyan123/p/7170593.html ...
- 三十三、DNS资源记录类型和请求流程
DNS分布均衡(Load balance)的实现 在上级数据库中写两条记录(同一个名字对应对个IP时),DNS会自动将请求基于轮循方式,分给每个DNS服务器 例如: 第一次将请求给第一个DNS,第二次 ...
- shell 脚本同时对远程多台机器执行命令
脚本1:需要机器之间免密 ssh-copy-id [-i [identity_file]] [user@]machine #!/bin/bash # ------------------------- ...
- Tkinter 之Canvas画布
一.参数说明 参数 作用 background(bg) 指定 Canvas 的背景颜色 borderwidth(bd) 指定 Canvas 的边框宽度 closeenough 指定一个距离,当鼠标与画 ...
- [游戏开发]LÖVE2D(1):引擎介绍
什么是LÖVE引擎 Love引擎是一个非常棒的框架,你可以用来在Lua制作2D游戏.它是免费的,开源的,适用于Windows,Mac OS X,Linux,Android和iOS. 怎么安装 在官网下 ...
- C++ private,public,protected 关键字
第一: private,public,protected的访问范围: private: 只能由该类中的函数.其友元函数访问,不能被任何其他访问,该类的对象也不能访问. protected: 可以被 ...
- C# GDI+ 简单实现图片写文字和图片叠加(水印)(转)
using System; using System.Collections; using System.Configuration; using System.Data; using System. ...
- Vuex 通俗版教程告诉你Vuex怎么用
写在文前: 最近一直在用vue开发项目,写来写去就是那么些方法,对于简单的项目一些常用的vue方法足以解决,但是涉及到页面状态,权限判断等一些复杂的传值,vuex是必须的.对于vuex也运用一段时间, ...