Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
2db2c097
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,发现更多精彩内容 >>
提交
2db2c097
编写于
3月 05, 2019
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
31bb8798
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
25 addition
and
19 deletion
+25
-19
docs/notes/Leetcode 题解.md
docs/notes/Leetcode 题解.md
+25
-19
未找到文件。
docs/notes/Leetcode 题解.md
浏览文件 @
2db2c097
...
...
@@ -3199,31 +3199,34 @@ return -1.
-
物品大小:面额
-
物品价值:数量
因为硬币可以重复使用,因此这是一个完全背包问题。完全背包只需要将
01背包中逆序遍历bp
数组改为正序遍历即可。
因为硬币可以重复使用,因此这是一个完全背包问题。完全背包只需要将
0-1 背包中逆序遍历 dp
数组改为正序遍历即可。
```
java
public
int
coinChange
(
int
[]
coins
,
int
amount
)
{
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
);
if
(
amount
==
0
||
coins
==
null
||
coins
.
length
==
0
)
{
return
0
;
}
int
[]
dp
=
new
int
[
amount
+
1
];
for
(
int
coin
:
coins
)
{
for
(
int
i
=
coin
;
i
<=
amount
;
i
++)
{
//将逆序遍历改为正序遍历
if
(
i
==
coin
)
{
dp
[
i
]
=
1
;
}
else
if
(
dp
[
i
]
==
0
&&
dp
[
i
-
coin
]
!=
0
)
{
dp
[
i
]
=
dp
[
i
-
coin
]
+
1
;
}
else
if
(
dp
[
i
-
coin
]
!=
0
)
{
dp
[
i
]
=
Math
.
min
(
dp
[
i
],
dp
[
i
-
coin
]
+
1
);
}
}
}
return
bp
[
amount
]
==
0
?
-
1
:
b
p
[
amount
];
return
dp
[
amount
]
==
0
?
-
1
:
d
p
[
amount
];
}
```
**找零钱的硬币数组合**
[
518. Coin Change 2 (Medium)
](
https://leetcode.com/problems/coin-change-2/description/
)
[
518
\
. Coin Change 2 (Medium)
](
https://leetcode.com/problems/coin-change-2/description/
)
```
html
```
text-html-basic
Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
...
...
@@ -3233,14 +3236,17 @@ Explanation: there are four ways to make up the amount:
5=1+1+1+1+1
```
完全背包问题,使用
dp
记录可达成目标的组合数目。
完全背包问题,使用
dp
记录可达成目标的组合数目。
```
java
public
int
change
(
int
amount
,
int
[]
coins
)
{
if
(
amount
==
0
||
coins
==
null
||
coins
.
length
==
0
)
{
return
0
;
}
int
[]
dp
=
new
int
[
amount
+
1
];
dp
[
0
]
=
1
;
for
(
int
coin
:
coins
)
{
for
(
int
i
=
coin
;
i
<=
amount
;
i
++)
{
for
(
int
coin
:
coins
)
{
for
(
int
i
=
coin
;
i
<=
amount
;
i
++)
{
dp
[
i
]
+=
dp
[
i
-
coin
];
}
}
...
...
@@ -3260,7 +3266,7 @@ Return true because "leetcode" can be segmented as "leet code".
dict 中的单词没有使用次数的限制,因此这是一个完全背包问题。该问题涉及到字典中单词的使用顺序,因此可理解为涉及顺序的完全背包问题。
求解
该类型问题时,应调整两次循环的顺序,0-1 背包对物品的迭代是在最外层,而涉及顺序的完全背包问题对物品的迭代是
在最里层。
求解
顺序的完全背包问题时,对物品的迭代应该放
在最里层。
```
java
public
boolean
wordBreak
(
String
s
,
List
<
String
>
wordDict
)
{
...
...
@@ -3268,7 +3274,7 @@ public boolean wordBreak(String s, List<String> wordDict) {
boolean
[]
dp
=
new
boolean
[
n
+
1
];
dp
[
0
]
=
true
;
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
for
(
String
word
:
wordDict
)
{
//
完全一个物品可以使用多次
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
];
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录