日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode - Easy - 191. Number of 1 Bits

發布時間:2023/12/13 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode - Easy - 191. Number of 1 Bits 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Topic

  • Bit Manipulation

Description

https://leetcode.com/problems/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).

Note:

Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation 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?

Example 1:

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

Example 2:

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

Example 3:

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

Analysis

方法一:

  • An Integer in Java has 32 bits, e.g. 00101000011110010100001000011010.
  • To count the 1s in the Integer representation we put the input int n in bit AND with 1 (that is represented as 00000000000000000000000000000001, and if this operation result is 1, that means that the last bit of the input integer is 1. Thus we add it to the 1s count.ones = ones + (n & 1);
  • Then we shift the input Integer by one on the right, to check for the next bit.n = n>>>1;
  • We keep doing this until the input Integer is 0.

方法二:

n & (n - 1) drops the lowest set bit. It’s a neat little bit trick.

Let’s use n = 00101100 as an example. This binary representation has three 1s.

  • If n = 00101100, then n - 1 = 00101011, so n & (n - 1) = 00101100 & 00101011 = 00101000. Count = 1.
  • If n = 00101000, then n - 1 = 00100111, so n & (n - 1) = 00101000 & 00100111 = 00100000. Count = 2.
  • If n = 00100000, then n - 1 = 00011111, so n & (n - 1) = 00100000 & 00011111 = 00000000. Count = 3.

n is now zero, so the while loop ends, and the final count (the numbers of set bits) is returned.

Submission

public class NumberOfOneBits {// 方法一:public int hammingWeight1(int n) {int result = 0;while (n != 0) {// if((n & 1) == 1) result++;result += (n & 1);n >>>= 1;}return result;}// 方法二:int hammingWeight2(int n) {int count = 0;while (n != 0) {n &= (n - 1);count++;}return count;} }

Test

import static org.junit.Assert.*;import org.junit.Test;public class NumberOfOneBitsTest {@Testpublic void test() {NumberOfOneBits obj = new NumberOfOneBits();assertEquals(3, obj.hammingWeight1(Integer.parseInt("00000000000000000000000000001011", 2)));assertEquals(1, obj.hammingWeight1(Integer.parseInt("00000000000000000000000010000000", 2)));assertEquals(31, obj.hammingWeight1(-3));//Integer.parseInt("11111111111111111111111111111101", 2) 拋錯assertEquals(3, obj.hammingWeight2(Integer.parseInt("00000000000000000000000000001011", 2)));assertEquals(1, obj.hammingWeight2(Integer.parseInt("00000000000000000000000010000000", 2)));assertEquals(31, obj.hammingWeight2(-3));} }

總結

以上是生活随笔為你收集整理的LeetCode - Easy - 191. Number of 1 Bits的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。