【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 ...
随机推荐
- JSP对象和JavaBean
1. JSP 客户端请求 当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的,如下图所示: Http请求头对应的内容如下: 对应方 ...
- 【extjs6学习笔记】1.9 初始: Mixins
Mixin允许我们使用一个类的函数作为另一个类的函数而不继承. Mixins可以使用mixins关键字定义,并将值指定为JSON对象,其中属性的名称应该是要使用的方法的名称,属性的值将是定义方法的类的 ...
- 【作业留存】根据IATF框架,设计的一种中小型企业安全拓扑
- C语言——字符串长度的计算方法
1.不带转义字符的字符串 如:“abc!x=/”,其长度为7 2.带转义字符的字符串 (1) 字符串“abc\n”:其中的'\n'为转义字符(换行符),计算字符串长度时只能计作一个字符,所以该字符串的 ...
- pat乙级1067
1.用cin输入数据后,再用getline 输入,还是会输入cin已经输入的数据,即cin和getline互相独立. 2.题目中没有说尝试的密码不包含空格,因此不能用cin,而用getline. #i ...
- UVA1363 - Joseph's Problem(数学,迷之优化)
题意:给出n和k,1≤n,k≤1e9,计算 切入点是k/i 和 k/(i+1)差距不大.令pi = k/i, ri = k%i.如果pi+1 == pi,那么ri+1 == k - pi(i+1) = ...
- 【BZOJ1857】传送带(分治经典:三分套三分)
点此看题面 大致题意: 一个二维平面上有两条传送带\(AB\)和\(CD\),\(AB\)传送带的移动速度为\(P\),\(CD\)传送带的移动速度为\(Q\),步行速度为\(R\),问你从\(A\) ...
- 反转链表[剑指offer]之python实现
输入一个链表,输出反转后的链表. 非递归实现: # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val ...
- python_74_pickle反序列化
import pickle def say(name):#序列化时用完会释放,要想反序列化,要重新写上该函数,否则会出错 print('我的高中', name)#可以和之前的序列化函数不同 f=ope ...
- lsscsi 与 cat /proc/scsi/scsi
[root@localhost ~]# lsscsi[0:0:0:0] disk SEAGATE ST300MM0048 N001 /dev/sda [0:0:2:0] ...