Skip to content

Commit 0596cb8

Browse files
push all leetcode source to github
0 parents  commit 0596cb8

File tree

244 files changed

+5383
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

244 files changed

+5383
-0
lines changed

leetcode.iml

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$/../leetcode">
6+
<sourceFolder url="file://$MODULE_DIR$/../leetcode/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://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
14+
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.4 KB
Binary file not shown.
859 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
747 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
989 Bytes
Binary file not shown.
742 Bytes
Binary file not shown.
Binary file not shown.
972 Bytes
Binary file not shown.
985 Bytes
Binary file not shown.
948 Bytes
Binary file not shown.
952 Bytes
Binary file not shown.
Binary file not shown.
1.08 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.19 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
953 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
919 Bytes
Binary file not shown.
935 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
788 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.89 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.08 KB
Binary file not shown.
Binary file not shown.
2.37 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.74 KB
Binary file not shown.
1.66 KB
Binary file not shown.
1.47 KB
Binary file not shown.
1.38 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
752 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3.39 KB
Binary file not shown.
1.64 KB
Binary file not shown.
2.65 KB
Binary file not shown.
361 Bytes
Binary file not shown.
1007 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
780 Bytes
Binary file not shown.

src/BestTimetoBuyandSellStock.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* @author zhangyu
5+
* @version V1.0
6+
* @ClassName: BestTimetoBuyandSellStock
7+
* @Description: TOTO
8+
* @date 2018/12/6 15:28
9+
**/
10+
/*
11+
做这种题目时候,要先进行判断,然后再动手写算法,防止边界问题的出现
12+
*/
13+
14+
public class BestTimetoBuyandSellStock {
15+
@Test
16+
public void fun() {
17+
int[] nums = {7, 1, 5, 3, 6, 4};
18+
int maxProfit = maxProfit(nums);
19+
System.out.println(maxProfit);
20+
}
21+
22+
private int maxProfit(int[] prices) {
23+
if (prices.length < 1) {
24+
return 0;
25+
}
26+
int maxProfit = 0;
27+
for (int i = 0; i < prices.length - 1; i++) {
28+
for (int j = i + 1; j < prices.length; j++) {
29+
int profit = prices[j] - prices[i];
30+
if (maxProfit < profit) {
31+
maxProfit = profit;
32+
}
33+
}
34+
}
35+
return maxProfit;
36+
}
37+
}

src/BestTimetoBuyandSellStock2.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* @author zhangyu
5+
* @version V1.0
6+
* @ClassName: BestTimetoBuyandSellStock
7+
* @Description: TOTO
8+
* @date 2018/12/6 15:28
9+
**/
10+
11+
12+
public class BestTimetoBuyandSellStock2 {
13+
@Test
14+
public void fun() {
15+
int[] nums = {7, 1, 5, 3, 6, 4};
16+
int maxProfit = maxProfit(nums);
17+
System.out.println(maxProfit);
18+
}
19+
20+
// 找出最大的profit
21+
private int maxProfit(int[] prices) {
22+
// 如果数组的长度为0就直接返回
23+
if (prices.length == 0) {
24+
return 0;
25+
}
26+
27+
// 定义一个非变量,数组的下标
28+
final int pricesIndex = prices.length - 1;
29+
// 记录一个标记位,把最后一个数设置为最大值
30+
int maxPrice = prices[pricesIndex];
31+
// 把最大收益设置为0
32+
int maxProfit = 0;
33+
for (int i = pricesIndex - 1; i >= 0; i--) {
34+
if (prices[i] > maxPrice) {
35+
// 更新maxPrice的值,把它更新
36+
maxPrice = prices[i];
37+
}
38+
// 更新最大收益值
39+
maxProfit = Math.max(maxProfit, maxPrice - prices[i]);
40+
// maxProfit=maxProfit>(maxPrice-prices[i])?maxProfit:(maxPrice-prices[i]);
41+
}
42+
return maxProfit;
43+
}
44+
}

src/GenerateParentheses1.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import org.junit.Test;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author zhangyu
8+
* @version V1.0
9+
* @ClassName: GenerateParentheses1
10+
* @Description: TOTO
11+
* @date 2018/12/15 14:31
12+
**/
13+
14+
15+
public class GenerateParentheses1 {
16+
@Test
17+
public void fun() {
18+
int n = 3;
19+
List<String> list = generateParentheses(n);
20+
System.out.println(list);
21+
}
22+
23+
private List<String> generateParentheses(int n) {
24+
List<String> list = new ArrayList<>();
25+
if (n < 1) {
26+
return null;
27+
}
28+
bracketTrace(list, "", 0, 0, n);
29+
return list;
30+
}
31+
32+
private void bracketTrace(List<String> list, String s, int left, int right, int n) {
33+
if (s.length() == n * 2) {
34+
list.add(s);
35+
return;
36+
}
37+
if (left < n)
38+
bracketTrace(list, s + "(", left + 1, right, n);
39+
if (right < left)
40+
bracketTrace(list, s + ")", left, right + 1, n);
41+
}
42+
}

src/LongestValidParentheses.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import org.junit.Test;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author zhangyu
7+
* @version V1.0
8+
* @ClassName: LongestValidParentheses
9+
* @Description: TOTO
10+
* @date 2018/12/17 19:29
11+
**/
12+
13+
14+
public class LongestValidParentheses {
15+
@Test
16+
public void fun() {
17+
String s = ")()())";
18+
int length = longestValidParentheses(s);
19+
System.out.println(length);
20+
}
21+
22+
private int longestValidParentheses(String s) {
23+
Stack<Character> stack = new Stack<>();
24+
int count = 0;
25+
int max = 0;
26+
char chs[] = s.toCharArray();
27+
for (int i = 0; i < s.length(); i++) {
28+
stack.push(chs[i]);
29+
if (!stack.isEmpty()) {
30+
char c = stack.peek();
31+
if (c == '(') {
32+
stack.pop();
33+
count++;
34+
}
35+
}
36+
if (stack.isEmpty()) {
37+
if (max < count) {
38+
max = count;
39+
}
40+
count = 0;
41+
}
42+
}
43+
return max * 2;
44+
}
45+
}

src/WordBreak.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import org.junit.Test;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author zhangyu
8+
* @version V1.0
9+
* @ClassName: WrodBreak
10+
* @Description: TOTO
11+
* @date 2018/12/13 13:53
12+
**/
13+
14+
15+
public class WordBreak {
16+
@Test
17+
public void fun() {
18+
String s = "applepenapple";
19+
List<String> wordDict = new ArrayList<>();
20+
wordDict.add("apple");
21+
wordDict.add("pen");
22+
boolean flag = wordBreak2(s, wordDict);
23+
System.out.println(flag);
24+
}
25+
26+
// 这个有问题
27+
/*private boolean wordBreak(String s, List<String> wordDict) {
28+
for (String word : wordDict) {
29+
if (s.contains(word)) {
30+
// int index = s.indexOf(word);
31+
// s = s.substring(index, index + word.length());
32+
s = s.replaceFirst(word, "");
33+
}
34+
}
35+
if (s.equals("") || wordDict.contains(s)) {
36+
return true;
37+
}
38+
return false;
39+
}*/
40+
41+
private boolean wordBreak2(String s, List<String> wordDict) {
42+
boolean[] f = new boolean[s.length() + 1];
43+
f[0] = true;
44+
for (int i = 1; i <= s.length(); i++) {
45+
for (int j = 0; j < i; j++) {
46+
if (f[j] && wordDict.contains(s.substring(j, i))) {
47+
f[i] = true;
48+
break;
49+
}
50+
}
51+
}
52+
return f[s.length()];
53+
}
54+
}

src/leetcode/ArrayTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author zhangyu
7+
* @version V1.0
8+
* @ClassName: ArrayTest
9+
* @Description: TOTO
10+
* @date 2018/12/17 21:28
11+
**/
12+
13+
14+
public class ArrayTest {
15+
@Test
16+
public void fun() {
17+
int sum1 = getSum(1, 2, 3, 4);
18+
int sum2 = getSum(1, 2, 3);
19+
System.out.println(sum1);
20+
System.out.println(sum2);
21+
}
22+
23+
private int getSum(int... arr) {
24+
int sum = 0;
25+
for (int num : arr) {
26+
sum += num;
27+
}
28+
return sum;
29+
}
30+
}

src/leetcode/ArraysSortTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
8+
/**
9+
* @author zhangyu
10+
* @version V1.0
11+
* @ClassName: ArraysSortTest
12+
* @Description: 测试Arrays.sort的逆序排序
13+
* @date 2018/12/10 14:06
14+
**/
15+
16+
// Arrays.sort()可以使用内部的比较器进行比较,也可以自己定义比较器进行逆序排序
17+
public class ArraysSortTest {
18+
@Test
19+
public void fun() {
20+
Integer[] nums = {5, 2, 1, 3, 4, 9, 0, 7, 8, 6};
21+
Comparator<Integer> comparator = new MyComparator();
22+
Arrays.sort(nums, comparator);
23+
System.out.println(Arrays.toString(nums));
24+
}
25+
}
26+
27+
class MyComparator implements Comparator<Integer> {
28+
@Override
29+
public int compare(Integer a, Integer b) {
30+
if (a > b) {
31+
return -1;
32+
} else if (a == b) {
33+
return 0;
34+
} else {
35+
return 1;
36+
}
37+
}
38+
39+
}

src/leetcode/ArraysSortTest2.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package leetcode;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
8+
/**
9+
* @author zhangyu
10+
* @version V1.0
11+
* @ClassName: ArraysSortTest
12+
* @Description: 测试Arrays.sort的逆序排序
13+
* @date 2018/12/10 14:06
14+
**/
15+
16+
// Arrays.sort()可以使用内部的比较器进行比较,也可以自己定义比较器进行逆序排序
17+
public class ArraysSortTest2 {
18+
@Test
19+
public void fun() {
20+
Integer[] nums = {5, 2, 1, 3, 4, 9, 0, 7, 8, 6};
21+
// Comparator<Integer> comparator = new MyComparator();
22+
Arrays.sort(nums, new Comparator<Integer>() {
23+
@Override
24+
public int compare(Integer a, Integer b) {
25+
if (a > b) {
26+
return -1;
27+
} else if (a == b) {
28+
return 0;
29+
} else {
30+
return 1;
31+
}
32+
}
33+
});
34+
System.out.println(Arrays.toString(nums));
35+
}
36+
}
37+

src/leetcode/ArraysSortTest3.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
8+
/**
9+
* @author zhangyu
10+
* @version V1.0
11+
* @ClassName: ArraysSortTest
12+
* @Description: 测试Arrays.sort的逆序排序
13+
* @date 2018/12/10 14:06
14+
**/
15+
16+
// Arrays.sort()可以使用内部的比较器进行比较,也可以自己定义比较器进行逆序排序
17+
public class ArraysSortTest3 {
18+
@Test
19+
public void fun() {
20+
Integer[] nums = {5, 2, 1, 3, 4, 9, 0, 7, 8, 6};
21+
Arrays.sort(nums, new Comparator<Integer>() {
22+
@Override
23+
public int compare(Integer a, Integer b) {
24+
return b - a;
25+
}
26+
});
27+
System.out.println(Arrays.toString(nums));
28+
}
29+
}
30+

0 commit comments

Comments
 (0)