-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathRotateArray.java
More file actions
55 lines (36 loc) · 1.42 KB
/
RotateArray.java
File metadata and controls
55 lines (36 loc) · 1.42 KB
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
/******************************************************************************
189.) Rotate Array
Difficulty: Medium
Description: Given an array, rotate the array to the right by k steps, where k is non-negative.
Naive Approach: Create a function that rotates the array onces, then rotate the array k times.
Efficient Approach: Reverse the whole list, then reverse the first k elements, and the last n - k elements.
*******************************************************************************/
public class RotateArray
{
public static void main(String[] args) {
/* This is the test, expected output is [4,5,1,2,3] */
int [] arr = {1,2,3,4,5};
rotate(arr, 2);
int[] rotate_arr = arr;
for(int i = 0; i < rotate_arr.length; i++){
System.out.print(rotate_arr[i] + " ");
}
}
public static void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length - 1);
}
public static int [] reverse(int [] nums, int start, int end){
int indx_end = end, indx_start = start;
while(indx_start < indx_end){
int temp = nums[indx_start];
nums[indx_start] = nums[indx_end];
nums[indx_end] = temp;
indx_start++;
indx_end--;
}
return nums;
}
}