C#LeetCode刷题之#191-位1的个数(Number of 1 Bits)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4052 访问。
编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
输入: 11
输出: 3
解释: 整数 11 的二进制表示为 00000000000000000000000000001011
输入: 128
输出: 1
解释: 整数 128 的二进制表示为 00000000000000000000000010000000
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
Input: 11
Output: 3
Explanation: Integer 11 has binary representation 00000000000000000000000000001011
Input: 128
Output: 1
Explanation: Integer 128 has binary representation 00000000000000000000000010000000
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4052 访问。
public class Program {
public static void Main(string[] args) {
var n = 11U;
var res = HammingWeight(n);
Console.WriteLine(res);
n = 128U;
res = HammingWeight2(n);
Console.WriteLine(res);
Console.ReadKey();
}
public static int HammingWeight(uint n) {
//10进制转2进制,除2取余法
var count = 0;
while(n != 0) {
if(n % 2 != 0) count++;
n /= 2;
}
return count;
}
public static int HammingWeight2(uint n) {
//基本思路同 HammingWeight
var count = 0;
while(n != 0) {
//10进制的1,2,3,4,5对应于2进制的1,10,11,100,101
//由于2进制的特点,末位数在1,0之间循环
//用 n 和 1 做“与运算”若值为1,必为奇数
//即除2余1
if((n & 1) == 1) count++;
//2进制右移1位即10进制除2
n >>= 1;
}
return count;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4052 访问。
3
1
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#191-位1的个数(Number of 1 Bits)的更多相关文章
- leetcode刷题笔记191 位1的个数
题目描述: 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例: 输入: 输出: 解释: 32位整数 的二进制表示为 . 题目分析: 判断3 ...
- [Swift]LeetCode191. 位1的个数 | Number of 1 Bits
Write a function that takes an unsigned integer and return the number of '1' bits it has (also known ...
- C#LeetCode刷题之#258-各位相加(Add Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3860 访问. 给定一个非负整数 num,反复将各个位上的数字相加 ...
- C#LeetCode刷题之#374-猜数字大小(Guess Number Higher or Lower)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3993 访问. 我们正在玩一个猜数字游戏. 游戏规则如下: 我从 ...
- C#LeetCode刷题之#9-回文数(Palindrome Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3840 访问. 判断一个整数是否是回文数.回文数是指正序(从左向右 ...
- C#LeetCode刷题之#202-快乐数(Happy Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3856 访问. 编写一个算法来判断一个数是不是"快乐数& ...
- C#LeetCode刷题之#507-完美数(Perfect Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...
- C#LeetCode刷题之#268-缺失数字(Missing Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4056 访问. 给定一个包含 0, 1, 2, ..., n 中 ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
随机推荐
- Ethical Hacking - GAINING ACCESS(8)
Server Side Attacks NeXpose - configure and launch a scan Configure and initialize the application. ...
- OSCP Learning Notes - Post Exploitation(2)
Windows Post Exploitation Target Server: IE8-Win 7 VM 1. Download and upload the fgdump, PwDump7, wc ...
- P1290 欧几里德的游戏(洛谷)
欧几里德的两个后代 Stan 和 Ollie 正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数 M 和 N,从 Stan 开始,从其中较大的一个数,减去较小的数的正整数倍,当然, ...
- Python基础点记录1
1 变量:一个变量就是一个单词,只有一个单一的值 1 Python里面的数据类型 interage , floats , booleans , String等 2 Python是一个区分大小写的语言 ...
- Python过滤掉numpy.array中非nan数据实例
代码 需要先导入pandas arr的数据类型为一维的np.array import pandas as pd arr[~pd.isnull(arr)] 补充知识:python numpy.mean( ...
- spring学习(一)spring简介
Spring简介: Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AOP(Asp ...
- List接口(动态数组)
List接口(动态数组) List集合类中元素有序且可重复 ArrayList(重要) 作为List接口的主要实现类 线程不安全的,效率高 底层使用Object[] elementData数组存储 A ...
- 看完这篇。再也不怕被问 HandlerThread 的原理
HandlerThread是什么 官网介绍 A Thread that has a Looper. The Looper can then be used to create Handlers. No ...
- 第九章 Lambda&方法引用
9.1.Lambda表达式 9.1.1.标准格式 (形式参数) -> {代码块} 9.1.2.使用前提 有一个接口并且接口中有且仅有一个抽象方法 9.1.3.常见应用 9.1.3.1.无参无返回 ...
- PHP array_search() 函数
实例 在数组中搜索键值 "red",并返回它的键名: <?php$a=array("a"=>"red","b" ...