【LeetCode算法-7】Reverse Integer
LeetCode第7题:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
解题思路:
假如:x=1234,y=0
1)y乘以10,x把末位4移除掉,加给y(x=123,y=4)
2)y乘以10,x把末位3移除掉,加给y(x=12,y=43)
3)y乘以10,x把末位2移除掉,加给y(x=1,y=432)
4)y乘以10,x把末位1移除掉,加给y(x=0,y=4321)
代码:
int y = 0;
while(x/10!=0){
y*=10;
y+=x%10;
x/=10;
}
y=y*10 +x;
结果报错
原因是int的范围是-2147483647~2147483648
leetcode给的input是1534236469,倒转之后是9646324351,导致溢出了。所以要加上溢出判断
最后的代码
class Solution {
public int reverse(int x) { int negative = 1;
if(x <= Integer.MIN_VALUE)
return 0;
if(x < 0){
x = -x;
negative = -1;
} long y = 0;
while(x/10!=0){
y*=10;
y+=x%10;
x/=10;
}
y=y*10 +x; if(y > Integer.MAX_VALUE)
return 0;
else
return (int)y * negative;
}
}
这也是很坑啊,溢出就输出0,鬼知道啊
【LeetCode算法-7】Reverse Integer的更多相关文章
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- LeetCode算法题-Reverse Bits(Java实现)
这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:4326 ...
- LeetCode算法题-Reverse Words in a String III(Java实现)
这是悦乐书的第259次更新,第272篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第126题(顺位题号是557).给定一个字符串,您需要反转句子中每个单词中的字符顺序,同 ...
- LeetCode算法题-Reverse String II(Java实现)
这是悦乐书的第256次更新,第269篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第123题(顺位题号是541).给定一个字符串和一个整数k,你需要反转从字符串开头算起的 ...
- LeetCode算法题-Reverse String(Java实现)
这是悦乐书的第205次更新,第217篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第73题(顺位题号是344).编写一个以字符串作为输入并返回字符串的函数.例如: 输入: ...
- LeetCode算法题-Reverse Linked List(Java实现)
这是悦乐书的第192次更新,第195篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第51题(顺位题号是206).反转单链表.例如: 输入:1-> 2-> 3- ...
- LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)
这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- Leetcode练习题 7. Reverse Integer
7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...
随机推荐
- gulp.基础
1.安装 全局安装 npm install --global gulp 作为项目的开发依赖安装 npm install gulp --save-dev 2.在根目录下创建一个名为gulpfile.js ...
- nginx常用命令及简单配置
nginx常用命令 nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx(windows下start nginx); nginx -s quit 停止ng ...
- Wireless Penetration Testing(命令总结)
1.对本书出现的无线网络涉及的命令做一总结 查看无线网卡( Create a monitor mode interface using your card as shown in the follow ...
- lightoj1259 线性筛的另一种写法 v变成bool标记数组
也是用线性筛,但是v用int会爆,所以这个线性筛用的是另外一种写法 #include<cstdio> #include<cmath> #include<queue> ...
- 第七周学习总结-C#
2018年8月26日 这个周二突然得知另一位老师留的暑假作业,群文件里早就上传了,我居然一直没翻到那里,要不是同学问作业做完没,我可能开学就要“真●裸考”了
- animate方法使用总结
<!DOCTYPE html><html lang="en" class="loading"><head> <meta ...
- python面向对象三大特性之继承
继承是创建新类的方法,以下是几个概念 父类,基类,超类: 被继承的类 子类,派生类:继承而产出的类 单继承:继承一个父类 多继承:继承多个父类 继承: 什么是什么的关系 父类中没有的属性,在字类中出现 ...
- spring boot引入json,jsonobject,需要指定jdk15
spring boot引入json,需要指定jdk15 <dependency> <groupId>net.sf.json-lib</groupId> <ar ...
- PAT Basic 1071. 小赌怡情(15)
题目内容 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注t个筹码后,计算机给出第二个数.若玩家猜对 ...
- mongodb 安装时错误
1.安装MongoDB进度条长时间不动 根据在网上搜的步骤安装mongoDB到这步,就基本上卡死不动,在网上查到的办法是死等,等了半个小时,但运气不好半个小时也不一定安装成功. 如果进行到这步,卡死在 ...