-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRotateArraySln.cs
62 lines (57 loc) · 1.65 KB
/
RotateArraySln.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ==============================================================================
* 功能描述:RotateArraySln
* 创 建 者:gz
* 创建日期:2017/5/3 16:03:03
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Array.Lib
{
/// <summary>
/// RotateArraySln
/// </summary>
public class RotateArraySln
{
public void Rotate(int[] nums, int k)
{
int n = nums.Length;
if (n == 0 || n == 1) return;
if ((double)k / n > 1)
k = k % n;
if (k == 0) return;
System.Array.Reverse(nums, 0, n - k); //index, length
System.Array.Reverse(nums, n - k, k);
System.Array.Reverse(nums);
}
public void Rotate2(int[] nums, int k)
{
int n = nums.Length;
if (n == 0 || n == 1) return;
if ((double)k / n > 1)
k = k % n;
if (k == 0) return;
Reverse(nums, 0, n - k); //index, length
Reverse(nums, n - k, k);
Reverse(nums, 0, n);
}
private void Reverse(int[] nums, int index, int length)
{
int i = 0;
int loop = length/2;
while (i < loop)
{
swap(ref nums[index + i], ref nums[length + index - i - 1]);
i++;
}
}
private void swap(ref int a, ref int b)
{
int tmp = a;
a = b;
b = tmp;
}
}
}