[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 ...
随机推荐
- java 多态 (知识点不完整)
1.多态的条件 1.要有继承 2.方法重写 3.父类引用指向子类对象 多态中成员访问 1. 访问成员变量: 编译看左边,运行也看左边 f.num = 10 因为这里是父类的,看是父类Father ...
- pip源地址
pip国内的一些镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...
- jQuery 的attr()与css()的区别
attr是attribute的缩写,意思是标签属性. css是,样式,意思是元素的style样式的 我的理解是: attr是操作元素的 属性 css是操作元素的 style属性 前者可以修改&l ...
- React父子组件间的传值
父组件: import React, { Component } from 'react'; import Child from './chlid'; class parent extends Com ...
- R语言基础篇——数据对象
1.基本数据类型(numeric,logical,character,NA,double,complex,integer) 2.日期变量 常用函数 Sys.Date()-返回系统当前的日期,Sys.t ...
- C语言如何使输出的数字对齐
右对齐%numd(num是位数,比如按5位数的长度输出,num为正数则右对齐) #include <stdio.h> int main() { printf(, ); printf(, ) ...
- 怎样使一个宽为200px和高为200px的层垂直居中于浏览器中?写出CSS样式代码。
div{ height:100px; width:100px; position:absolute; top:50%; width:50%; margin-letf:-100px; margin-to ...
- java操作mongodb工具类
新建maven项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- 一、ARM
1.1 ARM 分类 1.1.1 版本号分类 以前分类的是 ARM7,ARM9... ARM11,在 ARM11 之后,就是以 Cortex 系列分类了: Cortex-R:应用在实时系统上的系列 C ...
- gbase整合mybatis出现: Cause: java.sql.SQLException: Can't convert to: binary stream
参考地址:http://mybatis-user.963551.n3.nabble.com/Map-SQL-Type-LVARCHAR-x-to-JDBC-Type-VARCHAR-globally- ...