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 ...
随机推荐
- ipone 5s上,字体rem遇到的问题
webapp中,12px的字体,利用rem实现自适应布局, 发现只有在ipone 5s中字体超大, 这两个class元素中字体一样大小,发现上面元素字体在ipone 5s中很大, 后来验证问题在哪里, ...
- 企业面试题-find结合sed查找替换
题:把/oldboy目录及其子目录下所有以扩展名.sh结尾的文件中包含oldboy的字符串全部替换成oldgirl 解答: 建立测试数据: [root@tan data]# mkdir /oldboy ...
- redis设置过期时间
- ASP.NET中出现内存溢出错误System.OutOfMemoryException
原因1:数据库服务器性能问题导致内存不够用,从而引起内存溢出 原因2:在IIS的应用程序池中进行配置,引起IIS服务器的内存分配问题,从而引起内存溢出 分析: 32位操作系统的寻址空间是 ...
- cocos2dx开发之util类&方法——字符串替换
/*将originStr字符串中的searchStr替换成replaceStr*/ std::string str_replace(std::string originStr,std::string ...
- 开源WHMCS支付宝当面付和即时到账插件
开源WHMCS支付宝当面付和即时到账插件 链接: https://pan.baidu.com/s/1i5HU4hn 密码: crq7
- JS 验证字符串是否为空
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 斐波那契数列中获取第n个数据值
class Fibonacci { /** * Description:迭代方法获取fibonacci第n项数值 * * @param int $n * @return int */ public s ...
- bvlc_reference_caffenet网络权值可视化
一.网络结构 models/bvlc_reference_caffenet/deploy.prototxt 二.显示conv1的网络权值 clear; clc; close all; addpath( ...
- Springboot 允许跨域访问
服务提供段Application.java中添加如下代码: @Beanpublic CorsFilter corsFilter() { UrlBasedCorsConfigurationSource ...