Skip to content

Commit a594abc

Browse files
author
robot
committed
feat: $661
1 parent 7e744ba commit a594abc

File tree

3 files changed

+191
-30
lines changed

3 files changed

+191
-30
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
256256
- [0504. 七进制数](./problems/504.base-7.md)
257257
- [0575. 分糖果](./problems/575.distribute-candies.md)
258258
- [0606. 根据二叉树创建字符串](./problems/606.construct-string-from-binary-tree.md)
259+
- [0661. 图片平滑器](./problems/661.image-smoother.md)
259260
- [0665. 非递减数列](./problems/665.non-decreasing-array.md)
260261
- [821. 字符的最短距离](./problems/821.shortest-distance-to-a-character.md) 91
261262
- [0874. 模拟行走机器人](./problems/874.walking-robot-simulation.md)

SUMMARY.md

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
- [0504. 七进制数](./problems/504.base-7.md)
8888
- [0575. 分糖果](problems/575.distribute-candies.md)
8989
- [0665. 非递减数列](./problems/665.non-decreasing-array.md)
90+
- [0661. 图片平滑器](./problems/661.image-smoother.md)
9091
- [821. 字符的最短距离](problems/821.shortest-distance-to-a-character.md) 91
9192
- [0874. 模拟行走机器人](problems/874.walking-robot-simulation.md)
9293
- [1128. 等价多米诺骨牌对的数量](./problems/1128.number-of-equivalent-domino-pairs.md)
@@ -99,7 +100,8 @@
99100
- [面试题 17.23. 最大黑方阵](./problems/max-black-square-lcci.md)🆕
100101
- [面试题 16.16. 部分排序](./problems/sub-sort-lcci.md)
101102
- [Increasing Digits](./problems/Increasing-Digits.md) 👍
102-
- [Longest Contiguously Strictly Increasing Sublist After Deletion](./problems/Longest-Contiguously-Strictly-Increasing-Sublist-After-Deletion.md) 👍
103+
- [Longest Contiguously Strictly Increasing Sublist After Deletion](./problems/Longest-Contiguously-Strictly-Increasing-Sublist-After-Deletion.md)
104+
👍
103105
- [Consecutive Wins](./problems/consecutive-wins.md)
104106
- [Number of Substrings with Single Character Difference](./problems/Number-of-Substrings-with-Single-Character-Difference.md)
105107
- [Bus Fare](./problems/Bus-Fare.md) 👍
@@ -171,16 +173,20 @@
171173
- [0238. 除自身以外数组的乘积](./problems/238.product-of-array-except-self.md)
172174
- [0240. 搜索二维矩阵 II](./problems/240.search-a-2-d-matrix-ii.md)
173175
- [0279. 完全平方数](./problems/279.perfect-squares.md)
174-
- [0309. 最佳买卖股票时机含冷冻期](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) 👍
176+
- [0309. 最佳买卖股票时机含冷冻期](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md)
177+
👍
175178
- [0322. 零钱兑换](./problems/322.coin-change.md)
176179
- [0328. 奇偶链表](./problems/328.odd-even-linked-list.md)
177-
- [0331. 验证二叉树的前序序列化](./problems/331.verify-preorder-serialization-of-a-binary-tree.md) 👍
178-
- [0334. 递增的三元子序列](./problems/334.increasing-triplet-subsequence.md) 👍
180+
- [0331. 验证二叉树的前序序列化](./problems/331.verify-preorder-serialization-of-a-binary-tree.md)
181+
👍
182+
- [0334. 递增的三元子序列](./problems/334.increasing-triplet-subsequence.md)
183+
👍
179184
- [0337. 打家劫舍 III](./problems/337.house-robber-iii.md)
180185
- [0343. 整数拆分](./problems/343.integer-break.md) 👍
181186
- [0365. 水壶问题](./problems/365.water-and-jug-problem.md)
182187
- [0378. 有序矩阵中第 K 小的元素](./problems/378.kth-smallest-element-in-a-sorted-matrix.md)
183-
- [0380. 常数时间插入、删除和获取随机元素](./problems/380.insert-delete-getrandom-o1.md) 👍
188+
- [0380. 常数时间插入、删除和获取随机元素](./problems/380.insert-delete-getrandom-o1.md)
189+
👍
184190
- [0394. 字符串解码](./problems/394.decode-string.md) 91 👍
185191
- [0416. 分割等和子集](./problems/416.partition-equal-subset-sum.md)
186192
- [0424. 替换后的最长重复字符](./problems/424.longest-repeating-character-replacement.md)
@@ -203,18 +209,21 @@
203209
- [0611. 有效三角形的个数](./problems/611.valid-triangle-number.md) 👍
204210
- [0673. 最长递增子序列的个数](./problems/673.number-of-longest-increasing-subsequence.md)
205211
- [0686. 重复叠加字符串匹配](./problems/686.repeated-string-match.md)
206-
- [0714. 买卖股票的最佳时机含手续费](./problems/714.best-time-to-buy-and-sell-stock-with-transaction-fee.md) 👍
212+
- [0714. 买卖股票的最佳时机含手续费](./problems/714.best-time-to-buy-and-sell-stock-with-transaction-fee.md)
213+
👍
207214
- [0718. 最长重复子数组](./problems/718.maximum-length-of-repeated-subarray.md)
208215
- [0735. 行星碰撞](./problems/735.asteroid-collision.md)
209216
- [0754. 到达终点数字](./problems/754.reach-a-number.md) 👍
210217
- [0785. 判断二分图](./problems/785.is-graph-bipartite.md) 👍
211218
- [0790. 多米诺和托米诺平铺](./problems/790.domino-and-tromino-tiling.md) 👍
212219
- [0799. 香槟塔](./problems/799.champagne-tower.md) 👍
213-
- [0801. 使序列递增的最小交换次数](./problems/801.minimum-swaps-to-make-sequences-increasing.md) 👍
220+
- [0801. 使序列递增的最小交换次数](./problems/801.minimum-swaps-to-make-sequences-increasing.md)
221+
👍
214222
- [0816. 模糊坐标](./problems/816.ambiguous-coordinates.md) 👍
215223
- [0820. 单词的压缩编码](./problems/820.short-encoding-of-words.md)
216224
- [0838. 推多米诺](./problems/838.push-dominoes.md)
217-
- [0873. 最长的斐波那契子序列的长度](./problems/873.length-of-longest-fibonacci-subsequence.md) 👍
225+
- [0873. 最长的斐波那契子序列的长度](./problems/873.length-of-longest-fibonacci-subsequence.md)
226+
👍
218227
- [0875. 爱吃香蕉的珂珂](./problems/875.koko-eating-bananas.md)
219228
- [0877. 石子游戏](./problems/877.stone-game.md)
220229
- [0886. 可能的二分法](./problems/886.possible-bipartition.md)
@@ -224,42 +233,60 @@
224233
- [0912. 排序数组](./problems/912.sort-an-array.md)
225234
- [0932. 漂亮数组](./problems/932.beautiful-array.md)
226235
- [0935. 骑士拨号器](./problems/935.knight-dialer.md)
227-
- [0947. 移除最多的同行或同列石头](./problems/947.most-stones-removed-with-same-row-or-column.md) 👍
236+
- [0947. 移除最多的同行或同列石头](./problems/947.most-stones-removed-with-same-row-or-column.md)
237+
👍
228238
- [0959. 由斜杠划分区域](./problems/959.regions-cut-by-slashes.md)
229239
- [0978. 最长湍流子数组](./problems/978.longest-turbulent-subarray.md) 👍
230-
- [0987. 二叉树的垂序遍历](./problems/987.vertical-order-traversal-of-a-binary-tree.md) 91
240+
- [0987. 二叉树的垂序遍历](./problems/987.vertical-order-traversal-of-a-binary-tree.md)
241+
91
231242
- [1004. 最大连续 1 的个数 III](./problems/1004.max-consecutive-ones-iii.md)
232243
- [1011. 在 D 天内送达包裹的能力](./problems/1011.capacity-to-ship-packages-within-d-days.md)
233244
- [1014. 最佳观光组合](./problems/1014.best-sightseeing-pair.md) 👍
234-
- [1015. 可被 K 整除的最小整数](./problems/1015.smallest-integer-divisible-by-k.md) 👍
245+
- [1015. 可被 K 整除的最小整数](./problems/1015.smallest-integer-divisible-by-k.md)
246+
👍
235247
- [1019. 链表中的下一个更大节点](./problems/1019.next-greater-node-in-linked-list.md)
236248
- [1020. 飞地的数量](./problems/1020.number-of-enclaves.md)
237249
- [1023. 驼峰式匹配](./problems/1023.camelcase-matching.md)
238250
- [1031. 两个非重叠子数组的最大和](./problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md)
239-
- [1043. 分隔数组以得到最大和](./problems/1043.partition-array-for-maximum-sum.md) 👍
240-
- [1104. 二叉树寻路](./problems/1104.path-in-zigzag-labelled-binary-tree.md) 👍
251+
- [1043. 分隔数组以得到最大和](./problems/1043.partition-array-for-maximum-sum.md)
252+
👍
253+
- [1104. 二叉树寻路](./problems/1104.path-in-zigzag-labelled-binary-tree.md)
254+
👍
241255
- [1129. 颜色交替的最短路径](./problems/1129.shortest-path-with-alternating-colors.md)
242-
- [1131.绝对值表达式的最大值](./problems/1131.maximum-of-absolute-value-expression.md) 👍
256+
- [1131.绝对值表达式的最大值](./problems/1131.maximum-of-absolute-value-expression.md)
257+
👍
243258
- [1138. 字母板上的路径](./problems/1138.alphabet-board-path.md)
244-
- [1186. 删除一次得到子数组最大和](./problems/1186.maximum-subarray-sum-with-one-deletion.md) 👍
245-
- [1218. 最长定差子序列](./problems/1218.longest-arithmetic-subsequence-of-given-difference.md) 👍
246-
- [1227. 飞机座位分配概率](./problems/1227.airplane-seat-assignment-probability.md) 👍
247-
- [1261. 在受污染的二叉树中查找元素](./problems/1261.find-elements-in-a-contaminated-binary-tree.md) 👍
248-
- [1262. 可被三整除的最大和](./problems/1262.greatest-sum-divisible-by-three.md) 👍
249-
- [1297. 子串的最大出现次数](./problems/1297.maximum-number-of-occurrences-of-a-substring.md) 👍
259+
- [1186. 删除一次得到子数组最大和](./problems/1186.maximum-subarray-sum-with-one-deletion.md)
260+
👍
261+
- [1218. 最长定差子序列](./problems/1218.longest-arithmetic-subsequence-of-given-difference.md)
262+
👍
263+
- [1227. 飞机座位分配概率](./problems/1227.airplane-seat-assignment-probability.md)
264+
👍
265+
- [1261. 在受污染的二叉树中查找元素](./problems/1261.find-elements-in-a-contaminated-binary-tree.md)
266+
👍
267+
- [1262. 可被三整除的最大和](./problems/1262.greatest-sum-divisible-by-three.md)
268+
👍
269+
- [1297. 子串的最大出现次数](./problems/1297.maximum-number-of-occurrences-of-a-substring.md)
270+
👍
250271
- [1310. 子数组异或查询](./problems/1310.xor-queries-of-a-subarray.md)
251-
- [1334. 阈值距离内邻居最少的城市](./problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md) 👍
272+
- [1334. 阈值距离内邻居最少的城市](./problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md)
273+
👍
252274
- [1371.每个元音包含偶数次的最长子字符串](./problems/1371.find-the-longest-substring-containing-vowels-in-even-counts.md)
253-
- [1381. 设计一个支持增量操作的栈](./problems/1381.design-a-stack-with-increment-operation.md) 91 👍
254-
- [1438. 绝对差不超过限制的最长连续子数组](./problems/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md) 👍
255-
- [1558. 得到目标数组的最少函数调用次数](./problems/1558.minimum-numbers-of-function-calls-to-make-target-array.md) 👍
275+
- [1381. 设计一个支持增量操作的栈](./problems/1381.design-a-stack-with-increment-operation.md)
276+
91 👍
277+
- [1438. 绝对差不超过限制的最长连续子数组](./problems/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md)
278+
👍
279+
- [1558. 得到目标数组的最少函数调用次数](./problems/1558.minimum-numbers-of-function-calls-to-make-target-array.md)
280+
👍
256281
- [1574. 删除最短的子数组使剩余数组有序](./problems/1574.shortest-subarray-to-be-removed-to-make-array-sorted.md)
257282
- [1631. 最小体力消耗路径](./problems/1631.path-with-minimum-effort.md)
258283
- [1658. 将 x 减到 0 的最小操作数](./problems/1658.minimum-operations-to-reduce-x-to-zero.md)
259284
- [1697. 检查边长度限制的路径是否存在](./problems/1697.checking-existence-of-edge-length-limited-paths.md)
260-
- [1737. 满足三条件之一需改变的最少字符数](./problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md) 👍
285+
- [1737. 满足三条件之一需改变的最少字符数](./problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md)
286+
👍
261287
- [1834. 单线程 CPU](./problems/1834.single-threaded-cpu.md)
262-
- [1899. 合并若干三元组以形成目标三元组](./problems/1899.merge-triplets-to-form-target-triplet.md) 👍
288+
- [1899. 合并若干三元组以形成目标三元组](./problems/1899.merge-triplets-to-form-target-triplet.md)
289+
👍
263290
- [1904. 你完成的完整对局数](./problems/1904.the-number-of-full-rounds-you-have-played.md)
264291
- [1906. 查询差绝对值的最小值](./problems/1906.minimum-absolute-difference-queries.md)
265292
- [1906. 查询差绝对值的最小值](./problems/1906.minimum-absolute-difference-queries.md)
@@ -277,7 +304,8 @@
277304
- [Triple-Inversion](./problems/Triple-Inversion.md) 91
278305
- [Kth-Pair-Distance](./problems/Kth-Pair-Distance.md) 91
279306
- [Minimum-Light-Radius](./problems/Minimum-Light-Radius.md) 91
280-
- [Largest Equivalent Set of Pairs](./problems/Largest-Equivalent-Set-of-Pairs.md) 👍
307+
- [Largest Equivalent Set of Pairs](./problems/Largest-Equivalent-Set-of-Pairs.md)
308+
👍
281309
- [Ticket-Order.md](./problems/Ticket-Order.md)
282310
- [Connected-Road-to-Destination](./problems/Connected-Road-to-Destination.md)
283311
- [0004. 寻找两个正序数组的中位数](./problems/4.median-of-two-sorted-arrays.md)
@@ -302,7 +330,8 @@
302330
- [0212. 单词搜索 II](./problems/212.word-search-ii.md)
303331
- [0239. 滑动窗口最大值](./problems/239.sliding-window-maximum.md)
304332
- [0295. 数据流的中位数](./problems/295.find-median-from-data-stream.md)
305-
- [0297. 二叉树的序列化与反序列化](./problems/297.serialize-and-deserialize-binary-tree.md) 91
333+
- [0297. 二叉树的序列化与反序列化](./problems/297.serialize-and-deserialize-binary-tree.md)
334+
91
306335
- [0301. 删除无效的括号](./problems/301.remove-invalid-parentheses.md)
307336
- [0312. 戳气球](./problems/312.burst-balloons.md)
308337
- [330. 按要求补齐数组](./problems/330.patching-array.md)
@@ -317,7 +346,8 @@
317346
- [0679. 24 点游戏](./problems/679.24-game.md)
318347
- [0715. Range 模块](./problems/715.range-module.md)
319348
- [0726. 原子的数量](./problems/726.number-of-atoms.md)
320-
- [0768. 最多能完成排序的块 II](./problems/768.max-chunks-to-make-sorted-ii.md) 91
349+
- [0768. 最多能完成排序的块 II](./problems/768.max-chunks-to-make-sorted-ii.md)
350+
91
321351
- [0805. 数组的均值分割](./problems/805.split-array-with-same-average.md)
322352
- [0839. 相似字符串组](./problems/839.similar-string-groups.md)
323353
- [0887. 鸡蛋掉落](./problems/887.super-egg-drop.md)
@@ -348,6 +378,9 @@
348378
- [2009. 使数组连续的最少操作数](./problems/2009.minimum-number-of-operations-to-make-array-continuous.md)
349379
- [2025. 分割数组的最多方案数](./problems/2025.maximum-number-of-ways-to-partition-an-array.md)
350380
- [2030. 含特定字母的最小子序列](./problems/2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.md)
351-
- [5999. 统计数组中好三元组数目](./problems/5999.count-good-triplets-in-an-array.md) 👍
381+
- [2102. 序列顺序查询](./problems/2102.sequentially-ordinal-rank-tracker.md)
382+
- [2209. 用地毯覆盖后的最少白色砖块](./problems/2209.minimum-white-tiles-after-covering-with-carpets.md)
383+
- [5999. 统计数组中好三元组数目](./problems/5999.count-good-triplets-in-an-array.md)
384+
👍
352385

353386
- [后序](epilogue.md)

problems/661.image-smoother.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
## 题目地址(661. 图片平滑器)
2+
3+
https://leetcode-cn.com/problems/image-smoother/
4+
5+
## 题目描述
6+
7+
```
8+
图像平滑器 是大小为 3 x 3 的过滤器,用于对图像的每个单元格平滑处理,平滑处理后单元格的值为该单元格的平均灰度。
9+
10+
每个单元格的  平均灰度 定义为:该单元格自身及其周围的 8 个单元格的平均值,结果需向下取整。(即,需要计算蓝色平滑器中 9 个单元格的平均值)。
11+
12+
如果一个单元格周围存在单元格缺失的情况,则计算平均灰度时不考虑缺失的单元格(即,需要计算红色平滑器中 4 个单元格的平均值)。
13+
14+
给你一个表示图像灰度的 m x n 整数矩阵 img ,返回对图像的每个单元格平滑处理后的图像 。
15+
16+
 
17+
18+
示例 1:
19+
20+
输入:img = [[1,1,1],[1,0,1],[1,1,1]]
21+
输出:[[0, 0, 0],[0, 0, 0], [0, 0, 0]]
22+
解释:
23+
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
24+
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
25+
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
26+
27+
28+
示例 2:
29+
30+
输入: img = [[100,200,100],[200,50,200],[100,200,100]]
31+
输出: [[137,141,137],[141,138,141],[137,141,137]]
32+
解释:
33+
对于点 (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
34+
对于点 (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
35+
对于点 (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
36+
37+
38+
 
39+
40+
提示:
41+
42+
m == img.length
43+
n == img[i].length
44+
1 <= m, n <= 200
45+
0 <= img[i][j] <= 255
46+
```
47+
48+
## 前置知识
49+
50+
-
51+
52+
## 公司
53+
54+
- 暂无
55+
56+
## 思路
57+
58+
简单思路就是统计以每个点 (i, j) 为中心的周围八个点的数值和,然后计算平均数更新答
59+
案 ans,最后返回 ans 即可。
60+
61+
注意到遍历过程需要更新,于是新建一个数组可以避免这种情况。注意到 img[i][j] 值都
62+
介于 0-255 之间,因此使用 int 的低八位存储值,9-16 位存储新值的原地算法也是可以
63+
的,感兴趣的可以试下。
64+
65+
注意到前面我们需要计算数值和,因此二维前缀和也是可以节省时间的。只不过题目明确了
66+
是周围八个点的和,因此节省的时间也是常数,复杂度不变。
67+
68+
前缀和我直接复制的我
69+
[刷题插件]([力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/)的模板
70+
,没改直接用的。
71+
72+
![image.png](https://pic.leetcode-cn.com/1648089491-zsRKDU-image.png)
73+
74+
## 关键点
75+
76+
- 位运算
77+
- 前缀和
78+
79+
## 代码
80+
81+
- 语言支持:Python3
82+
83+
Python3 Code:
84+
85+
```python
86+
87+
class Solution:
88+
def imageSmoother(self, matrix: List[List[int]]) -> List[List[int]]:
89+
m,n = len(matrix), len(matrix[0])
90+
# 建立
91+
pre = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
92+
for i in range(1, m+1):
93+
for j in range(1, n +1):
94+
pre[i][j] = pre[i-1][j]+ pre[i][j-1] - pre[i-1][j-1] + matrix[i-1][j-1]
95+
ans = [[0 for _ in range(n)] for _ in range(m)]
96+
# 使用,等价于以(x1,y1)为矩阵左上角以(x2,y2)为矩阵右下角的所有格子的和
97+
for i in range(m):
98+
for j in range(n):
99+
x1,y1,x2,y2 = max(0, i-1),max(0, j-1),min(m-1, i+1),min(n-1, j+1)
100+
cnt = (y2 - y1 + 1) * (x2 - x1 + 1)
101+
ans[i][j] = (pre[x2+1][y2+1] + pre[x1][y1] - pre[x1][y2+1] - pre[x2+1][y1])//cnt
102+
return ans
103+
104+
105+
```
106+
107+
**复杂度分析**
108+
109+
令 n 为数组长度。
110+
111+
- 时间复杂度:$O(m*n)$
112+
- 空间复杂度:$O(m*n)$ 可以原地算法优化到 O(1)
113+
114+
> 此题解由
115+
> [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template)
116+
> 自动生成。
117+
118+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时
119+
间收到我的动态啦~
120+
121+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回
122+
答。更多算法套路可以访问我的 LeetCode 题解仓库
123+
https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关
124+
注我的公众号《力扣加加》带你啃下算法这块硬骨头。
125+
126+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你
127+
识别套路,高效刷题。

0 commit comments

Comments
 (0)