189. Rotate Array
Rotate an array of n elements to the right byk steps.
For example, with n = 7 and k = 3, the array
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
[show hint]Hint:
Could you do it in-place with O(1) extra space?
given an array of n lengths, rotate it to the right by k positions.first convert k into the number in [0, n-1]. Flip the entire array, flip the number at the [0, K-1] position, and flip the rest.
note that k is positive
Rotate an array of n elements to the right byk steps.
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]
.Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
[show hint]Hint:
Could you do it in-place with O(1) extra space?
given an array of n lengths, rotate it to the right by k positions.first convert k into the number in [0, n-1]. Flip the entire array, flip the number at the [0, K-1] position, and flip the rest.
note that k is positive
public void rotate(int[] nums, int k) {
int len = nums.length;
// Ensure that k is positive
k = (len + (k % len)) % len;
// Flip the entire array.
rotateCore(nums, 0, len - 1);
// Flip the array of subscript 0~k-1.
rotateCore(nums, 0, k - 1);
// Flip the array of subscript k~len-1.
rotateCore(nums, k, len - 1);
}
/**
* @param nums
* Flip the array (the subscript is closed front to back).
* @param start
* Subscript to the beginning of the flipped array
* @param end
* End subscript for flipping arrays
*/
private void rotateCore(int[] nums, int start, int end) {
for (int i = start, j = end; i < j; i++, j--) {
nums[i] = nums[i] ^ nums[j];
nums[j] = nums[i] ^ nums[j];
nums[i] = nums[i] ^ nums[j];
}
}
Read More:
- Leetcode 34 finds the first and last position of an element in the sorted array (medium)
- Array of PHP_ diff,array_ intersect,array_ merge, in_ Is there a limit on the number of arrays in array?
- LeetCode 23. Merge k Sorted Lists(java)
- Leetcode: 7. Reverse Integer(JAVA)
- Error: Java: error: release version 5 not supported solution to run error
- [solved] error: valueerror: expected 2D array, got scalar array instead
- Solution to null pointer error in array
- Two dimensional array and pointer to one dimensional array
- “Could not import PIL.Image . The use of array_ to_ The solution of img requires PIL. “
- This version of chromedriver only supports chrome version 92 crawler simulates the problem of Google plug-in version when the browser clicks and reports an error
- Variable type error: error of array occurs when TP5 controller receives the array of post
- Leetcode 832. Flip image
- The sum of the two numbers of leetcode
- Uninstall JDK and run Java – version with error
- LeetCode 332. Reconstruct Itinerary
- Java:java.lang.OutOfMemoryError : GC overhead limit exceeded solution
- Idea community version starts Tomcat to report error running ‘XXXXXX’: java.lang.NullPointerException
- Registry key Error: Java version has value ‘1.8‘, but ‘1.7‘ is required
- If JavaScript exceeds the length of the array, no error will be reported
- Android studio error Error:Could not Determine java version from ‘9.0.4’