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+ ...
随机推荐
- Python9-反射-day27(大年初三)
复习 class 类名(父类,父类2): 静态属性 = '' #静态属性 类属性 def __init__(self): #初始化方法 self.name = 'alex' def func(self ...
- Python9-面对对象2-day23
#计算正方形的周长和面积 class Square: def __init__(self,side_len): self.side_len = side_len def perimeter(self) ...
- 【练习】reserving.kr 之Direct3D FPS
算法函数如图,关键点在标志处,加密字符串如下图 于是写如下脚本: flag_c='436B666B62756C694C455C455F5A461C07252529701734390116494C201 ...
- OpenGLES2.0着色器语言glsl
OpenGLES2.0中是强制使用可编程的渲染管线的,使用的是glsl着色器语言,因为着色器语言是使用的GPU,即图形处理单元,而不是CPU,这样可以使CPU从繁重的几何计算和像素的处理中解脱出来了. ...
- Myeclipse 添加Android开发工具
1.JDK是必须的,同时配置相应环境变量. 2.Android SDK 下载后解压缩需要把SDK目录下的tools和platform-tools加入环境变量. 3.MyEclipse中安装ADT插件 ...
- 【SaltStack】在Master上给Minion端安装zabbix
一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...
- 关于Relay Log无法自动删除的问题
本文介绍了一次运维实践中relay-log长期无法自动删除的原因和解决过程 背景: 今天在运维一个mysql实例时,发现其数据目录下的relay-log 长期没有删除,已经堆积了几十个relay-lo ...
- Leetcode 410.分割数组的最大值
分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意:数组长度 n 满足以下条件: 1 ≤ n ...
- 树状数组 Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains
C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- .NET重构(七):VS报表的制作
导读:机房做到最后阶段,就是报表的制作了.想到第一次,是借助外部控件进行实现的,这次采用VS进行编写,在这个软件中,有自带的报表编辑工具,更加的方便和简洁,现在就对这一块的学习,进行总结. 一.报表制 ...