Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit04e0517

Browse files
refactor 189
1 parentcf1de47 commit04e0517

File tree

1 file changed

+52
-77
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+52
-77
lines changed
Lines changed: 52 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,64 @@
11
packagecom.fishercoder.solutions;
22

3-
/**
4-
* 189. Rotate Array
5-
6-
Given an array, rotate the array to the right by k steps, where k is non-negative.
7-
8-
Example 1:
9-
Input: [1,2,3,4,5,6,7] and k = 3
10-
Output: [5,6,7,1,2,3,4]
11-
Explanation:
12-
rotate 1 steps to the right: [7,1,2,3,4,5,6]
13-
rotate 2 steps to the right: [6,7,1,2,3,4,5]
14-
rotate 3 steps to the right: [5,6,7,1,2,3,4]
15-
16-
Example 2:
17-
Input: [-1,-100,3,99] and k = 2
18-
Output: [3,99,-1,-100]
19-
Explanation:
20-
rotate 1 steps to the right: [99,-1,-100,3]
21-
rotate 2 steps to the right: [3,99,-1,-100]
22-
23-
Note:
24-
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
25-
Could you do it in-place with O(1) extra space?
26-
* */
27-
283
publicclass_189 {
294

30-
publicstaticclassSolution1 {
31-
/**
32-
* O(n*k) time
33-
* O(1) space
34-
*/
35-
publicvoidrotate(int[]nums,intk) {
36-
inttmp;
37-
for (inti =0;i <k;i++) {
38-
tmp =nums[nums.length -1];
39-
for (intj =nums.length -1;j >0;j--) {
40-
nums[j] =nums[j -1];
5+
publicstaticclassSolution1 {
6+
/**
7+
* O(n*k) time
8+
* O(1) space
9+
*/
10+
publicvoidrotate(int[]nums,intk) {
11+
inttmp;
12+
for (inti =0;i <k;i++) {
13+
tmp =nums[nums.length -1];
14+
for (intj =nums.length -1;j >0;j--) {
15+
nums[j] =nums[j -1];
16+
}
17+
nums[0] =tmp;
18+
}
4119
}
42-
nums[0] =tmp;
43-
}
4420
}
45-
}
4621

47-
publicstaticclassSolution2 {
48-
/**
49-
* using an extra array of the same size to copy it
50-
* O(n) time
51-
* O(n) space
52-
*/
53-
publicvoidrotate(int[]nums,intk) {
54-
intlen =nums.length;
55-
int[]tmp =newint[len];
56-
for (inti =0;i <len;i++) {
57-
tmp[(i +k) %len] =nums[i];
58-
}
59-
for (inti =0;i <len;i++) {
60-
nums[i] =tmp[i];
61-
}
22+
publicstaticclassSolution2 {
23+
/**
24+
* using an extra array of the same size to copy it
25+
* O(n) time
26+
* O(n) space
27+
*/
28+
publicvoidrotate(int[]nums,intk) {
29+
intlen =nums.length;
30+
int[]tmp =newint[len];
31+
for (inti =0;i <len;i++) {
32+
tmp[(i +k) %len] =nums[i];
33+
}
34+
for (inti =0;i <len;i++) {
35+
nums[i] =tmp[i];
36+
}
37+
}
6238
}
63-
}
6439

65-
publicstaticclassSolution3 {
66-
/**
67-
* reverse three times
68-
* O(n) time
69-
* O(1) space
70-
*/
71-
publicvoidrotate(int[]nums,intk) {
72-
intlen =nums.length;
73-
k %=len;
74-
reverse(nums,0,len -1);
75-
reverse(nums,0,k -1);
76-
reverse(nums,k,len -1);
77-
}
40+
publicstaticclassSolution3 {
41+
/**
42+
* reverse three times
43+
* O(n) time
44+
* O(1) space
45+
*/
46+
publicvoidrotate(int[]nums,intk) {
47+
intlen =nums.length;
48+
k %=len;
49+
reverse(nums,0,len -1);
50+
reverse(nums,0,k -1);
51+
reverse(nums,k,len -1);
52+
}
7853

79-
privatevoidreverse(int[]nums,intstart,intend) {
80-
while (start <end) {
81-
inttmp =nums[start];
82-
nums[start] =nums[end];
83-
nums[end] =tmp;
84-
start++;
85-
end--;
86-
}
54+
privatevoidreverse(int[]nums,intstart,intend) {
55+
while (start <end) {
56+
inttmp =nums[start];
57+
nums[start] =nums[end];
58+
nums[end] =tmp;
59+
start++;
60+
end--;
61+
}
62+
}
8763
}
88-
}
8964
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp