Skip to content

Commit

Permalink
Updated Array Rotation problems
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Mar 19, 2019
1 parent 47f5dd9 commit 60d84d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ public static void main(String[] args) {
}

/* Reverses array from "start" to "end" inclusive */
private static void reverse(int [] array, int start, int end) {
if (array == null || start < 0 || start >= array.length ||
end < 0 || end >= array.length || start >= end) {
private static void reverse(int[] array, int start, int end) {
if (array == null || start < 0 || start >= array.length || end < 0 || end >= array.length) {
return;
}
int mid = (start + end) / 2;
for (int i = start; i <= mid; i++) {
int offset = i - start;
swap(array, start + offset, end - offset);
while (start < end) {
swap(array, start++, end--);
}
}

Expand Down
11 changes: 4 additions & 7 deletions Data Structures/Arrays/Left Rotation/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ public static void main(String[] args) {
}

/* Reverses array from "start" to "end" inclusive */
private static void reverse(int [] array, int start, int end) {
if (array == null || start < 0 || start >= array.length ||
end < 0 || end >= array.length || start >= end) {
private static void reverse(int[] array, int start, int end) {
if (array == null || start < 0 || start >= array.length || end < 0 || end >= array.length) {
return;
}
int mid = (start + end) / 2;
for (int i = start; i <= mid; i++) {
int offset = i - start;
swap(array, start + offset, end - offset);
while (start < end) {
swap(array, start++, end--);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ public static void main(String[] args) {
}

/* Reverses array from "start" to "end" inclusive */
private static void reverse(int [] array, int start, int end) {
if (array == null || start < 0 || start >= array.length ||
end < 0 || end >= array.length || start >= end) {
private static void reverse(int[] array, int start, int end) {
if (array == null || start < 0 || start >= array.length || end < 0 || end >= array.length) {
return;
}
int mid = (start + end) / 2;
for (int i = start; i <= mid; i++) {
int offset = i - start;
swap(array, start + offset, end - offset);
while (start < end) {
swap(array, start++, end--);
}
}

Expand Down

0 comments on commit 60d84d6

Please sign in to comment.