Skip to content

Commit 47dd915

Browse files
Merge pull request #88 from nik132-eng/patch-2
Create Rotate_Array.java
2 parents 36822a0 + 1175011 commit 47dd915

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

LeetCode/Rotate_Array.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//189. Rotate Array
2+
3+
//Given an array, rotate the array to the right by k steps,
4+
//where k is non-negative.
5+
6+
//Example 1:
7+
//
8+
//Input: nums = [1,2,3,4,5,6,7], k = 3
9+
//Output: [5,6,7,1,2,3,4]
10+
11+
//Explanation:
12+
//rotate 1 steps to the right: [7,1,2,3,4,5,6]
13+
//rotate 2 steps to the right: [6,7,1,2,3,4,5]
14+
//rotate 3 steps to the right: [5,6,7,1,2,3,4]
15+
16+
//Example 2:
17+
//
18+
//Input: nums = [-1,-100,3,99], k = 2
19+
//Output: [3,99,-1,-100]
20+
21+
//Explanation:
22+
//rotate 1 steps to the right: [99,-1,-100,3]
23+
//rotate 2 steps to the right: [3,99,-1,-100]
24+
25+
//Constraints:
26+
27+
//1 <= nums.length <= 105
28+
//-231 <= nums[i] <= 231 - 1
29+
//0 <= k <= 105
30+
31+
public class Rotate_Array {
32+
33+
public static void main(String[] args) {
34+
// take array
35+
int[] nums = { 1, 0, 0, 0, 0, 0, 0 };
36+
// take value for rotation
37+
int k = 2;
38+
// call the function rotate
39+
rotate(nums, k);
40+
}
41+
42+
// -----------------------------------------------
43+
// method one o(n2) time complexity
44+
45+
// take two for loops and with the help of temp variable rotate array
46+
47+
// -----------------------------------------------
48+
// public static void rotate(int[] nums, int k) {
49+
// int temp,n=nums.length,i,j;
50+
// for (i = 0; i < k; i++) {
51+
// temp = nums[n - 1];
52+
// for (j = n - 1; j > 0; j--) {
53+
// nums[j] = nums[j - 1];
54+
// }
55+
// nums[0] = temp;
56+
// }
57+
//
58+
// for(j=0;j<n;j++) {
59+
// System.out.print(nums[j]+ " ");
60+
// }
61+
// }
62+
63+
// -----------------------------------------------
64+
// method for get perfect solution in leetcode
65+
66+
// take the another array for storing the value into that but
67+
// that is not proper answer for the leetcode
68+
// -----------------------------------------------
69+
70+
// private static void rotate(int[] nums, int k) {
71+
// int[] reordered = new int[nums.length];
72+
// for (int i = 0; i < nums.length; i++)
73+
// reordered[i] = nums[(k + i + 1) % nums.length];
74+
// System.out.print("[");
75+
// for (int j = 0; j < nums.length; j++) {
76+
//
77+
// if(j!=(nums.length-1))
78+
// System.out.print(reordered[j] + ",");
79+
// else
80+
// System.out.print(reordered[j]);
81+
// }
82+
// System.out.println("]");
83+
// }
84+
85+
// -----------------------------------------------
86+
// code submitted in leetcode
87+
88+
// first copy the array in duplicate array.
89+
// than using the k value and positioning with the i set nums array
90+
// -----------------------------------------------
91+
private static void rotate(int[] nums, int k) {
92+
int[] duplicate = new int[nums.length];
93+
for (int i = 0; i < nums.length; i++)
94+
duplicate[i] = nums[i];
95+
96+
for (int i = 0; i < nums.length; i++)
97+
nums[(k + i) % nums.length] = duplicate[i];
98+
99+
for (int j = 0; j < nums.length; j++) {
100+
System.out.print(nums[j] + " ");
101+
}
102+
103+
}
104+
105+
}

0 commit comments

Comments
 (0)