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 ...
随机推荐
- 实现CTreeCtrl父子节点的联动选择
本文实现了下面的功能: 当选中父节点时, 其所有子节点全部选中. 当取消选中父节点时, 其所以子节点全部取消选中. 点击子节点时, 根据子节点与其兄弟节点的选中状态, 自动设置父节点的选中状态. 通过 ...
- LeetCode 休闲计划
老年退役选手的 LeetCode 休闲之旅 前言 不知不觉两年多的大学时光悄然流逝,浑浑噩噩的状态似乎从来没有离开过自己. 这两年刷题似乎一直是常态.在退役之后的现在,深感有些东西一段时间没有接触,很 ...
- 【adb命令】在cmd窗口中使用adb install命令安装 中文名字apk报错的解决办法
1.在cmd窗口中使用adb install命令安装中文名字apk报错,安装英文名字apk就正常,详细报错如下图: 2.查看adb版本号:adb version 3.怀疑是adb版本的原因,尝试换个最 ...
- Oracle 11g AWR 系列五:如何生成 AWR 报告?
1.生成单实例 AWR 报告: @$ORACLE_HOME/rdbms/admin/awrrpt.sql 2.生成 Oracle RAC AWR 报告: @$ORACLE_HOME/rdbms/adm ...
- CSS样式定义的优先级顺序总结
CSS样式定义的优先级顺序总结 层叠优先级是: 浏览器缺省 < 外部样式表 < 内部样式表 < 内联样式 其中样式表又有: 类选择器 < 类派生选择器 < ID选择器 & ...
- BZOJ 1934 善意的投票
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1934 题目大意: 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问 ...
- WorldWind源码剖析系列:二维点类Point2d和三维点类Point3d
PluginSDK中的点主要有二维和三维两种类型,分别用来描述平面和立体点.其类图如下. 这两个类比较简单.其字段成员主要用来描述点对象在各坐标轴上的分量. 属性Length用来返回二维和三维点的距离 ...
- Python2.7-matplotlib
matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表 一般用以下形式导入:import matplotlib.pyplot as plt 一般用法:1. ...
- JAVA框架 Spring AOP--切入点表达式和通知类型
一:AOP的相关术语: 1)Joinpoint(连接点):所谓的连接点是指那些可以被拦截点,在spring中这些点是指方法.因为在spring中支持方法类型的连接点. 2)Pointcut(切入点): ...
- python下载安装搭建
python官网下载python运行环境(https://www.python.org/downloads/),建议下载稳定版本,不推荐使用最新版本 安装 然后我们打开CMD,在里面输入python, ...