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解法 方法一:异或 + 字符串分割 方法二: ...
随机推荐
- xml Schema 基础
Schema比DTD好在哪儿? 后者简单易用,前者功能更强大也更复杂.DTD可以定义XML文档的结构,但无法对XML元素的内容进行约束,例如,如果希望某个XML元素的内容只能是日期型的数据,DTD就无 ...
- [译]:Orchard入门——手动安装Orchard
原文链接:Manually Installing Orchard 文章内容基于Orchard 1.8版本 本文将演示通过zip安装包安装Orchard所需要的步骤. 本文包括如下三种不同的Orchar ...
- Reversing Bits in C
英文原文: http://corner.squareup.com/2013/07/reversing-bits-on-arm.html 本文讲解翻转bit位的一些方法,例如如何将1001 1101变为 ...
- SharePoint 2013 Apps TokenHelper SharePointContext OAuth Provider-Hosted App (抄袭,测试 csc.rsp 用)
namespace Microshaoft.SharePointApps { using Microsoft.IdentityModel; using Microsoft.IdentityModel. ...
- 防止 jsp被sql注入的五种方法
一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...
- VR寒冬AR暖春,以色列AR公司再获3000万美元融资
据统计,2015年国内至少有近70家VR公司获得天使或者A轮投资,不过狂欢并没有持续太久.2016年即将结束,资本寒冬和VR头盔的出货远不如预期,让投资者放慢了步伐,不过AR领域的热度依然不减.近日, ...
- SpringMVC核心分发器DispatcherServlet分析[附带源码分析]
目录 前言 DispatcherServlet初始化过程 DispatcherServlet处理请求过程 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不 ...
- T4学习资料
网址:http://www.olegsych.com/2007/12/text-template-transformation-toolkit/
- android studio的Beta, Canary, Dev, Stable四种Channel版本介绍、分析与选择
一.概述 在Android Studio下载官网上,有如下介绍: ` Android Studio's built-in update mechanism can be set to receive ...
- 如何完全卸载oracle11g?
步骤一: 停止Oracle的所有服务. 步骤二: 运行%oracle_home%\app\Administrator\product\11.2.0\dbhome_1\deinstall \deinst ...