7. Reverse Integer【Leetcode by java】
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
解题:正数倒置的问题。我首先想到的是转化成字符串进行倒置,因为转化成字符串后,就不用考虑数字在个位,还是百位,还是其他什么位的。当然,负号肯定是要特殊对待的。返回值是int,还得再转回去。最重要的是,溢出问题要处理一下,Note中有提示。代码如下:
class Solution {
public int reverse(int x) {
String str = String.valueOf(x);
if(str == null)
return 0;
if(str.charAt(0) == '-')
str = '-' + reverse_str(str.substring(1));
else str = reverse_str(str);
int res = 0;
try{
res = Integer.parseInt(str);
return res;
}catch(NumberFormatException e){
return 0;
}
}
public static String reverse_str(String str){
if(str == null || str.length() < 2)
return str;
return reverse_str(str.substring(1)) + str.charAt(0);
}
}
评论区找到一个很精简也很高效的算法,尤其是判断是否溢出的方法更加巧妙。代码如下:
public int reverse(int x)
{
int result = 0; while (x != 0)
{
int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult - tail) / 10 != result)
{ return 0; }
result = newResult;
x = x / 10;
} return result;
}
第8行处用一个newResult来暂存答案,第9行的时候,反解出result,看和原来的等不等,如果精度没有损失(依然相等),则说明没有溢出,反之有溢出。学习了!
7. Reverse Integer【Leetcode by java】的更多相关文章
- 290. Word Pattern【LeetCode by java】
今天发现LintCode页面刷新不出来了,所以就转战LeetCode.还是像以前一样,做题顺序:难度从低到高,每天至少一题. Given a pattern and a string str, fin ...
- 501. Find Mode in Binary Search Tree【LeetCode by java】
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-9】Palindrome Number
LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- 【LeetCode算法-7】Reverse Integer
LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
随机推荐
- [Spark Core] Spark 核心组件
0. 说明 [Spark 核心组件示意图] 1. RDD resilient distributed dataset , 弹性数据集 轻量级的数据集合,逻辑上的集合.等价于 list 没有携带数据. ...
- MySQL索引原理以及类型
1.什么是索引 索引是在MySQL的存储引擎上,对其表中的某个列或多列通过一些算法实现可快速查询出结果的一种方法. 2.为什么要有索引 就像一本书要有目录一样,我们可快速通过目录来查找对应的章节得出结 ...
- python + pyqt5 QlineEdit QMessageBox实现信息录入和消息弹框提醒
本人现在在做自动化工具开发的工作,因此,记录下自己平时遇到的问题和解决之道,还有一些简单的小工具 以下为主代码 # --*-- coding:utf-8 --*-- from first import ...
- vue问题总结
1.通过判断绑定class及点击事件总结<a :class='[item.status=="yes" ? `btn-primary` : `btn-danger`]' :ti ...
- python第四十一课——析构函数
3.析构函数 格式:__del__(self): 作用: 在程序结束前将对象回收,释放资源的行为 演示析构函数的使用: class Animal: #定义构造函数 def __init__(self, ...
- RMAN 备份数据库到DISK后进行数据恢复
RMAN 备份数据库到DISK,然后进行数据恢复 一.rman备份 1. 全备脚本 vi bakup_level0.sql connect target / run { allocate channe ...
- Android开发之自定义局部导航菜单
如今,要实现导航功能方案有很多.比如: 1.用3.0+自带的Toolbar + Fragment导航. 2.用Tabhost实现导航.小弟学浅,就只用过这两种方案实现导航. 但是这两种方案都有一个很明 ...
- tomcat:8080/返回404;/etc/hosts(identifier-Namespace-scope)
我以为 就oracle 的 oracle db ,weblogic喜欢和 hostname 死磕: 没想到开源的tomcat也是如出一辙,名不正则言不顺,为什么,“名”的力量这么大呢?命名空间. 有个 ...
- Python3 常见数据类型的转换
Python3 常见数据类型的转换 一.数据类型的转换,你只需要将数据类型作为函数名即可 OCP培训说明连接:https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16H ...
- css样式匹配苹果个型号手机
/*适配苹果X*/ @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-devic ...