GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-3/problems/src/array/RotateArray.java at master · abcdvemm/leetcode-3

Expand file tree

/

RotateArray.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
42 lines (39 loc) · 1.08 KB
/

RotateArray.java

Copy path

File metadata and controls

42 lines (39 loc) · 1.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package array;
/**
* Created by gouthamvidyapradhan on 01/08/2017. Rotate an array of n elements to the right by k
* steps.
*
* <p>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].
*
* <p>Note: Try to come up as many solutions as you can, there are at least 3 different ways to
* solve this problem.
*
* <p>Hint: Could you do it in-place with O(1) extra space? Related problem: Reverse Words in a
* String II
*/
public class RotateArray {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
int[] A = {1, 2, 3, 4, 5, 6};
new RotateArray().rotate(A, 2);
for (int i : A) System.out.print(i + " ");
}
public void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
private void reverse(int[] nums, int s, int e) {
for (int i = s, j = e; i < j; i++, j--) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.