【Reverse Integer】cpp
题目:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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的更多相关文章
- 【Palindrome Number】cpp
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- 【Roman To Integer】cpp
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【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 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Gray Code】cpp
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Next Permutation】cpp
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
随机推荐
- Thymeleaf的模板使用介绍
参考网址: https://blog.csdn.net/hry2015/article/details/73476973 先定义一个html文件, 如下: 文件路径: templates/templa ...
- shell中的判断语句
1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空,变量) -z str ...
- pecl install msgpack
Before the beginning: There are two php version, php5.5, php7.1. we need to install msgpack under ph ...
- 实战:ADFS3.0单点登录系列-ADFS3.0安装配置
本文为系列第三章,主要讲下ADFS3.0的安装和配置.本文和前面的文章是一个系列,因此有些地方是有前后关联,比如本文中使用的通配符证书就是第二篇讲解的,因此需要连贯的进行阅读. 全文目录如下: 实战: ...
- X86/X64 函数调用约定
C 语言有 __cdecl.__stdcall.__fastcall.naked.__pascal. C++ 语言有 __cdecl.__stdcall.__fastcall.naked.__pasc ...
- NOIP2018赛前停课集训记——最后的刷板子计划
前言 再过两天就\(NOIP2018\)了. 于是,我决定不做其他题目,开始一心一意刷板子了. 这篇博客记录的就是我的刷板子计划. [洛谷3383][模板]线性筛素数 这种普及-的题目我还写挂了两次( ...
- 将数据库数据显示到TreeView控件中
实现效果: 知识运用: TreeView控件中的Nodes集合的Add方法 实现代码: private void init() { treeView1.ShowLines = true; treeVi ...
- Problem I: Satellite Photographs
Problem I: Satellite Photographs Time Limit: 1 Sec Memory Limit: 128 MB Submit: 208 Solved: 118 [S ...
- iOS 多线程(NSThread、GCD、NSOperation)
ios中得多线程技术主要使用3种:NSThread.NSOperation和GCD 一.NSThread: 最轻量级方法,但是不安全需要手动加锁,需要自己管理生命周期 NSThread的使用方法有2种 ...
- JavaScript 交换两个变量的值
方法一 let a = "a", b = "b"; console.log(a, b); let t = a; a = b; b = t; console.lo ...