-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |