Skip to content

Commit 1948ab1

Browse files
RotateArray
1 parent 3996e1d commit 1948ab1

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

EASY/src/easy/RotateArray.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package easy;
2+
import java.util.*;
3+
4+
/**Rotate an array of n elements to the right by k steps.
5+
6+
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].*/
7+
public class RotateArray {
8+
9+
10+
/**My original idea and got AC'ed.
11+
* One thing to notice is that when k > nums.length, we'll continue to rotate the array, it just becomes k -= nums.length
12+
*
13+
* TODO: look at its Editorial Solution and use O(1) approach to redo it.*/
14+
public static void rotate(int[] nums, int k) {
15+
if(k == 0 || k == nums.length) return;
16+
if(k > nums.length) k -= nums.length;
17+
List<Integer> tmp = new ArrayList();
18+
int i = 0;
19+
if(nums.length-k >= 0){
20+
i = nums.length-k;
21+
for(; i < nums.length; i++){
22+
tmp.add(nums[i]);
23+
}
24+
} else {
25+
i = nums.length-1;
26+
for(; i >= 0; i--){
27+
tmp.add(nums[i]);
28+
}
29+
30+
}
31+
for(i = 0; i < nums.length-k; i++){
32+
tmp.add(nums[i]);
33+
}
34+
for(i = 0; i < tmp.size(); i++){
35+
nums[i] = tmp.get(i);
36+
}
37+
}
38+
39+
public static void main(String...strings){
40+
// int k = 1;
41+
// int[] nums = new int[]{1,2,3};
42+
// int[] nums = new int[]{1};
43+
// int[] nums = new int[]{1,2};
44+
45+
// int k = 3;
46+
// int[] nums = new int[]{1,2};
47+
48+
// int k = 2;
49+
// int[] nums = new int[]{1,2};
50+
51+
int k = 4;
52+
int[] nums = new int[]{1,2,3};
53+
54+
// int k = 2;
55+
// int[] nums = new int[]{-1};
56+
rotate(nums, k);
57+
}
58+
59+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)|[Solution](../../blob/master/MEDIUM/src/medium/MinimumSizeSubarraySum.java)| O(n)|O(1) | Medium|
2323
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)|[Solution](../../blob/master/EASY/src/easy/ReverseLinkedList.java)| O(n)|O(1) | Easy
2424
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Union Find](../../blob/master/MEDIUM/src/medium/NumberOfIslandsUnionFind.java) [DFS](../../blob/master/MEDIUM/src/medium/NumberofIslandsDFS.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS
25+
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[Solution](../../blob/master/EASY/src/easy/RotateArray.java)| O(n)|O(n), could be optimized to O(1) | Easy
2526
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)|[Queue](../../blob/master/MEDIUM/src/medium/BSTIterator_using_q.java) [Stack](../../blob/master/MEDIUM/src/medium/BSTIterator_using_stack.java)| O(1) |O(h) | Medium|
2627
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../../blob/master/HARD/src/hard/WordBreakII.java)| ? |O(n^2) | Hard| Backtracking/DFS
2728
|139|[Word Break](https://leetcode.com/problems/word-break/)|[Solution](../../blob/master/MEDIUM/src/medium/WordBreak.java)| O(n^2)|O(n) | Medium| DP

0 commit comments

Comments
 (0)