007. Reverse Integer
题目链接:https://leetcode.com/problems/reverse-integer/description/
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: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
思路:
- 对待处理的整数X,拷贝一份并命名为 copy,取拷贝值的绝对值进行下述数据处理;
- 用长整型变量 ans 来存储翻转后的数值, ans变量赋初值为0; 当copy > 0 时对copy进行整除和取余操作:
- int cur = copy % 10; // 当前处理的数位的值
- ans = ans * 10 + cur;
- copy = copy / 10;
- 对求得的ans值进行范围检验;
- 若X值为正数,则返回ans;若X值为负数,则返回 -ans 。
编码如下:
class Solution {
public:
int reverse(int x) {
// 若数值不在 [−2^31, 2^31 − 1]范围内,则返回0
//if (x < (1 << 31) || x > (~(1 << 31))) return 0;
// 若x属于[-9, 9]这个范围内,则返回x
if (- < x && x < ) return x;
// 其他情况
long int ans = ;
int copy = (x >= ? x : -x);
while (copy > )
{
int cur = copy % ; // 当前处理的数位值
ans = ans * + cur;
copy = copy / ;
}
ans = (x >= ? ans : -ans);
if (ans < ( << ) || ans > (~( << ))) return ;
return static_cast<int>(ans);
}
};
007. Reverse Integer的更多相关文章
- No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- LeetCode--No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【LeetCode】007. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [Leetcode]007. Reverse Integer
public class Solution { public int reverse(int x) { long rev=0; while(x!=0){ rev = rev*10+x%10; x=x/ ...
- 007 Reverse Integer 旋转整数
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- Python字符串倒序-7. Reverse Integer
今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...
随机推荐
- [Ahoi2009]self 同类分布
1799: [Ahoi2009]self 同类分布 Time Limit: 50 Sec Memory Limit: 64 MBSubmit: 2357 Solved: 1079[Submit][ ...
- [TypeScript] @OnChange for ngOnChanges
Take away from NGCONF talk. It is a good show case to how to use decorator. export interface SimpleC ...
- Python 多线程Ⅲ
线程优先级队列( Queue) Python的Queue模块中提供了同步的.线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列Prior ...
- JS转换/Date(-28800000)/格式
去除/Date() if (value.includes('/Date')) { var re = /-?\d+/; value = re.exec(value); value = new Date( ...
- EL表达式接收值
- CDOJ 1256 打表+数组 统计
昊昊爱运动 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- K8s中RS和Deployment
什么是ReplicaSet? ReplicaSet是下一代复本控制器.ReplicaSet和 Replication Controller之间的唯一区别是现在的选择器支持.Replication Co ...
- 「美团 CodeM 资格赛」试题泛做
LibreOJ真是吼啊! 数码 推个式子,把枚举因数转为枚举倍数.然后就发现它是根号分段的.然后每一段算一下就好了. #include <cstdio> #include <cstr ...
- objdump命令解析
[objdump] 相关链接: 实例分析objdump反汇编用法 - 在路上 - CSDN博客 https://blog.csdn.net/u012247418/article/details/80 ...
- ZAP-Queries【luogu3455】
题目大意 有不超过\(50000\)个询问,每次询问有多少正整数对\(x\),\(y\),满足\(x\leqslant a\),\(y \leqslant b\),并且\(gcd(x,y)=c\).其 ...