Skip to content

Commit ecc3b2f

Browse files
committed
day29
1 parent 385c815 commit ecc3b2f

File tree

13 files changed

+446
-1
lines changed

13 files changed

+446
-1
lines changed

.idea/leetcode/editor.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

architecture-algorithms/src/main/java/leetcode/editor/cn/ImageSmoother.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int avg(int[][] img, int height, int width, int x, int y) {
8585
cnt++;
8686
}
8787
}
88-
return (int) Math.floor(sum / (double) cnt);
88+
return sum / cnt;
8989
}
9090
}
9191
//leetcode submit region end(Prohibit modification and deletion)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package leetcode.editor.cn;
2+
3+
/**
4+
* 请实现一个函数用来匹配包含'. '和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指
5+
* 字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但与"aa.a"和"ab*a"均不匹配。
6+
* <p>
7+
* 示例 1:
8+
* <p>
9+
* 输入:
10+
* s = "aa"
11+
* p = "a"
12+
* 输出: false
13+
* 解释: "a" 无法匹配 "aa" 整个字符串。
14+
* <p>
15+
* <p>
16+
* 示例 2:
17+
* <p>
18+
* 输入:
19+
* s = "aa"
20+
* p = "a*"
21+
* 输出: true
22+
* 解释: 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
23+
* <p>
24+
* <p>
25+
* 示例 3:
26+
* <p>
27+
* 输入:
28+
* s = "ab"
29+
* p = ".*"
30+
* 输出: true
31+
* 解释: ".*" 表示可匹配零个或多个('*')任意字符('.')。
32+
* <p>
33+
* <p>
34+
* 示例 4:
35+
* <p>
36+
* 输入:
37+
* s = "aab"
38+
* p = "c*a*b"
39+
* 输出: true
40+
* 解释: 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。
41+
* <p>
42+
* <p>
43+
* 示例 5:
44+
* <p>
45+
* 输入:
46+
* s = "mississippi"
47+
* p = "mis*is*p*."
48+
* 输出: false
49+
* <p>
50+
* <p>
51+
* s 可能为空,且只包含从 a-z 的小写字母。
52+
* p 可能为空,且只包含从 a-z 的小写字母以及字符 . 和 *,无连续的 '*'。
53+
* <p>
54+
* <p>
55+
* 注意:本题与主站 10 题相同:https://leetcode-cn.com/problems/regular-expression-matching/
56+
* Related Topics 递归 字符串 动态规划 👍 357 👎 0
57+
*/
58+
public class ZhengZeBiaoDaShiPiPeiLcof {
59+
static
60+
//leetcode submit region begin(Prohibit modification and deletion)
61+
class Solution {
62+
63+
/**
64+
* 动态规划
65+
*
66+
* @param s
67+
* @param p
68+
* @return
69+
*/
70+
public boolean isMatch(String s, String p) {
71+
int m = s.length() + 1, n = p.length() + 1;
72+
boolean[][] dp = new boolean[m][n];
73+
dp[0][0] = true;
74+
// 初始化首行
75+
for (int j = 2; j < n; j += 2) {
76+
dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
77+
}
78+
// 状态转移
79+
for (int i = 1; i < m; i++) {
80+
for (int j = 1; j < n; j++) {
81+
if (p.charAt(j - 1) == '*') {
82+
if (dp[i][j - 2]) dp[i][j] = true; // 1.
83+
else if (dp[i - 1][j] && s.charAt(i - 1) == p.charAt(j - 2)) dp[i][j] = true; // 2.
84+
else if (dp[i - 1][j] && p.charAt(j - 2) == '.') dp[i][j] = true; // 3.
85+
} else {
86+
if (dp[i - 1][j - 1] && s.charAt(i - 1) == p.charAt(j - 1)) dp[i][j] = true; // 1.
87+
else if (dp[i - 1][j - 1] && p.charAt(j - 1) == '.') dp[i][j] = true; // 2.
88+
}
89+
}
90+
}
91+
return dp[m - 1][n - 1];
92+
}
93+
}
94+
//leetcode submit region end(Prohibit modification and deletion)
95+
96+
97+
public static void main(String[] args) {
98+
99+
}
100+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<p>我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。</p>
2+
3+
<p>&nbsp;</p>
4+
5+
<p><strong>示例:</strong></p>
6+
7+
<pre><strong>输入:</strong> n = 10
8+
<strong>输出:</strong> 12
9+
<strong>解释: </strong><code>1, 2, 3, 4, 5, 6, 8, 9, 10, 12</code> 是前 10 个丑数。</pre>
10+
11+
<p><strong>说明:&nbsp;</strong>&nbsp;</p>
12+
13+
<ol>
14+
<li><code>1</code>&nbsp;是丑数。</li>
15+
<li><code>n</code>&nbsp;<strong>不超过</strong>1690。</li>
16+
</ol>
17+
18+
<p>注意:本题与主站 264 题相同:<a href="https://leetcode-cn.com/problems/ugly-number-ii/">https://leetcode-cn.com/problems/ugly-number-ii/</a></p>
19+
<div><div>Related Topics</div><div><li>哈希表</li><li>数学</li><li>动态规划</li><li>堆(优先队列)</li></div></div><br><div><li>👍 310</li><li>👎 0</li></div>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是
3+
O(1)。
4+
5+
若队列为空,pop_front 和 max_value 需要返回 -1
6+
7+
示例 1:
8+
9+
输入:
10+
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
11+
[[],[1],[2],[],[],[]]
12+
输出: [null,null,null,2,1,2]
13+
14+
15+
示例 2:
16+
17+
输入:
18+
["MaxQueue","pop_front","max_value"]
19+
[[],[],[]]
20+
输出: [null,-1,-1]
21+
22+
23+
24+
25+
限制:
26+
27+
28+
1 <= push_back,pop_front,max_value的总操作数 <= 10000
29+
1 <= value <= 10^5
30+
31+
Related Topics 设计 队列 单调队列 👍 336 👎 0
32+
33+
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p><strong>图像平滑器</strong> 是大小为&nbsp;<code>3 x 3</code> 的过滤器,用于对图像的每个单元格平滑处理,平滑处理后单元格的值为该单元格的平均灰度。</p>
2+
3+
<p>每个单元格的<strong>&nbsp; 平均灰度</strong> 定义为:该单元格自身及其周围的 8 个单元格的平均值,结果需向下取整。(即,需要计算蓝色平滑器中 9 个单元格的平均值)。</p>
4+
5+
<p>如果一个单元格周围存在单元格缺失的情况,则计算平均灰度时不考虑缺失的单元格(即,需要计算红色平滑器中 4 个单元格的平均值)。</p>
6+
7+
<p><img src="https://assets.leetcode.com/uploads/2021/05/03/smoother-grid.jpg" style="height: 493px; width: 493px;" /></p>
8+
9+
<p>给你一个表示图像灰度的 <code>m x n</code> 整数矩阵 <code>img</code> ,返回对图像的每个单元格平滑处理后的图像&nbsp;。</p>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<p><img src="https://assets.leetcode.com/uploads/2021/05/03/smooth-grid.jpg" /></p>
16+
17+
<pre>
18+
<strong>输入:</strong>img = [[1,1,1],[1,0,1],[1,1,1]]
19+
<strong>输出:</strong>[[0, 0, 0],[0, 0, 0], [0, 0, 0]]
20+
<strong>解释:</strong>
21+
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
22+
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
23+
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
24+
</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/smooth2-grid.jpg" />
28+
<pre>
29+
<strong>输入:</strong> img = [[100,200,100],[200,50,200],[100,200,100]]
30+
<strong>输出:</strong> [[137,141,137],[141,138,141],[137,141,137]]
31+
<strong>解释:</strong>
32+
对于点 (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
33+
对于点 (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
34+
对于点 (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>m == img.length</code></li>
43+
<li><code>n == img[i].length</code></li>
44+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
45+
<li><code>0 &lt;= img[i][j] &lt;= 255</code></li>
46+
</ul>
47+
<div><div>Related Topics</div><div><li>数组</li><li>矩阵</li></div></div><br><div><li>👍 155</li><li>👎 0</li></div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<p>把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。</p>
2+
3+
<p>&nbsp;</p>
4+
5+
<p>你需要用一个浮点数数组返回答案,其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例 1:</strong></p>
10+
11+
<pre><strong>输入:</strong> 1
12+
<strong>输出:</strong> [0.16667,0.16667,0.16667,0.16667,0.16667,0.16667]
13+
</pre>
14+
15+
<p><strong>示例&nbsp;2:</strong></p>
16+
17+
<pre><strong>输入:</strong> 2
18+
<strong>输出:</strong> [0.02778,0.05556,0.08333,0.11111,0.13889,0.16667,0.13889,0.11111,0.08333,0.05556,0.02778]</pre>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>限制:</strong></p>
23+
24+
<p><code>1 &lt;= n &lt;= 11</code></p>
25+
<div><div>Related Topics</div><div><li>数学</li><li>动态规划</li><li>概率与统计</li></div></div><br><div><li>👍 385</li><li>👎 0</li></div>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<p>总共有 <code>n</code>&nbsp;个颜色片段排成一列,每个颜色片段要么是&nbsp;<code>'A'</code>&nbsp;要么是&nbsp;<code>'B'</code>&nbsp;。给你一个长度为&nbsp;<code>n</code>&nbsp;的字符串&nbsp;<code>colors</code>&nbsp;,其中&nbsp;<code>colors[i]</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个颜色片段的颜色。</p>
2+
3+
<p>Alice 和 Bob 在玩一个游戏,他们 <strong>轮流</strong>&nbsp;从这个字符串中删除颜色。Alice <strong>先手</strong>&nbsp;。</p>
4+
5+
<ul>
6+
<li>如果一个颜色片段为 <code>'A'</code>&nbsp;且 <strong>相邻两个颜色</strong>&nbsp;都是颜色 <code>'A'</code>&nbsp;,那么 Alice 可以删除该颜色片段。Alice&nbsp;<strong>不可以</strong>&nbsp;删除任何颜色&nbsp;<code>'B'</code>&nbsp;片段。</li>
7+
<li>如果一个颜色片段为 <code>'B'</code>&nbsp;且 <strong>相邻两个颜色</strong>&nbsp;都是颜色 <code>'B'</code>&nbsp;,那么 Bob 可以删除该颜色片段。Bob <strong>不可以</strong>&nbsp;删除任何颜色 <code>'A'</code>&nbsp;片段。</li>
8+
<li>Alice 和 Bob <strong>不能</strong>&nbsp;从字符串两端删除颜色片段。</li>
9+
<li>如果其中一人无法继续操作,则该玩家 <b>输</b>&nbsp;掉游戏且另一玩家 <strong>获胜</strong>&nbsp;。</li>
10+
</ul>
11+
12+
<p>假设 Alice 和 Bob 都采用最优策略,如果 Alice 获胜,请返回&nbsp;<code>true</code>,否则 Bob 获胜,返回<em>&nbsp;</em><code>false</code>。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre><b>输入:</b>colors = "AAABABB"
19+
<b>输出:</b>true
20+
<b>解释:</b>
21+
A<em><strong>A</strong></em>ABABB -&gt; AABABB
22+
Alice 先操作。
23+
她删除从左数第二个 'A' ,这也是唯一一个相邻颜色片段都是 'A' 的 'A' 。
24+
25+
现在轮到 Bob 操作。
26+
Bob 无法执行任何操作,因为没有相邻位置都是 'B' 的颜色片段 'B' 。
27+
因此,Alice 获胜,返回 true 。
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre><b>输入:</b>colors = "AA"
33+
<b>输出:</b>false
34+
<strong>解释:</strong>
35+
Alice 先操作。
36+
只有 2 个 'A' 且它们都在字符串的两端,所以她无法执行任何操作。
37+
因此,Bob 获胜,返回 false 。
38+
</pre>
39+
40+
<p><strong>示例 3:</strong></p>
41+
42+
<pre><b>输入:</b>colors = "ABBBBBBBAAA"
43+
<b>输出:</b>false
44+
<strong>解释:</strong>
45+
ABBBBBBBA<em><strong>A</strong></em>A -&gt; ABBBBBBBAA
46+
Alice 先操作。
47+
她唯一的选择是删除从右数起第二个 'A' 。
48+
49+
ABBBB<strong><em>B</em></strong>BBAA -&gt; ABBBBBBAA
50+
接下来轮到 Bob 操作。
51+
他有许多选择,他可以选择任何一个 'B' 删除。
52+
53+
然后轮到 Alice 操作,她无法删除任何片段。
54+
所以 Bob 获胜,返回 false 。
55+
</pre>
56+
57+
<p>&nbsp;</p>
58+
59+
<p><strong>提示:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li>
63+
<li><code>colors</code>&nbsp;只包含字母&nbsp;<code>'A'</code>&nbsp;和&nbsp;<code>'B'</code></li>
64+
</ul>
65+
<div><div>Related Topics</div><div><li>贪心</li><li>数学</li><li>字符串</li><li>博弈</li></div></div><br><div><li>👍 54</li><li>👎 0</li></div>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
城堡守卫游戏的胜利条件为使恶魔无法从出生点到达城堡。游戏地图可视作 `2*N` 的方格图,记作字符串数组 `grid`,其中:
2+
- `"."` 表示恶魔可随意通行的平地;
3+
- `"#"` 表示恶魔不可通过的障碍物,玩家可通过在 **平地** 上设置障碍物,即将 `"."` 变为 `"#"` 以阻挡恶魔前进;
4+
- `"S"` 表示恶魔出生点,将有大量的恶魔该点生成,恶魔可向上/向下/向左/向右移动,且无法移动至地图外;
5+
- `"P"` 表示瞬移点,移动到 `"P"` 点的恶魔可被传送至任意一个 `"P"` 点,也可选择不传送;
6+
- `"C"` 表示城堡。
7+
8+
然而在游戏中用于建造障碍物的金钱是有限的,请返回玩家最少需要放置几个障碍物才能获得胜利。若无论怎样放置障碍物均无法获胜,请返回 `-1`
9+
10+
**注意:**
11+
- 地图上可能有一个或多个出生点
12+
- 地图上有且只有一个城堡
13+
14+
**示例 1**
15+
>输入:`grid = ["S.C.P#P.", ".....#.S"]`
16+
>
17+
>输出:`3`
18+
>
19+
>解释:至少需要放置三个障碍物
20+
![image.png](https://pic.leetcode-cn.com/1614828255-uuNdNJ-image.png)
21+
22+
23+
**示例 2:**
24+
>输入:`grid = ["SP#P..P#PC#.S", "..#P..P####.#"]`
25+
>
26+
>输出:`-1`
27+
>
28+
>解释:无论怎样修筑障碍物,均无法阻挡最左侧出生的恶魔到达城堡位置
29+
![image.png](https://pic.leetcode-cn.com/1614828208-oFlpVs-image.png)
30+
31+
**示例 3:**
32+
>输入:`grid = ["SP#.C.#PS", "P.#...#.P"]`
33+
>
34+
>输出:`0`
35+
>
36+
>解释:无需放置障碍物即可获得胜利
37+
![image.png](https://pic.leetcode-cn.com/1614828242-oveClu-image.png)
38+
39+
**示例 4:**
40+
>输入:`grid = ["CP.#.P.", "...S..S"]`
41+
>
42+
>输出:`4`
43+
>
44+
>解释:至少需要放置 4 个障碍物,示意图为放置方法之一
45+
![image.png](https://pic.leetcode-cn.com/1614828218-sIAYkb-image.png)
46+
47+
48+
**提示:**
49+
- `grid.length == 2`
50+
- `2 <= grid[0].length == grid[1].length <= 10^4`
51+
- `grid[i][j]` 仅包含字符 `"."``"#"``"C"``"P"``"S"`
52+
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li><li>矩阵</li></div></div><br><div><li>👍 7</li><li>👎 0</li></div>

0 commit comments

Comments
 (0)