leetcode461
public class Solution
{
public int HammingDistance(int x, int y)
{
int[] aryA = new int[];
int[] aryB = new int[]; int i = ;
int j = ; do
{
aryA[i] = x % ;//将10进制转换为2进制
x = x / ;
i++;
}
while (x != ); do
{
aryB[j] = y % ;//将10进制转换为2进制
y = y / ;
j++;
}
while (y != ); int result = ;
for (int k = ; k < ; k++)
{
if (aryA[k] != aryB[k])//查找对应的二进制位,如果一个是0一个是1
{
result++;
}
}
//Console.WriteLine(result);
return result;
}
}
https://leetcode.com/problems/hamming-distance/#/description
将10进制转为2进制
补充一个python的实现,使用位操作:
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
z = x ^ y
cnt =
mask =
for i in range():
t = z & mask
mask <<=
if t != :
cnt +=
return cnt
leetcode461的更多相关文章
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- 【转】Win32程序中调用ActiveX控件
#include "stdafx.h" #include <iostream> #include <windows.h> #include <comd ...
- ela的UNASSIGNED索引修复
1.查找UNASSIGNED未分片的索引: #curl -s "http://localhost:9200/_cat/shards" -u username:passwd | gr ...
- postman之如何获取cookie
1.最近在学习postman的使用方法,为了保证后续模块操作,必须在登录时获取的session值,并将其设置为环境变量,session的位置处于response headers里面返回的set-coo ...
- pacbio bax.h5文件处理及ccs计算
1.NCBI文件格式如下: 2.格式转换 (1) bas.h5 -> ccs source /share/nas2/genome/biosoft/smrtanalysis/2.3.0/smrta ...
- 学习笔记TF044:TF.Contrib组件、统计分布、Layer、性能分析器tfprof
TF.Contrib,开源社区贡献,新功能,内外部测试,根据反馈意见改进性能,改善API友好度,API稳定后,移到TensorFlow核心模块.生产代码,以最新官方教程和API指南参考. 统计分布.T ...
- freebsd安装python2
第一步 cd /usr/ports/lang/python27 第二步 输入以下命令(需要联网) make make install 到此如果输入python无效 继续 第三步 cd /usr/ ...
- 使用centos7构建本地git服务器
git的安装非常简单 直接yum install git -y即可 大概看一下我的执行步骤吧 首先在服务器上执行以下操作,完成创建仓库 mkdir -p /data/test/bp.git #创建仓库 ...
- algernon 基于golang 的独立的支持redis lua pg。。。 的web server
algernon 看到github 的介绍很很强大,一下子想到了openresty,功能看着很强大,支持 redis pg lua markdown quic http2 mysql 限速 pongo ...
- Go之unsafe.Pointer && uintptr 类型
Go语言是个强类型语言.Go语言要求所有统一表达式的不同的类型之间必须做显示的类型转换.而作为Go语言鼻祖的C语言是可以直接做隐式的类型转换的. 也就是说Go对类型要求严格,不同类型不能进行赋值操作. ...
- jQuery 点击后退(返回)执行函数
<html> <head> <meta charset="UTF-8"> <meta name="viewport" ...