C#LeetCode刷题之#461-汉明距离(Hamming Distance)
问题
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 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)相关推荐
- LeetCode刷题(37)--Edit Distance
类似于模式匹配,采用DP算法, class Solution(object):def minDistance(self, word1, word2):""":type w ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 1 ...
- Leetcode刷题指南和top100题目
原文链接:https://blog.csdn.net/qq_39521554/article/details/79160815 参考博文:https://blog.csdn.net/mmc2015/a ...
- 个人LeetCode刷题记录(带题目链接及解答)持续更新
Leetcode 刷题 注:~[完成]代表还有一些方法没看,最后再看 一.一些需要重刷的典型题: 1.快速排序,归并排序,堆排序(递归的思想) 2.链表中的回文链表,其中的快慢指针,多看,多练 3.链 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- 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 ...
最新文章
- linux下makefile使用
- 戴尔:发力互联互通 构建世界基础设施中心
- Hyper-V和多处理器虚机
- 怎么样才能玩转前端所有的CSS背景相关问题?
- c语言成绩转换绩点,如何将平时成绩转化为GPA成绩?
- StreamWriter打开文件报错:”不支持给定路径的格式。“
- python概念-Socket到底有多骚
- FFmpeg安卓平台编译
- 基于TensorFlow的车牌号识别系统
- R语言进行主成分分析PCA和探索性因子分析EFA的常用步骤:准备资料、选择因子模型、提取多少主成分/因子、提取主成分或者因子、旋转主成分或者因子、数据结果解读、计算主成分或因子得分(或者系数)
- SourceGenerator入门指北
- 基于vue添加刻度线比例尺
- ubuntu-20.04.3-详细安装教程(图文)附下载地址
- GPU和CPU计算速度
- 解决PageHelper版本不匹配,结果可能全部返回问题
- 手工冰粉社区forum.bingfen.com.cn上线...xiuno模版V2完善记录
- 国际高性能计算和人工智能咨询委员会公布第五届亚太区 HPC-AI竞赛结果
- Git学习系列 -- Not a git repository错误解决方法
- 动画animate的使用
- 64位微型计算机64指技术指标,智慧职教:64位微型计算机中的64,是指下列技术指标中的...