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+ ...
随机推荐
- Mining of Massive Datasets-1
given lots of data->discover patterns and models that are: valid, useful, unexpected, understanda ...
- Login登录页面的制作流程(摘要)
*****我的QQ号:1539832180.欢迎一起讨论学习.***** 第一步:拿到设计图,先别急着切,先分析. 因为切图不只是切图,设计不只是设计.你得考虑四方面的因素: 1. 合理的切图,语 ...
- luogu1129 [ZJOI2007]矩阵游戏
其实,只用考虑某一行能否放到某一行就行了 #include <iostream> #include <cstring> #include <cstdio> usin ...
- luogu2146 [NOI2015]软件包管理器
安装就把根节点到它全设为 1 删除就把以它为根的子树全设为 0 记得标记初始化为-1,因为标记是 0 的情况也是要处理的. #include <iostream> #include < ...
- 有关C语言指针访问问题
C语言指针访问问题今天有了一些理解. char *p; char *q; char k[10000]; 我之前一直以为他们两个一样用,因为之前看到说k也是一个地址,我忽略了后面的一句话,k是连续的一段 ...
- 信安实验-RC4加密算法
RC4加密算法 算法具体就不介绍了,应信安老师要求整理及掌握. #include<bits/stdc++.h> using namespace std; const int N=256; ...
- 12.jsp概述及指令
JSP全名是Java Server Page,它是建立在Servlet规范之上的动态网页开发技术.在JSP文件中,HTML代码与Java代码共同存在.Html代码用来实现网页中静态内容的显示,Java ...
- URAL Formula 1 ——插头DP
[题目分析] 一直听说这是插头DP入门题目. 难到爆炸. 写了2h,各种大常数,ural垫底. [代码] #include <cstdio> #include <cstring> ...
- 启动uwsgi报错error while loading shared libraries: libpcre.so.1:
启动uwsgi时候报错: [root@ richie]# /usr/bin/uwsgi --ini /usr/local/nginx/conf/uwsgi.ini /usr/bin/uwsgi: er ...
- Codeforces 894.A QAQ
A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...