-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain139.java
More file actions
84 lines (75 loc) · 2.39 KB
/
Main139.java
File metadata and controls
84 lines (75 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package HOT100;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main139 {
Set<String> set = new HashSet<>();
public boolean wordBreak(String s, List<String> wordDict) {
int length = s.length();
if(length==0){
return true;
}
boolean[] dp = new boolean[length+1];
dp[0] = true;
for(int i=0; i<wordDict.size(); i++){
set.add(wordDict.get(i));
}
for(int i=1; i<=s.length(); i++){
for(int j=0;j<i;j++){
if(dp[j]&&check(s.substring(j,i))){
dp[i]=true;
}
}
}
return dp[length];
}
private boolean check(String str){
if(set.contains(str)){
return true;
}
return false;
}
}
/*
0-1背包问题
可定义状态dp: dp[i][j]表示将前i件物品装进限重为j的背包可以获得的最大价值,0<=i<=N, 0<=j<=W
当i>0时dp[i][j]有两种情况:
1.不装入第i件物品,即dp[i-1][j].
2.装入第i件物品(前提是能装下),即dp[i-1][j-w[i]] + v[i].
即状态转移方程为
dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]) // j>=w[i]
完全背包问题:
完全背包与0-1背包不同就是每种物品可以有无限多个:一共有N种物品,每种物品有无限多个,第i(i从1开始)种物品
的重量为w[i],价值为v[i].
背包问题具备的特征:
是否可以根据一个target(直接给出或间接求出),target可以是数字也可以是字符串,再给定一个数组arr
问:能否使用arr中的元素做各种排列组合得到target.
*/
class Main139_1{
Set<String> set = new HashSet<>();
public boolean wordBreak(String s, List<String> wordDict) {
int length = s.length();
if(length==0){
return true;
}
boolean[] dp = new boolean[length+1];
dp[0] = true;
for(int i=0; i<wordDict.size(); i++){
set.add(wordDict.get(i));
}
for(int i=1; i<=s.length(); i++){
for(int j=0;j<i;j++){
if(dp[j]&&check(s.substring(j,i))){
dp[i]=true;
}
}
}
return dp[length];
}
private boolean check(String str){
if(set.contains(str)){
return true;
}
return false;
}
}