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 ...
随机推荐
- Python Generator 运行细节验证
今天来__next__和send, 改天来throw和close class A: def __setattr__(self, key, val): print('set %s to %s'%(key ...
- Java中的泛型类和泛型方法区别和联系
泛型的概念大家应该都会,不懂的百度或者google,在java中泛型类的定义较为简单 <span style="font-size:18px;"><span st ...
- Dynamics CRM2016 Web API之删除
相比之前的增改查,删除就显得简单的多了. 这里的request的type为delete,删除成功的status为204,404则是要删除的记录不存在 var id = 'BAD90A95-7FEA-E ...
- Git运用基础之如何删除Github上不想要的项目
今天突然想删除,(强迫症想删除)之前练习时多创建的多个Github上的源代码或者无用Demo地址,然后看了一些文章都比较老式,这里我展示一下最新的删除步骤. 一.首先登录自己的Github账户主页(没 ...
- android的activity栈管理
在进行BlackBerry程序开发的时候,BlackBerry提供了一个管理Screen的栈,用来从任何地方来关闭位于最上一层的Screen,使用UiApplication.getUiApplicat ...
- 2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程
1 Lucen目录介绍 2 lucene-core-3.6.2.jar是lucene开发核心jar包 contrib 目录存放,包含一些扩展jar包 3 案例 建立第一个Lucene项目 ...
- TCP的发送系列 — 发送缓存的管理(一)
主要内容:TCP发送缓存的初始化.动态调整.申请和释放. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 数据结构 TCP对发送缓存的管理是在两个层面上进 ...
- Android开发学习之路--RxAndroid之操作符
学习了RxAndroid的一些基本知识,上篇文章也试过了RxAndroid的map操作符,接着来学习更多的操作符的功能吧. 操作符就是为了解决对Observable对象的变换的问题,操作符用于 ...
- 深入解剖unsigned int 和 int
就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种: 1.signed----有符号,可修饰char.int.Int ...
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
应用场合:参考网上查询数据表的所有字段名代码,使用游标生成指定单个表的所有字段名跟逗号组成的用于select 逗号隔开的字段名列表 from字符串等场合. 查询结果输出如下: 当前数据表TB_UD_ ...