题目来源:https://leetcode.com/problems/reverse-integer/

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

解题思路:

其实这道题看起来非常简单,要实现也是几行代码的事。但是有个小问题容易被忽略,就是边界问题。什么意思呢?如果我们输入的整数超出了int的表达范围,这个问题要怎么解决呢?
用比int更大的数据类型存储我们转换后的结果,然后与int的边界比较,超出了边界则返回0。
Java实现:
 public class Solution {
public int reverse(int x) {
long reverse = 0; while(x != 0){
reverse = reverse * 10 + x % 10;
if(reverse > Integer.MAX_VALUE || reverse < Integer.MIN_VALUE)
return 0;
x = x / 10;
}
return (int)reverse;
}
}

LeetCode 7 Reverse Integer(反转数字)的更多相关文章

  1. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

  2. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

  3. [leetcode]7. Reverse Integer反转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  4. leetcode:Reverse Integer 及Palindrome Number

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

  5. Reverse Integer - 反转一个int,溢出时返回0

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

  6. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  7. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  8. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  9. [Leetcode] reverse integer 反转整数

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

随机推荐

  1. HMM 自学教程(六)维特比算法

    本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...

  2. [mysql]brew 安装 配置 操作 mysql(中文问题)

    mac 下卸载mysqldmg mac下mysql的DMG格式安装内有安装文件,却没有卸载文件--很郁闷的事. 网上搜了一下,发现给的方法原来得手动去删. 很多文章记述要删的文件不完整,后来在stac ...

  3. 前端比较好的学习资料(包括js和css)以及 最全前端资源汇集

    js详细资料: http://javascript.ruanyifeng.com/ 『引』最全前端资源汇集: 来源:http://www.jeffjade.com/2016/03/30/104-fro ...

  4. JS魔法堂:追忆那些原始的选择器

    一.前言                                                                                                 ...

  5. sprint3冲刺团队贡献分-软件工程

    蔡舜 : 20 卢晓洵 : 19 林宇粲 :22 王昕明 :21

  6. 安装 Oracle P6 EPPM 16 R1 database for 12C A

  7. 面向对象的Javascript(4):重载

    在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...

  8. C语言字符串匹配函数

    C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...

  9. HTML常用符号

    HTML转义符号 HTML常用符号: 显示一个空格    < 小于 < <> 大于 > >& &符号 & &" 双引号 & ...

  10. 分享一些WinForm数据库连接界面UI

    1.动软代码生成器源码中. 2.DevExpress控件包中有类似的界面 3.代码生成器:http://www.csharpwin.com/csharpspace/11666r2577.shtml 4 ...