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】的更多相关文章

  1. 290. Word Pattern【LeetCode by java】

    今天发现LintCode页面刷新不出来了,所以就转战LeetCode.还是像以前一样,做题顺序:难度从低到高,每天至少一题. Given a pattern and a string str, fin ...

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

  3. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  4. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  5. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  6. 【LeetCode算法-9】Palindrome Number

    LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...

  7. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  8. 【LeetCode算法-7】Reverse Integer

    LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...

  9. leetcode:Reverse Integer【Python版】

    1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...

随机推荐

  1. Centos kernel panic-not syncing:VFS:Unable to mount root fs on unknown block 解决办法

    昨晚更新了一下内核,今晚开机就无法进系统了...提示如下图: 解决方案:开机启动时按Esc,然后选择下面的旧版本的内核启动即可. (成功进入系统后,你可以选择改变开机默认选择的内核). uname - ...

  2. VS2017C++单元测试

    0.欢迎食用 希望对点进来的你有所帮助. 全文记流水账,内心想法如示例项目名称. 1.建立需测试的项目 新建项目 正常书写.h 和.cpp文件 2.新建单元测试 右击解决方案 -> 添加 -&g ...

  3. Java BAT大型公司面试必考技能视频-1.HashMap源码分析与实现

    视频通过以下四个方面介绍了HASHMAP的内容 一. 什么是HashMap Hash散列将一个任意的长度通过某种算法(Hash函数算法)转换成一个固定的值. MAP:地图 x,y 存储 总结:通过HA ...

  4. 让sublime text3支持Vue语法高亮显示[转]

    1.准备语法高亮插件vue-syntax-highlight. 下载地址:https://github.com/vuejs/vue-syntax-highlight 下载页面并下载: 解开压缩包vue ...

  5. shp转oracle spatial

    2010年12月1日  终于搞定了shp到oracle spatial,说下步骤和感受吧! 1 XP系统:转换工具的下载(shp2sdo.exe ):下载后把此文件复制到PATH变量包含的目录下(E: ...

  6. TDD&BDD

    BDD行为驱动开发的一种敏捷开发技术 TDD测试驱动开发

  7. APR Recipe

    1,  pt fix 后生成的 icctcl,回到 icc source 后,如果需要eco_route,建议先关掉 timing_driven,因为 icc 与 pt 看到的 timing 情况一般 ...

  8. MSMQ消息队列总结

    1.总体介绍: http://www.cnblogs.com/beniao/archive/2008/06/26/1229934.html 2.windows服务各项参数介绍及安装 https://w ...

  9. 关于YARN Node Labels的一点理解

    最近在做实验,实验需要进行分区域计算,网上查了资料后发现Yarn Node Labels + Capacity-Scheduler可以实现我的需求 但是当任务提交到capacity-scheduler ...

  10. 备份时如何排除掉默认的 information_schema 和 mysql 库?

    备份时如何排除掉默认的 information_schema 和 mysql 库? mysql -e "show databases;" -uroot -ppassword | g ...