Skip to content

Commit 8fa6c3b

Browse files
committed
feat: add more
1 parent 8a41e1f commit 8fa6c3b

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"DP",
55
"backtracking",
66
"binary_search",
7+
"bit_manipulation",
78
"compiler",
89
"sliding_window",
910
"String",

bit_manipulation/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "leetcode_bit_manipulation"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
path = "lib.rs"
9+
doctest = false
10+
11+
[dependencies]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// https://leetcode.com/problems/number-of-1-bits
2+
//
3+
// Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
4+
//
5+
// **Note:**
6+
//
7+
// * 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.
8+
// * In Java, the compiler represents the signed integers using [2's complement notation](https://en.wikipedia.org/wiki/Two%27s_complement). Therefore, in **Example 3**, the input represents the signed integer. `-3`.
9+
//
10+
// **Example 1:**
11+
//
12+
// ```
13+
// **Input:** n = 00000000000000000000000000001011
14+
// **Output:** 3
15+
// **Explanation:** The input binary string **00000000000000000000000000001011** has a total of three '1' bits.
16+
// ```
17+
//
18+
// **Example 2:**
19+
//
20+
// ```
21+
// **Input:** n = 00000000000000000000000010000000
22+
// **Output:** 1
23+
// **Explanation:** The input binary string **00000000000000000000000010000000** has a total of one '1' bit.
24+
// ```
25+
//
26+
// **Example 3:**
27+
//
28+
// ```
29+
// **Input:** n = 11111111111111111111111111111101
30+
// **Output:** 31
31+
// **Explanation:** The input binary string **11111111111111111111111111111101** has a total of thirty one '1' bits.
32+
// ```
33+
//
34+
// **Constraints:**
35+
//
36+
// * The input must be a **binary string** of length `32`.
37+
//
38+
// **Follow up:** If this function is called many times, how would you optimize it?
39+
40+
// you can just use `return n.count_ones() as i32;`
41+
pub fn hamming_weight(n: u32) -> i32 {
42+
return n.count_ones() as i32;
43+
// let mut count = 0;
44+
// let mut n_clone = n;
45+
// while n_clone > 0 {
46+
// count += n_clone & 1;
47+
// n_clone = n_clone >> 1;
48+
// }
49+
// return count as i32;
50+
}
51+
52+
#[test]
53+
pub fn t1() {
54+
assert_eq!(hamming_weight(00000000000011), 2);
55+
}

bit_manipulation/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![allow(dead_code)]
2+
3+
mod _0191_number_of_1_bits;

0 commit comments

Comments
 (0)