[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 as the Hamming weight).

Example 1:

Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three ‘1‘ bits.

Example 2:

Input: 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one ‘1‘ bit.

Example 3:

Input: 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one ‘1‘ bits.

Note:

  • Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2‘s complement notation. Therefore, in Example 3 above the input represents the signed integer -3.

Follow up:

If this function is called many times, how would you optimize it?



编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。

示例 1:

输入:00000000000000000000000000001011
输出:3
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 ‘1‘。

示例 2:

输入:00000000000000000000000010000000
输出:1
解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 ‘1‘。

示例 3:

输入:11111111111111111111111111111101
输出:31
解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 ‘1‘。

提示:

  • 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
  • 在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在上面的 示例 3 中,输入表示有符号整数 -3

进阶:
如果多次调用这个函数,你将如何优化你的算法?



12ms

 1 class Solution {
 2     func hammingWeight(_ n: UInt32) -> UInt32 {
 3         var n = n
 4         var res: UInt32 = 0
 5         for i in 0..<32
 6         {
 7             res += (n & 1)
 8             n = n >> 1
 9         }
10         return res
11     }
12 }

原文地址:https://www.cnblogs.com/strengthen/p/10180164.html

时间: 2024-10-07 16:24:25

[Swift]LeetCode191. 位1的个数 | Number of 1 Bits的相关文章

[Swift]LeetCode233. 数字1的个数 | Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. Example: Input: 13 Output: 6 Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 给定一个整数 n,计算所有小于等于 n 的非负整数中数字

leetcode191. 位1的个数

编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 00000000000000000000000000001011 示例 2: 输入: 128 输出: 1 解释: 整数 128 的二进制表示为 00000000000000000000000010000000 int hammingWeight(uint32_t n) { uint32_t bit = 0; while(

[LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight). 输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数. 解析 消除最后的1 观察一下 n 与 n-1 这两个数的二进制表示:对于 n-1 这个数的二进制来说,相对于 n 的二进制,它的最末位的一个 1 会变成 0,最末位一个 1

[LeetCode] Number of 1 Bits 位1的个数

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four

位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. (Easy) 分析: 数字相关题有的可以考虑用位运算,例如&可以作为筛选器. 比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0) 代码: 1 class Solution { 2 public: 3 bool isP

LeetCode Number of 1 Bits 计算1的个数

题意:提供一个无符号32位整型uint32_t变量,返回其二进制形式的1的个数. 思路:取出一位,就右移1位,挤掉它,循环32次,逐个判断.没难度就不解释了,可能有更好解法,等待第2次思考. 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 if(!n) return 0; 5 if(n==1) return 1; 6 7 uint32_t MASK = 1, temp=n; 8 int i, cnt = 0; 9

leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so t

键盘输入一个数number,输出从1到number的所有奇数和偶数

最近看到一个题,键盘输入一个数number,输出从1到number的所有奇数和偶数,有好几种算法 一. 取余+集合算法输出: 1 package com.company; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Scanner; 8 9 public class Test3 { 10

LeetCode 191. Number of 1 bits (位1的数量)

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu