cs: add «189. Rotate Array»

URL:	https://leetcode.com/problems/rotate-array/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-03 18:52:43 +01:00
parent 3285c5515e
commit 92c3e12aad
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

15
cs/rotate-array.cs Normal file
View file

@ -0,0 +1,15 @@
public class Solution {
public void Rotate(int[] nums, int k) {
void Reverse(int begin, int end) {
for (/* no-op */; begin < end; ++begin, --end) {
(nums[begin], nums[end]) = (nums[end], nums[begin]);
}
}
k %= nums.Length;
Reverse(0, nums.Length - 1);
Reverse(0, k - 1);
Reverse(k, nums.Length - 1);
}
}