Q7:Reverse Integer
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
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的更多相关文章
- LeetCode第[7]题(Java):Reverse Integer 标签:数学
题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...
- No.007:Reverse Integer
问题: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 官方难度: Ea ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 【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 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
- Reverse Integer 2015年6月23日
题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...
随机推荐
- 2019 OI日记
// 我觉得记日记是个好习惯吧 毕竟指不定哪天就学不下去了 就AFO了 就没有梦了 // [置顶]活跃于你谷普及训练场.ybt(没底气说全部).loj(提高基础部分) //优先级从前往后 因为 ...
- Solr查询和过滤器执行顺序剖析
一.简介 Solr的搜索主要由两个操作组成:找到与请求参数相匹配的文档:对这些文档进行排序,返回最相关的匹配文档.默认情况下,文档根据相关度进行排序.这意味着,找到匹配的文档集之后,需要另一个操作来计 ...
- JAVA实现数组的反转--基础
直接上代码 这个算法比较简单,唯一需要注意的就是第8行和第9行.一定要多减去1 因为for循环从0开始,而数组长度是从0到length-1的. class ArrReverse { //实现数组元素的 ...
- 关于js中异步问题的解决方案
在js中有一个始终无法绕过的问题,如何优雅地解决异步问题.实际上,js在执行过程中,每遇到一个异步函数,都会将这个异步函数放入一个异步队列中,只有当同步线程执行结束之后,才会开始执行异步队列中的函数, ...
- 八、Vue-lazyload
一.Vue懒加载 文档:https://github.com/hilongjw/vue-lazyload 1.安装 cnpm i vue-lazyload -S 或 npm i vue-lazyloa ...
- 从0开始自己配置一个vps虚拟服务器(2)
配置php环境 1.安装php安装所依赖的包 yum -y install gcc gcc-c++ libxml2 libxml2-devel 2.cd usr/local/src 进入目录,在这个目 ...
- edge浏览器两个标签页localStorage不同步,解决办法
今天遇到个奇怪的问题,edge两个标签页之间的localStorage值不同步,网上说ie和edge如果想让localStorage值同步,需要主动出发localStorage的change事件 wi ...
- IO流常用模式
主要运用2个设计模式,适配器和装饰者模式.
- 如何禁用AD OU 下面的邮箱用户的Exchange ActiveSync 和 适用于设备的OWA
Get-Mailbox -OrganizationalUnit QQ禁用名单 | Set-CASMailbox -ActiveSyncEnabled $false -OWAforDevicesEna ...
- bzoj 4008、4011、1499
全是扒题解,,,太弱了... 不乱BB了. 4008 #include <bits/stdc++.h> #define LL long long #define lowbit(x) x&a ...