Skip to content

Commit 0554cec

Browse files
committed
20190626
1 parent eb42850 commit 0554cec

18 files changed

+379
-9
lines changed

Diff for: code/lc1028.java

+49
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+
}

Diff for: code/lc1093.java

+45
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+
}

Diff for: code/lc1094.java

+44
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+
}

Diff for: code/lc139.java

+1-1
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
}

Diff for: code/lc22.java

+1-1
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
}

Diff for: code/lc3.java

+1-1
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
* 算法:两个指针,记录没有重复字母的子串的首和尾

Diff for: code/lc32.java

+2-2
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())

Diff for: code/lc49.java

+1-1
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
}

Diff for: code/lc50.java

+1-1
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

Diff for: code/lc63.java

+1-1
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思路一样

Diff for: code/lc81.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package code;
22
/*
3-
* 31. Search in Rotated Sorted Array II
3+
* 81. Search in Rotated Sorted Array II
44
* 题意:在翻转有序数组中查找指定数,数组中可能有相等的元素
55
* 难度:Medium
66
* 分类:Array, Binary Search

Diff for: interview/byte1.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package interview;
2+
3+
import java.util.Scanner;
4+
5+
public class byte1 {
6+
public static void main (String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int n = sc.nextInt();
9+
int[] arr = new int[n];
10+
for (int i = 0; i < n; i++) {
11+
arr[i] = sc.nextInt();
12+
}
13+
14+
int res = Integer.MIN_VALUE;
15+
int[] dp = new int[n];
16+
dp[0] = arr[0];
17+
for (int i = 1; i < n; i++) {
18+
dp[i] = Math.max(dp[i-1], arr[i]+i);
19+
}
20+
for (int i = 1; i < n ; i++) {
21+
res = Math.max(arr[i]-i+dp[i-1], res);
22+
}
23+
System.out.println(res);
24+
}
25+
26+
public static void main2 (String[] args) {
27+
Scanner sc = new Scanner(System.in);
28+
int n = sc.nextInt();
29+
int[] arr = new int[n];
30+
for (int i = 0; i < n; i++) {
31+
arr[i] = sc.nextInt();
32+
}
33+
int res = 0;
34+
for (int i = 0; i < n ; i++) {
35+
for (int j = 0; j < i ; j++) {
36+
int temp = arr[i]+arr[j]-i+j;
37+
res = Math.max(res, temp);
38+
}
39+
}
40+
System.out.println(res);
41+
}
42+
}
43+

Diff for: interview/byte2.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package interview;
2+
3+
import java.util.Scanner;
4+
5+
public class byte2 {
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int n = sc.nextInt();
9+
int m = sc.nextInt();
10+
if(m*n==0) {
11+
System.out.println(0);
12+
return;
13+
}
14+
int[][] grid = new int[n][m];
15+
for(int i = 0; i < n; i++){
16+
for(int j = 0; j < m; j++){
17+
grid[i][j] = sc.nextInt();
18+
}
19+
}
20+
int count = 0;
21+
for (int i = 0; i < n ; i++) {
22+
for (int j = 0; j < m ; j++) {
23+
if( grid[i][j]==1 ){
24+
count++;
25+
grid[i][j] = 0; //置0, 下次不用考虑
26+
search(grid,i,j);
27+
}
28+
}
29+
}
30+
System.out.println(count);
31+
}
32+
33+
public static void search(int[][] grid, int i, int j){
34+
if( i>0 && grid[i-1][j]==1) {
35+
grid[i-1][j] = 0; //置0, 下次不用考虑
36+
search(grid, i - 1, j);
37+
}
38+
if( j>0 && grid[i][j-1]==1) {
39+
grid[i][j-1] = 0;
40+
search(grid, i, j-1);
41+
}
42+
if( i+1<grid.length && grid[i+1][j]==1) {
43+
grid[i+1][j] = 0;
44+
search(grid, i + 1, j);
45+
}
46+
if( j+1<grid[0].length && grid[i][j+1]==1) {
47+
grid[i][j+1] = 0;
48+
search(grid, i, j+1);
49+
}
50+
if( i>0 && j>0 && grid[i-1][j-1]==1){
51+
grid[i-1][j-1] = 0;
52+
search(grid, i-1, j-1);
53+
}
54+
if( i+1<grid.length && j+1<grid[0].length && grid[i+1][j+1]==1){
55+
grid[i+1][j+1] = 0;
56+
search(grid, i+1, j+1);
57+
}
58+
if( i>0 && j+1<grid[0].length && grid[i-1][j+1]==1){
59+
grid[i-1][j+1] = 0;
60+
search(grid, i-1, j+1);
61+
}
62+
if( i+1<grid.length && j>0 && grid[i+1][j-1]==1){
63+
grid[i+1][j-1] = 0;
64+
search(grid, i+1, j-1);
65+
}
66+
return;
67+
}
68+
}

Diff for: interview/byte21.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package interview;
2+
3+
import java.util.Scanner;
4+
5+
public class byte21 {
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int w = sc.nextInt();
9+
int b = sc.nextInt();
10+
int[][] dp = new int[5000][2];
11+
int i = 1;
12+
dp[0][0] = 1;
13+
dp[0][1] = 1;
14+
for (; i < 5000; i++) {
15+
if(w-i>=0){
16+
dp[i][0] = dp[i-1][0] + dp[i-1][1];//白
17+
w=w-i;
18+
}else{
19+
dp[i][0] = 0;
20+
}
21+
if(b-i>=0){
22+
dp[i][1] = dp[i-1][0] + dp[i-1][1];//黑
23+
b = b-i;
24+
}else{
25+
dp[i][1] = 0;
26+
}
27+
if(dp[i][0]==0&&dp[i][1]==0) break;
28+
}
29+
System.out.println(i-1);
30+
System.out.println(dp[i-1][0]+dp[i-1][1]);
31+
}
32+
}

Diff for: interview/byte22.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package interview;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
public class byte22 {
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
String str1 = sc.nextLine();
11+
String str2 = sc.nextLine();
12+
String[] str_arr = str2.split(" ");
13+
List<String> str_list = new ArrayList<>();
14+
for(String s : str_arr) str_list.add(s);
15+
System.out.println(helper(str1, str_list));
16+
}
17+
18+
19+
20+
public static String helper(String s, List<String> wordDict) {
21+
boolean[] dp = new boolean[s.length()+1];
22+
dp[0] = true;
23+
for (int i = 1; i < dp.length ; i++) {
24+
for (int j = 0; j < i ; j++) { //遍历之前计算出来的结果
25+
if( dp[j]==true && wordDict.contains(s.substring(j,i)))
26+
dp[i] = true;
27+
}
28+
}
29+
if(dp[s.length()]) return "True";
30+
return "Fales";
31+
}
32+
33+
}

0 commit comments

Comments
 (0)