[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 ...
随机推荐
- URL 字段简析
URL:统一资源定位符:URL是uri的一个子集,另外一个子集是URN. URL语法:(来自HTTP权威指南中文版P29) 组件 描述 默认值 方案 访问服务器以获取资源时要使用哪种协议 无 用户 某 ...
- Nginx入门详解文档
1 文章内容 掌握nginx+tomcat反向代理的使用方法. 掌握nginx作为负载均衡器的使用方法. 掌握nginx实现web缓存方法. 2 nginx介绍 2.1 什么是nginx Nginx是 ...
- 【Poj1090】Chain
Chain Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3414 Accepted: 1126 Description ...
- bzoj 2428 均分数据
题目大意: 已知N个正整数 将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小 求最小均方差 思路: 模拟退火 #include<iostream> #include<c ...
- [POI2011]LIZ-Lollipop
https://www.zybuluo.com/ysner/note/1303462 题面 给一个只有\(1\)和\(2\)的序列,每次询问有没有一个子串的和为\(x\). \(n\leq10^6\) ...
- Face alignment at 3000FPS via Regressing Local Binrary features 理解
这篇是Ren Shaoqing发表在cvpr2014上的paper,论文是在CPR框架下做的,想了解CPR的同学可以参见我之前的博客,网上有同学给出了code,该code部分实现了LBF,链接为htt ...
- mysql status关键字 数据表设计中慎重使用
mysql status关键字 数据表设计中慎重使用
- srand()
//第一次:5 0 第二次:5 16 srand(); //seed为常数,则每次运行产生的随机数一样 printf(); //产生的随机数都是一样的,都是5 srand(time(NULL)); p ...
- F - System Overload(约瑟夫环变形)
Description Recently you must have experienced that when too many people use the BBS simultaneously, ...
- kafka的server.properties配置文件参考示范(图文详解)(多种方式)
简单点的,就是 kafka_2.11-0.8.2.2.tgz的3节点集群的下载.安装和配置(图文详解) 但是呢,大家在实际工作中,会一定要去牵扯到调参数和调优问题的.以下,是我给大家分享的kafka的 ...