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

Commit8cf5335

Browse files
committed
01 (1) recover
1 parent1027d25 commit8cf5335

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

‎.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.classpath
2+
.project
3+
bin
4+
*.swp
5+
*.class
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Time : O(N) ; Space: O(1)
3+
* @tag : Array
4+
* @by : Steven Cooks
5+
* @date: Aug 23, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given a rotated sorted array, recover it to sorted array in-place.
10+
*
11+
***************************************************************************
12+
* {@link http://www.lintcode.com/en/problem/recover-rotated-sorted-array/ }
13+
*/
14+
package_01_RecoverRotatedSortedArray;
15+
16+
importjava.util.ArrayList;
17+
18+
/** see test {@link _01_RecoverRotatedSortedArray.SolutionTest } */
19+
publicclassSolution {
20+
21+
publicvoidrecoverRotatedSortedArray(ArrayList<Integer>nums) {
22+
if (nums ==null ||nums.size() <=1) {
23+
return;
24+
}
25+
while (true) {
26+
if (nums.get(0) >nums.get(1)) {
27+
break;
28+
}
29+
nums.add(nums.get(0));
30+
nums.remove(0);
31+
}
32+
nums.add(nums.get(0));
33+
nums.remove(0);
34+
}
35+
36+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package_01_RecoverRotatedSortedArray;
2+
3+
importstaticorg.junit.Assert.*;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.Arrays;
7+
8+
importorg.junit.After;
9+
importorg.junit.Before;
10+
importorg.junit.Rule;
11+
importorg.junit.Test;
12+
importorg.junit.rules.Timeout;
13+
14+
import_01_RecoverRotatedSortedArray.Solution;
15+
16+
publicclassSolutionTest {
17+
18+
/** Test method for {@link _01_RecoverRotatedSortedArray.Solution } */
19+
Solutionsolution;
20+
21+
@Rule
22+
publicTimeoutglobalTimeout =newTimeout(200);
23+
24+
@Before
25+
publicvoidsetUp()throwsException {
26+
solution =newSolution();
27+
}
28+
29+
@After
30+
publicvoidtearDown()throwsException {
31+
solution =null;
32+
}
33+
34+
@Test
35+
publicvoidTest1() {
36+
ArrayList<Integer>nums =newArrayList<>(Arrays.asList(4,5,1,2,3));
37+
solution.recoverRotatedSortedArray(nums);
38+
ArrayList<Integer>exps =newArrayList<>(Arrays.asList(1,2,3,4,5));
39+
assertEquals(exps,nums);
40+
}
41+
42+
@Test
43+
publicvoidTest2() {
44+
ArrayList<Integer>nums =newArrayList<>(Arrays.asList(5,1,2,3,4));
45+
solution.recoverRotatedSortedArray(nums);
46+
ArrayList<Integer>exps =newArrayList<>(Arrays.asList(1,2,3,4,5));
47+
assertEquals(exps,nums);
48+
}
49+
50+
@Test
51+
publicvoidTest3() {
52+
ArrayList<Integer>nums =newArrayList<>(Arrays.asList(1,2,3,4,5));
53+
solution.recoverRotatedSortedArray(nums);
54+
ArrayList<Integer>exps =newArrayList<>(Arrays.asList(1,2,3,4,5));
55+
assertEquals(exps,nums);
56+
}
57+
58+
@Test
59+
publicvoidTest4() {
60+
ArrayList<Integer>nums =newArrayList<>(Arrays.asList(3,4,5,1,2));
61+
solution.recoverRotatedSortedArray(nums);
62+
ArrayList<Integer>exps =newArrayList<>(Arrays.asList(1,2,3,4,5));
63+
assertEquals(exps,nums);
64+
}
65+
66+
@Test
67+
publicvoidTest5() {
68+
ArrayList<Integer>nums =newArrayList<>(Arrays.asList(1,1,1,1,1,
69+
1,1,1,1, -1,1,1,1,1,1,1,1,1,1,1,1));
70+
solution.recoverRotatedSortedArray(nums);
71+
ArrayList<Integer>exps =newArrayList<>(Arrays.asList(-1,1,1,1,1,
72+
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1));
73+
assertEquals(exps,nums);
74+
}
75+
76+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp