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

Commit adcb5aa

Browse files
add missing_number (#13)
* init missing_number * add * add more * add missing_number * add mroe * add sol4 * add sol3 * udpate readme
1 parent f388781 commit adcb5aa

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ cargo test
5555
0201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/bitwise-and-of-numbers-range.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/bitwise-and-of-numbers-range.py) <br> [Rust](./kamyu104/src/bitwise_and_of_numbers_range.rs) | _O(1)_ | _O(1)_ | Medium ||
5656
0231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/power-of-two.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/power-of-two.py) <br> [Rust](./kamyu104/src/power_of_two.rs) | _O(1)_ | _O(1)_ | Easy | LintCode |
5757
0260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/single-number-iii.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/single-number-iii.py) <br> [Rust](./kamyu104/src/single_number_iii.rs) | _O(n)_ | _O(1)_ | Medium ||
58+
0268| [Missing Number](https://leetcode.com/problems/missing-number/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/missing-number.cpp), [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/missing-number.py) <br> [Rust](./kamyu104/src/missing_number.rs) | _O(n)_ | _O(1)_ | Medium | LintCode ||
5859

5960
<!--
6061
61-
0268| [Missing Number](https://leetcode.com/problems/missing-number/) | [C++](./C++/missing-number.cpp) [Python](./Python/missing-number.py) | _O(n)_ | _O(1)_ | Medium | LintCode ||
6262
0318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-lengths.py) | _O(n)_ ~ _O(n^2)_ | _O(n)_ | Medium || Bit Manipulation, Counting Sort, Pruning|
6363
0342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](./C++/power-of-four.cpp) [Python](./Python/power-of-four.py) | _O(1)_ | _O(1)_ | Easy | |
6464
0371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode |

kamyu104/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod bitwise_and_of_numbers_range;
2+
mod missing_number;
23
mod number_of_1_bits;
34
mod power_of_two;
45
mod reverse_bits;

kamyu104/src/missing_number.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
pub struct Solution1 {}
4+
impl Solution1 {
5+
pub fn missing_number(nums: Vec<i32>) -> i32 {
6+
let mut num: i32 = 0;
7+
for i in 0..nums.len() {
8+
num ^= nums[i] ^ (i + 1) as i32;
9+
}
10+
num
11+
}
12+
}
13+
14+
// Time: O(n)
15+
// Space: O(n)
16+
// pub struct Solution2 {}
17+
// impl Solution2 {
18+
// pub fn missing_number(nums: Vec<i32>) -> i32 {
19+
// 0
20+
// }
21+
// }
22+
23+
// Time: O(n)
24+
// Space: O(1)
25+
pub struct Solution3 {}
26+
impl Solution3 {
27+
pub fn missing_number(nums: Vec<i32>) -> i32 {
28+
let len_xor = (0..nums.len() + 1).fold(0, |acc, i| acc ^ i as i32);
29+
nums.iter().fold(len_xor, |acc, &i| acc ^ i)
30+
}
31+
}
32+
33+
// Time: O(n)
34+
// Space: O(1)
35+
pub struct Solution4 {}
36+
impl Solution4 {
37+
pub fn missing_number(nums: Vec<i32>) -> i32 {
38+
((0..nums.len() + 1).fold(0, |acc, i| acc + (i as i32))) - nums.iter().sum::<i32>()
39+
}
40+
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
46+
#[test]
47+
fn test_missing_number() {
48+
assert_eq!(Solution1::missing_number(vec![3, 0, 1]), 2);
49+
assert_eq!(
50+
Solution1::missing_number(vec![9, 6, 4, 2, 3, 5, 7, 0, 1]),
51+
8
52+
);
53+
54+
// assert_eq!(Solution2::missing_number(vec![3, 0, 1]), 2);
55+
// assert_eq!(
56+
// Solution2::missing_number(vec![9, 6, 4, 2, 3, 5, 7, 0, 1]),
57+
// 8
58+
// );
59+
60+
assert_eq!(Solution3::missing_number(vec![3, 0, 1]), 2);
61+
assert_eq!(
62+
Solution3::missing_number(vec![9, 6, 4, 2, 3, 5, 7, 0, 1]),
63+
8
64+
);
65+
66+
assert_eq!(Solution4::missing_number(vec![3, 0, 1]), 2);
67+
assert_eq!(
68+
Solution4::missing_number(vec![9, 6, 4, 2, 3, 5, 7, 0, 1]),
69+
8
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)