题目:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.

代码:

class Solution {
public:
int reverse(int x) {
int v = x;
queue<int> que;
while ( true )
{
int digit = v % ;
que.push(digit);
v = v / ;
if ( abs(v)< ) break;
}
if ( v!=) que.push(v);
int ret = ;
while ( !que.empty() )
{
// cout << que.front() << endl;
if ( ret > INT_MAX/ || ret < INT_MIN/) return ; // overflow
ret = ret * + que.front();
que.pop();
}
return ret;
}
};

tips:

主要考虑出现overflow怎么处理,上述代码第一次写用了queue的结构,先进先出,方便调试各种case。

AC后对上述的代码进行了改进,改进结果如下,也可以AC。

class Solution {
public:
int reverse(int x) {
int ret = ;
while (x)
{
if ( ret > INT_MAX/ || ret < INT_MIN/ ) return ;
ret = ret* + x%;
x = x /;
}
return ret;
}
};

可以看到代码精炼了很多。另外判断是否越界不要直接比较 value>INT_MAX或者value<INT_MIN一般都是比较最大值除以10;因为value本身就越界了,再跟最大值比较也没有意义了。这是一个放缩的技巧。

=============================================

第二次过这道题,把corner case考虑完全了,就AC了。

class Solution {
public:
int reverse(int x) {
vector<int> digits;
int sign = x> ? : -;
int remain = ;
while ( remain> )
{
digits.push_back(fabs(x%));
x = x/;
remain = fabs(x);
}
int ret = ;
for ( int i=; i<digits.size(); ++i )
{
if ( INT_MAX/<ret || (INT_MAX/==ret && INT_MAX%<digits[i]) )
{
return ;
}
ret = ret* + digits[i];
}
return ret*sign;
}
};

【Reverse Integer】cpp的更多相关文章

  1. 【Palindrome Number】cpp

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

  2. 【Roman To Integer】cpp

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  3. 【Reorder List】cpp

    题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do ...

  4. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  5. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  6. 【Gray Code】cpp

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  7. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  8. 【Next Permutation】cpp

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  9. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

随机推荐

  1. 解决The Network Adapter could not establish the connection

    解决1 主机与虚拟机ping不通 解决2 状态: 失败 -测试失败: IO 错误: The Network Adapter could not establish the connection 本次尝 ...

  2. CSS中box-sizing属性的作用

    今天在项目中看到box-sizing这个属性,以前用过,但是不常用!注意,它是CSS3里的属性喔! W3C 盒子模型:标准盒模型,是指块元素box-sizing属性为content-box的盒模型.一 ...

  3. font:inherit

    font:inherit 字体的设置 设置所有元素的字体保持一致: 所有元素:*{font:inherit;} /* IE8+ */ body体用percent:body{font:100%/1 sa ...

  4. 怎样在github里面写个人主页

    1 登录你的账号 打开

  5. 如何修改集群的公网信息(包括 VIP) (文档 ID 1674442.1)

    适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.3 到 12.1.0.2 [发行版 11.2 到 12.1]本文档所含信息适用于所有平台 用 ...

  6. Tarjan在图论中的应用(一)——用Tarjan来实现强连通分量缩点

    前言 \(Tarjan\)是一个著名的将强连通分量缩点的算法. 大致思路 它的大致思路就是在图上每个联通块中任意选一个点开始进行\(Tarjan\)操作(依据:强连通分量中的点可以两两到达,因此从任意 ...

  7. 题解 P1280 【尼克的任务】

    传送门 f[i]表示i~n的最长空闲时间: 如果当前无任务就休息一秒(f[i]=f[i+1]+1): 否则f[i]=max(f[i],f[i+当前工作时间]); 用结构体来记录,我们对于每一个时刻开一 ...

  8. 零基础快速入门SpringBoot2.0教程 (三)

    一.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-boot/doc ...

  9. Arguments Optional-freecodecamp算法题目

    Arguments Optional 1.要求 创建一个计算两个参数之和的 function.如果只有一个参数,则返回一个 function,该 function 请求一个参数然后返回求和的结果. 如 ...

  10. 网络流_Edmond-Karp算法、Dinic算法

    转载:网络流基础篇——Edmond-Karp算法             BY纳米黑客 网络流的相关定义: 源点:有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点. 汇点:另一个点也很特殊, ...