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

Commit2fe08c3

Browse files
add a solution for 189
1 parent1efa219 commit2fe08c3

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

‎src/main/java/com/fishercoder/solutions/_189.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,28 @@ private void reverse(int[] nums, int start, int end) {
6161
}
6262
}
6363
}
64+
65+
publicstaticclassSolution4 {
66+
/**
67+
* O(n) time
68+
* O(1) space
69+
* The most optimal, we can safely ignore all the above three solutions... :)
70+
*/
71+
publicvoidrotate(int[]nums,intk) {
72+
k =k %nums.length;
73+
intcount =0;
74+
for (intstart =0;count <nums.length;start++) {
75+
intcurrent =start;
76+
intprev =nums[start];
77+
do {
78+
intnextIndex = (current +k) %nums.length;
79+
inttmp =nums[nextIndex];
80+
nums[nextIndex] =prev;
81+
prev =tmp;
82+
current =nextIndex;
83+
count++;
84+
}while (start !=current);
85+
}
86+
}
87+
}
6488
}
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,59 @@
11
packagecom.fishercoder;
22

3-
importcom.fishercoder.common.utils.CommonUtils;
43
importcom.fishercoder.solutions._189;
54
importorg.junit.BeforeClass;
65
importorg.junit.Test;
76

7+
importstaticorg.junit.Assert.assertArrayEquals;
8+
89
publicclass_189Test {
910
privatestatic_189.Solution1solution1;
1011
privatestatic_189.Solution2solution2;
1112
privatestatic_189.Solution3solution3;
13+
privatestatic_189.Solution4solution4;
1214
privatestaticint[]nums;
1315

1416
@BeforeClass
1517
publicstaticvoidsetup() {
1618
solution1 =new_189.Solution1();
1719
solution2 =new_189.Solution2();
1820
solution3 =new_189.Solution3();
21+
solution4 =new_189.Solution4();
1922
}
2023

2124
@Test
2225
publicvoidtest1() {
2326
nums =newint[]{1,2,3};
2427
solution1.rotate(nums,1);
25-
CommonUtils.printArray(nums);
28+
assertArrayEquals(newint[]{3,1,2},nums);
2629
}
2730

2831
@Test
2932
publicvoidtest2() {
3033
nums =newint[]{1,2,3};
3134
solution2.rotate(nums,1);
32-
CommonUtils.printArray(nums);
35+
assertArrayEquals(newint[]{3,1,2},nums);
3336
}
3437

3538
@Test
3639
publicvoidtest3() {
3740
nums =newint[]{1,2,3};
3841
solution3.rotate(nums,1);
39-
CommonUtils.printArray(nums);
42+
assertArrayEquals(newint[]{3,1,2},nums);
43+
}
44+
45+
@Test
46+
publicvoidtest4() {
47+
nums =newint[]{1,2,3};
48+
solution4.rotate(nums,1);
49+
assertArrayEquals(newint[]{3,1,2},nums);
50+
}
51+
52+
@Test
53+
publicvoidtest5() {
54+
nums =newint[]{-1, -100,3,99};
55+
solution4.rotate(nums,2);
56+
assertArrayEquals(newint[]{3,99, -1, -100},nums);
4057
}
4158

4259
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp