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 ...
随机推荐
- Windows窗体数据抓取详解
最近在客户项目上刚好遇到一个问题,项目需求是要获取某台机床的实时状态,问题点刚好就在于该机床不是传统意义上的数控机床,也不是PLC控制器,只有一个上传下载程序文件的应用程序,上面刚好有几个按钮可以大概 ...
- openlayer3 基础学习一创建&显示地图
<!doctype html> <html lang="en"> <head> <link rel="stylesheet&qu ...
- python闭包,看不懂请揍我
什么是闭包? 闭包就是一个个内嵌函数+内嵌函数里面引用了外部变量+返回这个内嵌函数(一般是这样) 为什么使用闭包? 有点类似与函数模板?.. 举一个实际的例子: class people: name ...
- 如何将XML文件导入Excel中
如下图所示为一个规范的XML文件,在Excel中可以将规范的XML文件导入到Excel成为规范的表格.具体有如下几种方法: 工具/原料 Excel 方法/步骤 单击“数据”选项卡下的“ ...
- Android开发之jdk安装及环境变量配置
然后开始配置环境变量,JAVA_HOME,Path和classpath三部分: (1)在变量名输入框中写入“JAVA_HOME”,在变量值输入框中写入“C:\Program Files\Java\jd ...
- Kafka学习之路 (二)Kafka的架构
一.Kafka的架构 如上图所示,一个典型的Kafka集群中包含若干Producer(可以是web前端产生的Page View,或者是服务器日志,系统CPU.Memory等),若干broker(Kaf ...
- mysql刚启动就停止是什么原因
1.找到mysql安装目录,将其配置文件my.default.ini改名为my.ini,并且将my.ini移至bin目录下. 2.启动命令行,将目录切换到mysql安装目录的bin目录下.3.接下来, ...
- Stay true to yourself
https://zhuanlan.zhihu.com/p/22928614 艾伦·德詹尼斯,1958年1月26日出生于美国路易斯安纳州梅泰里,美国主持人.演员.凭借出众的诙谐幽默的口才和喜剧天赋,活跃 ...
- 竟然是它:# vi /etc/resolv.conf
有数据库主机: 前端tomcat应用连接慢,开发人员本地pl/sql连接也超慢: 检查了系统i/o ,nethogs,top,检查了数据listener.ora,awr,wait_event,sql ...
- 使用Tensoflow实现梯度下降算法的一次线性拟合
# Author Qian Chenglong import tensorflow as tf import numpy as np #生成100个随机数据点 x_date=np.random.ran ...