Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 895c78b

Browse files
Merge pull request #9 from rust-interview/dev
add single-number
2 parents 6671ca4 + bc64409 commit 895c78b

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ cargo test
5151
0136 | [Single Number](https://leetcode.com/problems/single-number/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/single-number.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/single-number.py) <br> [Rust](./kamyu104/src/single_number.rs) | _O(n)_ | _O(1)_ | Easy |||
5252
0137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/single-number-ii.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/single-number-ii.py) <br> [Rust](./kamyu104/src/single_number_ii.rs) | _O(n)_ | _O(1)_ | Medium |||
5353
0190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/reverse-bits.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/reverse-bits.py) <br> [Rust](./kamyu104/src/reverse_bits.rs) | _O(1)_ | _O(1)_ | Easy |||
54+
0191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/number-of-1-bits.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/number-of-1-bits.py) <br> [Rust](./kamyu104/src/number_of_1_bits.rs) | _O(1)_ | _O(1)_ | Easy |||
5455

5556
<!--
56-
0191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy |||
57+
5758
0201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _O(1)_ | _O(1)_ | Medium ||
5859
0231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [C++](./C++/power-of-two.cpp) [Python](./Python/power-of-two.py) | _O(1)_ | _O(1)_ | Easy | LintCode |
5960
0260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium ||

kamyu104/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod number_of_1_bits;
12
mod reverse_bits;
23
mod single_number;
34
mod single_number_ii;

kamyu104/src/number_of_1_bits.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Time: O(logn) = O(32)
2+
// Space: O(1)
3+
4+
pub struct Solution {}
5+
6+
impl Solution {
7+
pub fn hamming_weight(mut n: u32) -> u8 {
8+
let mut cnt: u8 = 0;
9+
while n > 0 {
10+
cnt += 1;
11+
n &= n - 1;
12+
}
13+
return cnt;
14+
}
15+
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use super::*;
20+
21+
#[test]
22+
fn test_hamming_weight() {
23+
assert_eq!(
24+
Solution::hamming_weight(
25+
u32::from_str_radix("00000000000000000000000000001011", 2).unwrap()
26+
),
27+
3
28+
);
29+
assert_eq!(
30+
Solution::hamming_weight(
31+
u32::from_str_radix("00000000000000000000000010000000", 2).unwrap()
32+
),
33+
1
34+
);
35+
assert_eq!(
36+
Solution::hamming_weight(
37+
u32::from_str_radix("11111111111111111111111111111101", 2).unwrap()
38+
),
39+
31
40+
);
41+
}
42+
}

kamyu104/src/single_number_ii.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ mod tests {
5454
use super::*;
5555

5656
#[test]
57-
fn test_add() {
57+
fn test_single_number() {
5858
assert_eq!(Solution1::single_number(vec![2, 2, 3, 2]), 3);
5959
assert_eq!(Solution1::single_number(vec![0, 1, 0, 1, 0, 1, 99]), 99);
6060
assert_eq!(Solution1::single_number(vec![0, 0, 0, 1, 1, 1, 5]), 5);

0 commit comments

Comments
 (0)