[Algorithm] 10. Reverse Integer
Description
Given a 32-bit signed integer, reverse digits of an integer.
Example
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: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Solution
int reverse(int x) {
bool minus_flag = false;
bool zeroIgnore = true;
int result=;
stack<char> numPool; if( x < ){
x = -x;
minus_flag = true;
} if( x/ == ){
return minus_flag ? -x : x;
} // Put each number into Stack container.
while(x){
if ( x % ) {
numPool.push(x % );
zeroIgnore = false;
}else{
if ( !zeroIgnore )
numPool.push();
}
x = x/;
} // Calculate total sum.
int cnt=numPool.size();
for(int i=; i<cnt; i++)
{
result+=numPool.top() * pow(, i);
// Judge if the result overflows.
if (result < numPool.top() * pow(, i) ) {
return ;
}
numPool.pop();
} return minus_flag ? -result : result;
}
[Algorithm] 10. Reverse Integer的更多相关文章
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- Reverse Integer 2015年6月23日
题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- LeetCode--No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
随机推荐
- APDU命令的结构和处理【转】
本文转载自:http://blog.csdn.net/yonghenzhita/article/details/36402525 版权声明:本文为博主原创文章,未经博主允许不得转载. 简单说,IFD( ...
- Chrome查看JavaScript函数
在页面上右键view page source(Ctrl+U),然后在弹出来的界面可以查找JavaScript函数 注意:这个只能看到内嵌在网页上的JavaScript函数 一般来讲,JavaScrip ...
- cordova常用命令
安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld 添加平台 cor ...
- swoole简易实时聊天
最近公司拓展业务,需要做一个即时聊天业务,就顺带研究了一下swoole,文档地址贴出来:https://wiki.swoole.com/ 文档写得很详细,demo也很简洁明了,所以废话就不多说了. 工 ...
- 并不对劲的spoj nsubstr
题意是求一个字符串每个长度的子串出现次数最多的那个出现了多少次,也就是求每个到根的最长路的right集合最大值 . 先建后缀自动机,然后将每个前缀所在的集合的初值设为1,因为所有前缀的right集合肯 ...
- bzoj2705 [SDOI2012]Longge的问题——因数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 一开始自己想了半天... 有了点思路:遍历 n 的因数 k,每个因数要预处理出 gcd ...
- 【175】Easy CHM的使用
首先下载软件,EasyCHM3.84完美破解版.rar! 安装好之后,打开程序,点击“新建”,然后浏览到存放 htm 文件的目录. 鼠标右键,选择“添加目录项”!如下图所示. 在弹出的文本框中,首先选 ...
- c++ class does not name a type (转载)
转载:http://blog.csdn.net/typename/article/details/7173550 declare class does not name a type 出现这个编译错误 ...
- SpringBoot整合SpringSecurity简单案例
在我们开发项目的过程中经常会用到一些权限管理框架,Java领域里边经常用的可能就是shiro了,与之对应的还有SpringSecurity,SpringSecurity可以说是非常强大,与Spring ...
- webpack+vue-cli中proxyTable配置接口地址代理详细解释
在vue-cli项目中config目录里面的index.js配置接口地址代理,详细解释如下图所示: