Skip to content

Commit 66e4328

Browse files
committed
https://leetcode.com/problems/rotate-array/
1 parent 686f44c commit 66e4328

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+

2+
3+
namespace LeetCodeSolutions.ArrayAndString
4+
{
5+
class Rotate_Array
6+
{
7+
public void Rotate(int[] nums, int k)
8+
{
9+
if (nums != null && nums.Length > 0 && k > 0)
10+
{
11+
k = k % nums.Length;
12+
13+
//***
14+
//*** Reverse the entire array
15+
//***
16+
Array.Reverse(nums);
17+
//***
18+
//*** Reverse the array from 0 to k
19+
//***
20+
Helper(nums, 0, k - 1);
21+
//***
22+
//*** Reverse the array from k to the end of array
23+
//***
24+
Helper(nums, k, nums.Length - 1);
25+
}
26+
}
27+
28+
private void Helper(int[] arry, int start, int end)
29+
{
30+
while (start < end)
31+
{
32+
int temp = arry[start];
33+
arry[start] = arry[end];
34+
arry[end] = temp;
35+
start++;
36+
end--;
37+
}
38+
}
39+
40+
[Test(Description = "https://leetcode.com/problems/rotate-array/")]
41+
[Category("Easy")]
42+
[Category("LeetCode")]
43+
[Category("Rotate Array")]
44+
[TestCaseSource(nameof(Input))]
45+
[Category("TopInterview")]
46+
[Category("Remove Element")]
47+
public void Test1((int[] Output, (int[], int) Input) item)
48+
{
49+
Rotate(item.Input.Item1, item.Input.Item2);
50+
Assert.That(item.Input.Item1, Is.EqualTo(item.Output));
51+
}
52+
53+
public static IEnumerable<(int[] Output, (int[], int) Input)> Input =>
54+
new List<(int[] Output, (int[], int) Input)>()
55+
{
56+
([5, 6, 7, 1, 2, 3, 4], ([1, 2, 3, 4, 5, 6, 7], 3)),
57+
};
58+
}
59+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
4949
| 3 | Remove Duplicates from Sorted Array | Easy ||
5050
| 4 | Remove Duplicates from Sorted Array II | Medium ||
5151
| 5 | Majority Element | Easy ||
52-
| 6 | Rotate Array | Medium | |
52+
| 6 | Rotate Array | Medium | |
5353
| 7 | Best Time to Buy and Sell Stock | Easy | |
5454
| 8 | Best Time to Buy and Sell Stock II | Medium | |
5555
| 9 | Jump Game | Medium | |

0 commit comments

Comments
 (0)