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方法 ...
随机推荐
- java中int转成String位数不足前面补零
java中int转成String位数不足前面补零 转载自:http://ych0108.iteye.com/blog/2174134 java中int转String位数不够前面补零 String.fo ...
- 数据管理必看!Kendo UI for jQuery过滤器概述
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- Acwing-196-质数距离(素数区间筛法)
链接: https://www.acwing.com/problem/content/198/ 题意: 给定两个整数L和U,你需要在闭区间[L,U]内找到距离最接近的两个相邻质数C1和C2(即C2-C ...
- Django后台获取不到前端axios-post请求提交的参数的解决方法
解决方法 用 URLSearchParams 传递参数 let param = new URLSearchParams() param.append('username', 'admin') para ...
- 关于int指令
1.关于int指令 格式:int n n为中断类型码: 作用: 调用n号中断程序: 指令“int n”的执行过程: 1]获取中断类型码n 2]标志寄存器入栈,IF. ...
- webstorm 使用指南
1. webstrom打开多个项目 默认情况下一次只能打开一个项目,如果需要打开多个就按照下面的方法: Preferences -> Directories -> Add Content ...
- Javascript调试技巧整理
整理一下网上看到的实用调试技巧! 1. 不要使用alert 首先,alert只能打印出字符串,如果打印的对象不是String,则会调用toString()方法将该对象转成字符串(比如转成[object ...
- 牛客训练41D最小相似度bfs
最小相似度 题目大意:对于位数相同的两个二进制串,SIM(A,B)为它们的相似度,也就是A^B中0的个数.现在给定一堆串,找出一个T使得max{SIM(S1,T),SIM(S2,T),......,S ...
- CF762F Tree nesting
题目连接 问题分析 可以给小树钦定一个根, \(Dp[i][j]\) 表示大树上的点 \(i\) 对应到小树上的点 \(j\) 的可能的方案数.然后每一步转移都是一个状压DP(将小树是否被匹配状压,然 ...
- js 获取地址栏信息,可以传递多个参数
//获取多个地址栏信息,name为地址栏参数名,可以传递多个参数 // 形式为 .html?id=12&a=2 function getQueryString(name){ var reg = ...