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:
parent
3285c5515e
commit
92c3e12aad
1 changed files with 15 additions and 0 deletions
15
cs/rotate-array.cs
Normal file
15
cs/rotate-array.cs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue