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. October 16th 2017 Week 42nd Monday

    The more decisions that you are forced to make alone, the more you are aware of your freedom to choo ...

  2. skimage 安装和子模块

    平台:Windows 10 1.安装anaconda anaconda是python环境的集成安装软件,建议安装2.7版本的python 2.安装skimage 打开windows的命令提示符:  输 ...

  3. 1.js基础(以通俗易懂的语言解释JavaScript)

    1.JavaScript组成: ECMAScript: 解释器.翻译 -->几乎没有兼容问题 DOM: Document Object Model -->有一些操作不兼容 BOM: Bro ...

  4. jQ判断一个元素是否为空

    // 方法一 if (!$('#jb51').html()) { //http://www.jb51.net 什么都没有找到; } // 方法二 if ($('#jb51').is(":em ...

  5. intellij IDEA软件java项目No SDK配置jdk开发,安装IDEA软件步骤

    我们在使用intellij idea开发java项目的时候,我们在创建的时候会发现提示No SDK,影响创建和使用项目,我们需要下载和配置需要的JDK 电脑 1我们使用intellij idea创建j ...

  6. Jmeter函数助手中添加自定义函数

    最近,群里的牛肉面大神有个需求,是将每个post请求的body部分做一个加密操作,其实这个需求不算难,用beanshell引入加密函数的包,然后调用就行了.只是,如果请求多了,每次都要调用一下自己加密 ...

  7. javascript:void(0)的问题

    最近看了好几个关于<a>标签和javascript:void(0)的帖子,谨记于此,以资查阅. 注:以下代码未经全面测试,但每一种方法可能会出现的情况都基本做了说明. 在做页面时,如果想做 ...

  8. c++ 智能指针、函数指针和指针函数

    智能指针: 1.内存泄漏memory leak :是指程序在申请内存后,无法释放已申请的内存空间,一次内存泄漏似乎不会有大的影响,但内存泄漏堆积后的后果就是内存溢出. 2.内存溢出 out of me ...

  9. SpringMVC之ajax+select下拉框交互常用方式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. 或许因为缺少默认route配置而导致的的ping超慢,甚至timeout

    内网有台dell r620 做测试用: 4个网口都插了 网线,通过小交换直接连在中心路由的第3个口上: 安装了kvm,有virbr0   :但还没装其它的虚拟机. ping 外网域名 ,IP地址 都超 ...