LeetCode——Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
数字逆转。
可转成字符数组。再处理。可是这样要考虑的情况比較多,并且使用了新的空间,效率不高。
能够通过求模运算获得依次低位,再把原乘以10加上新取得的数。
public static int reverse(int x) {
int ret = 0;
while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
}
return ret;
}
比方:输入-192,计算步骤例如以下,经过了三轮循环:
ret x
---------------
0 -192
-2 -192
-2 -19
---------------
-2 -19
-29 -19
-29 -1
---------------
-29 -1
-291 -1
-291 0
---------------
-291
LeetCode——Reverse Integer的更多相关文章
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- C++ leetcode::Reverse Integer
第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- LeetCode——Reverse Integer(逆置一个整数)
问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321 Ha ...
- Leetcode: Reverse Integer 正确的思路下-要考虑代码简化
题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- [Leetcode]Reverse Integer
核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...
- leetcode reverse integer&&Palindrome Number
public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...
随机推荐
- cf 1016D
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- eclipse代码格式化快捷键无法使用
[产生原因] Ctrl+Shift+F快捷键组合被其他应用占有,如输入法. [解决方案] 关闭或更换其他应用快捷键或更换eclipse对应的快捷键组合.
- lnmp环境运行laravel open_basedir restriction in effect 问题
环境配置:centos 7 : php 7.1.5 Warning: require(): open_basedir restriction in effect. File(/home/wwwroot ...
- 序列化 random模块应用
序列化 我们今天学习下序列化,什么是序列化呢? 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 为什么要有序列化模块: 比如,我们在python代码中计算的一个数据需要给另外一段程序使用 ...
- Django框架简介及模板Template,filter
Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...
- java 协程框架kilim
http://phl.iteye.com/blog/2247112 http://chen-tao.github.io/2015/10/02/kilim-work-way/ 待丰富
- GT使用说明
GT文档:https://gt.qq.com/docs.html GT Android版的详细使用手册: https://gt.qq.com/docs/a/GTAndroidUserGuide.pdf
- POJ 2187 Beauty Contest ——计算几何
貌似直接求出凸包之后$O(n^2)$暴力就可以了? #include <cstdio> #include <cstring> #include <string> # ...
- Win10下安装虚拟机提示“Intel VT-x处于禁用状态”如何解决
今天在自己的笔记电脑上想安装一个Kali虚拟机学习用,安装的时候遇到这个问题,提示"Intel VT-x处于禁用状态". 要进入Win10的BIOS,先进入"设置&quo ...
- MongoDB_起步
MongoDB基本概念 <1> mogoDB是一个文档存储类型的nosql数据库,文档存储一般用类似json的格式存储,存储的内容是文档型的. 这样也就有机会对某些字段建立索引, < ...