反向整数

给定一个 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实现】的更多相关文章

  1. 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 + ...

  2. 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 ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. 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 ...

  5. Spring JdbcTemplate 查询结果集Map反向生成Java实体(转)

    原文地址:Spring JdbcTemplate 查询结果集Map反向生成Java实体 以前写过一篇文章吐槽过Spring JdbcTemplate的queryForList方法(参见:http:// ...

  6. C通过JNI反向调用JAVA程序方法

    JNI反向调用JAVA程序 引述:上文讲过java线程---OS线程的关系,然后C怎样反向调用JAVA程序方法是我们这篇讲的重点 1.ThreadTest中添加run()方法 2.编译ThreadTe ...

  7. leetcode python两整数之和

    # Leetcode 371 两整数之和***### 题目描述 **不使用**运算符 `+` 和 `-` ​​​​​​​,计算两整数 `​​​​​​​a `.`b` ​​​​​​​之和. **示例1: ...

  8. leetcode.字符串.12整数转罗马数字-Java

    1. 具体题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. I 1V   5X 10L     50C    100D    500M   1000例如, 罗马数字 2 写做  ...

  9. Java实现 LeetCode 371 两整数之和

    371. 两整数之和 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a .b ​​​​​​​之和. 示例 1: 输入: a = 1, b = 2 输出: 3 示例 2: 输入: ...

随机推荐

  1. 删除唯一性约束unique

    删除唯一性约束 语法如下: alter table tableName drop index key_name;#删除唯一性约束,实际就是删除索引 drop index key_name on tab ...

  2. P3410 拍照

    漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P3410 题目描述 小B有n个下属,现小B要带着一些下属让别人拍照. 有m个人,每个人都愿意付给小B一 ...

  3. js自动访问数据库

    js自动访问数据库 maven依赖 <dependencies> <dependency> <groupId>junit</groupId> <a ...

  4. cross-env 使用方式

    cross-env能跨平台设置及使用环境变量 大多数情况下,在windows平台下使用类似于: NODE_ENV=production的命令行指令会卡住,windows平台与POSIX在使用命令行时有 ...

  5. sql server 使用SqlBulkCopy批量插入数据库

    sql server sqlbulkcopy 批量数据插入数据库使用的是System.Data.SqlClient中的 SqlBulkCopy批量数据插入数据库 sql server 使用SqlBul ...

  6. [Java复习] 分布式锁 Zookeeper Redis

    一般实现分布式锁都有哪些方式? 使用 Redis 如何设计分布式锁?使用 Zookeeper 来设计分布式锁可以吗? 这两种分布式锁的实现方式哪种效率比较高? 1. Zookeeper 都有哪些使用场 ...

  7. 001-poi-excel-基础、单元格使用操作

    一.概述 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI ...

  8. osg::Texture2D 贴纹理

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...

  9. Unicode浅析——调用科大讯飞语音合成接口(日语)所遇到的天坑

    如题,最近做的项目需要调用科大讯飞的语音合成接口,将日文合成日语.然后坑爹的是跟我对接的那一方直接扔过来一份接口文档,里面并未提及日语合成所需要的参数.中文.英文合成倒是没问题,就这个日语合成的音频始 ...

  10. linux禁止特定ip访问某个端口

    linux禁止特定ip访问某个端口   解决方法: 禁止特定ip访问8501端口的命令0:iptables -I INPUT -s 192.168.0.232 -ptcp --dport 8501 - ...