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

Commit 834c193

Browse files
committed
this week. Array
1 parent 2c53302 commit 834c193

6 files changed

+370
-0
lines changed

src/solution/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ mod s0169_majority_element;
1212
mod s0217_contains_duplicate;
1313
mod s0219_contains_duplicate_ii;
1414
mod s0228_summary_ranges;
15+
mod s0268_missing_number;
16+
mod s0283_move_zeroes;
17+
mod s0303_range_sum_query_immutable;
18+
mod s0409_longest_palindrome;
19+
mod s0349_intersection_of_two_arrays;
20+
mod s0350_intersection_of_two_arrays_ii;

src/solution/s0268_missing_number.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* [268] Missing Number
3+
*
4+
* Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
5+
*
6+
* <strong class="example">Example 1:
7+
*
8+
* Input: nums = [3,0,1]
9+
* Output: 2
10+
* Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
11+
*
12+
* <strong class="example">Example 2:
13+
*
14+
* Input: nums = [0,1]
15+
* Output: 2
16+
* Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
17+
*
18+
* <strong class="example">Example 3:
19+
*
20+
* Input: nums = [9,6,4,2,3,5,7,0,1]
21+
* Output: 8
22+
* Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
23+
*
24+
*
25+
* Constraints:
26+
*
27+
* n == nums.length
28+
* 1 <= n <= 10^4
29+
* 0 <= nums[i] <= n
30+
* All the numbers of nums are unique.
31+
*
32+
*
33+
* Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
34+
*
35+
*/
36+
pub struct Solution {}
37+
38+
// problem: https://leetcode.com/problems/missing-number/
39+
// discuss: https://leetcode.com/problems/missing-number/discuss/?currentPage=1&orderBy=most_votes&query=
40+
41+
// submission codes start here
42+
43+
impl Solution {
44+
pub fn missing_number(nums: Vec<i32>) -> i32 {
45+
let total = nums.len() * (nums.len() + 1) / 2;
46+
let mut sum = 0;
47+
for n in 0..nums.len() {
48+
sum += nums[n];
49+
}
50+
total as i32 - sum
51+
}
52+
}
53+
54+
// submission codes end
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
60+
#[test]
61+
fn test_268_1() {
62+
assert_eq!(Solution::missing_number(vec![9, 6, 4, 2, 3, 5, 7, 0, 1]), 8)
63+
}
64+
#[test]
65+
fn test_268_2() {
66+
assert_eq!(Solution::missing_number(vec![0, 3, 1]), 2)
67+
}
68+
}

src/solution/s0283_move_zeroes.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* [283] Move Zeroes
3+
*
4+
* Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
5+
* Note that you must do this in-place without making a copy of the array.
6+
*
7+
* <strong class="example">Example 1:
8+
* Input: nums = [0,1,0,3,12]
9+
* Output: [1,3,12,0,0]
10+
* <strong class="example">Example 2:
11+
* Input: nums = [0]
12+
* Output: [0]
13+
*
14+
* Constraints:
15+
*
16+
* 1 <= nums.length <= 10^4
17+
* -2^31 <= nums[i] <= 2^31 - 1
18+
*
19+
*
20+
* Follow up: Could you minimize the total number of operations done?
21+
*/
22+
pub struct Solution {}
23+
24+
// problem: https://leetcode.com/problems/move-zeroes/
25+
// discuss: https://leetcode.com/problems/move-zeroes/discuss/?currentPage=1&orderBy=most_votes&query=
26+
27+
// submission codes start here
28+
impl Solution {
29+
pub fn move_zeroes(nums: &mut Vec<i32>) {
30+
let mut end = 0;
31+
for it in 0..nums.len() {
32+
if nums[it] != 0 {
33+
nums[end] = nums[it];
34+
if it != end {
35+
nums[it] = 0;
36+
}
37+
end += 1;
38+
}
39+
}
40+
}
41+
pub fn move_zeroes_1(nums: &mut Vec<i32>) {
42+
let mut start = 0;
43+
let mut end = 0;
44+
while end < nums.len() {
45+
while start < nums.len() && nums[start] != 0 {
46+
start += 1;
47+
}
48+
if start >= nums.len() {
49+
return;
50+
}
51+
end = start;
52+
while end < nums.len() && nums[end] == 0 {
53+
end += 1;
54+
}
55+
if end >= nums.len() {
56+
return;
57+
}
58+
nums.swap(start, end);
59+
start += 1;
60+
}
61+
}
62+
}
63+
64+
// submission codes end
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
69+
70+
#[test]
71+
fn test_283() {
72+
let mut v = vec![0, 1, 0, 3, 12];
73+
let res = vec![1, 3, 12, 0, 0];
74+
Solution::move_zeroes(&mut v);
75+
assert_eq!(v, res);
76+
}
77+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* [303] Range Sum Query - Immutable
3+
*
4+
* Given an integer array nums, handle multiple queries of the following type:
5+
* <ol>
6+
* Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.
7+
* </ol>
8+
* Implement the NumArray class:
9+
*
10+
* NumArray(int[] nums) Initializes the object with the integer array nums.
11+
* int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).
12+
*
13+
*
14+
* <strong class="example">Example 1:
15+
*
16+
* Input
17+
* ["NumArray", "sumRange", "sumRange", "sumRange"]
18+
* [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
19+
* Output
20+
* [null, 1, -1, -3]
21+
* Explanation
22+
* NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
23+
* numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
24+
* numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
25+
* numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
26+
*
27+
*
28+
* Constraints:
29+
*
30+
* 1 <= nums.length <= 10^4
31+
* -10^5 <= nums[i] <= 10^5
32+
* 0 <= left <= right < nums.length
33+
* At most 10^4 calls will be made to sumRange.
34+
*
35+
*/
36+
pub struct Solution {}
37+
38+
// problem: https://leetcode.com/problems/range-sum-query-immutable/
39+
// discuss: https://leetcode.com/problems/range-sum-query-immutable/discuss/?currentPage=1&orderBy=most_votes&query=
40+
41+
// submission codes start here
42+
43+
struct NumArray {
44+
preSum:Vec<i32>,
45+
}
46+
47+
48+
/**
49+
* `&self` means the method takes an immutable reference.
50+
* If you need a mutable reference, change it to `&mut self` instead.
51+
*/
52+
impl NumArray {
53+
54+
fn new(nums: Vec<i32>) -> Self {
55+
let mut preSum=vec![0;nums.len()+1];
56+
for i in 1..=nums.len(){
57+
preSum[i] = preSum[i-1] + nums[i-1];
58+
}
59+
Self{preSum}
60+
}
61+
62+
fn sum_range(&self, left: i32, right: i32) -> i32 {
63+
self.preSum[(right+1) as usize] - self.preSum[left as usize]
64+
}
65+
}
66+
67+
/**
68+
* Your NumArray object will be instantiated and called as such:
69+
* let obj = NumArray::new(nums);
70+
* let ret_1: i32 = obj.sum_range(left, right);
71+
*/
72+
73+
// submission codes end
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
79+
#[test]
80+
fn test_303() {
81+
}
82+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* [349] Intersection of Two Arrays
3+
*
4+
* Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
5+
*
6+
* <strong class="example">Example 1:
7+
*
8+
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
9+
* Output: [2]
10+
*
11+
* <strong class="example">Example 2:
12+
*
13+
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
14+
* Output: [9,4]
15+
* Explanation: [4,9] is also accepted.
16+
*
17+
*
18+
* Constraints:
19+
*
20+
* 1 <= nums1.length, nums2.length <= 1000
21+
* 0 <= nums1[i], nums2[i] <= 1000
22+
*
23+
*/
24+
pub struct Solution {}
25+
26+
// problem: https://leetcode.com/problems/intersection-of-two-arrays/
27+
// discuss: https://leetcode.com/problems/intersection-of-two-arrays/discuss/?currentPage=1&orderBy=most_votes&query=
28+
29+
// submission codes start here
30+
31+
use std::collections::HashMap;
32+
impl Solution {
33+
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
34+
let mut res: Vec<i32> = vec![];
35+
let mut mp: HashMap<i32, i32> = HashMap::new();
36+
//1. collect nums in nums1, set count to 1
37+
for i in 0..nums1.len() {
38+
mp.insert(nums1[i], 1);
39+
}
40+
//2. go through nums2
41+
for i in 0..nums2.len() {
42+
if let Some(&tmp) = mp.get(&nums2[i]) {
43+
if tmp == 1 {
44+
res.push(nums2[i]);
45+
}
46+
mp.insert(nums2[i], tmp + 1);
47+
}
48+
}
49+
res
50+
}
51+
}
52+
53+
// submission codes end
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::*;
58+
59+
#[test]
60+
fn test_349() {}
61+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* [350] Intersection of Two Arrays II
3+
*
4+
* Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
5+
*
6+
* <strong class="example">Example 1:
7+
*
8+
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
9+
* Output: [2,2]
10+
*
11+
* <strong class="example">Example 2:
12+
*
13+
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
14+
* Output: [4,9]
15+
* Explanation: [9,4] is also accepted.
16+
*
17+
*
18+
* Constraints:
19+
*
20+
* 1 <= nums1.length, nums2.length <= 1000
21+
* 0 <= nums1[i], nums2[i] <= 1000
22+
*
23+
*
24+
* Follow up:
25+
*
26+
* What if the given array is already sorted? How would you optimize your algorithm?
27+
* What if nums1's size is small compared to nums2's size? Which algorithm is better?
28+
* What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
29+
*
30+
*/
31+
pub struct Solution {}
32+
33+
// problem: https://leetcode.com/problems/intersection-of-two-arrays-ii/
34+
// discuss: https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/?currentPage=1&orderBy=most_votes&query=
35+
36+
// submission codes start here
37+
38+
use std::collections::HashMap;
39+
impl Solution {
40+
pub fn intersect(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
41+
let mut res: Vec<i32> = vec![];
42+
let mut mp: HashMap<i32, i32> = HashMap::new();
43+
//1. collect nums in nums1, record counts
44+
for i in 0..nums1.len() {
45+
match mp.get(&nums1[i]) {
46+
Some(&cnt) => {
47+
mp.insert(nums1[i], cnt + 1);
48+
}
49+
None => {
50+
mp.insert(nums1[i], 1);
51+
}
52+
}
53+
}
54+
//2. go through nums2
55+
for i in 0..nums2.len() {
56+
if let Some(&cnt) = mp.get(&nums2[i]) {
57+
res.push(nums2[i]);
58+
if cnt - 1 <= 0 {
59+
mp.remove(&nums2[i]);
60+
} else {
61+
mp.insert(nums2[i], cnt - 1);
62+
}
63+
}
64+
}
65+
res
66+
}
67+
}
68+
// submission codes end
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
74+
#[test]
75+
fn test_350() {}
76+
}

0 commit comments

Comments
 (0)