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 ...
随机推荐
- Jenkins+Gitlab+Ansible自动化部署(二)
接Jenkins+Gitlab+Ansbile自动化部署(一):https://www.cnblogs.com/zd520pyx1314/p/10210727.html Ansible的配置与部署 工 ...
- DevExpress GridControl 控件二表连动
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Typora--Draw Diagrams With Markdown
Typora Typora supports some Markdown extension for diagrams, you could enable this feature from pref ...
- Strut2 Action的生命周期
一般而言,Action都是放在Spring容器中管理的,我会把属性设为prototype,这样,每一个请求,都会创建一个action对象. 今天碰到一个问题,当我用从一个jsp页面中输入一个属性,比如 ...
- 集合(List、Set)
第19天 集合 第1章 List接口 我们掌握了Collection接口的使用后,再来看看Collection接口中的子类,他们都具备那些特性呢? 接下来,我们一起学习Collection中的常用几个 ...
- canvas雪花特效-jQuery插件实现
这是一款效果十分逼真的html5 canvas下雪场景动画特效插件.这款下雪特效是基于Jason Brown的Snowfall jquery plugin的基础上制作的.在Snowfall jquer ...
- AJAX不能访问MVC后台程序的问题
AJAX不能访问后台的MVC有可能是MVC的后台程序加入了身份验证[Authorize]标记,这样前台的AJAX虽然结果显示的是4和200但是responsetext的值可以看到是返回了一个配置文件中 ...
- 【UML】部署图Deployment diagram(实现图)(转)
http://blog.csdn.net/sds15732622190/article/details/49049665 前言 下面要介绍UML中的部署图,和构件图一样,它也属于实现图的一种,五种静态 ...
- 《队长说得队》第九次团队作业:Beta冲刺与验收准备
项目 内容 这个作业属于哪个课程 >>2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 >>实验十三 团队作业9:Beta冲刺与团队项目验收 团队名 ...
- VMware vSphere6.0 服务器虚拟化部署安装图解
一 VMware vSphere部署的前期规划要点 1 vSphere的优点 (略) 2 如何利用现在的设备架构虚拟化环境 在虚拟化过程中,用户大多会考虑目前现有的服务器.存储.交换机等基础设备是否可 ...