g0101_0200.s0189_rotate_array.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
package g0101_0200.s0189_rotate_array;
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays
// #2022_06_27_Time_0_ms_(100.00%)_Space_58_MB_(96.22%)
public class Solution {
private void reverse(int[] nums, int l, int r) {
while (l <= r) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
}
public void rotate(int[] nums, int k) {
int n = nums.length;
int t = n - (k % n);
reverse(nums, 0, t - 1);
reverse(nums, t, n - 1);
reverse(nums, 0, n - 1);
}
}