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

Commit 6c947ea

Browse files
add hamming_distance (#33)
1 parent 68ed20b commit 6c947ea

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ cargo test
7171
0401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/binary-watch.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/binary-watch.py) <br>------</br> [Rust](./kamyu104/src/binary_watch.rs) | _O(1)_ | _O(1)_ | Easy | |
7272
0411 | [~~Minimum Unique Word Abbreviation~~](https://leetcode.com/problems/minimum-unique-word-abbreviation/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/minimum-unique-word-abbreviation.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/minimum-unique-word-abbreviation.py) | _O(2^n)_ | _O(n)_ | Hard | 🔒 |
7373
0421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-xor-of-two-numbers-in-an-array.py) <br>------</br> [Rust](./kamyu104/src/maximum_xor_of_two_numbers_in_an_array.rs) | _O(n)_ | _O(n)_ | Medium ||
74+
0461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/hamming-distance.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/hamming-distance.py) <br>------</br> [Rust](./kamyu104/src/hamming_distance.rs) | _O(1)_ | _O(1)_ | Easy ||
75+
76+
<!--
77+
78+
=======
7479
7580
<!--
7681
77-
0461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C++](./C++/hamming-distance.cpp) [Python](./Python/hamming-distance.py) | _O(1)_ | _O(1)_ | Easy ||
7882
0462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/minimum-moves-to-equal-array-elements-ii.py) | _O(n)_ on average | _O(1)_ | Medium ||
7983
0477 | [Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [C++](./C++/total-hamming-distance.cpp) [Python](./Python/total-hamming-distance.py) | _O(n)_ | _O(1)_ | Medium ||
8084
0645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch/) | [C++](./C++/set-mismatch.cpp) [Python](./Python/set-mismatch.py) | _O(n)_ | _O(1)_ | Easy ||

kamyu104/src/hamming_distance.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
pub struct Solution1 {}
5+
impl Solution1 {
6+
pub fn hamming_distance(x: i32, y: i32) -> i32 {
7+
let mut distance: i32 = 0;
8+
let mut z: i32 = x ^ y;
9+
while z != 0 {
10+
distance += 1;
11+
z &= z - 1;
12+
}
13+
distance
14+
}
15+
}
16+
17+
pub struct Solution2 {}
18+
impl Solution2 {
19+
pub fn hamming_distance(x: i32, y: i32) -> i32 {
20+
let z = format!("{:b}", x ^ y);
21+
z.chars().filter(|&c| c == '1').count() as i32
22+
}
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_hamming_distance() {
31+
assert_eq!(Solution1::hamming_distance(1, 4), 2);
32+
assert_eq!(Solution2::hamming_distance(1, 4), 2);
33+
}
34+
}

kamyu104/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod binary_watch;
22
mod bitwise_and_of_numbers_range;
33
mod find_the_difference;
4+
mod hamming_distance;
45
mod maximum_product_of_word_lengths;
56
mod maximum_xor_of_two_numbers_in_an_array;
67
mod missing_number;

0 commit comments

Comments
 (0)