[LeetCode] 461. Hamming Distance(位操作)
Description
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance.
Note:
0 ≤ x, y < 231.
Example:
Input: x = 1, y = 4 Output: 2 Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
? ? The above arrows point to positions
where the corresponding bits are different.
思路
题意:给定两个数,求出其汉明距离
题解:将两个数异或,那么二进制位上相同的都变成0,不同变成1,统计一下有多少个1即可
class Solution {
public:
//6ms
int hammingDistance(int x, int y) {
int res = 0;
while (x && y){
if ((x & 1) != (y & 1)) res++;
x >>= 1;
y >>= 1;
}
while (x){
if (x & 1) res++;
x >>= 1;
}
while (y){
if (y & 1) res++;
y >>= 1;
}
return res;
}
//3ms
int hammingDistance(int x,int y){
x ^= y;
y = 0;
while (x){
y += (x & 1);
x >>= 1;
}
return y;
}
};
[LeetCode] 461. Hamming Distance(位操作)的更多相关文章
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 4. leetcode 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode 461 Hamming Distance 解题报告
题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...
- LeetCode 461. Hamming Distance (C++)
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- Leetcode - 461. Hamming Distance n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
随机推荐
- 基于 Python 的自定义分页组件
基于 Python 的自定义分页组件 分页是网页中经常用到的地方,所以将分页功能分出来,作为一个组件可以方便地使用. 分页实际上就是不同的 url ,通过这些 url 获取不同的数据. 业务逻辑简介 ...
- [TabControl] TabControl控件的最佳实践,可以把一个窗体和用户控件添加进来
看下效果吧<ignore_js_op> 下面是一个公共的添加方法看代码 [C#] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 1 ...
- MySQL解决忘记密码问题
解决Win10下Mysql 的Access denied for user'root'@'localhost' (using password: NO)问题 mysql一旦忘记密码即会出现这样的错误. ...
- [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (5/5)
- 初步理解React
1.组件化 在 MV* 架构出现之前,组件主要分为两种: 狭义上的组件,又称为 UI 组件,比如 Tabs 组件.Dropdown 组件.组件主要围绕在交互 动作上的抽象,针对这些交互动作,利用 Ja ...
- 5-基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台
基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台 一.板卡概述 本板卡系自主研发,基于CPCI 6U架构,符合CPCI2.0标准.采用 DSP TMS320C66 ...
- 使用Varnish加速Web
通过配置Varnish缓存服务器,实现如下目标: - 使用Varnish加速后端Web服务 - 代理服务器可以将远程的Web服务器页面缓存在本地 - 远程Web服务器对客户端用户是透明的 - 利用缓存 ...
- flask之视图函数从前端接收数据的方法
一:从前端接收查询字符串 query-string 注意:get和post都可以在url后面添加查询字符串?a=1&b=2 测试工具:postman 1:get方式接收 视图函数 from ...
- springboot 集成rabbitMQ
package com.jd.ng.shiro.config.rabbitMQconfig; import com.jd.ng.shiro.rabbitMqListener.SimpleMessage ...
- centos 6.5 安装 jdk 8
首先,检查是否已安装jdk,如果有,要先删除 rpm -qa|grep java rpm -e --nodeps filename 然后,从oracle官方网站下载jdk安装包:jdk-8u121-l ...