经历了三道树的题后,完全崩溃中,急需一道非树图的题来挽救信心。

题目:反转数字。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的更多相关文章

  1. LeetCode题目_Reverse Integer

    最近在LeetCode上做题,写点东西记录一下,虽然自己做的都是些很水的题目,但是重在练手. 题号7:Reverse Integer,题目描述: Reverse digits of an intege ...

  2. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  3. LeetCode 397. Integer Replacement

    397. Integer Replacement QuestionEditorial Solution My Submissions   Total Accepted: 5878 Total Subm ...

  4. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

  5. 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 ...

  6. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  7. C++ leetcode::Reverse Integer

    第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...

  8. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  9. leetcode 343. Integer Break(dp或数学推导)

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

随机推荐

  1. role在标签中的作用是什么?

    html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组件的可访问性.可用性和可交互性 ...

  2. inno setup 执行SQL

    参考之:1.可将导入数据的功能写入一个小程序,再外部调用(楼上已经说了):2.可用程序代码:[Setup] AppName=科發醫院管理系統 AppVerName=科發醫院管理系統4.0 AppPub ...

  3. Android 上的代码阅读器 CoderBrowserHD 修改支持 go 语言代码

    我在Android上的代码阅读器用的是 https://github.com/zerob13/CoderBrowserHD 改造的版本,改造后的版本我放在 https://github.com/ghj ...

  4. [SQL]断开并更改数据库名

    EXEC sp_dboption 'my', 'Single User', 'TRUE' EXEC sp_renamedb 'my', 'mycrjtest' EXEC sp_dboption 'my ...

  5. poj 1125 Stockbroker Grapevine dijkstra算法实现最短路径

    点击打开链接 Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23760   Ac ...

  6. android点击状态分析

    android:addStatesFromChildren="true" :父类从子类中获取点击状态. android:duplicateParentState="tru ...

  7. Oracle Profile 使用详解--zhuanzai

    一.目的: Oracle系统中的profile可以用来对用户所能使用的数据库资源进行限制,使用Create Profile命令创建一个Profile,用它来实现对数据库资源的限制使用,如果把该prof ...

  8. Spring定时任务配置

    可参照http://gong1208.iteye.com/blog/1773177 的详细配置 <!-- 系统定时任务工厂类 --> <bean id="systemDes ...

  9. Flex4 双选下拉列表的实现(源代码)

    本文属原创文章,如需转载,请注明出处,谢谢 企业应用中少不了双选下拉列表控件,但几乎都没有独立的控件,Flex在这上面得天独厚,ArrayCollection的过滤功能使得我们只需要一个数据源就可以将 ...

  10. 在EXCEL中使用SQL查询

    Excel2007及以上版本才有这个功能,2003版本的要么路过学习一下.要么去升级下自己的版本. Microsoft query 用的是 access 语法 如:判断空 oracle ------- ...