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 ...
随机推荐
- RockeMq源码之Client
Client中既有producer,也有consumer,先看前者 org.apache.rocketmq.client.producer.DefaultMQProducer.class 一般都是调用 ...
- 著名的Log4j是怎么来的?
Java在设计之初,借鉴了很多其他语言不错的特性和优点,唯独没有设计日志系统,但是日志的重要性不言而喻,一旦程序运行起来,运行结果与预期不一致,基本就是出Bug了,这个时候需要进行Bug排查,一般有两 ...
- python 2
1. 格式化输出 %s 可以代替str %d 可以代替int %f 可以代替浮点数( float ) 格式: Name = input('你的名字:') Age = int(input('你的年龄: ...
- Redis之在Linux上安装和简单的使用
我只是一个搬运工 Redis之在Linux上安装和简单的使用https://blog.csdn.net/qq_20989105/article/details/76390367 一.安装gcc 1.R ...
- crontab 相关
修改编辑器 select-editor 查看服务状态 service cron status (linux下为crond ,ubuntu为cron) 1-59/2 1,10,12 * * * ...
- GoStudy——Go语言入门第一个事例程序:HelloWorld.go
package main import ( "fmt" ) func main() { fmt.Println("Hello,world!!!--2019年4月1 ...
- node 下less无法编译的问题
vue+less的项目中,npm run dev不通过,提示以下错误: These dependencies were not found: * !!vue-style-loader!css-load ...
- [转]Java反射机制详解
目录 1反射机制是什么 2反射机制能做什么 3反射机制的相关API ·通过一个对象获得完整的包名和类名 ·实例化Class类对象 ·获取一个对象的父类与实现的接口 ·获取某个类中的全部构造函数 - 详 ...
- ubuntu18关闭系统自动更新
ubuntu18.04关闭系统自动更新有两个方法:1.修改配置文件 修改配置文件/etc/apt/apt.conf.d/10periodic#0是关闭,1是开启,将所有值改为0vi etc/apt/a ...
- MySql Scaffolding an Existing Database in EF Core
官方文档详见:https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-scaffold-exampl ...