惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
I
InfoQ
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园_首页
罗磊的独立博客
V
V2EX
李成银的技术随笔
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
True Tiger Recordings
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
F
Fox-IT International blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
Microsoft Research Blog - Microsoft Research
Know Your Adversary
Know Your Adversary
爱范儿
爱范儿
The Register - Security
The Register - Security
G
Google Developers Blog
The Hacker News
The Hacker News
Malwarebytes
Malwarebytes
S
Securelist
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
博客园 - 叶小钗
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 聂微东
T
Threatpost
博客园 - 【当耐特】
D
Docker
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
V
Visual Studio Blog
C
Cisco Blogs
IT之家
IT之家
S
Security Archives - TechRepublic
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志

浮华生

Elasticsearch 检索性能优化 舆情监控系统综述 2024 半年度总结 2023 年终总结 - 浮华生 异地机器组网方案 Kubernetes 部署 Elasticsearch 和 Kibana 2022 年终总结 RabbitMQ connection channel 的关系 Kafka Java 客户端 Producer 原理分析 RabbitMQ 和 Kafka 应用原理简单对比 - 浮华生 阿里云 OpenSearch 介绍 Golang Array 和 Slice 区别 - 浮华生 Mac OS 下打造 golang nvim 编程环境之基础配置 电商搜索技术总结 电商搜索业务总结 2021 年终总结 Cypress 实践总结 年终总结 关于我 使用 cucumber 进行行为驱动开发(BDD) 微服务应用集成 SpringCloud 步骤 电商搜索数据同步方案 - 浮华生 通过一道数值转换题重温计算机补码 - 浮华生 macOS 系统推荐的一些软件 - 浮华生 DevOps 实施规划(持续更新) rabbitmq 如何提高可靠性并保证消费端幂等 AMQ Model总结 结对编程 RSocket 介绍 面向对象的理解 企业平台技术框架 对创业的思考 - 浮华生 知难行易 - 浮华生 一年工作经验总结 - 浮华生 我与领域驱动之缘 TDD 中使用的工具 tf-idf 算法 gitlab 添加代码规范检测 query 改写 阶段性总结 操作系统作用 - 浮华生 无标题 ElasticSearch API 基本操作 elasticsearch 集群容错 ElasticSearch 基础概念 技术选型怎么做 条件概率、全概率与贝叶斯公式 年终总结 迁移到 ubuntu18 的问题及配置 - 浮华生 总结 使用 psi-probe 监控 Tomcat Tomcat 远程 Debug jstack 死循环和死锁定位 jmap & mat 内存溢出 JVM 常用参数查看 - 浮华生 周总结(8.13-8.19) - 浮华生 周总结(7.30-8.4) 使用移位运算符 master 公式 - 浮华生 VMware 12 NAT网络下配置 ubuntu 16.04 LTS 系统静态 IP - 浮华生 关于进制的计算 项目总结 第三篇 项目总结 第二篇 editor.md 富文本编辑器的使用 项目总结 第一篇 - 浮华生 2017至今总结 谈谈微服务 单例模式 - 浮华生 tor 使用 - 浮华生 归档 搜索 - 浮华生 搜索 && 推荐
hamming-distance - 浮华生
2019-05-28 · via 浮华生

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.

Consideration

This problem is also have a relationship with ‘^’ , Think about it :

1(0001) and 4(0100) their Xor is 5(0101) . next we use & Operator to calculate the number of 1 , let the binary & 1 ,if result is 1 ,sum’s up , use » to move the postion.

5(0101)
0&1  = 0            pass
01&01 = 1           sum
010&001 = 0         pass
0101&0001 = 1       sum

so,the anwser is 2

Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public int hammingDistance(int x, int y) {
	         
         int sum = 0;
         int xor = x ^ y;
	 
	    for(int i = 0;i<32;i++){
	    	
             sum+=(xor>>i)&1;
	    }
	       
		return sum;
	    }
}

tips: if you don’t understand the Xor ,you can see my older article which about Xor .and the flowing is about Hamming Distance from wiki.

461. Hamming Distance

3-bit binary cube for finding Hamming distance

461. Hamming Distance

Two example distances: 100→011 has distance 3; 010→111 has distance 2

The minimum distance between any two vertices is the Hamming distance between the two binary strings.