-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate_Array.java
More file actions
32 lines (30 loc) · 798 Bytes
/
Rotate_Array.java
File metadata and controls
32 lines (30 loc) · 798 Bytes
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
package com.leet_code;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Rotate_Array {
public static void main(String[] args) {
int[] arr={1,2,3,4,5};
rotate(arr,2);
}
public static void rotate(int[] nums, int k) {
int[] a=new int[nums.length];
for (int i = 0; i < nums.length; i++) {
a[(i+k)%nums.length] = nums[i];
}
nums=a;
}
public void rotate2(int[] nums, int k) {
int n = nums.length;
int[] ans = new int[n];
for(int i =0;i<nums.length;i++)
{
int s = (i+k)%n;
ans[s] = nums[i];
}
for (int i = 0; i < nums.length; i++) {
nums[i] = ans[i];
}
}
}