7. Reverse Integer

官方的链接:7. Reverse Integer

Description :

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

Example1:


Input: 123

Output: 321


Example2:


Input: -123

Output: -321


Example3:


Input: 120

Output: 21


Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

问题描述

反转32位的数字

思路

上一次的模*10 + 这一次的模。中间判断是否有溢出

注意:last_mod * 10 + this_mod,而x /= 10

[github-here]

 public class Q7_ReverseInteger {
public int reverse(int x) {
int revResult = 0;
while (0 != x){
int newResult = revResult * 10 + x % 10;
//judge whether it overflows
if (newResult / 10 != revResult) {
return 0;
}
revResult = newResult;
x /= 10;
}
return revResult;
}
}

Q7:Reverse Integer的更多相关文章

  1. LeetCode第[7]题(Java):Reverse Integer 标签:数学

    题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...

  2. No.007:Reverse Integer

    问题: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 官方难度: Ea ...

  3. leetcode:Reverse Integer(一个整数反序输出)

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

  4. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  5. leetcode:Reverse Integer 及Palindrome Number

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

  6. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  7. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. lintcode :reverse integer 颠倒整数

    题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...

  9. Reverse Integer 2015年6月23日

    题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...

随机推荐

  1. 怎样设置使IntelliJ IDEA智能提示忽略大小写?

    打开设置(CTRL+ALT+S)打开editor,找到“Code Completion”->点击Match case前面的框不勾选即可.如下图:

  2. Redis详解(五)——主从复制

    Redis详解(五)--主从复制 面临问题 机器故障.我们部署到一台 Redis 服务器,当发生机器故障时,需要迁移到另外一台服务器并且要保证数据是同步的.而数据是最重要的,如果你不在乎,基本上也就不 ...

  3. python_os 的知识点

    1. os.getcwd() #获得当前路径 2. os.listdir(path) #列出path路径下的所有目录名和文件名包括后缀 3. os.mkdir(path) #在path创建一个目录 4 ...

  4. 【linux】linux系统安全设置

    1.下载安装安全软件 2.取消Telnet登录采用SSH方式并更改ssh服务端远程登录的配置 1)Telnet登录协议是明文不加密不安全,所以采用更安全的SSH协议. 2)更改ssh服务端远程登录相关 ...

  5. 039-PHP使用闭包函数来进行父实例的变量自增,错误示例

    <?php // 如何使用闭包函数来进行父实例的变量自增,错误示例 function demo(){ $num = 1; $func = function() use($num){ echo $ ...

  6. Oracle SQL语句记录

    1.oracel 查看表空间使用情况. ) AS free_space, tablespace_name FROM dba_free_space GROUP BY tablespace_name; ) ...

  7. JAVA基本数据类型和注释

    一.注释 1.注释的概念 注释是程序中给人看的提示信息,会被编译器忽略:在程序编译和执行过程中不会有任何影响,仅仅在代码阅读时提供提示信息. 2.注释的形式 基本语法://注释的内容   a.行注释 ...

  8. CF1209A Paint the Numbers

    You are given a sequence of integers a1,a2,…,an. You need to paint elements in colors, so that: If w ...

  9. 第十八篇 admin组件

    admin组件 admin组件使用 admin源码解析 admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以 ...

  10. 【转载】WebDriver拾级而上·之零 WebDriver理论

    Selenium2.0 = Selenium1.0 + WebDriver(也就是说Selenium2.0合并了这两个项目)   Selenium1.0可以使用任何编程语言,但是有个先决条件就是必须支 ...