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的更多相关文章

  1. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  2. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  3. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  4. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  5. Reverse Integer 2015年6月23日

    题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...

  6. Reverse Integer - 反转一个int,溢出时返回0

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  7. LeetCode之Easy篇 ——(7)Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...

  8. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  9. LeetCode--No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

随机推荐

  1. POJ 3650:The Seven Percent Solution

    The Seven Percent Solution Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7684   Accep ...

  2. C# 判断两个矩形是否相交

    源代码 public bool JudgeRectangleIntersect(double RecAleftX, double RecAleftY, double RecArightX, doubl ...

  3. 常用开源<监控软件>介绍

    转载地址:http://blog.csdn.net/lx_9986/article/details/6803243 一.Zenoss Core Zenoss Core是开源企业级IT管理软件-是智能监 ...

  4. 关于Jedis无法连接上Linux上Redis问题

    环境:CentOS7.Redis 主要解决Jedis客户端无法连接Linux上Redis服务问题 1.修改Redis目录下的redis.conf配置文件 注释掉bind本地回环地址:bind 127. ...

  5. 为什么JavaWeb项目要分层

    首先让我们坐着时光机回到n年前的web开发.那个时候最早都是静态的html页面,后来有了数据库,有了所谓的动态页面,然后程序猿在编码的时候,会把所有的代码都写在页面上,包括数据库连接,包括事务控制,接 ...

  6. spring data elasticsearch的 @Documnet 和 @Field 注解

    @Documnet 注解 public @interface Document { String indexName(); //索引库的名称,个人建议以项目的名称命名 String type() de ...

  7. 状压DP UVA 11795 Mega Man's Mission

    题目传送门 /* 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码 ...

  8. Spring抽象JDBC,使用JdbcTemplate

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. ADB Usage Complete / ADB 用法大全

    ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具. 持续更新中,欢迎提 PR 和 Issue 补充指 ...

  10. 笔试题的各种trick

    %x 默认去掉前导零 #include<stdint.h> #include<stdio.h> union X {     int32_t a;     struct      ...