Skip to content

Commit 39ec46f

Browse files
committed
Rotate array done + some minor refactorings
1 parent 3571327 commit 39ec46f

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.leetcode.arrays;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Problem: https://leetcode.com/problems/rotate-array/
7+
*
8+
* @author rampatra
9+
* @since 2019-04-20
10+
*/
11+
public class RotateArray {
12+
13+
/**
14+
* Time complexity: O(n)
15+
* where,
16+
* n = no. of elements in the array
17+
* <p>
18+
* Runtime: <a href="https://leetcode.com/submissions/detail/224425565/">0 ms</a>.
19+
*
20+
* @param nums
21+
* @param k
22+
*/
23+
public static void rotate(int[] nums, int k) {
24+
25+
if (k > nums.length) {
26+
k = k % nums.length;
27+
}
28+
29+
reverse(nums, 0, nums.length);
30+
reverse(nums, 0, k);
31+
reverse(nums, k, nums.length);
32+
}
33+
34+
private static void reverse(int[] nums, int start, int end) {
35+
int temp;
36+
for (int i = start, j = end - 1; i < j; i++, j--) {
37+
temp = nums[i];
38+
nums[i] = nums[j];
39+
nums[j] = temp;
40+
}
41+
}
42+
43+
public static void main(String[] args) {
44+
// normal case
45+
int[] arr = {1, 2, 3, 4, 5, 6, 7};
46+
System.out.println(Arrays.toString(arr));
47+
rotate(arr, 3);
48+
System.out.println(Arrays.toString(arr));
49+
50+
// edge cases
51+
arr = new int[]{1, 2};
52+
System.out.println(Arrays.toString(arr));
53+
rotate(arr, 2);
54+
System.out.println(Arrays.toString(arr)); // should be [1, 2]
55+
56+
arr = new int[]{1, 2};
57+
System.out.println(Arrays.toString(arr));
58+
rotate(arr, 3);
59+
System.out.println(Arrays.toString(arr)); // should be [2, 1]
60+
61+
arr = new int[]{1, 2, 3};
62+
System.out.println(Arrays.toString(arr));
63+
rotate(arr, 4);
64+
System.out.println(Arrays.toString(arr)); // should be [3, 1, 2]
65+
66+
arr = new int[]{1};
67+
System.out.println(Arrays.toString(arr));
68+
rotate(arr, 2);
69+
System.out.println(Arrays.toString(arr));
70+
}
71+
}

src/main/java/com/leetcode/strings/AnagramsInString.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.List;
66

77
/**
8-
* Leetcode Problem: https://leetcode.com/problems/find-all-anagrams-in-a-string/
8+
* Problem: https://leetcode.com/problems/find-all-anagrams-in-a-string/
99
*
1010
* @author rampatra
1111
* @since 2019-04-13

src/main/java/com/leetcode/strings/ReverseVowels.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.leetcode.strings;
22

33
/**
4-
* Leetcode Problem: https://leetcode.com/problems/reverse-vowels-of-a-string/
4+
* Problem: https://leetcode.com/problems/reverse-vowels-of-a-string/
55
*
66
* @author rampatra
77
* @since 2019-04-19

src/main/java/com/leetcode/strings/UniqueCharacterInString.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.leetcode.strings;
22

33
/**
4-
* Leetcode Problem: https://leetcode.com/problems/first-unique-character-in-a-string/
4+
* Problem: https://leetcode.com/problems/first-unique-character-in-a-string/
55
*
66
* @author rampatra
77
* @since 2019-04-16

src/main/java/com/leetcode/strings/ValidPalindrome.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.leetcode.strings;
22

33
/**
4+
* Problem: https://leetcode.com/problems/valid-palindrome/
5+
*
46
* @author rampatra
57
* @since 2019-04-19
68
*/

src/main/java/com/leetcode/trie/LongestWord.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Stack;
55

66
/**
7-
* Leetcode Problem: https://leetcode.com/problems/longest-word-in-dictionary/
7+
* Problem: https://leetcode.com/problems/longest-word-in-dictionary/
88
*
99
* @author rampatra
1010
* @since 2019-04-10

0 commit comments

Comments
 (0)