反向整数

给定一个 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. java+web+下载断点续传

    1.先将 webuploader-0.1.5.zip 这个文件下载下来:https://github.com/fex-team/webuploader/releases  根据个人的需求放置自己需要的 ...

  2. learning scala pattern matching 03

    code: package com.aura.scala.day01 object patternMatching03 { //当不同类型对象需要调用不同方法时,仅匹配类型的模式非常有用. def g ...

  3. [Luogu] 运输问题 -- 00

    https://www.luogu.org/problemnew/show/4015 #include <bits/stdc++.h> #define gc getchar() using ...

  4. tarjan模板完整版

    https://www.luogu.org/problem/P2863 #include<cstdio> #include<vector> using namespace st ...

  5. Python的模块,模块的使用、安装,别名,模块作用域

    模块和包 所谓的模块就是将不同功能的函数分别放到不同的文件中,这样不仅有利于函数的维护,也方便了函数的调用.在Python中,一个.py文件就是一个模块(Module). 在模块的上层有一个叫做包(P ...

  6. 学习C/C++的简单方法

    如何学习C++呢.C和C++是很多专业的必修课,尤其对计算机专业来说,更是重中之重.C++语言是早期发展的高级语言,具备执行速度快,语法优美等特点.是底层高效率系统的首选开发语言.今天就和大家分享一下 ...

  7. C语言中怎样定义能够保存16进制整数的变量

    可以通过int 或long int存储,16进制整数说到底还是整数,16进制只是一种记数方式.例如,int x=0x16;十六进制(hexadecimal)只是计算机中数据的一种表示方法,规则是“逢十 ...

  8. postgresql数据的入门教程

    postgreSQL数据库简介 PostgreSQL 是一个免费的对象-关系数据库服务器(ORDBMS),在灵活的BSD许可证下发行. PostgreSQL 开发者把它念作 post-gress-Q- ...

  9. mysql 导入导出表结构和表数据

    mysqldump -u用户名 -p密码 -d 数据库名 表名 > 脚本名; 导出整个数据库结构和数据mysqldump -h localhost -uroot -p123456 databas ...

  10. excel被保护或者锁定时候按住alt和enter可以输入换行

    excel被保护或者锁定时候按住alt和enter可以输入换行