Skip to content

Commit 995a62e

Browse files
committed
Added solution of problem 189
1 parent 42c3d7e commit 995a62e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub mod s0172_factorial_trailing_zeroes;
9898
pub mod s0173_binary_search_tree_iterator;
9999
pub mod s0179_largest_number;
100100
pub mod s0187_repeated_dna_sequences;
101+
pub mod s0189_rotate_array;
101102
pub mod s0231_power_of_two;
102103
pub mod s0232_implement_queue_using_stacks;
103104
pub mod s0234_palindrome_linked_list;

src/solutions/s0189_rotate_array.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
2+
let n: usize = nums.len();
3+
let k: usize = k as usize % n;
4+
5+
let result: Vec<i32> = nums.iter().cloned().collect();
6+
for i in 0..n {
7+
nums[(i + k) % n] = result[i];
8+
}
9+
}
10+
11+
#[cfg(test)]
12+
mod test {
13+
use super::*;
14+
15+
#[test]
16+
fn test_case_1() {
17+
let mut nums: Vec<i32> = vec![1, 2, 3, 4, 5, 6, 7];
18+
19+
rotate(&mut nums, 3);
20+
21+
assert_eq!(nums, [5, 6, 7, 1, 2, 3, 4]);
22+
}
23+
24+
#[test]
25+
fn test_case_2() {
26+
let mut nums: Vec<i32> = vec![-1, -100, 3, 99];
27+
28+
rotate(&mut nums, 2);
29+
30+
assert_eq!(nums, [3, 99, -1, -100]);
31+
}
32+
}

0 commit comments

Comments
 (0)