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. linux stat 简单介绍

    stat 命令查看文件或文件系统的状态时间等属性 用法:stat [参数]... 文件... 简单的介绍一下stat命令显示出来的文件其他信息: - File:显示文件名 - Size:显示文件大小 ...

  2. jQuery插件实例七:一棵Tree的生成史

    在需要表示级联.层级的关系中,Tree作为最直观的表达方式常出现在组织架构.权限选择等层级关系中.典型的表现形试类似于: 一颗树的生成常常包括三个部分:1)数据库设计:2)后台程序:3)前端代码.那么 ...

  3. AfxBeginThread

    用户界面线程和工作者线程都是由AfxBeginThread创建的.现在,考察该函数:MFC提供了两个重载版的AfxBeginThread,一个用于用户界面线程,另一个用于工作者线程,分别有如下的原型和 ...

  4. leaflet学习一 入门

    1从官网https://leafletjs.com/下载的Leaflet包含文件: leaflet.js - 简化版的 Leaflet JavaScript代码 leaflet-src.js - 这是 ...

  5. 2017 SDN第一次作业

    (1)我会选择的,因为网络现在越来越重要,各行各业都离不开网络,这个方向可以适合各种岗位,感觉比较容易就业.但选这个课是为了多学一点东西,没想太多,嘎嘎嘎. (2)SDNLAB,是一个SDN的大的中文 ...

  6. [Python] 同时安装了python2和python3时,pip命令该如何使用?

    当python2和python3同时安装windows上时,它们对应的pip都叫pip.exe,所以不能够直接使用 pip install 命令来安装软件包. 而是要使用启动器py.exe来指定pip ...

  7. 9.算法之顺序、二分、hash查找

    一.查找/搜索 - 我们现在把注意力转向计算中经常出现的一些问题,即搜索或查找的问题.搜索是在元素集合中查找特定元素的算法过程.搜索通常对于元素是否存在返回 True 或 False.有时它可能返回元 ...

  8. 关于requests.exceptions.SSLError: HTTPSConnectionPool

    问题: requests.exceptions.SSLError: HTTPSConnectionPool(host='mall.christine.com.cn', port=443): Max r ...

  9. PHPer是草根吗

    以下文字并没有非常多的技术词汇,所以只要对PHP感兴趣的人都可以看看. PHPer是草根吗? 从PHP诞生之日起,PHP就开始在Web应用方面为广大的程序员服务.同时,作为针对Web开发量身定制的脚本 ...

  10. Kafka学习之路 (五)Kafka在zookeeper中的存储

    一.Kafka在zookeeper中存储结构图 二.分析 2.1 topic注册信息 /brokers/topics/[topic] : 存储某个topic的partitions所有分配信息 [zk: ...