-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathp0189_rotate_array.rs
58 lines (49 loc) · 1.5 KB
/
p0189_rotate_array.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 189. Rotate Array
// Easy
// Given an array, rotate the array to the right by k steps, where k is non-negative.
// Example 1:
// Input: [1,2,3,4,5,6,7] and k = 3
// Output: [5,6,7,1,2,3,4]
// Explanation:
// rotate 1 steps to the right: [7,1,2,3,4,5,6]
// rotate 2 steps to the right: [6,7,1,2,3,4,5]
// rotate 3 steps to the right: [5,6,7,1,2,3,4]
// Example 2:
// Input: [-1,-100,3,99] and k = 2
// Output: [3,99,-1,-100]
// Explanation:
// rotate 1 steps to the right: [99,-1,-100,3]
// rotate 2 steps to the right: [3,99,-1,-100]
// Note:
// Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
// Could you do it in-place with O(1) extra space?
pub struct Solution {}
impl Solution {
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
let mut count = k % (nums.len() as i32);
if count == 0 {
return;
}
while count > 0 {
let num = nums.pop().unwrap();
nums.insert(0, num);
count -= 1;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rotate_test() {
let mut nums = vec![1, 2, 3, 4, 5, 6, 7];
Solution::rotate(&mut nums, 3);
assert_eq!(nums, vec![5, 6, 7, 1, 2, 3, 4]);
nums = vec![-1, -100, 3, 99];
Solution::rotate(&mut nums, 2);
assert_eq!(nums, vec![3, 99, -1, -100]);
nums = vec![-1, -100, 3, 99];
Solution::rotate(&mut nums, 8);
assert_eq!(nums, vec![-1, -100, 3, 99]);
}
}