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

Appearance settings

Commit 42224d2

Browse filesBrowse files
committed
commit
Change-Id: I691b70a8c8e75476ab877915ebe2dfc7f7a0dc34
1 parent 1b0d9b2 commit 42224d2
Copy full SHA for 42224d2

File tree

Expand file treeCollapse file tree

17 files changed

+492
-57
lines changed
Filter options
Expand file treeCollapse file tree

17 files changed

+492
-57
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+5Lines 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

Copy file name to clipboardExpand all lines: .idea/workspace.xml
+65-57Lines changed: 65 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+21Lines 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>
+39Lines 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+
}
+26Lines 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+
}
+21Lines 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>
+54Lines 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+
}
+31Lines 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+
}
+21Lines 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>
+41Lines 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+
}
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.junit.Assert;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 20:12
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 tree2str() {
14+
TreeNode root = new TreeNode(1);
15+
root.left = new TreeNode(2);
16+
root.right = new TreeNode(3);
17+
root.left.left = new TreeNode(4);
18+
19+
Solution s = new Solution();
20+
Assert.assertEquals("1(2(4))(3)", s.tree2str(root));
21+
}
22+
23+
@org.junit.Test
24+
public void tree2str2() {
25+
TreeNode root = new TreeNode(1);
26+
root.left = new TreeNode(2);
27+
root.right = new TreeNode(3);
28+
root.left.right = new TreeNode(4);
29+
30+
Solution s = new Solution();
31+
Assert.assertEquals("1(2()(4))(3)", s.tree2str(root));
32+
}
33+
}
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Author: 王俊超
3+
* Date: 2015-08-21
4+
* Time: 18:45
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class TreeNode {
8+
int val;
9+
TreeNode left;
10+
TreeNode right;
11+
12+
TreeNode(int x) {
13+
val = x;
14+
}
15+
}
16+
+21Lines 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>
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 20:23
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 maximumProduct(int[] nums) {
12+
if (nums == null || nums.length < 3) {
13+
return 0;
14+
}
15+
16+
Arrays.sort(nums);
17+
18+
int len = nums.length;
19+
20+
int max = nums[len - 1] * nums[len - 2] * nums[len - 3];
21+
22+
if (nums[1] < 0) { // 前两个是负数
23+
max = Math.max(max, nums[0]*nums[1]*nums[len-1]);
24+
}
25+
26+
27+
return max;
28+
}
29+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.