【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 ...
随机推荐
- UOJ#130 【NOI2015】荷马史诗 K叉哈夫曼树
[NOI2015]荷马史诗 链接:http://uoj.ac/problem/130 因为不能有前缀关系,所以单词均为叶子节点,就是K叉哈夫曼树.第一问直接求解,第二问即第二关键字为树的高度. #in ...
- POJ - 3685 Matrix
二分kth,答案满足的条件为:m ≤ 小于等于x的值数cntx.x和cntx单调不减,随着x增大,条件成立可表示为:0001111. 本地打一个小型的表可以发现列编号j固定时候,目标函数f(i,j)似 ...
- POJ 3181 Dollar Dayz(递推,两个long long)
题意:John有N美元,有价格为1~K的工具,可以买的个数不限,问1~K组合出N的方案数. f[i = 第i中工具][j = 花费为j] = 方案数. f[i][j] = sigma{ f[i-1][ ...
- POJ-3080 Blue Jeans---字符串+暴力
题目链接: https://vjudge.net/problem/POJ-3080 题目大意: 找最长的公共字串(长度>=3),长度相同就找字典序最小的 解题思路: 枚举第一个串的所以子串,处理 ...
- 一、Web 如何工作的
平常我们在浏览器中输入一个网址,随即看到一个页面,这个过程是怎样实现的呢?下面用一幅图来说明: 整个流程如下: 1.域名解析 浏览器会解析域名对应的IP地址 PS:DNS服务器的知识 2.建立TCP ...
- 动态规划专题(五)——斜率优化DP
前言 斜率优化\(DP\)是难倒我很久的一个算法,我花了很长时间都难以理解.后来,经过无数次的研究加以对一些例题的理解,总算啃下了这根硬骨头. 基本式子 斜率优化\(DP\)的式子略有些复杂,大致可以 ...
- Maven 虐我千百遍,我待 Maven 如初恋
前言 在如今的互联网项目开发当中,特别是Java领域,可以说Maven随处可见.Maven的仓库管理.依赖管理.继承和聚合等特性为项目的构建提供了一整套完善的解决方案,可以说如果你搞不懂Maven,那 ...
- H1ctf-Vote
用来练习IO_FILE利用 glibc-2.23 # coding:utf-8 from pwn import * from FILE import * context.arch = 'amd64' ...
- Java基础——动态代理
1.什么是动态代理? 简单的来说,就是本来让我自己做的事,请给别人来做,这个请的人就是代理对象 那么动态代理就是在程序运行过程中产生这个代理对象,而程序运行中产生的对象就是用反射的来生成一个代理. 举 ...
- XAMPP安装过程中,出现的问题
这次运行一个简单的前端(html+css+js+ajax)+php后端项目,运行XAMPP的时候,出现两个问题: phpmyadmin运行不起来,一直报1544错误 请求本地图片及php文件报403错 ...