问题

两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。

给出两个整数 x 和 y,计算它们之间的汉明距离。

注意:0 ≤ x, y < 231.

输入: x = 1, y = 4

输出: 2

解释:
1   (0 0 0 1)
4  (0 1 0 0)
        ↑   ↑

上面的箭头指出了对应二进制位不同的位置。


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.

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.


示例

public class Program {public static void Main(string[] args) {var a = 16;var b = 13;var res = GetSum(a, b);Console.WriteLine(res);a = 168;b = 136;res = GetSum2(a, b);Console.WriteLine(res);Console.ReadKey();}public static int GetSum(int a, int b) {//按位取异或int result = a ^ b;//判断是否需要进位int forward = (a & b) << 1;if(forward != 0) {//如有进位,则将二进制数左移一位,进行递归return GetSum(result, forward);}return result;}public static int GetSum2(int a, int b) {while(b != 0) {int carry = a & b;a = a ^ b;b = carry << 1;}return a;}}

以上给出2种算法实现,以下是这个案例的输出结果:

True
False

分析:

显而易见,IsPowerOfTwo 的时间复杂度为:  ,IsPowerOfTwo2 的时间复杂度为:  。

C#LeetCode刷题之#461-汉明距离​​​​​​​(Hamming Distance)相关推荐

  1. LeetCode刷题(37)--Edit Distance

    类似于模式匹配,采用DP算法, class Solution(object):def minDistance(self, word1, word2):""":type w ...

  2. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 1 ...

  3. Leetcode刷题指南和top100题目

    原文链接:https://blog.csdn.net/qq_39521554/article/details/79160815 参考博文:https://blog.csdn.net/mmc2015/a ...

  4. 个人LeetCode刷题记录(带题目链接及解答)持续更新

    Leetcode 刷题 注:~[完成]代表还有一些方法没看,最后再看 一.一些需要重刷的典型题: 1.快速排序,归并排序,堆排序(递归的思想) 2.链表中的回文链表,其中的快慢指针,多看,多练 3.链 ...

  5. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  6. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  7. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  8. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  9. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  10. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

最新文章

  1. linux下makefile使用
  2. 戴尔:发力互联互通 构建世界基础设施中心
  3. Hyper-V和多处理器虚机
  4. 怎么样才能玩转前端所有的CSS背景相关问题?
  5. c语言成绩转换绩点,如何将平时成绩转化为GPA成绩?
  6. StreamWriter打开文件报错:”不支持给定路径的格式。“
  7. python概念-Socket到底有多骚
  8. FFmpeg安卓平台编译
  9. 基于TensorFlow的车牌号识别系统
  10. R语言进行主成分分析PCA和探索性因子分析EFA的常用步骤:准备资料、选择因子模型、提取多少主成分/因子、提取主成分或者因子、旋转主成分或者因子、数据结果解读、计算主成分或因子得分(或者系数)
  11. SourceGenerator入门指北
  12. 基于vue添加刻度线比例尺
  13. ubuntu-20.04.3-详细安装教程(图文)附下载地址
  14. GPU和CPU计算速度
  15. 解决PageHelper版本不匹配,结果可能全部返回问题
  16. 手工冰粉社区forum.bingfen.com.cn上线...xiuno模版V2完善记录
  17. 国际高性能计算和人工智能咨询委员会公布第五届亚太区 HPC-AI竞赛结果
  18. Git学习系列 -- Not a git repository错误解决方法
  19. 动画animate的使用
  20. 64位微型计算机64指技术指标,智慧职教:64位微型计算机中的64,是指下列技术指标中的...

热门文章

  1. 【AI视野·今日CV 计算机视觉论文速览 第179期】Tue, 25 Feb 2020
  2. 使用c#对xml文件进行解析 功能演示 161483724
  3. 数组算法 中部删除数据 1202
  4. 演练 制作爱奇异视频播放列表 0929
  5. fastdfs-客户端配置
  6. mysql报错Attempted to open a previously opened tablespace的解决办法
  7. Shiro的鉴权方式
  8. Junit3.8源码--核心类
  9. C# 利用位运算传递多个参数方法
  10. 4-asa-url-filter