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 ...
随机推荐
- 命令行下载工具 wget
wget 是一个简单而强大的跨平台命令行下载工具,包括 Windows 也有对应的版本.全称 GNU Wget,属于 GNU 计划的一部分,自由软件.支持 HTTP.HTTPS 和 FTP 协议,可在 ...
- Java的常量接口思考,项目中的常量是放在接口里还是放在类里呢?
最近在看一本书 Java与模式,里面提了一句不建议使用常量接口,甚至举了个java源码的反例, 蛋疼的是没有说为什么? 查了网上一圈发现他们也是知道怎么做而不知道为什么这么做. 然后我只能找谷歌了,翻 ...
- Python网络编程中的服务器架构(负载均衡、单线程、多线程和同步、异步等)
这篇文章主要介绍服务器架构. 网络服务需要面对两个挑战. 第一个问题是核心挑战,要编写出能够正确处理请求并构造合适响应的代码. 第二个挑战是如何将网络代码部署到随系统自动启动的Windows服务或者是 ...
- 关于Identityserver4和IdentityServer3 授权不兼容的问题
使用IdentityServer3 作为授权服务器,如果没有设置证书,而且client又没有设置AccessTokenType = AccessTokenType.Reference,则获取token ...
- Java分页下载
需求.提供公共的可以按照一定条件查询出结果,并提供将查询结果全部下载功能(Excel.CSV.TXT),由于一次性查出结果放到内存会占用大量内存.需要支持分页模式查询出所有数据. 实现思路 1.在公共 ...
- css隐藏元素的几种方法与区别
css隐藏元素的几种方法与区别 一:display:none;隐藏不占位 display 除了不能加入 CSS3 动画豪华大餐之外,基本效果卓越,没什么让人诟病的地方. 二:position:abso ...
- JMeter配置元件作用域
- SQL 字符串函数
http://www.w3cschool.cn/sql/sql-string-functions.html SQL 字符串函数 序号 Name Description 备注 1 ASCII() 返回最 ...
- Delphi7使用二维码
参考:http://jingyan.baidu.com/article/e75057f2ad6481ebc81a897b.html 首先下载对应的 dll (已经上传到博客园文件) 然后就是Delph ...
- oracle 11g r2安装
安装环境:windows 7 安装版本:Oracle_win32_11gR2 目的:用于模拟服务器环境,学习Oracle操作 1. 下载oracle 11g r2,下载地址:www.oracle.co ...