[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 ...
随机推荐
- input keyevent发送按键值【转】
本文转载自:http://blog.csdn.net/moyu123456789/article/details/71209893 1.adb shell进入android设备,执行命令input k ...
- TFLearn 在给定模型精度时候提前终止训练
拿来主义:看我的代码,我是在模型acc和验证数据集val_acc都达到99.8%时候才终止训练. import numpy as np import tflearn from tflearn.laye ...
- Zookeeper日志文件&事务日志&数据快照
Zookeeper持久化两类数据,Transaction以及Snapshot,logDir存储transaction命令,dataDir存储snap快照,其下子目录名称以version-2命名,子目录 ...
- ubuntu 16.04 Sqoop 安装
1.下载:https://mirrors.tuna.tsinghua.edu.cn/apache/sqoop/1.4.6/ sqoop-1.4.6.bin__hadoop-2.0.4-alpha.ta ...
- Nginx配置try_files实践二
本文内容承接<Nginx配置try_files实践一> 1. 环境: OS:Ubuntu 15.10 nginx:nginx/1.9.3 (Ubuntu) 假设有三台虚拟机db1(IP:1 ...
- LOGO免费在线设计
http://www.logomaker.com.cn/ 藏经阁技术资料分享群二维码
- sql 获取当前季度期间
select year(getdate())*10000+((month(getdate())/3)*3+1)*100 + 1 --季度第一天 select year(getdate())*10000 ...
- SQL Split函数,将一串字符串返回成table
写法一: CREATE FUNCTION [dbo].[Split] ( @str VARCHAR(MAX), --传进来的字符串 ) --分割符 ) RETURNS @t TABLE --定义一个虚 ...
- 6.12---前提两个对象的成员必须一致,才能将有数据的对象将数据传给反射获取的对象conver(有数据对象,目标对象)
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)/ ...
- 前端面试题HTML
浏览器页面有哪三层构成,分别是什么,作用是什么?