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 ...
随机推荐
- Java笔记--基础
1.Java中内存的基本结构: 栈(stack):存放局部变量.对象的引用: 堆(heap):new出来的东西(对象) 方法区:常量池等 静态域:全局变量等 变量在其生命周期结束后将出栈,此时堆中的空 ...
- Scala 线性化规则和 super 操作
如果一个类有多个父类,且父类的有相同的函数 f,在子类和父类中调用 super.f 都是按从右到左的调用函数的顺序. 这个规则名为:Linearization Rules 如下的代码 trait Ba ...
- 京东首页如何实现pc端和移动端加载不同的html的?
进入www.jd.com后代码判断是手机的话就跳转m.jd.com let ua = window.navigator.userAgent.toLocaleLowerCase() let murl = ...
- 20190108PLC学习心得
应该是数据类型不对 F1查看了帮助文件以后 ,看到 LD应该是用指针类型的数据 改正以后 LD0下的红线消失了 . 绿色 代表没有给 符号 定义 地址 假设 我现在给 符号 字节数 ...
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- Java 逆序打印链表
递归 package cookie; public class PrintListReversal { public void reversalOut(Node head) { if (head != ...
- junit基础学习之-参数初始化(5)
package swust.edu.cn.postdoctors.service.impl; import java.util.Arrays; import java.util.Collection; ...
- 第十篇 Form表单
Form表单 阅读目录(Content) Form介绍 普通的登录 使用form组件 Form那些事儿 常用字段演示 校验 使用Django Form流程 补充进阶 应用Bootstrap样式 批量添 ...
- vue学习(七)refs的使用
ref的使用只有在特殊的情况下使用 1.如果给标签添加ref,获取的就是真实的DOM节点2. 如果给子组件添加ref,获取的就是当前的子组件对象 例子: <div id="app&qu ...
- yagmail四行代码发送邮件
yagmail四行代码发送邮件 import yagmail # 链接邮箱服务器 yag = yagmail.SMTP(user="xxxx@163.com", password= ...