Skip to content

Commit

Permalink
feat: Rotate Right Array
Browse files Browse the repository at this point in the history
  • Loading branch information
rldnrl committed Oct 23, 2022
1 parent d594ec4 commit 4ece6af
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/leetcode/RotateRightArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package leetcode;

import java.util.Arrays;

public class RotateRightArray {
public static void main(String[] args) {
int[] test1 = new int[] {1,2,3,4,5,6,7};
rotateRight(test1, 3);
System.out.println(Arrays.toString(test1));
}

public static void rotateRight(int[] nums, int k) {
int size = nums.length;
int[] copiedNums = Arrays.copyOf(nums, size);

k = k % size;
for (int i = 0; i < size; i++) {
nums[k] = copiedNums[i];
k = (k + 1) % size;
}
}
}
21 changes: 21 additions & 0 deletions test/leetcode/RotateRightArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package leetcode;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static leetcode.RotateRightArray.rotateRight;

public class RotateRightArrayTest {
@Test
void rotateRightTest() {
int[] testNums1 = new int[] {1, 2, 3, 4, 5, 6, 7};
int[] testNums1Result = new int[] {5,6,7,1,2,3,4};
rotateRight(testNums1, 3);
Assertions.assertArrayEquals(testNums1, testNums1Result);

int[] testNum2 = new int[] {-1,-100,3,99};
int[] testNum2Result = new int[] {3,99,-1,-100};
rotateRight(testNum2, 2);
Assertions.assertArrayEquals(testNum2, testNum2Result);
}
}

0 comments on commit 4ece6af

Please sign in to comment.