LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer
题目要求:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
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.
这道题主要要注意末尾为0和越界的问题。程序如下:
class Solution {
public:
int reverse(int x) {
if(x == INT_MIN)
return ;
int num = abs(x);
int last = ;
long result = ;
while(num != )
{
last = num % ;
result = result * + last;
num /= ;
}
if(result > INT_MAX)
return ;
return (x > ) ? result : -result;
}
};
2. Reverse Bits
题目要求:
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
Related problem: Reverse Integer
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
下边的程序中用到了bitset数据结构,需要注意的是,bits[0]才是最低位。
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t one = ;
bitset<> bits;
int i = ;
while(n != && i > -)
{
bits[i] = n & one;
n = n >> ;
i--;
}
return bits.to_ulong();
}
};
LeetCode之“数学”:Reverse Integer && Reverse Bits的更多相关文章
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode第[7]题(Java):Reverse Integer 标签:数学
题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
随机推荐
- NuGet包断线续传下载
NuGet包断线续传下载(金庆的专栏)NuGet是VC的扩展,用来下载依赖包.NuGet下载没有断线续传,下载源又很容易断开. https://nuget.org/api/v2/ https:// ...
- oracle伪列
Oracle的伪列以及伪表 oracle系统为了实现完整的关系数据库功能,系统专门提供了一组成为伪列(Pseudocolumn)的数据库列,这些列不是在建立对象时由我们完成的,而是在我们建立时由Ora ...
- CCSpriteBatchNode中存放元素的一点理解
该对象只能包含基于CCSprite的对象,并且该要求适用于一切子孙对象.即加入CCSpriteBatchNode的任何对象都必须是CCSprite或其子类. 比如CCSpriteBatchNode包含 ...
- UE4 读取本地图片
参考链接:https://answers.unrealengine.com/questions/235086/texture-2d-shows-wrong-colors-from-jpeg-on-ht ...
- 深入浅出seesion和cookie
session在计算机中,尤其是在网络应用中,称为"会话控制".session 对象存储特定用户会话所需的属性及配置信息.session跟踪是Web程序中常用的技术,用来跟踪用户的 ...
- iOS关于图片点到像素转换之杂谈
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 不管是以什么方法生成的图片,是从磁盘上读取的,还是从其他对象中 ...
- python 子包引用父包和其他子包
python 子包引用父包和其他子包 python引用子目录很简单, 里面放个__init__.py就可以了. 如何在子目录里面引用其他目录(父目录,爷目录和同辈分目录)呢? 例如: python有项 ...
- python进行md5加密
代码函数 import hashlib def md5(str): m = hashlib.md5() m.update(str) return m.hexdigest() f = open('idf ...
- Android 优质精准的用户行为统计和日志打捞方案
Android 自定义优质精准的用户行为和日志打捞方案 Tamic csdn博客 :http://blog.csdn.net/sk719887916/article/details/51398416 ...
- Uva - 1589 - Xiangqi
Xiangqi is one of the most popular two-player board games in China. The game represents a battle bet ...