LeetCode- Bit Manipulation LeetCode总结(1) —— 位运算
来源:互联网 发布:玄空飞星软件下载 编辑:程序博客网 时间:2024/05/18 00:43
Bit Manipulation(位运算):主要思想参考《剑指Offer》第二章2.4.3节。
扩展:
201 Bitwise AND of Numbers Range http://blog.csdn.net/xsloop/article/details/47002421
一共五种运算:与,或,异或,左移,右移。
常用技巧:
(1)n & (n-1)能够消灭n中最右侧的一个1。
(2) 右移:除以2, 左移:乘以2。
(3)异或性质:交换律,0^a=a, a^a=0;
(3)将常用字符、数字等均转为按位运算,可以节约空间。
LeetCode题目解析
191 Number of 1 Bits http://blog.csdn.net/xsloop/article/details/46997851
- 使用右移。
- 使用n&(n-1)可以消灭一个1的性质来求解。
231 Power of Two http://blog.csdn.net/xsloop/article/details/47000827
使用n&(n-1)=0来判断。
注意0和负数的情况。
190 Reverse Bits http://blog.csdn.net/xsloop/article/details/47000917
使用右移和左移。
169 Majority Element http://blog.csdn.net/xsloop/article/details/46999037
求解出现次数大于n/2的数字。
- 按照顺序循环,如果两个元素不相等,则删除这两个元素。则剩下的为出现大于n/2次的那个元素。
- 使用sort的解决方法,耗时48ms。
- 不进行实质的删除操作,只是做mark而已。
- 位运算方法:统计32位每个位上出现1的次数大于n/2的,如果是则直接赋值为1,否则为0。
- 其他算法
136 Single Number http://blog.csdn.net/xsloop/article/details/46998819
数组中,只有一个数出现一次,其余数均出现两次。求出现一次的那个值。
了解异或的三个性质:
//主要思想:
// 异或满足交换律,a1 ^ a2 ^ .... ^ an ^ a1 ^ .... ^ an-1 = (a1^a1)^(a2 ^a2) ^(....) ^(an-1 ^ an-1) ^ an
// a ^ a = 0;
// a ^ 0 = a; 显然将所有数字异或后,得到的结果即为出现一次的值。
有两个出现一次的数字,其余都出现两次。找出这两个出现一次的数字。
- 将所有数字做异或运算,按照异或结果中1的位置将数组分成两组。(因为1的位置表示有不同的取值)
- 对两组分别求一次异或,值不为0,则为求得的结果。
137 Single Number II http://blog.csdn.net/xsloop/article/details/47000471
找出现一次的元素,其余元素均出现三次。
将每个元素的每一位分别相加,取模3,则会得到该位的取值,原理类似于(3(a1+a2+...+an)+a)%3 = a;
扩展:
可以扩展到K(元素出现K次,只有一个出现一次)的做法:
1、K为偶数:参考 Single Number 的做法;
2、K为奇数:参考 Single Number II 的做法;
78 Subsets http://blog.csdn.net/xsloop/article/details/47003033
求全组合。
对于全组合,也就是对于N个元素,如果元素出现则用1表示,不出现则用0;
那么,得到的组合序列是2^N, 其中0表示所有元素都不取,1表示只取第一个,。。。。。
这样可以位运算的方法得到一个数组中的所有组合。
201 Bitwise AND of Numbers Range http://blog.csdn.net/xsloop/article/details/47002421
给两个数M, N, (0<=M <=N<=INT_MAX),求M,N中所有数字的按位与运算。
将M和N分别按位移动,直到M和N相等,记录移动的位数count,则返回值为M<<count;
如果M和N始终不相等,则返回0;
187 Repeated DNA Sequences http://blog.csdn.net/xsloop/article/details/47004769
使用位运算计算DNA序列中重复的10个字符组合。
DNA只有四个字符,分别用00, 01, 10, 11表示;
10个字符需要2^20位,2^20 = 1024 *1024,则只需要即可1024*1024的哈希表即可。
对于字符串,从头开始以10个字符为单位,统计出现的结果放入哈希表中,当出现次数大于1则保存到重复dna序列中。
0 0
- LeetCode- Bit Manipulation LeetCode总结(1) —— 位运算
- leetcode第九周解题总结(Bit Manipulation位运算)
- Leetcode bit manipulation 总结
- Leetcode.Bit Manipulation.位运算专题.(持续更新)
- Leetcode Bit Manipulation 题型总结
- leetcode Bit Manipulation
- Leetcode:bit manipulation
- LeetCode--Bit Manipulation
- LeetCode系列Medium【1】 Algorithms - Bit manipulation
- LeetCode系列Easy【1】 Algorithms - Bit manipulation
- 【Leetcode】Single Number (Bit Manipulation)
- [leetcode] Summary of Bit Manipulation
- LeetCode总结,位运算总结(该)
- LeetCode总结,位运算总结
- (位运算总结)A summary: how to use bit manipulation to solve problems easily and efficiently
- 【Leetcode】Single Number II (Bit Manipulation)
- LeetCode之Bit Manipulation题目汇总
- [LeetCode]--342. Power of Four (Bit Manipulation)
- Fragment实现Tab
- lua中的io
- Hadoop学习之MapReduce框架
- 如何使用devstack配置openstack(单节点)
- mysql数据恢复(通过日志)
- LeetCode- Bit Manipulation LeetCode总结(1) —— 位运算
- HDU 5294 Tricks Device(最短路+最小割)
- HTTP Content-type整理
- The constructor Intent(new View.OnClickListener(){}, Class<ButtonStudyActivity>) is undefined
- 图片点击放大左右切换
- Spring事务配置的5中方式
- cc.net使用subversion(SVN)报错
- spring mvc Controller中使用@Value无法获取属性值
- 从一个MySQL left join优化的例子加深对查询计划的理解