189. Rotate Array Medium
1/**
2 * [189] Rotate Array
3 *
4 * Given an array, rotate the array to the right by k steps, where k is non-negative.
5 *
6 * Example 1:
7 *
8 * Input: nums = [1,2,3,4,5,6,7], k = 3
9 * Output: [5,6,7,1,2,3,4]
10 * Explanation:
11 * rotate 1 steps to the right: [7,1,2,3,4,5,6]
12 * rotate 2 steps to the right: [6,7,1,2,3,4,5]
13 * rotate 3 steps to the right: [5,6,7,1,2,3,4]
14 *
15 * Example 2:
16 *
17 * Input: nums = [-1,-100,3,99], k = 2
18 * Output: [3,99,-1,-100]
19 * Explanation:
20 * rotate 1 steps to the right: [99,-1,-100,3]
21 * rotate 2 steps to the right: [3,99,-1,-100]
22 *
23 *
24 * Constraints:
25 *
26 * 1 <= nums.length <= 10^5
27 * -2^31 <= nums[i] <= 2^31 - 1
28 * 0 <= k <= 10^5
29 *
30 *
31 * Follow up:
32 *
33 * Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
34 * Could you do it in-place with O(1) extra space?
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/rotate-array/
40// discuss: https://leetcode.com/problems/rotate-array/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn rotate(nums: &mut Vec<i32>, k: i32) {
46
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_189() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.