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 ...
随机推荐
- B -- RE:从零开始的异世界生活 线段树
http://www.ifrog.cc/acm/problem/1117?contest=1016&no=1 其实我是第一次这样用线段树. 首先把所有出现过的数字全部离散化.那么数字就是从[1 ...
- (转)mysqldump: Got error: 1556: You can't use locks with log tables.
mysqldump: Got error: 1556: You can't use locks with log tables. 原文:http://blog.51cto.com/oldboy/112 ...
- Corn 表达式
CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...
- ExpandoObject的使用
IDictionary<string, object> obj = new System.Dynamic.ExpandoObject(); obj.Add(); dynamic obj2 ...
- Hi,bro
这是我第一次写部落格,也是我刚开始学python,希望我以后能把To Do List 做好,也希望大家可以好好学习,为了以后good life去努力,Do SomeThing OK?
- java中循环的不同终止方式
1.break:直接强行跳出当前循环,不再执行剩余代码.但在多重循环的情况下,若break在内层循环中,则仅仅终止了内层循环,外循环照常执行. 2.continue:仅仅终止此次循环. 3.retur ...
- iOS 应用架构 (三)
iOS 客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答 iOS 应用架构中的种种问题,本文是其中的第二篇,主要讲 View 层的组织和调用方案.下篇主要讨论做 Vi ...
- uvm_mem——寄存器模型(十二)
看完了寄存器,再来看看存储器: //------------------------------------------------------------------------------ // ...
- freebsd问题
http://community.spiceworks.com/topic/91708-server-freezes
- vue.js的package.json相关问题解惑
使用vue-cli创建vue.webpack项目,在项目中用到了iSlider移动端滑动插件,只在本地命令工具中npm install islider.js:提交之后,partner下载代码后一直运行 ...