Skip to content

Commit 42224d2

Browse files
committed
commit
Change-Id: I691b70a8c8e75476ab877915ebe2dfc7f7a0dc34
1 parent 1b0d9b2 commit 42224d2

File tree

17 files changed

+492
-57
lines changed

17 files changed

+492
-57
lines changed

.idea/modules.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 65 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../lib/junit-4.12.jar!/" />
14+
<root url="jar://$MODULE_DIR$/../lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 19:24
6+
* CSDN: http://blog.csdn.net/derrantcm
7+
* Github: https://github.com/Wang-Jun-Chao
8+
* Declaration: All Rights Reserved !!!
9+
**/
10+
public class Solution {
11+
public int findUnsortedSubarray(int[] nums) {
12+
if (nums == null || nums.length < 2) {
13+
return 0;
14+
}
15+
16+
int[] sorted = Arrays.copyOf(nums, nums.length);
17+
Arrays.sort(sorted);
18+
19+
20+
int diffStart = 0; // 从开始找第一个位置变化的下标
21+
while (diffStart < nums.length && nums[diffStart] == sorted[diffStart]) {
22+
diffStart++;
23+
}
24+
25+
if (diffStart >= nums.length) {
26+
return 0;
27+
}
28+
29+
30+
int diffEnd = nums.length - 1; // 从最后找第一个位置变化的下标
31+
while (diffEnd >= 0 && nums[diffEnd] == sorted[diffEnd]) {
32+
diffEnd--;
33+
}
34+
35+
36+
37+
return diffEnd - diffStart + 1;
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import org.junit.Assert;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 19:32
6+
* CSDN: http://blog.csdn.net/derrantcm
7+
* Github: https://github.com/Wang-Jun-Chao
8+
* Declaration: All Rights Reserved !!!
9+
**/
10+
public class SolutionTest {
11+
12+
@org.junit.Test
13+
public void findUnsortedSubarray() {
14+
Solution s = new Solution();
15+
Object[][] data = {
16+
{new int[]{2, 6, 4, 8, 10, 9, 15}, 5},
17+
{new int[]{1, 2, 3, 4, 5, 6, 7}, 0},
18+
{new int[]{7, 6, 5, 4, 3, 2, 1}, 7},
19+
};
20+
21+
for (Object[] d : data) {
22+
Object result = s.findUnsortedSubarray((int[]) d[0]);
23+
Assert.assertEquals(d[1], result);
24+
}
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../lib/junit-4.12.jar!/" />
14+
<root url="jar://$MODULE_DIR$/../lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Author: 王俊超
3+
* Time: 2020-07-04 19:42
4+
* CSDN: http://blog.csdn.net/derrantcm
5+
* Github: https://github.com/Wang-Jun-Chao
6+
* Declaration: All Rights Reserved !!!
7+
**/
8+
public class Solution {
9+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
10+
if (flowerbed == null || flowerbed.length < 1) {
11+
return false;
12+
}
13+
14+
if (n < 1) {
15+
return true;
16+
}
17+
18+
// 找第一个1的位置
19+
int firstOneIdx = 0;
20+
while (firstOneIdx < flowerbed.length && flowerbed[firstOneIdx] == 0) {
21+
firstOneIdx++;
22+
}
23+
24+
// [0,0,0] => 2
25+
// [0,0] => 1
26+
// [0] => 0
27+
int num = firstOneIdx / 2 + firstOneIdx % 2;
28+
29+
if (firstOneIdx >= flowerbed.length) {
30+
return num >= n;
31+
} else {
32+
// [0,0,0,1] => 1
33+
// [0,0,1] => 1
34+
// [0,1] => 0
35+
num -= firstOneIdx % 2;
36+
}
37+
38+
int prev = flowerbed[firstOneIdx];
39+
for (int i = firstOneIdx + 1; i < flowerbed.length; i++) {
40+
if (prev == 0 && flowerbed[i] == 0) {
41+
// i是最后一个,或者下一个位置也没有种花
42+
if (i + 1 >= flowerbed.length || flowerbed[i + 1] == 0) {
43+
num++;
44+
flowerbed[i] = 1;
45+
}
46+
}
47+
48+
prev = flowerbed[i];
49+
}
50+
51+
52+
return num >= n;
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import org.junit.Assert;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 19:52
6+
* CSDN: http://blog.csdn.net/derrantcm
7+
* Github: https://github.com/Wang-Jun-Chao
8+
* Declaration: All Rights Reserved !!!
9+
**/
10+
public class SolutionTest {
11+
12+
@org.junit.Test
13+
public void canPlaceFlowers() {
14+
Solution s = new Solution();
15+
Object[][] data = {
16+
{new int[]{1, 0, 0, 0, 1}, 1, true},
17+
{new int[]{1, 0, 0, 0, 1}, 2, false},
18+
{new int[]{1, 0, 1, 0, 1}, 1, false},
19+
{new int[]{1, 0, 1, 0, 0}, 2, false},
20+
{new int[]{0, 0, 0, 0, 0}, 3, true},
21+
{new int[]{1, 0, 0, 0, 0, 1}, 2, false},
22+
{new int[]{1, 0, 0, 0, 0, 1}, 2, false},
23+
{new int[]{ 0, 0, 1, 0, 1}, 1, true},
24+
};
25+
26+
for (Object[] d : data) {
27+
Object result = s.canPlaceFlowers((int[]) d[0], (Integer) d[1]);
28+
Assert.assertEquals(d[2], result);
29+
}
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../lib/junit-4.12.jar!/" />
14+
<root url="jar://$MODULE_DIR$/../lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Author: 王俊超
3+
* Time: 2020-07-04 20:09
4+
* CSDN: http://blog.csdn.net/derrantcm
5+
* Github: https://github.com/Wang-Jun-Chao
6+
* Declaration: All Rights Reserved !!!
7+
**/
8+
public class Solution {
9+
public String tree2str(TreeNode t) {
10+
StringBuilder builder = new StringBuilder(100);
11+
tree2str(t, builder);
12+
return builder.toString();
13+
}
14+
15+
private void tree2str(TreeNode root, StringBuilder builder) {
16+
if (root == null) {
17+
return;
18+
}
19+
20+
builder.append(root.val);
21+
22+
if (root.left == null && root.right == null) {
23+
return;
24+
}
25+
26+
if (root.left != null && root.right == null) {
27+
builder.append("(");
28+
tree2str(root.left, builder);
29+
builder.append(")");
30+
}
31+
32+
if (root.right != null) {
33+
builder.append("(");
34+
tree2str(root.left, builder);
35+
builder.append(")");
36+
builder.append("(");
37+
tree2str(root.right, builder);
38+
builder.append(")");
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)