[leetcode]_Reverse Integer
经历了三道树的题后,完全崩溃中,急需一道非树图的题来挽救信心。
题目:反转数字。input : 123 , output : 321.
思路:直接,没什么好说的。
自己代码:很龊,有大量的冗余信息,还申请了一个List,虽然AC了,但有很大改进空间。
public int reverse(int x) {
boolean negative = false;
if(x < 0) {
negative = true;
x = x * (-1);
}
List<Integer> result = new ArrayList<Integer>();
while( x / 10 > 0 ){
result.add(x % 10);
x = x / 10;
}
result.add(x);
int num = 0;
int times = 1;
for(int i = result.size() - 1 ; i >= 0 ; i--){
num += result.get(i) * times;
times *= 10;
}
if(negative) return (-1)*num;
else return num;
}
在网络上看了别人的代码,下面是精简版:(非常清晰,没有什么冗余开销,只用了两个变量)
public int reverse(int x) {
boolean negative = false;
if(x < 0) {
negative = true;
x = x * (-1);
}
int temp = 0 ;
while(x > 0){
temp = temp * 10 + x % 10;
x = x / 10;
}
if(!negative) return temp;
else return (-1)*temp;
}
后面有个知识:负数%正数 等于负数。因此不用对负数单独进行符号判断,可以直接算。
最优版:
public int reverse(int x) {
int temp = 0 ;
while(x != 0){
temp = temp * 10 + x % 10;
x = x / 10;
}
return temp;
}
和第二个版本不同的地方在于while循环的条件由 x > 0 变为了 x != 0,这样负数也能适应该while。只使用了一个变量。五行代码。漂亮。
[leetcode]_Reverse Integer的更多相关文章
- LeetCode题目_Reverse Integer
最近在LeetCode上做题,写点东西记录一下,虽然自己做的都是些很水的题目,但是重在练手. 题号7:Reverse Integer,题目描述: Reverse digits of an intege ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- LeetCode 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- leetcode@ [343] Integer Break (Math & Dynamic Programming)
https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- C++ leetcode::Reverse Integer
第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- leetcode 343. Integer Break(dp或数学推导)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
随机推荐
- [ActionScript 3.0] AS3虚线绘制方法
import flash.geom.Point; import flash.display.MovieClip; import flash.display.Graphics; var mc:Movie ...
- BeanDefinitionStoreException: Failed to read candidate component class: URL
如题,遇到这种情况一般都是引用jar包版本不一致或者编译后的class除了问题 解决办法: a.如果是maven项目,把项目全部clean一下,重新mvn install b.如果不是maven项目, ...
- VML/SVG在Web开发中一些常见的框架
1.借鉴自: http://www.codefans.net/soft/3061.shtml 来源于网上. flowchart.js http://adrai.github.io/flowchart ...
- 初探appium之appium的使用
上一篇中已经讲了python+appium的环境搭建.这里简单的讲一下appium的使用. 我也是第一次使用appium,看了教程问了人.知道appium可以通过模拟也可以连接上手机使用.本篇中,先使 ...
- c# 调用 CRFs应用程序
今天想用C#调用crfs,但是老出问题.原因有几点.第一,我对crf不理解,虽然我用cmd跑了一遍,但是根本不理解为什么,而且只是草草看了下参数该输入什么,只是了解了形式,没有了解实质.所以在调用的时 ...
- 怎样调整CODESOFT中条码线的宽度
CODESOFT是一款功能强大.灵活便捷的标签条码设计打印软件.在使用CODESOFT设计并打印标签,有时会因为打印精度或扫面清晰度等原因,需要调整条形码中行的宽度,即调整条码线宽度.本文,小编 ...
- 慧自文档:代替 Everything 来快速查找文件的,实现文件显示在文件夹的层次结构中
1. 搜索功能和Everything一样快和强大 具有 Everything 搜索快.搜索功能强等优点, 解决了不能方便选择搜索哪个文件夹, 解决了不能同一个画面进行预览等问题 2.文件直接显示到文件 ...
- UDKtoUE4Tool-UDKUE3资源移植UE4工具
UDKtoUE4Tool UDKtoUE4Tool 是一个把UE3/UDK资源包(T3D格式)转换成UE4(T3D格式)的工具.作者Matt3D使用C#实现,未来考虑发布到Unreal Marketp ...
- 【LeetCode】16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序的处理方式
今天客户向我反映一个问题,当他们在用我们的系统导出excel表格时,报错:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 经过找资料终于得到解决方法,记录一下. 在对应 ...