日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 久久久久99精品成人片试看 | 亚洲综合在线一区二区 | 粗口调教gay2022.com| 亚洲影音 | 老头糟蹋新婚少妇系列小说 | 青青草手机在线视频 | 国产欧美一区二区精品忘忧草 | 免费一区二区在线观看 | 亚洲午夜一区二区三区 | 96av视频| 国产伦精品一区二区三区视频我 | 色屋视频 | 久久国产精品综合 | 青草久久久久 | 日韩中文字幕国产 | www成年人 | 黑人欧美一区二区三区 | 午夜在线视频免费观看 | 免费看a的网站 | 国产chinesehd天美传媒 | 国产高清一区二区三区四区 | 亚洲精品一区二区三区区别 | 成年人在线观看 | 美女视频黄a视频全免费观看 | 国产无遮挡又黄又爽在线观看 | 精品三区| 亚洲国产精品福利 | 三年中国片在线高清观看 | 精品一区二区三区电影 | av图片在线观看 | 欧美性在线观看 | 视频一区二区三区四区五区 | 欧美一区二区三区久久综合 | 九月丁香婷婷 | 少妇一级淫片免费 | 精品人妻无码在线 | 污视频在线播放 | 亚洲热久久| 污污视频网站免费观看 | 久久天天躁狠狠躁夜夜躁 | 免费黄片毛片 | 少妇的被肉日常np | 丰满大乳少妇在线观看网站 | 欧美日韩国产综合网 | 一个色在线 | 欧美日本不卡 | 日韩欧美四区 | 欧美日韩国产精品成人 | 日本一二三区视频 | 污免费视频 | 高h捆绑拘束调教小说 | 女优视频在线观看 | 在线观看视频一区 | 国产精品一区二区人人爽 | 日日干夜夜爽 | 国产精品资源在线观看 | 精品一区二区三区欧美 | 蜜臀av免费在线观看 | 五月天婷婷视频 | 国产精品久久久久久精 | 日本高清免费aaaaa大片视频 | 永久免费视频网站直接看 | 色综合久久天天综合网 | 中文字幕女同女同女同 | 成人黄色大片在线观看 | 国产又粗又长视频 | 色婷婷成人网 | 男女污污视频在线观看 | 国产免费无码一区二区视频 | 日韩亚洲第一页 | 与亲女洗澡时伦了毛片 | 国产黄色精品视频 | 国产在线观看网站 | 精品人妻互换一区二区三区 | 国产免费av片在线观看 | 欧美日韩小视频 | 亚洲天堂av女优 | 五月婷婷,六月丁香 | 玖玖爱国产 | 91你懂的| 国产福利久久久 | 99在线精品视频 | 日本在线中文字幕专区 | 欧美日韩中文字幕视频 | www日| 男女一区二区三区 | 韩日产理伦片在线观看 | 久久久久久成人精品 | 久久少妇av | 中文字幕日本人妻久久久免费 | av黄色片 | 亚洲国产97| 亚洲不卡在线观看 | 色5566| 影音先锋制服丝袜 | 五月天婷婷导航 | 欧美色图12p | 成人国产精品免费观看视频 | 久久特级毛片 |