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

Commit 0554cec

Browse files
committed
20190626
1 parent eb42850 commit 0554cec

File tree

18 files changed

+379
-9
lines changed

18 files changed

+379
-9
lines changed

‎code/lc1028.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package code;
2+
3+
import java.util.Stack;
4+
/*
5+
* 1028. Recover a Tree From Preorder Traversal
6+
* 题意:从先序遍历恢复二叉树
7+
* 难度:Hard
8+
* 分类:Depth-first Search
9+
* 思路:用栈存储,迭代比较就可以了
10+
* Tips:
11+
*/
12+
public class lc1028 {
13+
public class TreeNode {
14+
int val;
15+
TreeNode left;
16+
TreeNode right;
17+
TreeNode(int x) {
18+
val = x;
19+
}
20+
}
21+
public TreeNode recoverFromPreorder(String S) {
22+
int level, val;
23+
Stack<TreeNode> stack = new Stack<>();
24+
for (int i = 0; i < S.length();) {
25+
for (level = 0; S.charAt(i) == '-'; i++) {
26+
level++;
27+
}
28+
for (val = 0; i < S.length() && S.charAt(i) != '-'; i++) { //数字可能是大于9
29+
val = val * 10 + (S.charAt(i) - '0');
30+
}
31+
while (stack.size() > level) { //用stack的size和depth比就可以了
32+
stack.pop();
33+
}
34+
TreeNode node = new TreeNode(val);
35+
if (!stack.isEmpty()) {
36+
if (stack.peek().left == null) {
37+
stack.peek().left = node;
38+
} else {
39+
stack.peek().right = node;
40+
}
41+
}
42+
stack.add(node);
43+
}
44+
while (stack.size() > 1) {
45+
stack.pop();
46+
}
47+
return stack.pop();
48+
}
49+
}

‎code/lc1093.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package code;
2+
/*
3+
* 1093. Statistics from a Large Sample
4+
* 题意:从数组里统计出均值,最大,最小,中位数,众数
5+
* 难度:Medium
6+
* 分类:
7+
* 思路:
8+
* Tips:中位数记住可能是两个数的均值,可能是中间的一个数,用 pos/2 来处理
9+
*/
10+
public class lc1093 {
11+
public double[] sampleStats(int[] count) {
12+
double[] res = new double[5];
13+
res[0] = -1;
14+
res[1] = -1;
15+
int mode_num = 0;
16+
int count_num = 0;
17+
for (int i = 0; i < count.length ; i++) {
18+
if(res[0]==-1 && count[i]>0) res[0] = i;
19+
if(count[i]>0 && i>res[1]) res[1] = i;
20+
res[2] += count[i]*i;
21+
count_num += count[i];
22+
if(count[i]>mode_num) {
23+
res[4] = i;
24+
mode_num = count[i];
25+
}
26+
}
27+
int medium_pos1 = (count_num+1)/2; //中位数
28+
int medium_pos2 = (count_num+2)/2;
29+
count_num = 0;
30+
for (int i = 0; i < count.length ; i++) {
31+
count_num += count[i];
32+
if(medium_pos1>0&&medium_pos1<=count_num) {
33+
res[3]+= i;
34+
medium_pos1 = -1;
35+
}
36+
if(medium_pos2>0&&medium_pos2<=count_num) {
37+
res[3]+= i;
38+
medium_pos2 = -1;
39+
}
40+
}
41+
res[2] = res[2]/count_num;
42+
res[3] = res[3]/2;
43+
return res;
44+
}
45+
}

‎code/lc1094.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package code;
2+
/*
3+
* 1094. Car Pooling
4+
* 题意:
5+
* 难度:Medium
6+
* 分类:
7+
* 思路:lc56 类似的题,但该题的集合没有传递覆盖的特性,所以遍历的时候又加了个循环,N^2
8+
* 非常巧妙的方法,每次记录上车,下车人数,然后遍历一遍进行模拟上下车
9+
* Tips:lc253, lc56
10+
*/
11+
import java.util.Arrays;
12+
import java.util.Comparator;
13+
14+
public class lc1094 {
15+
public boolean carPooling(int[][] trips, int capacity) {
16+
Arrays.sort(trips, new Comparator<int[]>() { // 记住这种写法
17+
@Override
18+
public int compare(int[] o1, int[] o2) {
19+
if(o1[1]!=o2[1])
20+
return o1[1] - o2[1];
21+
else
22+
return o2[2] - o1[2];
23+
}
24+
});
25+
for (int i = 0; i < trips.length ; i++) {
26+
int current = trips[i][0];
27+
for (int j = i-1; j >=0 ; j--) { //往前找是否重叠
28+
if(trips[j][2]>trips[i][1]) current += trips[j][0];
29+
}
30+
if(current>capacity) return false;
31+
}
32+
return true;
33+
}
34+
35+
public boolean carPooling2(int[][] trips, int capacity) {
36+
int stops[] = new int[1001];
37+
for (int t[] : trips) {
38+
stops[t[1]] += t[0];
39+
stops[t[2]] -= t[0];
40+
}
41+
for (int i = 0; capacity >= 0 && i < 1001; ++i) capacity -= stops[i];
42+
return capacity >= 0;
43+
}
44+
}

‎code/lc139.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public boolean wordBreak(String s, List<String> wordDict) {
2020
dp[i] = true;
2121
}
2222
}
23-
return dp[s.length()+1];
23+
return dp[s.length()];
2424
}
2525
}

‎code/lc22.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.List;
1313
import java.util.Set;
1414

15-
public class lc22 {
15+
public class lc22 {
1616
public static void main(String[] args) {
1717
System.out.println(generateParenthesis2(4));
1818
}

‎code/lc3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package code;
22
/*
33
* 3. Longest Substring Without Repeating Characters
4-
* 题意:找出字符串中没有重复子串的最大长度
4+
* 题意:找出字符串中没有重复字母的最大长度
55
* 难度:Medium
66
* 分类:Hash Table, Two Pointers, String
77
* 算法:两个指针,记录没有重复字母的子串的首和尾

‎code/lc32.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public static int longestValidParentheses(String s) {
4040
public static int longestValidParentheses2(String s) {
4141
//栈方法 ()(())
4242
Stack<Integer> st = new Stack();
43-
st.add(-1);
43+
st.add(-1);//栈内先入-1
4444
int res = 0;
4545
for (int i = 0; i < s.length() ; i++) {
4646
char ch = s.charAt(i);
4747
if(ch=='(')
48-
st.add(i);
48+
st.push(i);
4949
else if(ch==')'){
5050
st.pop();
5151
if(st.isEmpty())

‎code/lc49.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public List<List<String>> groupAnagrams(String[] strs) {
2424
m.put(key,l);
2525
}
2626
}
27-
return new ArrayList(m.values());
27+
return new ArrayList(m.values());//学下这句的语法
2828
}
2929
}

‎code/lc50.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class lc50 {
1111
public double myPow(double x, int n) {
1212
if(n == 0) return 1;
13-
if(n==Integer.MIN_VALUE && x!=1 && x!=-1) return 0;
13+
if(n==Integer.MIN_VALUE && x!=1 && x!=-1) return 0;//负数最小值,结果溢出
1414
if(n<0){
1515
n = -n;
1616
x = 1/x; //负数变成 1/x

‎code/lc63.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* 63. Unique Paths II
5-
* 题意:路径数
5+
* 题意:路径数,添加了障碍
66
* 难度:Medium
77
* 分类:Array, Dynamic Programming
88
* 思路:和lc64, lc62思路一样

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /