7_Reverse Integer
7.Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321Example 2:
Input: -123
Output: -321Example 3:
Input: 120
Output: 21Note:
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.
没有想出来, 仿照(或者照搬?)http://www.cnblogs.com/grandyang/p/4125588.html来写
注意点:
反转溢出问题, 解决方法: 使用比该数字范围大的类型定义返回值
版本一: 拆分每个位的思想是我的, 其他细节参考上述博客
class Solution {
public:
int reverse(int x) {
long long res = 0;
vector<int> bit;
while (0 != x) {
bit.push_back(x % 10);
x /= 10;
}
for (int i = 0; i < bit.size(); i++) {
res =res * 10 + bit[i];
}
return (res < INT_MIN || res > INT_MAX) ? 0 : res;
}
};
版本二: 半个自己写的, 考虑符号, 虽然多余了
class Solution {
public:
int reverse(int x) {
long long ret = 0;
// int positive = 1;
long long positive = 1;
if (x < 0) {
positive = -1;
x *= positive;
}
while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
}
ret *= positive;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
class Solution {
public:
int reverse(int x) {
long long int ret = 0;
while(0 != x) {
ret = ret*10 + x%10;
x /= 10;
}
//return (ret > INT_MIN || ret < INT_MAX)? ret : 0;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
7_Reverse Integer的更多相关文章
- 9_Palindrome Number
9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- 引用类型之Array(一)
Array类型 除了Object之外,Array类型在ECMAScript中也很常用.ECMAScript中的数组与其他多数语言中的数组有着相当大的区别.ECMAScript数组的每一项可以保存任何类 ...
- MySQL数据库入侵及防御方法
来自:http://blog.51cto.com/simeon/1981572 作者介绍 陈小兵,高级工程师,具有丰富的信息系统项目经验及18年以上网络安全经验,现主要从事网络安全及数据库技术研究工作 ...
- Win10桌面不见了只显示开始菜单该怎么办?
来源:http://www.w10zj.com/Win10xy/Win10xf_4256.html 在Win10系统中,有用户反应桌面不见了,只显示开始菜单的情况,该怎么办呢?出现这样的情况一般由于桌 ...
- 洛谷比赛 「EZEC」 Round 4
洛谷比赛 「EZEC」 Round 4 T1 zrmpaul Loves Array 题目描述 小 Z 有一个下标从 \(1\) 开始并且长度为 \(n\) 的序列,初始时下标为 \(i\) 位置的数 ...
- 【题解】Ehab the Xorcist
\(\color{red}{Link}\) \(\color{blue}{\text{Solution:}}\) 题目要求构造一个最短的序列,使得异或和为\(u\),数列和为\(v\). 那么,因为是 ...
- java的string方法使用
1.将list转换为","隔开的字符串 //videoIdList值转换成 1,2,3 String videoIds = StringUtils.join(videoIdList ...
- Sticks(UVA - 307)【DFS+剪枝】
Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...
- 多测师讲解jmeter _接口请求_(003)高级讲师肖sir
1.简单接口的请求 2. 3. 正则查看: 正则提取:在后置处理器中正则请求 设置:正则表达式 JSESSIONID提取器: Debug sampler 总结:
- 第一个月多测师讲解__项目讲解以及注意事项(肖sir)
一.目的讲解流程:(讲述业务时长10-15分钟为宜)1.自我介绍礼貌用语,姓名,籍贯,学校,个人技能,经验,表现,兴趣爱好等 ,1分钟 ,谢谢2.介绍项目的名字 ,项目的背景,(涉及什么架构)3.对项 ...
- Mysql架构与内部模块-第一章
Mysql作为大多数中小型企业的首选数据库,也可能是众多同僚接触的第一个数据库,其热门程度不言而喻,一些相对基础的知识本系列不做赘述,主要简述Mysql相关的进阶知识. 本章将由浅入深的讲解从连接My ...