leetcode-27-exercise_bit maniputation
461. Hamming Distance
解题思路:
把两个数的每一位和1比较,如果结果不同说明这两位不同。要比较32次。
int hammingDistance(int x, int y) {
int result = 0;
for (int i = 0; i < 32; i++) {
if (((x>>i)&0x1) != ((y>>i)&0x1)) {
result ++;
}
}
return result;
}
477. Total Hamming Distance
解题思路:
因为数据是从0到10^9的,所以可以转化为31位二进制数(10^9 = (10^3)^3 ~ (2^10)^3 = 2^30)。对于所有数的每一位,
计算该位置上1的个数和0的个数,那么,这一位的总差异数应该是二者之积。取每一位的话,可以用右移来取。
int totalHammingDistance(vector<int>& nums) {
int result = 0;
int ones = 0;
for (int i = 0; i < 31; i++) {
for (int j = 0; j < nums.size(); j++) {
if ((nums[j] >> i) & 0x1)
ones ++;
}
result += ones * (nums.size() - ones);
ones = 0;
}
return result;
}
78. Subsets
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
解题思路:
首先,子集的数量应该是2^n。注意创建这种结构的写法。
对于result来说,nums的每一个元素都可能存在也可能不存在。如果把j写成二进制,如果第i位为1,就可以把nums[i]
放入result[j]。
vector<vector<int>> subsets(vector<int>& nums) {
int size = pow(2, nums.size());
vector<vector<int> > result(size, vector<int>{});
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < size; j++) {
if (j >> i & 0x1)
result[j].push_back(nums[i]);
}
}
return result;
}
201. Bitwise AND of Numbers Range
解题思路:
要想确定整个范围内的数,转换为二进制时各个位置是否全为1,全部写出来是没有必要的。注意,此处只需要找出起始数共同的前缀就好了,
因为两个数可以修改后面的部分,必然会存在有0的位置,所以通过右移找出共同前缀,记录右移次数,再左移回来就好。
int rangeBitwiseAnd(int m, int n) {
int i = 0;
while (m != n) {
m = m >> 1;
n = n >> 1;
i ++;
}
return (m << i);
}
187. Repeated DNA Sequences
解题思路:
使用unordered_map。其中size_t是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。
用hash存子串,节省查找时间。如果子串重复次数等于2,就保留。如果这个子串重复次数多于2了,那肯定保存过了,就不用管了。
vector<string> findRepeatedDnaSequences(string s) {
vector<string> result;
if (s.length() <= 10)
return result;
hash<string> h;
unordered_map<size_t, int> m;
for (int i = 0; i <= s.length() - 10; i++) {
string sub = s.substr(i, 10);
m[h(sub)] ++;
if (m[h(sub)] == 2) {
result.push_back(sub);
continue;
}
}
return result;
}
leetcode-27-exercise_bit maniputation的更多相关文章
- 包、继承以及 LeetCode 27、28题
1 package.import 和 import static 1.1 Package Java 引入了包(Package)机制,提供了类的多层命名空间,用于解决类的命名冲突.类文件管理问题.Jav ...
- 前端与算法 leetcode 27.移除元素
目录 # 前端与算法 leetcode 27.移除元素 题目描述 概要 提示 解析 算法 @(目录) # 前端与算法 leetcode 27.移除元素 题目描述 27.移除元素 概要 题目本身其实挺简 ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- Java实现 LeetCode 27 移除元素
27. 移除元素 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额 ...
- leetcode 27
27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- LeetCode(27)题解:Remove Element
https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of th ...
- LeetCode 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
随机推荐
- Mysql数据表的增删改查
---恢复内容开始--- Mysql数据表的增删改查 1.创建表 语法:CREATE TABLE 表名(字段1,字段2,字段3.......) CREATE TABLE `users` ( `us ...
- 微软官方NET Core 2.0
NET Core 2.0 微软官方发布的.NET Core 2.0相关的博客: Announcing .NET Standard 2.0 Announcing .NET Core 2.0 F# and ...
- NET Core应用中实现与第三方IoC/DI框架的整合?
NET Core应用中实现与第三方IoC/DI框架的整合? 我们知道整个ASP.NET Core建立在以ServiceCollection/ServiceProvider为核心的DI框架上,它甚至提供 ...
- ms sqlserver 清除数据库日志脚本
USE [master] GO ALTER DATABASE F360DW SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE F360DW SET ...
- [FACT_采购信息]增加了延期天数
[延期天数]是指的采购单上的货品交货日期 减 [厂家来货]单据货品第一次到货日期. [FACT_采购信息] SELECT p.[Purchase_ID] [采购单号ID], p.[Supply_No] ...
- Java编码优化
Java编码优化 1.尽可能使用局部变量 调用方法时传递的参数以及在调用中创建的临时变量都保存在栈中速度较快,其他变 量,如静态变量.实例变量等,都在堆中创建,速度较慢.另外,栈中创建的变量,随 着方 ...
- c#ADSL拨号类
class ADSLHelper { /// <summary> ///拨号 /// </summary> /// <param name="connectio ...
- sql 2008 中不能创建数据库关系图
执行以下命令: ALTER AUTHORIZATION ON DATABASE::[databasename] TO sa [databasename] 为数据库名: 此方法借鉴于<老高> ...
- 织梦DeDeCMS友情链接文字显示不全
文件:/include/taglib/flink.lib.php 把下面代码中的24改为合适的值 $attlist=”type|textall,row|24,titlelen|24,linktype| ...
- 使用java来压缩图片
使用java来压缩图片,简单几句,清清爽爽 使用0.3的压缩比得到的结果如下(从2.8M压缩到268K,且图片的清晰度看不出明显差别): package carlspringtest; import ...