Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
c0a6bc54
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c0a6bc54
编写于
3月 05, 2019
作者:
5
5renyuebing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
背包问题更新。
上级
dd65ec57
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
72 addition
and
43 deletion
+72
-43
docs/notes/Leetcode 题解.md
docs/notes/Leetcode 题解.md
+72
-43
未找到文件。
docs/notes/Leetcode 题解.md
浏览文件 @
c0a6bc54
...
...
@@ -3140,37 +3140,6 @@ private int findTargetSumWays(int[] nums, int start, int S) {
}
```
**字符串按单词列表分割**
[
139. Word Break (Medium)
](
https://leetcode.com/problems/word-break/description/
)
```
html
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
```
dict 中的单词没有使用次数的限制,因此这是一个完全背包问题。
0-1 背包和完全背包在实现上的不同之处是,0-1 背包对物品的迭代是在最外层,而完全背包对物品的迭代是在最里层。
```
java
public
boolean
wordBreak
(
String
s
,
List
<
String
>
wordDict
)
{
int
n
=
s
.
length
();
boolean
[]
dp
=
new
boolean
[
n
+
1
];
dp
[
0
]
=
true
;
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
for
(
String
word
:
wordDict
)
{
// 完全一个物品可以使用多次
int
len
=
word
.
length
();
if
(
len
<=
i
&&
word
.
equals
(
s
.
substring
(
i
-
len
,
i
)))
{
dp
[
i
]
=
dp
[
i
]
||
dp
[
i
-
len
];
}
}
}
return
dp
[
n
];
}
```
**01 字符构成最多的字符串**
[
474. Ones and Zeroes (Medium)
](
https://leetcode.com/problems/ones-and-zeroes/description/
)
...
...
@@ -3229,23 +3198,83 @@ return -1.
-
物品大小:面额
-
物品价值:数量
因为硬币可以重复使用,因此这是一个完全背包问题。
因为硬币可以重复使用,因此这是一个完全背包问题。
完全背包只需要将01背包中逆序遍历bp数组改为正序遍历即可。
```
java
public
int
coinChange
(
int
[]
coins
,
int
amount
)
{
if
(
coins
==
null
||
coins
.
length
==
0
)
{
return
0
;
if
(
amount
==
0
)
return
0
;
int
[]
bp
=
new
int
[
amount
+
1
];
for
(
int
coin
:
coins
){
for
(
int
i
=
coin
;
i
<=
amount
;
i
++){
//将逆序遍历改为正序遍历
if
(
i
==
coin
)
bp
[
i
]
=
1
;
else
if
(
bp
[
i
]
==
0
&&
bp
[
i
-
coin
]
!=
0
)
bp
[
i
]
=
bp
[
i
-
coin
]
+
1
;
else
if
(
bp
[
i
-
coin
]
!=
0
)
bp
[
i
]
=
Math
.
min
(
bp
[
i
],
bp
[
i
-
coin
]
+
1
);
}
}
return
bp
[
amount
]
==
0
?
-
1
:
bp
[
amount
];
}
```
**找零钱的硬币数组合**
[
518. Coin Change 2 (Medium)
](
https://leetcode.com/problems/coin-change-2/description/
)
```
html
Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
```
完全背包问题,使用dp记录可达成目标的组合数目。
```
java
public
int
change
(
int
amount
,
int
[]
coins
)
{
int
[]
dp
=
new
int
[
amount
+
1
];
dp
[
0
]
=
1
;
for
(
int
coin
:
coins
){
for
(
int
i
=
coin
;
i
<=
amount
;
i
++){
dp
[
i
]
+=
dp
[
i
-
coin
];
}
}
int
[]
minimum
=
new
int
[
amount
+
1
];
Arrays
.
fill
(
minimum
,
amount
+
1
);
minimum
[
0
]
=
0
;
Arrays
.
sort
(
coins
);
for
(
int
i
=
1
;
i
<=
amount
;
i
++)
{
for
(
int
j
=
0
;
j
<
coins
.
length
&&
coins
[
j
]
<=
i
;
j
++)
{
minimum
[
i
]
=
Math
.
min
(
minimum
[
i
],
minimum
[
i
-
coins
[
j
]]
+
1
);
return
dp
[
amount
];
}
```
**字符串按单词列表分割**
[
139. Word Break (Medium)
](
https://leetcode.com/problems/word-break/description/
)
```
html
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
```
dict 中的单词没有使用次数的限制,因此这是一个完全背包问题。该问题涉及到字典中单词的使用顺序,因此可理解为涉及顺序的完全背包问题。
求解该类型问题时,应调整两次循环的顺序,0-1 背包对物品的迭代是在最外层,而涉及顺序的完全背包问题对物品的迭代是在最里层。
```
java
public
boolean
wordBreak
(
String
s
,
List
<
String
>
wordDict
)
{
int
n
=
s
.
length
();
boolean
[]
dp
=
new
boolean
[
n
+
1
];
dp
[
0
]
=
true
;
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
for
(
String
word
:
wordDict
)
{
// 完全一个物品可以使用多次
int
len
=
word
.
length
();
if
(
len
<=
i
&&
word
.
equals
(
s
.
substring
(
i
-
len
,
i
)))
{
dp
[
i
]
=
dp
[
i
]
||
dp
[
i
-
len
];
}
}
}
return
minimum
[
amount
]
>
amount
?
-
1
:
minimum
[
amount
];
return
dp
[
n
];
}
```
...
...
@@ -3271,7 +3300,7 @@ Note that different sequences are counted as different combinations.
Therefore the output is 7.
```
完全背包。
涉及顺序的
完全背包。
```
java
public
int
combinationSum4
(
int
[]
nums
,
int
target
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录