LeetCode Hamming Distance
原题链接在这里:https://leetcode.com/problems/hamming-distance/
题目:
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.
题解:
Bits different自然而然就想到了xor.
利用Number of 1 Bits的数xor 结果的bit 1个数.
或者用Integer.bitCount() function.
Time Complexity: O(1). Space: O(1).
AC Java:
public class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x^y);
}
}
LeetCode Hamming Distance的更多相关文章
- LeetCode——Hamming Distance
LeetCode--Hamming Distance Question The Hamming distance between two integers is the number of posit ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
- [LeetCode] 477. Total 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 ...
- 【LeetCode】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
随机推荐
- code first 创建和初始化数据库
1.前言 Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码对象来生成数据库.当然我们在实 ...
- Google之Chromium浏览器源码学习——base公共通用库(三)
本节将介绍base公共通用库中的containers,其包含堆栈.列表.集合.以及Most Recently Used cache(最近使用缓存模板). linked_list.h:一个简单的列表类型 ...
- PHP "万能"输出随机字符串
<?php function getRandomCode ($length = 32, $mode = 0){ switch ($mode) { case "1": $str ...
- js常见报错之Unexpected token in JSON at position
源头 出现这个报错提示,根本原因只有一个--json解析异常,所以请大家直接去关注自己json的返回数据注意检查其返回内容和内容的格式是否正确,至于本文血案的导火索是因为json注释滴问题. 事发 ...
- Oracle两列字段替换和复制
- Unity3d uGUI适配
Cavas: 1.Render Model设置为:Screen Space- Camera.如果想在UI前面加特效我可以在创建一个摄像机(UIForward)深度大于这个UICamera就行了. 2. ...
- 【BZOJ】3991: [SDOI2015]寻宝游戏
题意 给一个\(n\)个点带边权的树.有\(m\)次操作,每一次操作一个点\(x\),如果\(x\)已经出现,则\(x\)消失.否则\(x\)出现.每一操作后,询问从某个点开始走,直到经过所有出现的点 ...
- C# CodeFirst(EF框架)代码优先创建数据库
namespace WebEF.Model{ public class ModelContext:DbContext //继承DBcontext 来自EF框架 { public ModelContex ...
- discuz sphinx全文检索搜索引擎方案
基于discuz的索引配置文件,这个配置文件比较灵活,可以根据不同的需求来配置 # # linuxTone full index search configure file # source lt_p ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...