-
Notifications
You must be signed in to change notification settings - Fork 112
/
0189-RotateArray.cs
36 lines (32 loc) · 1.02 KB
/
0189-RotateArray.cs
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
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 32.8 MB
// Link: https://leetcode.com/submissions/detail/409217846/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0189_RotateArray
{
public void Rotate(int[] nums, int k)
{
if (k >= nums.Length)
k %= nums.Length;
for (int start = 0, count = 0; start < k && count < nums.Length; start++)
{
int curr = start;
var prev = nums[curr];
do
{
var next = curr + k;
if (next >= nums.Length)
next -= nums.Length;
var temp = nums[next];
nums[next] = prev;
prev = temp;
curr = next;
count++;
} while (start != curr);
}
}
}
}