Shawn's Blog

  • Home
  • Tags
  • Categories
  • Archives
  • Search
  • Table of Contents
  • Overview

Shawn

若有恒,何必三更眠五更起;最无益,莫过一日曝十日寒。
392 posts
13 categories
114 tags

LeetCode 191. Number of 1 Bits

Posted on 2021-03-24 Edited on 2024-11-24 In LeetCode
1
2
3
4
5
6
7
8
9
10
11
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while (n != 0) {
n &= n - 1;
ans++;
}
return ans;
}
}
# Bit Manipulation
LeetCode 73. Set Matrix Zeroes
LeetCode 341. Flatten Nested List Iterator
© 2024 Shawn
Powered by Hexo & NexT.Mist
0%