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. Day6-T2

    原题目 给你一个长度为n的序列A,请求出最大的一对数(Ai ,Aj),使Ai&Aj最大. 第一行为n,接下来n行,每一个数表示Ai. 输出最大的“and”. S1: Input: Output ...

  2. 使用WinDbg分析蓝屏dump原因

    大多数人或许都经历过系统蓝屏问题,然而大多数人不清楚该怎么处理蓝屏问题,这里主要对系统蓝屏做一些解释,同时介绍下蓝屏问题分析工具WinDbg分析蓝屏问题的一般步骤. 微软官方对蓝屏的定义是,当系统遇到 ...

  3. JSON数组序列化C#方法

    /// <summary> /// dataTable转换成Json格式 JSON对应关系 三层数组 /// </summary> /// <param name=&qu ...

  4. poj 2456 Aggressive cows 贪心+二分

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25944   Accepted: 11982 ...

  5. Window Server 2019 配置篇(1)- 创建域并把本机设置成域控制器

    由于这个学期的Window Server大作业是做一个服务器群,在域中创建包括DNS,DHCP,网关,更新服务器,hyper-v,自动部署等服务,所以我会把制作过程分步写在这个博客上 首先我们新建一个 ...

  6. git仓库拆分

    例如: # 这就是那个大仓库 big-project $ git clone git@github.com:tom/big-project.git $ cd big-project # 把所有 `co ...

  7. ajax异步请求数据

    源码1: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  8. Biu一Biu--GDB

    gcc常见编译选项 ** -c **:只激活预处理.编译和汇编,也就是生成obj文件 ** -S **:只激活处理和编译,把文件编译成汇编代码 ** -o **:定制目标名称,缺省的时候编译出来的可执 ...

  9. 九十三、SAP中ALV事件之七,对自己定义的工具栏进行添加和删改

    一.我们来到工具栏页面,如果不想要某个工具栏,删掉相应的文字再双击空白就可以了 二.我们添加一个工具栏,如ZADD,双击文字 三.保存静态文本,会弹出一个功能文本框 四.填写相应的内容后,点击对勾保存 ...

  10. rsync错误

    rsync error:No route to host rsync服务端开启的iptables防火墙 [root@nfs01 tmp]# rsync -avz /etc/hosts rsync_ba ...