From 1ebc647c3338a10a27a15581696c2357c2eac74c Mon Sep 17 00:00:00 2001 From: robot Date: Wed, 4 Jan 2023 00:19:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9B=BE=E5=BA=8A=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 91/binary-search.md | 14 ++-- 91/season2.md | 4 +- 91/two-pointers.md | 4 +- README.en.md | 18 ++--- README.md | 20 ++--- daily/2019-06-27.md | 2 +- daily/2019-07-10.md | 2 +- daily/2019-07-23.md | 2 +- daily/2019-07-26.md | 2 +- daily/2019-07-29.md | 2 +- daily/2019-07-30.md | 2 +- daily/2019-08-13.md | 2 +- epilogue.md | 2 +- introduction.md | 10 +-- problems/1.two-sum.md | 2 +- problems/1004.max-consecutive-ones-iii.md | 2 +- problems/101.symmetric-tree.md | 8 +- problems/101.symmetrical-tree.en.md | 8 +- ...capacity-to-ship-packages-within-d-days.md | 2 +- problems/1014.best-sightseeing-pair.md | 2 +- ...um-sum-of-two-non-overlapping-subarrays.md | 2 +- .../104.maximum-depth-of-binary-tree.en.md | 2 +- problems/104.maximum-depth-of-binary-tree.md | 2 +- .../1043.partition-array-for-maximum-sum.md | 2 +- ...t-sorted-array-to-binary-search-tree.en.md | 2 +- ...vert-sorted-array-to-binary-search-tree.md | 2 +- problems/11.container-with-most-water.md | 6 +- ...104.path-in-zigzag-labelled-binary-tree.md | 10 +-- ...9.shortest-path-with-alternating-colors.md | 2 +- problems/113.path-sum-ii.md | 2 +- ...31.maximum-of-absolute-value-expression.md | 18 ++--- problems/1138.alphabet-board-path.md | 2 +- ...mize-water-distribution-in-a-village-en.md | 4 +- ...ptimize-water-distribution-in-a-village.md | 6 +- ...8.number-of-valid-words-for-each-puzzle.md | 2 +- ....maximum-subarray-sum-with-one-deletion.md | 2 +- ...items-by-groups-respecting-dependencies.md | 8 +- .../121.best-time-to-buy-and-sell-stock.en.md | 4 +- .../121.best-time-to-buy-and-sell-stock.md | 4 +- ...thmetic-subsequence-of-given-difference.md | 2 +- ...2.best-time-to-buy-and-sell-stock-ii.en.md | 4 +- .../122.best-time-to-buy-and-sell-stock-ii.md | 4 +- ...27.airplane-seat-assignment-probability.md | 6 +- problems/124.binary-tree-maximum-path-sum.md | 6 +- problems/125.valid-palindrome.en.md | 6 +- problems/125.valid-palindrome.md | 6 +- ...5.maximum-score-words-formed-by-letters.md | 2 +- problems/1260.shift-2d-grid.en.md | 4 +- problems/1260.shift-2d-grid.md | 4 +- ...-elements-in-a-contaminated-binary-tree.md | 10 +-- .../1262.greatest-sum-divisible-by-three.md | 10 +-- problems/128.longest-consecutive-sequence.md | 2 +- problems/129.sum-root-to-leaf-numbers.md | 6 +- ...um-number-of-occurrences-of-a-substring.md | 2 +- problems/130.surrounded-regions.md | 6 +- problems/131.palindrome-partitioning.md | 2 +- problems/1310.xor-queries-of-a-subarray.md | 4 +- problems/132.palindrome-partitioning-ii.md | 2 +- .../1332.remove-palindromic-sequences.en.md | 2 +- .../1332.remove-palindromic-subsequences.md | 2 +- ...er-of-neighbors-at-a-threshold-distance.md | 6 +- problems/136.single-number.en.md | 2 +- problems/136.single-number.md | 2 +- ...design-a-stack-with-increment-operation.md | 8 +- problems/139.word-break.md | 14 ++-- problems/140.word-break-ii.md | 2 +- problems/142.Linked-List-Cycle-II.md | 2 +- ...aximum-points-you-can-obtain-from-cards.md | 2 +- problems/1435.jump-game-iv.md | 2 +- ...solute-diff-less-than-or-equal-to-limit.md | 2 +- .../144.binary-tree-preorder-traversal.md | 4 +- ...teger-with-digits-that-add-up-to-target.md | 10 +-- .../145.binary-tree-postorder-traversal.md | 2 +- problems/147.insertion-sort-list.md | 4 +- problems/1494.parallel-courses-ii.md | 2 +- problems/15.3sum.md | 4 +- .../150.evaluate-reverse-polish-notation.md | 2 +- problems/152.maximum-product-subarray.md | 2 +- ...a-mysterious-function-closest-to-target.md | 6 +- ...nts-on-subarrays-to-form-a-target-array.md | 2 +- ...53.find-minimum-in-rotated-sorted-array.md | 2 +- problems/155.min-stack.en.md | 8 +- problems/155.min-stack.md | 8 +- ...-of-function-calls-to-make-target-array.md | 4 +- ...rray-to-be-removed-to-make-array-sorted.md | 6 +- ...maximum-sum-obtained-of-any-permutation.md | 2 +- ...160.Intersection-of-Two-Linked-Lists.en.md | 2 +- .../160.Intersection-of-Two-Linked-Lists.md | 2 +- problems/1631.path-with-minimum-effort.md | 6 +- ...reate-sorted-array-through-instructions.md | 2 +- ...167.two-sum-ii-input-array-is-sorted.en.md | 2 +- .../167.two-sum-ii-input-array-is-sorted.md | 2 +- ...mber-of-removals-to-make-mountain-array.md | 2 +- problems/169.majority-element.en.md | 4 +- problems/169.majority-element.md | 4 +- ...-existence-of-edge-length-limited-paths.md | 4 +- ...7.Letter-Combinations-of-a-Phone-Number.md | 4 +- ...inimum-operations-to-make-a-subsequence.md | 2 +- problems/172.factorial-trailing-zeroes.en.md | 8 +- problems/172.factorial-trailing-zeroes.md | 8 +- ...23.find-minimum-time-to-finish-all-jobs.md | 4 +- ...ters-to-satisfy-one-of-three-conditions.md | 2 +- ...om-performing-multiplication-operations.md | 2 +- ...e-the-xor-of-all-segments-equal-to-zero.md | 2 +- problems/1834.single-threaded-cpu.md | 2 +- ...5.find-xor-sum-of-all-pairs-bitwise-and.md | 2 +- problems/1871.jump-game-vii.md | 2 +- problems/1872.stone-game-viii.md | 2 +- ...9.merge-triplets-to-form-target-triplet.md | 2 +- problems/19.removeNthNodeFromEndofList.md | 2 +- problems/190.reverse-bits.en.md | 2 +- problems/190.reverse-bits.md | 2 +- ...e-number-of-full-rounds-you-have-played.md | 2 +- ...906.minimum-absolute-difference-queries.md | 2 +- problems/191.number-of-1-bits.en.md | 4 +- problems/191.number-of-1-bits.md | 4 +- ...1970.last-day-where-you-can-still-cross.md | 2 +- problems/198.house-robber.en.md | 2 +- problems/198.house-robber.md | 4 +- problems/2.add-two-numbers.en.md | 2 +- problems/2.add-two-numbers.md | 4 +- problems/20.valid-parentheses.md | 4 +- problems/20.valid-parents.en.md | 4 +- problems/200.number-of-islands.md | 4 +- ....find-original-array-from-doubled-array.md | 2 +- problems/2008.maximum-earnings-from-taxi.md | 2 +- ...-of-operations-to-make-array-continuous.md | 2 +- ...um-number-of-ways-to-partition-an-array.md | 2 +- .../203.remove-linked-list-elements.en.md | 2 +- problems/203.remove-linked-list-elements.md | 2 +- ...ubsequence-with-occurrences-of-a-letter.md | 2 +- problems/206.reverse-linked-list.md | 2 +- problems/208.implement-trie-prefix-tree.md | 2 +- problems/209.minimum-size-subarray-sum.md | 6 +- problems/21.merge-two-sorted-lists.en.md | 2 +- problems/21.merge-two-sorted-lists.md | 2 +- .../2102.sequentially-ordinal-rank-tracker.md | 2 +- ...d-and-search-word-data-structure-design.md | 2 +- problems/212.word-search-ii.md | 2 +- .../215.kth-largest-element-in-an-array.md | 4 +- problems/219.contains-duplicate-ii.en.md | 2 +- problems/219.contains-duplicate-ii.md | 2 +- problems/22.generate-parentheses.md | 2 +- problems/220.contains-duplicate-iii.md | 2 +- ...white-tiles-after-covering-with-carpets.md | 2 +- problems/221.maximal-square.md | 6 +- problems/226.invert-binary-tree.en.md | 2 +- problems/226.invert-binary-tree.md | 2 +- problems/227.basic-calculator-ii.md | 2 +- .../2281.sum-of-total-strength-of-wizards.md | 2 +- problems/229.majority-element-ii.md | 6 +- problems/23.merge-k-sorted-lists.md | 4 +- problems/230.kth-smallest-element-in-a-bst.md | 2 +- problems/2306.naming-a-company.md | 2 +- .../232.implement-queue-using-stacks.en.md | 10 +-- problems/232.implement-queue-using-stacks.md | 10 +-- ...lowest-common-ancestor-of-a-binary-tree.md | 6 +- problems/238.product-of-array-except-self.md | 2 +- problems/239.sliding-window-maximum.md | 4 +- problems/24.swapNodesInPairs.md | 6 +- problems/240.search-a-2-d-matrix-ii.md | 4 +- problems/25.reverse-nodes-in-k-groups-en.md | 6 +- problems/25.reverse-nodes-in-k-groups.md | 8 +- ....remove-duplicates-from-sorted-array.en.md | 4 +- .../26.remove-duplicates-from-sorted-array.md | 4 +- problems/263.ugly-number.en.md | 4 +- problems/263.ugly-number.md | 4 +- problems/279.perfect-squares.md | 2 +- problems/283.move-zeroes.en.md | 2 +- problems/283.move-zeroes.md | 2 +- problems/29.divide-two-integers.md | 4 +- problems/295.find-median-from-data-stream.md | 6 +- ...7.serialize-and-deserialize-binary-tree.md | 6 +- ...-substring-without-repeating-characters.md | 4 +- ...bstring-with-concatenation-of-all-words.md | 2 +- problems/301.remove-invalid-parentheses.md | 2 +- problems/31.next-permutation.md | 6 +- problems/312.burst-balloons.md | 2 +- problems/32.longest-valid-parentheses.md | 4 +- problems/322.coin-change.md | 2 +- problems/324.wiggle-sort-ii.md | 2 +- problems/328.odd-even-linked-list.md | 2 +- problems/33.search-in-rotated-sorted-array.md | 6 +- problems/330.patching-array.md | 2 +- ...preorder-serialization-of-a-binary-tree.md | 2 +- .../334.increasing-triplet-subsequence.md | 4 +- problems/335.self-crossing.md | 10 +-- problems/337.house-robber-iii.md | 2 +- problems/342.power-of-four.en.md | 8 +- problems/342.power-of-four.md | 8 +- problems/343.integer-break.md | 10 +-- problems/349.intersection-of-two-arrays.en.md | 2 +- problems/349.intersection-of-two-arrays.md | 2 +- problems/365.water-and-jug-problem.md | 2 +- problems/371.sum-of-two-integers.en.md | 6 +- problems/371.sum-of-two-integers.md | 6 +- ...kth-smallest-element-in-a-sorted-matrix.md | 10 +-- problems/380.insert-delete-getrandom-o1.md | 8 +- problems/385.mini-parser.md | 2 +- problems/39.combination-sum.md | 2 +- problems/394.decode-string.md | 14 ++-- problems/4.median-of-two-sorted-arrays.md | 12 +-- problems/40.combination-sum-ii.md | 4 +- problems/401.binary-watch.en.md | 2 +- problems/401.binary-watch.md | 2 +- problems/416.partition-equal-subset-sum.md | 2 +- problems/42.trapping-rain-water.en.md | 4 +- problems/42.trapping-rain-water.md | 4 +- ...longest-repeating-character-replacement.md | 2 +- problems/437.path-sum-iii.en.md | 6 +- problems/437.path-sum-iii.md | 6 +- problems/445.add-two-numbers-ii.md | 2 +- problems/454.4-Sum-ii.en.md | 2 +- problems/454.4-sum-ii.md | 4 +- problems/455.AssignCookies.en.md | 2 +- problems/455.AssignCookies.md | 2 +- problems/456.132-pattern.md | 2 +- problems/457.circular-array-loop.md | 2 +- problems/460.lfu-cache.md | 16 ++-- problems/464.can-i-win.md | 14 ++-- problems/470.implement-rand10-using-rand7.md | 2 +- problems/472.concatenated-words.md | 4 +- problems/473.matchsticks-to-square.md | 2 +- problems/474.ones-and-zeros-en.md | 2 +- problems/48.rotate-image.md | 6 +- problems/480.sliding-window-median.md | 2 +- problems/488.zuma-game.md | 8 +- problems/49.group-anagrams.md | 4 +- problems/493.reverse-pairs.md | 2 +- problems/494.target-sum.md | 8 +- problems/5.longest-palindromic-substring.md | 8 +- problems/50.pow-x-n.md | 10 +-- problems/504.base-7.md | 4 +- .../516.longest-palindromic-subsequence.md | 8 +- problems/518.coin-change-2.md | 4 +- problems/52.N-Queens-II.md | 4 +- problems/525.contiguous-array.md | 2 +- problems/5254.selling-pieces-of-wood.md | 2 +- problems/53.maximum-sum-subarray-cn.en.md | 4 +- problems/53.maximum-sum-subarray-cn.md | 6 +- problems/53.maximum-sum-subarray-en.md | 4 +- problems/547.friend-circles-en.md | 8 +- problems/547.number-of-provinces.md | 2 +- problems/55.jump-game.md | 2 +- problems/56.merge-intervals.md | 4 +- problems/560.subarray-sum-equals-k.en.md | 2 +- problems/560.subarray-sum-equals-k.md | 2 +- ....maximum-xor-with-an-element-from-array.md | 4 +- problems/57.insert-interval.md | 2 +- problems/575.distribute-candies.en.md | 4 +- problems/575.distribute-candies.md | 4 +- ...imum-skips-to-arrive-at-meeting-on-time.md | 2 +- .../5935.find-good-days-to-rob-the-bank.md | 2 +- problems/5936.detonate-the-maximum-bombs.md | 2 +- ...65.intervals-between-identical-elements.md | 2 +- .../5999.count-good-triplets-in-an-array.md | 2 +- problems/60.permutation-sequence.md | 2 +- problems/611.valid-triangle-number.md | 2 +- problems/62.unique-paths.md | 8 +- problems/63.unique-paths-ii.md | 8 +- problems/65.valid-number.md | 4 +- problems/661.image-smoother.md | 2 +- problems/664.strange-printer.md | 2 +- problems/665.non-decreasing-array.md | 2 +- ...umber-of-longest-increasing-subsequence.md | 2 +- problems/679.24-game.md | 2 +- problems/686.repeated-string-match.md | 2 +- problems/710.random-pick-with-blacklist.md | 2 +- ...buy-and-sell-stock-with-transaction-fee.md | 2 +- problems/715.range-module.md | 8 +- ...718.maximum-length-of-repeated-subarray.md | 2 +- problems/721.accounts-merge.md | 2 +- problems/726.number-of-atoms.md | 2 +- problems/73.set-matrix-zeroes.md | 6 +- problems/735.asteroid-collision.md | 2 +- problems/75.sort-colors.md | 14 ++-- problems/768.max-chunks-to-make-sorted-ii.md | 8 +- problems/78.subsets-en.md | 2 +- problems/78.subsets.md | 2 +- problems/79.word-search-en.md | 14 ++-- problems/79.word-search.md | 16 ++-- problems/790.domino-and-tromino-tiling.md | 6 +- problems/799.champagne-tower.md | 4 +- ....remove-duplicates-from-sorted-array-ii.md | 12 +-- ...imum-swaps-to-make-sequences-increasing.md | 2 +- problems/805.split-array-with-same-average.md | 2 +- problems/816.ambiguous-coordinates.md | 2 +- problems/820.short-encoding-of-words.md | 6 +- ...821.shortest-distance-to-a-character.en.md | 2 +- .../821.shortest-distance-to-a-character.md | 2 +- problems/838.push-dominoes.md | 2 +- problems/84.largest-rectangle-in-histogram.md | 6 +- problems/85.maximal-rectangle.md | 2 +- problems/86.partition-list.md | 4 +- problems/87.scramble-string.md | 2 +- ...length-of-longest-fibonacci-subsequence.md | 2 +- problems/874.walking-robot-simulation.en.md | 4 +- problems/874.walking-robot-simulation.md | 4 +- problems/875.koko-eating-bananas.md | 4 +- problems/88.merge-sorted-array.en.md | 8 +- problems/88.merge-sorted-array.md | 8 +- problems/886.possible-bipartition.md | 8 +- problems/887.super-egg-drop.md | 16 ++-- problems/895.maximum-frequency-stack.md | 10 +-- problems/898.bitwise-ors-of-subarrays.md | 2 +- problems/90.subsets-ii-en.md | 2 +- problems/909.snakes-and-ladders.md | 2 +- problems/91.decode-ways.md | 2 +- problems/912.sort-an-array.md | 4 +- problems/92.reverse-linked-list-ii.md | 4 +- problems/932.beautiful-array.md | 2 +- problems/935.knight-dialer.md | 4 +- problems/94.binary-tree-inorder-traversal.md | 4 +- ...-stones-removed-with-same-row-or-column.md | 8 +- problems/959.regions-cut-by-slashes.md | 6 +- problems/975.odd-even-jump.md | 2 +- problems/978.longest-turbulent-subarray.md | 2 +- problems/98.validate-binary-search-tree.md | 2 +- ...rtical-order-traversal-of-a-binary-tree.md | 2 +- ...nimum-number-of-k-consecutive-bit-flips.md | 2 +- problems/997.find-the-town-judge.md | 2 +- problems/Every-Sublist-Min-Sum.md | 2 +- problems/Ticket-Order.md | 2 +- problems/binode-lcci.en.md | 8 +- problems/binode-lcci.md | 8 +- problems/consecutive-wins.md | 2 +- problems/get-kth-magic-number-lcci.md | 2 +- problems/lcp20.meChtZ.md | 4 +- problems/lcp21.Za25hA.md | 2 +- problems/max-black-square-lcci.md | 10 +-- problems/sub-sort-lcci.md | 2 +- selected/LCS.md | 4 +- selected/LIS.md | 16 ++-- selected/LSS.md | 4 +- selected/a-deleted.md | 16 ++-- selected/atMostK.md | 8 +- selected/byte-dance-algo-ex-2017.md | 18 ++--- selected/byte-dance-algo-ex.md | 12 +-- selected/construct-binary-tree.md | 26 +++---- selected/mother-01.md | 6 +- selected/schedule-topic.md | 10 +-- selected/serialize.md | 12 +-- selected/zuma-game.md | 8 +- thanksGiving.md | 40 +++++----- thanksGiving2.md | 36 ++++----- thanksGiving3.md | 18 ++--- thinkings/GCD.en.md | 8 +- thinkings/GCD.md | 8 +- thinkings/backtrack.en.md | 10 +-- thinkings/backtrack.md | 10 +-- thinkings/balanced-tree.en.md | 8 +- thinkings/balanced-tree.md | 8 +- thinkings/basic-data-structure.en.md | 24 +++--- thinkings/basic-data-structure.md | 28 +++---- thinkings/binary-search-1.en.md | 2 +- thinkings/binary-search-1.md | 4 +- thinkings/binary-search-2.en.md | 6 +- thinkings/binary-search-2.md | 14 ++-- thinkings/binary-tree-traversal.en.md | 6 +- thinkings/binary-tree-traversal.md | 6 +- thinkings/bloom-filter.en.md | 2 +- thinkings/bloom-filter.md | 2 +- thinkings/dynamic-programming.en.md | 2 +- thinkings/dynamic-programming.md | 24 +++--- thinkings/graph.en.md | 14 ++-- thinkings/graph.md | 28 +++---- thinkings/greedy.en.md | 12 +-- thinkings/greedy.md | 12 +-- thinkings/heap-2.en.md | 8 +- thinkings/heap-2.md | 30 +++---- thinkings/heap.en.md | 46 +++++------ thinkings/heap.md | 46 +++++------ thinkings/island.en.md | 12 +-- thinkings/island.md | 12 +-- thinkings/linked-list.en.md | 30 +++---- thinkings/linked-list.md | 54 ++++++------- thinkings/monotone-stack.en.md | 4 +- thinkings/monotone-stack.md | 4 +- thinkings/prefix.en.md | 4 +- thinkings/prefix.md | 8 +- ...run-length-encode-and-huffman-encode.en.md | 2 +- .../run-length-encode-and-huffman-encode.md | 4 +- thinkings/search.en.md | 2 +- thinkings/search.md | 26 +++---- thinkings/slide-window.en.en.md | 4 +- thinkings/slide-window.en.md | 4 +- thinkings/slide-window.md | 4 +- thinkings/tree.en.md | 22 +++--- thinkings/tree.md | 78 +++++++++---------- thinkings/trie.en.md | 2 +- thinkings/trie.md | 6 +- thinkings/union-find.en.md | 18 ++--- thinkings/union-find.md | 18 ++--- 393 files changed, 1146 insertions(+), 1146 deletions(-) diff --git a/91/binary-search.md b/91/binary-search.md index 041e43099..9d76c4ea5 100644 --- a/91/binary-search.md +++ b/91/binary-search.md @@ -779,9 +779,9 @@ target 在右侧有序部分,我们就可以舍弃左边部分了(通过 start 我们以([6,7,8,1,2,3,4,5], 4)为例讲解一下: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh9ahf86uyj30if0b0t9w.jpg) +![](https://p.ipic.vip/e1eqm5.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh9ahoznqjj30gx0i2wgb.jpg) +![](https://p.ipic.vip/gmsqw5.jpg) 接下来,我们考虑重复元素的问题。如果存在重复数字,就可能会发生 nums[mid] == nums[start] 了,比如 30333 。这个时候可以选择舍弃 start,也就是 start 右移一位。 @@ -1059,7 +1059,7 @@ class Solution: 2. 如果中间元素 > 数组第一个元素,我们需要在 mid 右边搜索。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh99umkpjcj30q20c8aak.jpg) +![](https://p.ipic.vip/e5lrsi.jpg) - 如果中间元素 <= 数组第一个元素,我们需要在 mid 左边搜索。 @@ -1071,7 +1071,7 @@ class Solution: - nums[mid - 1] > nums[mid],因此 mid 是最小值。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh99yah60sj30mq0aidg8.jpg) +![](https://p.ipic.vip/c524lk.jpg) ###### 代码(Python) @@ -1129,7 +1129,7 @@ class Solution: 最简单的,如果这个二叉树是一个二叉搜索树(BST)。 那么实际上,在一个二叉搜索树种 进行搜索的过程就是二分法。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlvp2whsdj30zk0tngoh.jpg) +![](https://p.ipic.vip/bd2rnk.jpg) 如上图,我们需要在这样一个二叉搜索树中搜索 7。那么我们的搜索路径则会是 8 -> 3 -> 6 -> 7,这也是一种二分法。只不过相比于普通的**有序序列查找给定值**二分, 其时间 @@ -1138,13 +1138,13 @@ class Solution: 上面讲了二叉搜索树,我们再来看一种同样特殊的树 - 完全二叉树。 如果我们给一颗完全 二叉树的所有节点进行编号(二进制),依次为 01,10,11, ...。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlwv88wl2j30g508ht9m.jpg) +![](https://p.ipic.vip/exnxz6.jpg) 那么实际上,最后一行的编号就是从根节点到该节点的路径。 其中 0 表示向左, 1 表示 向右。(第一位数字不用)。 我们以最后一行的 101 为例,我们需要执行一次左,然后一次 右。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlwu1qyklj30ex081758.jpg) +![](https://p.ipic.vip/z5fob9.jpg) 其实原理也不难,如果你用数组表示过完全二叉树,那么就很容易理解。 我们可以发现,左节点的编号都是父节点的二倍,并且右节点都是父节点的二倍 + 1。从二进制的角度来看就是:**父节点的编号左移一位就是左节点的编号,左移一位 + 1 就是右节点的编号**。 因此反过来, 知道了子节点的最后一位,我们就能知道它是父节点的左节点还是右节点啦。 diff --git a/91/season2.md b/91/season2.md index ff4fe06e0..5ac5a88e8 100644 --- a/91/season2.md +++ b/91/season2.md @@ -10,7 +10,7 @@ 群里每天都会有题目,推荐大家讨论当天的题目。我们会帮助大家规划学习路线,91 天见证不一样的自己。群里会有专门的资深算法竞赛大佬坐阵解答大家的问题和疑问,并且会对前一天的题目进行讲解。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gf2b2zkclnj30xm0b6aat.jpg) +![](https://p.ipic.vip/7zxu6v.jpg) ## 活动时间 @@ -33,7 +33,7 @@ ## 课程大纲 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1giq98aux20j30ju0qt781.jpg) +![](https://p.ipic.vip/bno0ye.jpg) 第一期部分公开的讲义: diff --git a/91/two-pointers.md b/91/two-pointers.md index 40029efca..4dc0bb733 100644 --- a/91/two-pointers.md +++ b/91/two-pointers.md @@ -16,7 +16,7 @@ for(int i = 0;i < nums.size(); i++) { } ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gf5w79tciyj30aa0hl77b.jpg) +![](https://p.ipic.vip/s306f5.jpg) (图 1) @@ -35,7 +35,7 @@ while (l < r) { return l ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gf5yfe9da7j307504ut8r.jpg) +![](https://p.ipic.vip/duhwzn.jpg) (图 2) diff --git a/README.en.md b/README.en.md index 591f2e380..ac1afec4e 100644 --- a/README.en.md +++ b/README.en.md @@ -1,6 +1,6 @@ # LeetCode -[![Travis](https://img.shields.io/badge/language-C++-green.svg)]() [![Travis](https://img.shields.io/badge/language-JavaScript-yellow.svg)]() [![Travis](https://img.shields.io/badge/language-Python-red.svg)]() [![Travis](https://img.shields.io/badge/language-Java-blue.svg)]() ![Total visitor](https://visitor-count-badge.herokuapp.com/total.svg?repo_id=azl397985856.leetcode.en) ![Visitors in today](https://visitor-count-badge.herokuapp.com/today.svg?repo_id=azl397985856.leetcode.en) +[![Travis](https://p.ipic.vip/hnzzr3.jpg)]() [![Travis](https://p.ipic.vip/3zihse.jpg)]() [![Travis](https://p.ipic.vip/hh8zzk.jpg)]() [![Travis](https://p.ipic.vip/gd28pb.jpg)]() ![Total visitor](https://visitor-count-badge.herokuapp.com/total.svg?repo_id=azl397985856.leetcode.en) ![Visitors in today](https://visitor-count-badge.herokuapp.com/today.svg?repo_id=azl397985856.leetcode.en) > since 2019-09-03 19:40 @@ -8,7 +8,7 @@ --- -![leetcode.jpeg](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwf4xivj30dw0780sm.jpg) +![leetcode.jpeg](https://p.ipic.vip/u6nhhh.jpg) This essay records the course of and my emotion to this project from initialization to 10,000 stars. [Milestone for 10,000+ stars](./thanksGiving.md) @@ -16,7 +16,7 @@ If you are interested in this project, **do not mean your star**. This project w ## Introduction -![leetcode.jpeg](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwf4xivj30dw0780sm.jpg) +![leetcode.jpeg](https://p.ipic.vip/u6nhhh.jpg) LeetCode Solutions: A Journey of Problem Solving. @@ -49,7 +49,7 @@ If you want to do some contributions or collaborations, just feel free to contac - Here will be the place to update Anki Flashcards in the future as well. - Here is a mind mapping graph showing the summary of categorizations of problems that are questioned frequently in interviews. We could analyze according to the information in the graph. -![leetcode-zhihu](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwgi53bj30k00jx0te.jpg) +![leetcode-zhihu](https://p.ipic.vip/58vm3a.jpg) (Picture credited by [LeetCode-cn](https://www.zhihu.com/question/24964987/answer/586425979).) @@ -74,15 +74,15 @@ The data structures mainly include: [0547.friend-circles](./problems/547.friend-circles-en.md) : -![friend circle BFS](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwh1getj30u0140tdc.jpg) +![friend circle BFS](https://p.ipic.vip/5gg5y0.jpg) [backtrack problems](./problems/90.subsets-ii-en.md): -![backtrack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwhwowgj30n20nptas.jpg) +![backtrack](https://p.ipic.vip/w5g03x.jpg) [0454.4-sum-ii](./problems/454.4-sum-ii.en.md) : -![454.4-sum-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwivf65j30le0deab3.jpg) +![454.4-sum-ii](https://p.ipic.vip/vaniw4.jpg) ## Portals @@ -177,11 +177,11 @@ We're still on the early stage, so feedback from community is very welcome. For ### QQ (For China Region) -![qq-group-chat](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwje9plj3060060wel.jpg) +![qq-group-chat](https://p.ipic.vip/k88y70.jpg) ### WeChat (For China Region) -![wechat-group-chat](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwjrk6ij30e80e875j.jpg) +![wechat-group-chat](https://p.ipic.vip/d621ys.jpg) (Add this bot and reply "leetcode" to join the group.) diff --git a/README.md b/README.md index 3a7cd241e..0de01cae4 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # LeetCode -[![Travis](https://img.shields.io/badge/language-C++-green.svg)]() [![Travis](https://img.shields.io/badge/language-Python-red.svg)]() [![Travis](https://img.shields.io/badge/language-Java-blue.svg)]() [![Travis](https://img.shields.io/badge/language-Go-red.svg)]() [![Travis](https://img.shields.io/badge/language-Php-pink.svg)]() [![Travis](https://img.shields.io/badge/language-JavaScript-yellow.svg)]() +[![Travis](https://p.ipic.vip/k4pv1r.jpg)]() [![Travis](https://p.ipic.vip/32nfgh.jpg)]() [![Travis](https://p.ipic.vip/4a36ao.jpg)]() [![Travis](https://p.ipic.vip/fd1f82.jpg)]() [![Travis](https://p.ipic.vip/mhz5uy.jpg)]() [![Travis](https://p.ipic.vip/gp1hvz.jpg)]() -[![](https://img.shields.io/badge/WeChat-微信群-brightgreen)](#哪里能找到我) [![](https://img.shields.io/badge/公众号-力扣加加-blueviolet)](#哪里能找到我) [![](https://img.shields.io/badge/Juejin-掘金-blue)](https://juejin.im/user/58af98305c497d0067780b3b) [![](https://img.shields.io/badge/Zhihu-知乎-blue)](https://www.zhihu.com/people/lu-xiao-13-70) [![](https://img.shields.io/badge/bilili-哔哩哔哩-ff69b4)](https://space.bilibili.com/519510412/) +[![](https://img.shields.io/badge/WeChat-微信群-brightgreen)](#哪里能找到我) [![](https://img.shields.io/badge/公众号-力扣加加-blueviolet)](#哪里能找到我) [![](https://img.shields.io/badge/Juejin-掘金-blue)](https://p.ipic.vip/pj4t8y.jpg) [![](https://img.shields.io/badge/Zhihu-知乎-blue)](https://p.ipic.vip/n9co7k.jpg) [![](https://img.shields.io/badge/bilili-哔哩哔哩-ff69b4)](https://p.ipic.vip/m7g3to.jpg) 简体中文 | [English](./README.en.md) @@ -14,7 +14,7 @@ 我的新书《算法通关之路》出版了。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gu39d1zb7qj622g0u013a02.jpg) +![](https://p.ipic.vip/zo8cz5.jpg) - [实体版购书链接 1](https://union-click.jd.com/jdc?e=618|pc|&p=JF8BAOAJK1olXgEGUV9cAE4VCl8IGloXWgYCV1tcAE8TBl9MRANLAjZbERscSkAJHTdNTwcKBlMdBgABFksWAm0PG1sWWAcKUFpYFxJSXzI4GAhrA0IDUiM-FjFxQQtKWFx2AlkYElJROEonA24JG1MQWgMEUW5tCEwnQgEIGlkdXAQHUW5cOEsQBmkNElwWXgYGUFxtD0seMzRddVwVWFVWB10PXxtDVDo4K2sWbQECXRMcWgYnM284GGtXMwUKAw5VDEpDA2oBGl4SXwELUF5fCkkQVDtdH1JGX1EAZFxcCU8eMw) @@ -28,7 +28,7 @@ [在线阅读地址](https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/) -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm3r7y4dt8j30zx0u0hdt.jpg) +![](https://p.ipic.vip/x1u80k.jpg) **限时免费下载!后期随时可能收费** @@ -46,7 +46,7 @@ ## 九章算法班 -![](https://tva1.sinaimg.cn/large/e6c9d24ely1h5bw2q7urhj20m803ct8z.jpg) +![](https://p.ipic.vip/iyzbvl.jpg) 九章算法,由北大、清华校友于美国硅谷创办,已帮助数十万 IT 程序员找到高薪 offer! 提供 1 对 1 求职指导、算法指导、前后端项目、简历代笔等服务。 @@ -54,7 +54,7 @@ - 推荐北大 FB 双料大神的[《九章算法班》](https://www.jiuzhang.com/course/71/?utm_source=tf-github-lucifer2022),有人靠他连拿 3 个大厂 offer -![](https://tva1.sinaimg.cn/large/e6c9d24ely1h5bw3rtr9oj20m807sdgp.jpg) +![](https://p.ipic.vip/8a4bul.jpg) ## :calendar:《91 天学算法》限时活动 @@ -66,7 +66,7 @@ [点此参与](https://github.com/azl397985856/leetcode/discussions/532) -![](https://tva1.sinaimg.cn/large/008i3skNly1gq0mm4lscqj313h0r0diy.jpg) +![](https://p.ipic.vip/ltkyaq.jpg) - 🔥🔥🔥🔥 [活动首页](https://leetcode-solution.cn/91) 🔥🔥🔥🔥 - [91 第三期讲义 - 二分专题(上)](./thinkings/binary-search-1.md) @@ -96,7 +96,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - 这里有一张互联网公司面试中经常考察的问题类型总结的思维导图,我们可以结合图片中的信息分析一下。 -![leetcode-zhihu](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluennxvrj30k00jx0te.jpg) +![leetcode-zhihu](https://p.ipic.vip/a20o3x.jpg) (图片来自 leetcode) @@ -570,7 +570,7 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后 大家也可以加我微信好友进行交流! -![](https://tva1.sinaimg.cn/large/008i3skNly1gx11szd02ej30e80e8dg3.jpg) +![](https://p.ipic.vip/wciz1n.jpg) ## :chart_with_upwards_trend: 大事件 @@ -581,7 +581,7 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后 - 2020-04-12: [项目突破三万 Star](./thanksGiving3.md)。 - 2020-04-14: 官网`力扣加加`上线啦 💐💐💐💐💐,有专题讲解,每日一题,下载区和视频题解,后续会增加更多内容,还不赶紧收藏起来?地址: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluemaoj3j30z90dtmy5.jpg) +![](https://p.ipic.vip/98p19b.jpg) - 2021-02-23: star 破四万 diff --git a/daily/2019-06-27.md b/daily/2019-06-27.md index 3c7c90854..c992f5d4d 100644 --- a/daily/2019-06-27.md +++ b/daily/2019-06-27.md @@ -52,7 +52,7 @@ function sqrt(num) { 也就是说,函数上任一点(x,f(x))处的切线斜率是2x。 那么,x-f(x)/(2x)就是一个比x更接近的近似值。代入 f(x)=x^2-a得到x-(x^2-a)/(2x),也就是(x+a/x)/2。 -![2019-06-27](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludzm5xsg30ip0dct9s.gif) +![2019-06-27](https://p.ipic.vip/cs2twn.gif) (图片来自Wikipedia) diff --git a/daily/2019-07-10.md b/daily/2019-07-10.md index 37a5078a9..7b2ccd495 100644 --- a/daily/2019-07-10.md +++ b/daily/2019-07-10.md @@ -28,7 +28,7 @@ 这个题目解释起来比较费劲,我在网上找了一个现成的图来解释一下: -![weight-ball](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue317j6j30d80dcta4.jpg) +![weight-ball](https://p.ipic.vip/4r85gu.jpg) 图中“1+”是指“1号小球为重”这一可能性。“1-”是指“1号小球为轻”这一可能性。 一开始一共有24种可能性。 diff --git a/daily/2019-07-23.md b/daily/2019-07-23.md index 9f4507227..0994e07e9 100644 --- a/daily/2019-07-23.md +++ b/daily/2019-07-23.md @@ -14,7 +14,7 @@ ``` -![2019-07-23](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludxwb1aj30py1hc0tr.jpg) +![2019-07-23](https://p.ipic.vip/ynwmml.jpg) ## 参考答案 diff --git a/daily/2019-07-26.md b/daily/2019-07-26.md index 0ce407f2b..cbfe51022 100644 --- a/daily/2019-07-26.md +++ b/daily/2019-07-26.md @@ -8,7 +8,7 @@ ## 题目描述 -![2019-07-26](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludytrtlj30py1hcas1.jpg) +![2019-07-26](https://p.ipic.vip/2r3uxg.jpg) ## 参考答案 diff --git a/daily/2019-07-29.md b/daily/2019-07-29.md index b8790fcc4..ce6c1404b 100644 --- a/daily/2019-07-29.md +++ b/daily/2019-07-29.md @@ -37,7 +37,7 @@ Example 2: 2. row->col、col->row 的切换都伴随读取的初始位置的变化; 3. 结束条件是row头>row尾或者col顶>col底 -![剥洋葱](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue0ni96j30b00bdq35.jpg) +![剥洋葱](https://p.ipic.vip/l0rqs7.jpg) 时间复杂度O(m*n), 空间复杂度O(1) diff --git a/daily/2019-07-30.md b/daily/2019-07-30.md index 529eb8a15..8ef8c39e8 100644 --- a/daily/2019-07-30.md +++ b/daily/2019-07-30.md @@ -22,7 +22,7 @@ 那么沿着这条纬线(记为E纬线)上任意一点向东走一英里,始终会回到原点,只是走的圈数不同而已。 根据题目倒推,在这条纬线以北一英里存在一条纬线(记为N纬线),从N纬线的任意一点向南一英里到达E纬线W点,沿着E纬线向东一英里,必会回到W点,再向北走一英里恰好可以回到起点。北极点可能包含在这个集合中,也可能不在。 如下图示供参考: -![earth-problem](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue5gt6uj30u01441l0.jpg) +![earth-problem](https://p.ipic.vip/v0xcjn.jpg) 所以答案是无数个点 diff --git a/daily/2019-08-13.md b/daily/2019-08-13.md index ef9504fa8..1baf1d433 100644 --- a/daily/2019-08-13.md +++ b/daily/2019-08-13.md @@ -61,7 +61,7 @@ m 和 n =150,肯定超时。 最后将探测结果进行合并即可。合并的条件就是当前单元既能流入太平洋又能流入大西洋。 -![集合](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlue21s7aj30dw08cglo.jpg) +![集合](https://p.ipic.vip/r02fm7.jpg) 扩展: 如果题目改为能够流入大西洋或者太平洋,我们只需要最后合并的时候,条件改为求或即可 diff --git a/epilogue.md b/epilogue.md index 8fcb275b7..fdf4e90dd 100644 --- a/epilogue.md +++ b/epilogue.md @@ -8,6 +8,6 @@ 关注公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/iiew7e.jpg) lucifer 的博客地址:https://lucifer.ren/blog/ diff --git a/introduction.md b/introduction.md index 3f2992fa7..a71a19f1a 100644 --- a/introduction.md +++ b/introduction.md @@ -10,13 +10,13 @@ 我的新书《算法通关之路》出版了。这本书和本仓库内容几乎没有任何重叠,采用 Python 编写,不过也提供了 Java,CPP 以及 JS 代码供大家参考。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gu39d1zb7qj622g0u013a02.jpg) +![](https://p.ipic.vip/l9sxsa.jpg) [图书介绍](https://mp.weixin.qq.com/s?__biz=MzI4MzUxNjI3OA==&mid=2247489484&idx=1&sn=a16664605744a970f8a81e64affb01a7&chksm=eb88dbd5dcff52c3ecee38c7f594df6d16ed7ca2852ad4d0d86bab99483f4413c30e98b00e43&token=715489125&lang=zh_CN#rd) 大家也可以扫描下方二维码购买。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gu3sf6szzij60dw0i2aax02.jpg) +![](https://p.ipic.vip/ny26q0.jpg) ## 电子书 @@ -24,7 +24,7 @@ 这是我将我的所有公开的算法资料整理的一个电子书,全部题目信息中文化,以前会有一些英文描述,感谢 @CYL 的中文整理。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm3r7y4dt8j30zx0u0hdt.jpg) +![](https://p.ipic.vip/1nxfdk.jpg) **限时免费下载!后期随时可能收费** @@ -74,7 +74,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。 - 对于最近更新的部分, 后面会有 🖊 标注 - 这里有一张互联网公司面试中经常考察的问题类型总结的思维导图,我们可以结合图片中的信息分析一下。 -![leetcode-zhihu](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluennxvrj30k00jx0te.jpg) +![leetcode-zhihu](https://p.ipic.vip/pe0egq.jpg) (图片来自 leetcode) @@ -544,7 +544,7 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后 - 2020-04-12: [项目突破三万 Star](./thanksGiving3.md)。 - 2020-04-14//leetcode-solution.cn/ -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluemaoj3j30z90dtmy5.jpg) +![](https://p.ipic.vip/pq92y4.jpg) - 2021-02-23: star 破四万 diff --git a/problems/1.two-sum.md b/problems/1.two-sum.md index de56a899e..af635d545 100644 --- a/problems/1.two-sum.md +++ b/problems/1.two-sum.md @@ -158,4 +158,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/2tzysv.jpg) diff --git a/problems/1004.max-consecutive-ones-iii.md b/problems/1004.max-consecutive-ones-iii.md index 4282811f9..4596522ab 100644 --- a/problems/1004.max-consecutive-ones-iii.md +++ b/problems/1004.max-consecutive-ones-iii.md @@ -131,4 +131,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/d00epc.jpg) diff --git a/problems/101.symmetric-tree.md b/problems/101.symmetric-tree.md index e5e99cc6d..9c5754c19 100644 --- a/problems/101.symmetric-tree.md +++ b/problems/101.symmetric-tree.md @@ -53,7 +53,7 @@ https://leetcode-cn.com/problems/symmetric-tree/ 看到这题的时候,我的第一直觉是 DFS。然后我就想:`如果左子树是镜像,并且右子树也是镜像,是不是就说明整体是镜像?`。经过几秒的思考, 这显然是不对的,不符合题意。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu96e83wj31200iugme.jpg) +![](https://p.ipic.vip/bke0ic.jpg) 很明显其中左子树中的节点会和右子树中的节点进行比较,我把比较的元素进行了颜色区分,方便大家看。 @@ -61,7 +61,7 @@ https://leetcode-cn.com/problems/symmetric-tree/ 因此想法是两次遍历,第一次遍历的同时将遍历结果存储到哈希表中,然后第二次遍历去哈希表取。这种方法可行,但是需要 N 的空间(N 为节点总数)。我想到如果两者可以同时进行遍历,是不是就省去了哈希表的开销。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9a7sy7j31a30u0408.jpg) +![](https://p.ipic.vip/b9e8xo.jpg) 如果不明白的话,我举个简单例子: @@ -199,6 +199,6 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/m2fbex.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9b4p9ej30x20iwjtf.jpg) +![](https://p.ipic.vip/ee9bkp.jpg) diff --git a/problems/101.symmetrical-tree.en.md b/problems/101.symmetrical-tree.en.md index 7a411e810..a30977b5d 100644 --- a/problems/101.symmetrical-tree.en.md +++ b/problems/101.symmetrical-tree.en.md @@ -53,7 +53,7 @@ Can you use recursion and iteration to solve this problem? When I saw this question, my first instinct was DFS. Then I thought: `If the left subtree is a mirror image, and the right subtree is also a mirror image, does it mean that the whole is a mirror image? `. After a few seconds of thinking, this is obviously wrong and does not meet the meaning of the question. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu96e83wj31200iugme.jpg) +![](https://p.ipic.vip/mz2jix.jpg) Obviously, the nodes in the left subtree will be compared with the nodes in the right subtree. I have distinguished the colors of the compared elements for your convenience. @@ -61,7 +61,7 @@ My idea here is: `When traversing each node, if I can know who its corresponding Therefore, the idea is to traverse twice. During the first traversal, the traversal results are stored in the hash table at the same time, and then the second traversal goes to the hash table to fetch. This method is feasible, but it requires N space (N is the total number of nodes). I thought that if the two can be traversed at the same time, wouldn't the overhead of the hash table be eliminated? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9a7sy7j31a30u0408.jpg) +![](https://p.ipic.vip/sulryh.jpg) If you don't understand, let me give a simple example: @@ -199,6 +199,6 @@ return dfs(root. left, root. right) If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/9fe5yr.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9b4p9ej30x20iwjtf.jpg) +![](https://p.ipic.vip/gfsw33.jpg) diff --git a/problems/1011.capacity-to-ship-packages-within-d-days.md b/problems/1011.capacity-to-ship-packages-within-d-days.md index 71e58cebc..703ce10d8 100644 --- a/problems/1011.capacity-to-ship-packages-within-d-days.md +++ b/problems/1011.capacity-to-ship-packages-within-d-days.md @@ -63,7 +63,7 @@ https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days/ 题目给定了 weights 长度 <= 50000,因此大概就可以锁定为 nlogn 解法。为啥?大家可以看下我的插件就知道了。另外我的插件还提供了多种规模的复杂度速查表。地址:https://leetcode-pp.github.io/leetcode-cheat/?tab=data-structure-vis -![](https://tva1.sinaimg.cn/large/008i3skNly1gpwyi1zhc0j30mm0h2757.jpg) +![](https://p.ipic.vip/8maqov.jpg) 这道题和[猴子吃香蕉](https://github.com/azl397985856/leetcode/blob/master/problems/875.koko-eating-bananas.md) 简直一摸一样,没有看过的建议看一下那道题。 diff --git a/problems/1014.best-sightseeing-pair.md b/problems/1014.best-sightseeing-pair.md index d2ad2fbf9..94f6817ce 100644 --- a/problems/1014.best-sightseeing-pair.md +++ b/problems/1014.best-sightseeing-pair.md @@ -111,4 +111,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/owuwyw.jpg) diff --git a/problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md b/problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md index dabec9235..007a9e4e2 100644 --- a/problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md +++ b/problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md @@ -53,7 +53,7 @@ L + M <= A.length <= 1000 题目中要求在前 N(数组长度)个数中找出长度分别为 L 和 M 的非重叠子数组之和的最大值, 因此, 我们可以定义数组 A 中前 i 个数可构成的非重叠子数组 L 和 M 的最大值为 SUMM[i], 并找到 SUMM[i]和 SUMM[i-1]的关系, 那么最终解就是 SUMM[N]. 以下为图解: -![1031.Maximum Sum of Two Non-Overlapping Subarrays](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu02o63mj30iz0m9420.jpg) +![1031.Maximum Sum of Two Non-Overlapping Subarrays](https://p.ipic.vip/gzbr6i.jpg) ## 关键点解析 diff --git a/problems/104.maximum-depth-of-binary-tree.en.md b/problems/104.maximum-depth-of-binary-tree.en.md index ee00a78c7..6546910d2 100644 --- a/problems/104.maximum-depth-of-binary-tree.en.md +++ b/problems/104.maximum-depth-of-binary-tree.en.md @@ -307,4 +307,4 @@ return $depth; If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/k16rc2.jpg) diff --git a/problems/104.maximum-depth-of-binary-tree.md b/problems/104.maximum-depth-of-binary-tree.md index df4edcd65..c610e27c0 100644 --- a/problems/104.maximum-depth-of-binary-tree.md +++ b/problems/104.maximum-depth-of-binary-tree.md @@ -306,4 +306,4 @@ class Solution 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/2m75d5.jpg) diff --git a/problems/1043.partition-array-for-maximum-sum.md b/problems/1043.partition-array-for-maximum-sum.md index aeb81f12d..8a2da5763 100644 --- a/problems/1043.partition-array-for-maximum-sum.md +++ b/problems/1043.partition-array-for-maximum-sum.md @@ -143,4 +143,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/bx53fq.jpg) diff --git a/problems/108.convert-sorted-array-to-binary-search-tree.en.md b/problems/108.convert-sorted-array-to-binary-search-tree.en.md index 8515fd42e..79760e648 100644 --- a/problems/108.convert-sorted-array-to-binary-search-tree.en.md +++ b/problems/108.convert-sorted-array-to-binary-search-tree.en.md @@ -44,7 +44,7 @@ The title requirement is a binary search tree with a high degree of balance, so From an image point of view, it's like you lift a rope, and if you lift it from it, you can minimize the difference in the length of the rope on both sides. -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltysdgtvj30nj0hv3z2.jpg) +![image.png](https://p.ipic.vip/bxzaf0.jpg) ## Key points diff --git a/problems/108.convert-sorted-array-to-binary-search-tree.md b/problems/108.convert-sorted-array-to-binary-search-tree.md index 874ba0082..a570097ef 100644 --- a/problems/108.convert-sorted-array-to-binary-search-tree.md +++ b/problems/108.convert-sorted-array-to-binary-search-tree.md @@ -45,7 +45,7 @@ https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/ 形象一点来看就像你提起一根绳子,从中点提的话才能使得两边绳子长度相差最小。 -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltysdgtvj30nj0hv3z2.jpg) +![image.png](https://p.ipic.vip/idi8m0.jpg) ## 关键点 diff --git a/problems/11.container-with-most-water.md b/problems/11.container-with-most-water.md index 68a2813c6..aeaec61c2 100644 --- a/problems/11.container-with-most-water.md +++ b/problems/11.container-with-most-water.md @@ -9,7 +9,7 @@ https://leetcode-cn.com/problems/container-with-most-water/description/ 说明:你不能倾斜容器,且  n  的值至少为 2。 -![11.container-with-most-water-question](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4wyztmj30m90anwep.jpg) +![11.container-with-most-water-question](https://p.ipic.vip/ia6rj3.jpg) 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为  49。 @@ -66,7 +66,7 @@ return max; 如图: -![11.container-with-most-water](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4xr7ovj30bm0gct9b.jpg) +![11.container-with-most-water](https://p.ipic.vip/sp459l.jpg) 比如我们计算 n 面积的时候,假如左侧的线段高度比右侧的高度低,那么我们通过左移**右指针**来将长度缩短为 n - 1 的做法是没有意义的,因为`新形成的面积变成了(n-1) * heightOfLeft, 这个面积一定比刚才的长度为 n 的面积 (n * heightOfLeft) 小`。 @@ -156,4 +156,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4yqnsgj30p00dwt9t.jpg) +![](https://p.ipic.vip/gg5yw0.jpg) diff --git a/problems/1104.path-in-zigzag-labelled-binary-tree.md b/problems/1104.path-in-zigzag-labelled-binary-tree.md index 99ddbac32..2742a5be5 100644 --- a/problems/1104.path-in-zigzag-labelled-binary-tree.md +++ b/problems/1104.path-in-zigzag-labelled-binary-tree.md @@ -14,7 +14,7 @@ https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/ ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu59hpv3j310p0gumxz.jpg) +![](https://p.ipic.vip/t0ga06.jpg) ``` 给你树上某一个节点的标号 label,请你返回从根节点到该标号为 label 节点的路径,该路径是由途经的节点标号所组成的。 @@ -48,17 +48,17 @@ https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/ 如果是这样的话,这道题应该是 easy 难度,代码也不难写出。我们继续考虑之字形。我们不妨先观察一下,找下规律。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5au9j8j30lu093gmm.jpg) +![](https://p.ipic.vip/a8gogr.jpg) 以上图最后一行为例,对于 15 节点,之字变换之前对应的应该是 8 节点。14 节点对应的是 9 节点。。。 全部列举出来是这样的: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5d6os7j30mk0b6wfw.jpg) +![](https://p.ipic.vip/19lvv9.jpg) 我们发现之字变换前后的 label 相加是一个定值。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5f240wj309b08dmxj.jpg) +![](https://p.ipic.vip/82o3k7.jpg) 因此实际上只需要求解出每一层的这个定值,然后减去当前值就好了。(注意我们不需要区分偶数行和奇数行) 问题的关键转化为求解这个定值,这个定值其实很好求,因为每一层的最大值和最小值我们很容易求,而最大值和最小值的和正是我们要求的这个数字。 @@ -100,4 +100,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/yv222t.jpg) diff --git a/problems/1129.shortest-path-with-alternating-colors.md b/problems/1129.shortest-path-with-alternating-colors.md index f6b2505a1..d5cb9747a 100644 --- a/problems/1129.shortest-path-with-alternating-colors.md +++ b/problems/1129.shortest-path-with-alternating-colors.md @@ -144,4 +144,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/xha2vq.jpg) diff --git a/problems/113.path-sum-ii.md b/problems/113.path-sum-ii.md index db199bfcf..9b270233e 100644 --- a/problems/113.path-sum-ii.md +++ b/problems/113.path-sum-ii.md @@ -48,7 +48,7 @@ https://leetcode-cn.com/problems/path-sum-ii/ 我们先来看下通用解法的解题思路,我画了一张图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwyr0bkj31190u0jw4.jpg) +![](https://p.ipic.vip/m71dgr.jpg) > 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。 diff --git a/problems/1131.maximum-of-absolute-value-expression.md b/problems/1131.maximum-of-absolute-value-expression.md index 265290bd3..479e81fea 100644 --- a/problems/1131.maximum-of-absolute-value-expression.md +++ b/problems/1131.maximum-of-absolute-value-expression.md @@ -46,7 +46,7 @@ https://leetcode-cn.com/problems/maximum-of-absolute-value-expression/ > 红色竖线表示的是绝对值的符号 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu154cgej30q003y3yv.jpg) +![](https://p.ipic.vip/3ck1ei.jpg) 我们对其进行分类讨论,有如下八种情况: @@ -55,7 +55,7 @@ https://leetcode-cn.com/problems/maximum-of-absolute-value-expression/ > |i - j| 两种情况 > 因此一共是 2 \* 2 \* 2 = 8 种 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu1c4km1j30tg0viq7v.jpg) +![](https://p.ipic.vip/hy5sx0.jpg) 由于 i 和 j 之间没有大小关系,也就是说二者可以相互替代。因此: @@ -68,11 +68,11 @@ https://leetcode-cn.com/problems/maximum-of-absolute-value-expression/ 为了方便,我们将 i 和 j 都提取到一起: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu1j6ueoj30qs0g6di2.jpg) +![](https://p.ipic.vip/kpmax7.jpg) 容易看出等式的最大值就是前面的最大值,和后面最小值的差值。如图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu1oczs3j30r20kctb5.jpg) +![](https://p.ipic.vip/jn1mj1.jpg) 再仔细观察,会发现前面部分和后面部分是一样的,原因还是上面所说的 i 和 j 可以互换。因此我们要做的就是: @@ -107,17 +107,17 @@ class Solution: ### 思路 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu23wcsoj308l0a8aak.jpg) +![](https://p.ipic.vip/jisqnd.jpg) (图来自: https://zh.wikipedia.org/wiki/%E6%9B%BC%E5%93%88%E9%A0%93%E8%B7%9D%E9%9B%A2) 一维曼哈顿距离可以理解为一条线上两点之间的距离: |x1 - x2|,其值为 max(x1 - x2, x2 - x1) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2729n3j30l004mwel.jpg) +![](https://p.ipic.vip/9adcgt.jpg) 在平面上,坐标(x1, y1)的点 P1 与坐标(x2, y2)的点 P2 的曼哈顿距离为:|x1-x2| + |y1 - y2|,其值为 max(x1 - x2 + y1 - y2, x2 - x1 + y1 - y2, x1 - x2 + y2 - y1, x2 -x1 + y2 - y1) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu29xa0jj30rq0lmwga.jpg) +![](https://p.ipic.vip/axye9g.jpg) 然后这道题目是更复杂的三维曼哈顿距离,其中(i, arr[i], arr[j])可以看作三位空间中的一个点,问题转化为曼哈顿距离最远的两个点的距离。 延续上面的思路,|x1-x2| + |y1 - y2| + |z1 - z2|,其值为 : @@ -201,10 +201,10 @@ class Solution: - [1030. 距离顺序排列矩阵单元格](https://leetcode-cn.com/problems/matrix-cells-in-distance-order/) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2h4bnaj30xd0jzgom.jpg) +![](https://p.ipic.vip/7xfvm3.jpg) - [1162. 地图分析](https://leetcode-cn.com/problems/as-far-from-land-as-possible/) 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/nrftgw.jpg) diff --git a/problems/1138.alphabet-board-path.md b/problems/1138.alphabet-board-path.md index e357b76ca..3246dcb46 100644 --- a/problems/1138.alphabet-board-path.md +++ b/problems/1138.alphabet-board-path.md @@ -116,4 +116,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/edhrpv.jpg) diff --git a/problems/1168.optimize-water-distribution-in-a-village-en.md b/problems/1168.optimize-water-distribution-in-a-village-en.md index 97ff17542..c31df28d1 100644 --- a/problems/1168.optimize-water-distribution-in-a-village-en.md +++ b/problems/1168.optimize-water-distribution-in-a-village-en.md @@ -34,7 +34,7 @@ pipes[i][0] != pipes[i][1] ``` example 1 pic: -![example 1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltymocpgj30ci0bc3z0.jpg) +![example 1](https://p.ipic.vip/x8bb04.jpg) ## Solution @@ -61,7 +61,7 @@ For example:`n = 5, wells=[1,2,2,3,2], pipes=[[1,2,1],[2,3,1],[4,5,7]]` As below pic: -![minimum cost](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyopr5zj31400u0nfs.jpg) +![minimum cost](https://p.ipic.vip/ps5bth.jpg) From pictures, we can see that all nodes already connected with minimum costs. diff --git a/problems/1168.optimize-water-distribution-in-a-village.md b/problems/1168.optimize-water-distribution-in-a-village.md index 490a07f87..63e263cb4 100644 --- a/problems/1168.optimize-water-distribution-in-a-village.md +++ b/problems/1168.optimize-water-distribution-in-a-village.md @@ -40,7 +40,7 @@ pipes[i][0] != pipes[i][1] ## 思路 -![example 1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0bzlucj30ci0bc3z0.jpg) +![example 1](https://p.ipic.vip/22kjr8.jpg) 题意,在每个城市打井需要一定的花费,也可以用其他城市的井水,城市之间建立连接管道需要一定的花费,怎么样安排可以花费最少的前灌溉所有城市。 @@ -68,7 +68,7 @@ pipes[i][0] != pipes[i][1] 如图: -![minimum cost](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0jq6djj31400u0nfs.jpg) +![minimum cost](https://p.ipic.vip/euk0ct.jpg) 从图中可以看到,最后所有的节点都是连通的。 @@ -201,4 +201,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/lft48p.jpg) diff --git a/problems/1178.number-of-valid-words-for-each-puzzle.md b/problems/1178.number-of-valid-words-for-each-puzzle.md index 3cc7ee6f7..10709e6c5 100644 --- a/problems/1178.number-of-valid-words-for-each-puzzle.md +++ b/problems/1178.number-of-valid-words-for-each-puzzle.md @@ -258,4 +258,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/pviujz.jpg) diff --git a/problems/1186.maximum-subarray-sum-with-one-deletion.md b/problems/1186.maximum-subarray-sum-with-one-deletion.md index fa05a87cd..5e91652b7 100644 --- a/problems/1186.maximum-subarray-sum-with-one-deletion.md +++ b/problems/1186.maximum-subarray-sum-with-one-deletion.md @@ -169,4 +169,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/veoem5.jpg) diff --git a/problems/1203.sort-items-by-groups-respecting-dependencies.md b/problems/1203.sort-items-by-groups-respecting-dependencies.md index 36d7d26e9..fa06a01cb 100644 --- a/problems/1203.sort-items-by-groups-respecting-dependencies.md +++ b/problems/1203.sort-items-by-groups-respecting-dependencies.md @@ -21,7 +21,7 @@ group[i] 表示第 i 个项目所属的小组,如果这个项目目前无人 示例 1: ``` -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmkv6dy054j305b051mx9.jpg) +![](https://p.ipic.vip/u3bo4s.jpg) ``` 输入:n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] @@ -133,7 +133,7 @@ class Solution: 注意绿色线条不是题目给出的,而是需要我们自己生成。 -![](https://pic.leetcode-cn.com/1610425165-XDBpwE-008eGmZEly1gmksaezi8hj30lg0c375n.jpg) +![](https://p.ipic.vip/evgl7e.jpg) 生成绿色部分依赖关系的核心逻辑是**如果一个项目和这个项目的依赖(如果存在)需要不同的组来完成**,那么这两个组就拥有依赖关系。代码: @@ -157,7 +157,7 @@ pres 是题目中的 beforeItems,即项目的依赖关系。 一种方法是将这些无人处理的进行编号,只要给分别给它们一个不重复的 id 即可,注意这个 id 一定不能是已经存在的 id。由于原有的 group id 范围是 [0, m-1] 因此我们可以从 m 开始并逐个自增 1 来实现,详见代码。 -![](https://pic.leetcode-cn.com/1610425362-udnMrd-008eGmZEly1gmksm43n1aj30jg0f7ta6.jpg) +![](https://p.ipic.vip/426261.jpg) ## 代码 @@ -237,4 +237,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/lpaww1.jpg) diff --git a/problems/121.best-time-to-buy-and-sell-stock.en.md b/problems/121.best-time-to-buy-and-sell-stock.en.md index f46bc34b1..7005d7739 100644 --- a/problems/121.best-time-to-buy-and-sell-stock.en.md +++ b/problems/121.best-time-to-buy-and-sell-stock.en.md @@ -51,7 +51,7 @@ Since the topic has a limit on the number of transactions and can only be traded If it is represented by a diagram, it is like this: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6k05dqj30jg0c20tf.jpg) +![](https://p.ipic.vip/qv0alo.jpg) ## Analysis of key points @@ -155,4 +155,4 @@ return max_profit If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/jqr5bl.jpg) diff --git a/problems/121.best-time-to-buy-and-sell-stock.md b/problems/121.best-time-to-buy-and-sell-stock.md index 5fba3f07e..b7a9b2a48 100644 --- a/problems/121.best-time-to-buy-and-sell-stock.md +++ b/problems/121.best-time-to-buy-and-sell-stock.md @@ -50,7 +50,7 @@ https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/ 用图表示的话就是这样: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6k05dqj30jg0c20tf.jpg) +![](https://p.ipic.vip/n7skxl.jpg) ## 关键点解析 @@ -154,4 +154,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/yq8pg2.jpg) diff --git a/problems/1218.longest-arithmetic-subsequence-of-given-difference.md b/problems/1218.longest-arithmetic-subsequence-of-given-difference.md index 11ea4ab10..14e7a07fc 100644 --- a/problems/1218.longest-arithmetic-subsequence-of-given-difference.md +++ b/problems/1218.longest-arithmetic-subsequence-of-given-difference.md @@ -118,4 +118,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/07ms4k.jpg) diff --git a/problems/122.best-time-to-buy-and-sell-stock-ii.en.md b/problems/122.best-time-to-buy-and-sell-stock-ii.en.md index a523a03af..61b1e9745 100644 --- a/problems/122.best-time-to-buy-and-sell-stock-ii.en.md +++ b/problems/122.best-time-to-buy-and-sell-stock-ii.en.md @@ -63,7 +63,7 @@ Since the topic has no limit on the number of transactions, we should not let go If it is represented by a diagram, it is like this: -![122.best-time-to-buy-and-sell-stock-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8sjjprj30ff0bv0te.jpg) +![122.best-time-to-buy-and-sell-stock-ii](https://p.ipic.vip/o7rfjm.jpg) ## Analysis of key points @@ -143,4 +143,4 @@ return res; If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/5m6vmn.jpg) diff --git a/problems/122.best-time-to-buy-and-sell-stock-ii.md b/problems/122.best-time-to-buy-and-sell-stock-ii.md index 22a276501..da4f37ea6 100644 --- a/problems/122.best-time-to-buy-and-sell-stock-ii.md +++ b/problems/122.best-time-to-buy-and-sell-stock-ii.md @@ -62,7 +62,7 @@ https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 用图表示的话就是这样: -![122.best-time-to-buy-and-sell-stock-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8sjjprj30ff0bv0te.jpg) +![122.best-time-to-buy-and-sell-stock-ii](https://p.ipic.vip/bfrsv8.jpg) ## 关键点解析 @@ -154,4 +154,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/yzwo5w.jpg) diff --git a/problems/1227.airplane-seat-assignment-probability.md b/problems/1227.airplane-seat-assignment-probability.md index 8dd4dbe50..a49ae037f 100644 --- a/problems/1227.airplane-seat-assignment-probability.md +++ b/problems/1227.airplane-seat-assignment-probability.md @@ -61,12 +61,12 @@ https://leetcode-cn.com/problems/airplane-seat-assignment-probability/ 此时的问题转化关系如图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxcat1fj31bc0jutc4.jpg) +![](https://p.ipic.vip/vwe7p2.jpg) (红色表示票丢的人) 整个过程分析: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxcxtmvj318u0bgtbe.jpg) +![](https://p.ipic.vip/he3w1i.jpg) ### 代码 @@ -271,4 +271,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/hsf3pz.jpg) diff --git a/problems/124.binary-tree-maximum-path-sum.md b/problems/124.binary-tree-maximum-path-sum.md index 8f6d139b0..950610e84 100644 --- a/problems/124.binary-tree-maximum-path-sum.md +++ b/problems/124.binary-tree-maximum-path-sum.md @@ -50,11 +50,11 @@ https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/description/ 首先是官网给的两个例子: -![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaht4drj30kh07pq3p.jpg) +![124.binary-tree-maximum-path-sum](https://p.ipic.vip/2qkraq.jpg) 接着是我自己画的一个例子: -![124.binary-tree-maximum-path-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluai4m6dj30hu0cdq46.jpg) +![124.binary-tree-maximum-path-sum](https://p.ipic.vip/bu501r.jpg) 如图红色的部分是最大路径上的节点。大家可以结合上面的 demo 来继续理解一下 path, 除非你理解了 path,否则不要往下看。 @@ -203,4 +203,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/fc63zt.jpg) diff --git a/problems/125.valid-palindrome.en.md b/problems/125.valid-palindrome.en.md index c2470c611..e78006e46 100644 --- a/problems/125.valid-palindrome.en.md +++ b/problems/125.valid-palindrome.en.md @@ -50,11 +50,11 @@ The time complexity is O(n). Take a palindrome string like "noon” for example, our judgment process is like this: -![125.valid-palindrome-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxv0l6lj30fp0883yo.jpg) +![125.valid-palindrome-1](https://p.ipic.vip/mhufab.jpg) Take “abaa”, a string that is not a palindrome, for example, our judgment process is like this: -![125.valid-palindrome-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxzbhiqj30ff07y74k.jpg) +![125.valid-palindrome-2](https://p.ipic.vip/06pg33.jpg) ## Analysis of key points @@ -169,4 +169,4 @@ return s == s[::-1] If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/9k2xlg.jpg) diff --git a/problems/125.valid-palindrome.md b/problems/125.valid-palindrome.md index 7886a024f..f9c62c110 100644 --- a/problems/125.valid-palindrome.md +++ b/problems/125.valid-palindrome.md @@ -49,11 +49,11 @@ https://leetcode-cn.com/problems/valid-palindrome/description/ 拿“noon”这样一个回文串来说,我们的判断过程是这样的: -![125.valid-palindrome-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxv0l6lj30fp0883yo.jpg) +![125.valid-palindrome-1](https://p.ipic.vip/xp0fw3.jpg) 拿“abaa”这样一个不是回文的字符串来说,我们的判断过程是这样的: -![125.valid-palindrome-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxzbhiqj30ff07y74k.jpg) +![125.valid-palindrome-2](https://p.ipic.vip/fl9hcr.jpg) ## 关键点解析 @@ -195,4 +195,4 @@ class Solution { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/uueyvl.jpg) diff --git a/problems/1255.maximum-score-words-formed-by-letters.md b/problems/1255.maximum-score-words-formed-by-letters.md index 58bd83ec2..8e77b89c8 100644 --- a/problems/1255.maximum-score-words-formed-by-letters.md +++ b/problems/1255.maximum-score-words-formed-by-letters.md @@ -132,4 +132,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/k6xf1g.jpg) diff --git a/problems/1260.shift-2d-grid.en.md b/problems/1260.shift-2d-grid.en.md index cf10615d7..0090dd44c 100644 --- a/problems/1260.shift-2d-grid.en.md +++ b/problems/1260.shift-2d-grid.en.md @@ -89,7 +89,7 @@ Since it is easy, the above approach is barely acceptable, so we will consider o ### Idea If we look closely at the matrix, we will find that in fact, such matrix migration is regular. As shown in the figure: -![image](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluajlvo1j30us0u0439.jpg) +![image](https://p.ipic.vip/6w6n0m.jpg) Therefore, this problem has been transformed into our one-dimensional matrix transfer problem. LeetCode also has the original title [189. Rotating array](https://leetcode-cn.com/problems/rotate-array /), at the same time, I also wrote an article [Cyclic shift algorithm that liberal arts students can understand] (https://lucifer.ren/blog/2019/12/11/rotate-list /) To discuss this specifically, in the end we used the cubic rotation method. The relevant mathematical proofs are also written. They are very detailed and will not be repeated here. @@ -163,4 +163,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/4y5jnr.jpg) diff --git a/problems/1260.shift-2d-grid.md b/problems/1260.shift-2d-grid.md index c46662c96..b795605a7 100644 --- a/problems/1260.shift-2d-grid.md +++ b/problems/1260.shift-2d-grid.md @@ -89,7 +89,7 @@ class Solution: ### 思路 我们仔细观察矩阵会发现,其实这样的矩阵迁移是有规律的。 如图: -![image](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluajlvo1j30us0u0439.jpg) +![image](https://p.ipic.vip/26jb49.jpg) 因此这个问题就转化为我们一直的一维矩阵转移问题,LeetCode 也有原题[189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/),同时我也写了一篇文章[文科生都能看懂的循环移位算法](https://lucifer.ren/blog/2019/12/11/rotate-list/)专门讨论这个,最终我们使用的是三次旋转法,相关数学证明也有写,很详细,这里不再赘述。 @@ -163,4 +163,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vixp32.jpg) diff --git a/problems/1261.find-elements-in-a-contaminated-binary-tree.md b/problems/1261.find-elements-in-a-contaminated-binary-tree.md index efa26d037..4239003db 100644 --- a/problems/1261.find-elements-in-a-contaminated-binary-tree.md +++ b/problems/1261.find-elements-in-a-contaminated-binary-tree.md @@ -21,7 +21,7 @@ bool find(int target) 判断目标值 target 是否存在于还原后的二 示例 1: ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua6htirj308w03bdfo.jpg) +![](https://p.ipic.vip/t0vzeb.jpg) ``` 输入: @@ -36,7 +36,7 @@ findElements.find(2); // return True 示例 2: ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua84ataj30b405idfu.jpg) +![](https://p.ipic.vip/ga36n0.jpg) ``` 输入: @@ -52,7 +52,7 @@ findElements.find(5); // return False 示例 3: ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua8rj84j308i07m3yh.jpg) +![](https://p.ipic.vip/4ruo3z.jpg) ``` 输入: @@ -198,13 +198,13 @@ class FindElements: 如果我们把树中的数全部加 1 会怎么样? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaaphnnj30rs0kuwhb.jpg) +![](https://p.ipic.vip/ok30ok.jpg) (图参考 https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/discuss/431229/Python-Special-Way-for-find()-without-HashSet-O(1)-Space-O(logn)-Time) 仔细观察发现,每一行的左右子树分别有不同的前缀: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluackit8j312y0sgtdy.jpg) +![](https://p.ipic.vip/efrz8i.jpg) Ok,那么算法就来了,就是直接用 **target + 1** 的二进制表示进行**二叉树寻路** 即可。 diff --git a/problems/1262.greatest-sum-divisible-by-three.md b/problems/1262.greatest-sum-divisible-by-three.md index 92389d2ad..7349bad8d 100644 --- a/problems/1262.greatest-sum-divisible-by-three.md +++ b/problems/1262.greatest-sum-divisible-by-three.md @@ -52,7 +52,7 @@ https://leetcode-cn.com/problems/greatest-sum-divisible-by-three/ 更多回溯题目,可以访问上方链接查看(可以使用一套模板搞定): -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu49wysqj30f60c4my0.jpg) +![](https://p.ipic.vip/76j1db.jpg) ### 代码 @@ -94,11 +94,11 @@ class Solution: 以题目中的例 1 为例: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4dsqzhj30u00x2n0u.jpg) +![](https://p.ipic.vip/1oz70e.jpg) 以题目中的例 2 为例: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4l71rzj30u00xvwia.jpg) +![](https://p.ipic.vip/xkwlk4.jpg) ### 代码 @@ -180,7 +180,7 @@ class Solution: 我在[数据结构与算法在前端领域的应用 - 第二篇](https://lucifer.ren/blog/2019/09/19/algorthimn-fe-2/) 中讲到了有限状态机。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4nj6u8j30eq0bfdgl.jpg) +![](https://p.ipic.vip/stik8x.jpg) 状态机表示若干个状态以及在这些状态之间的转移和动作等行为的数学模型。通俗的描述状态机就是定义了一套状态変更的流程:状态机包含一个状态集合,定义当状态机处于某一个状态的时候它所能接收的事件以及可执行的行为,执行完成后,状态机所处的状态。 @@ -260,4 +260,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/i1eop5.jpg) diff --git a/problems/128.longest-consecutive-sequence.md b/problems/128.longest-consecutive-sequence.md index de05a758c..ed60d12f7 100644 --- a/problems/128.longest-consecutive-sequence.md +++ b/problems/128.longest-consecutive-sequence.md @@ -155,4 +155,4 @@ var longestConsecutive = function (nums) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/chi7a9.jpg) diff --git a/problems/129.sum-root-to-leaf-numbers.md b/problems/129.sum-root-to-leaf-numbers.md index 6a5434141..81bf6cf8a 100644 --- a/problems/129.sum-root-to-leaf-numbers.md +++ b/problems/129.sum-root-to-leaf-numbers.md @@ -61,11 +61,11 @@ https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/ 整个过程如图所示: -![129.sum-root-to-leaf-numbers-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu66bb27j30k10a6dgx.jpg) +![129.sum-root-to-leaf-numbers-1](https://p.ipic.vip/dkzk3q.jpg) 那么数字具体的计算逻辑,如图所示,相信大家通过这个不难发现规律: -![129.sum-root-to-leaf-numbers-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu67b2mkj30mo0agmys.jpg) +![129.sum-root-to-leaf-numbers-2](https://p.ipic.vip/am05qc.jpg) ## 关键点解析 @@ -335,4 +335,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/lymyiw.jpg) diff --git a/problems/1297.maximum-number-of-occurrences-of-a-substring.md b/problems/1297.maximum-number-of-occurrences-of-a-substring.md index fb78132d1..600cc9531 100644 --- a/problems/1297.maximum-number-of-occurrences-of-a-substring.md +++ b/problems/1297.maximum-number-of-occurrences-of-a-substring.md @@ -161,4 +161,4 @@ public boolean checkNum(String substr, int maxLetters) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/m22ud2.jpg) diff --git a/problems/130.surrounded-regions.md b/problems/130.surrounded-regions.md index c2b45c341..cd7c95c4a 100644 --- a/problems/130.surrounded-regions.md +++ b/problems/130.surrounded-regions.md @@ -42,7 +42,7 @@ X O X X 我们需要将所有被 X 包围的 O 变成 X,并且题目明确说了边缘的所有 O 都是不可以变成 X 的。 -![130.surrounded-regions](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7kk7n3j30ee09waap.jpg) +![130.surrounded-regions](https://p.ipic.vip/6x2fc3.jpg) 其实我们观察会发现,我们除了边缘的 O 以及和边缘 O 连通的 O 是不需要变成 X 的,其他都要变成 X。 @@ -53,7 +53,7 @@ X O X X > 我将`边缘的O以及和边缘O连通的O` 标记为了 "A" -![130.surrounded-regions](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7ms9mij30mr0b9q4c.jpg) +![130.surrounded-regions](https://p.ipic.vip/qs9r9e.jpg) ## 关键点解析 @@ -204,4 +204,4 @@ public: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/0cknrk.jpg) diff --git a/problems/131.palindrome-partitioning.md b/problems/131.palindrome-partitioning.md index 7e01d4215..e1817fe25 100644 --- a/problems/131.palindrome-partitioning.md +++ b/problems/131.palindrome-partitioning.md @@ -41,7 +41,7 @@ https://leetcode-cn.com/problems/palindrome-partitioning/ 这里我画了一个图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty0bvj4j31190u0jw4.jpg) +![](https://p.ipic.vip/6g2gvx.jpg) > 图是 [78.subsets](https://github.com/azl397985856/leetcode/blob/master/problems/78.subsets.md),都差不多,仅做参考。 diff --git a/problems/1310.xor-queries-of-a-subarray.md b/problems/1310.xor-queries-of-a-subarray.md index 7d4f64248..a7d1bc41d 100644 --- a/problems/1310.xor-queries-of-a-subarray.md +++ b/problems/1310.xor-queries-of-a-subarray.md @@ -106,7 +106,7 @@ pre[Li] ^ pre[Ri + 1] = (arr[0] ^ ... ^ arr[Li - 1]) ^ (arr[0] ^ ... ^ arr[Ri]) > 之所以是 pre[Li] ^ pre[Ri + 1],而不是 pre[Li - 1] ^ pre[Ri] 是因为 pre 中我使用了一个虚拟数字 0,如果你没有用到这个,则需要代码有所调整。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxsg8v8j30fm0bf74w.jpg) +![](https://p.ipic.vip/gea0wi.jpg) ### 代码 @@ -200,7 +200,7 @@ public: - [303. 区域和检索 - 数组不可变](https://leetcode-cn.com/problems/range-sum-query-immutable/description/) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxt83dtj30u00ftac4.jpg) +![](https://p.ipic.vip/b5patl.jpg) - [1186.删除一次得到子数组最大和](https://lucifer.ren/blog/2019/12/11/leetcode-1186/) diff --git a/problems/132.palindrome-partitioning-ii.md b/problems/132.palindrome-partitioning-ii.md index 6f7be0039..568aacfbe 100644 --- a/problems/132.palindrome-partitioning-ii.md +++ b/problems/132.palindrome-partitioning-ii.md @@ -136,4 +136,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/31kxcv.jpg) diff --git a/problems/1332.remove-palindromic-sequences.en.md b/problems/1332.remove-palindromic-sequences.en.md index 65e8640dd..b0de4c69a 100644 --- a/problems/1332.remove-palindromic-sequences.en.md +++ b/problems/1332.remove-palindromic-sequences.en.md @@ -133,4 +133,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/i6k4gt.jpg) diff --git a/problems/1332.remove-palindromic-subsequences.md b/problems/1332.remove-palindromic-subsequences.md index 16f5d3caa..22fbbc661 100644 --- a/problems/1332.remove-palindromic-subsequences.md +++ b/problems/1332.remove-palindromic-subsequences.md @@ -133,4 +133,4 @@ class Solution { 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/xz75nf.jpg) diff --git a/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md b/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md index f78487d6d..d1849a110 100644 --- a/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md +++ b/problems/1334.find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.md @@ -17,7 +17,7 @@ https://leetcode-cn.com/problems/find-the-city-with-the-smallest-number-of-neigh ``` -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubmx9n7j30qo0k03zm.jpg) +![image.png](https://p.ipic.vip/cb50vl.jpg) ``` @@ -36,7 +36,7 @@ https://leetcode-cn.com/problems/find-the-city-with-the-smallest-number-of-neigh ``` -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubnw6l9j30qo0k0q4c.jpg) +![image.png](https://p.ipic.vip/z1cs9t.jpg) ``` @@ -133,4 +133,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/fj3dba.jpg) diff --git a/problems/136.single-number.en.md b/problems/136.single-number.en.md index 02d2c528c..9160bd1d2 100644 --- a/problems/136.single-number.en.md +++ b/problems/136.single-number.en.md @@ -173,4 +173,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/fcqon4.jpg) diff --git a/problems/136.single-number.md b/problems/136.single-number.md index 78735191a..c08cfcba9 100644 --- a/problems/136.single-number.md +++ b/problems/136.single-number.md @@ -173,4 +173,4 @@ Done! 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/bao1ww.jpg) diff --git a/problems/1381.design-a-stack-with-increment-operation.md b/problems/1381.design-a-stack-with-increment-operation.md index 7287f3e28..c9f726124 100644 --- a/problems/1381.design-a-stack-with-increment-operation.md +++ b/problems/1381.design-a-stack-with-increment-operation.md @@ -112,7 +112,7 @@ class CustomStack: - push 操作不变,和上面一样 - increment 的时候,我们将用到 incremental 信息。那么这个信息是什么,从哪来呢?我这里画了一个图 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx11x0l0j30u014itck.jpg) +![](https://p.ipic.vip/o7iem7.jpg) 如图黄色部分是我们需要执行增加操作,我这里画了一个挡板分割,实际上这个挡板不存在。那么如何记录黄色部分的信息呢?我举个例子来说 @@ -121,14 +121,14 @@ class CustomStack: - 调用了 increment(3, 2),就把 increment[3] 增加 2。 - 继续调用 increment(2, 5),就把 increment[2] 增加 5。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx1fk7vcj30nm0c8wfb.jpg) +![](https://p.ipic.vip/jc470u.jpg) 而当我们 pop 的时候: - 只需要将栈顶元素**加上 increment[cnt - 1]** 即可, 其中 cnt 为栈当前的大小。 - 另外,我们需要将 increment[cnt - 1] 更新到 increment[cnt - 2],并将 increment[cnt - 1] 重置为 0。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx1ryzxpj31jq0hijte.jpg) +![](https://p.ipic.vip/278nhc.jpg) ### 代码 ```py @@ -222,4 +222,4 @@ class CustomStack: 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/at3ez5.jpg) diff --git a/problems/139.word-break.md b/problems/139.word-break.md index 37e3c1653..729ed8780 100644 --- a/problems/139.word-break.md +++ b/problems/139.word-break.md @@ -52,24 +52,24 @@ https://leetcode-cn.com/problems/word-break/ 我们用图来感受一下: -![139.word-break-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu370c2hj30n60cnmy6.jpg) +![139.word-break-1](https://p.ipic.vip/5b21ws.jpg) 没有明白也没有关系,我们分步骤解读一下: (以下的图左边都代表 s,右边都是 dict,灰色代表没有处理的字符,绿色代表匹配成功,红色代表匹配失败) -![139.word-break-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu37ydiwj30aw0b1mxc.jpg) +![139.word-break-2](https://p.ipic.vip/j3tv58.jpg) -![139.word-break-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3f3l6kj30bt0akdg0.jpg) +![139.word-break-3](https://p.ipic.vip/b19e31.jpg) -![139.word-break-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3mmjmtj30mw09ymxp.jpg) +![139.word-break-4](https://p.ipic.vip/dqxyvj.jpg) -![139.word-break-5](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3qr7ppj30n90kqabg.jpg) +![139.word-break-5](https://p.ipic.vip/w4t8bo.jpg) 上面分步解释了算法的基本过程,下面我们感性认识下这道题,我把它比喻为 你正在`往一个老式手电筒🔦中装电池` -![139.word-break-6](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu3rqvffj30mz0frwg3.jpg) +![139.word-break-6](https://p.ipic.vip/yu4j2f.jpg) ## 代码 @@ -130,4 +130,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/zxdbe9.jpg) diff --git a/problems/140.word-break-ii.md b/problems/140.word-break-ii.md index e37ee6507..0ee8addeb 100644 --- a/problems/140.word-break-ii.md +++ b/problems/140.word-break-ii.md @@ -203,4 +203,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vj8bnw.jpg) diff --git a/problems/142.Linked-List-Cycle-II.md b/problems/142.Linked-List-Cycle-II.md index 6fc7cfa89..5c0655c70 100644 --- a/problems/142.Linked-List-Cycle-II.md +++ b/problems/142.Linked-List-Cycle-II.md @@ -73,7 +73,7 @@ return null; 2. slow 指针继续前进,每次**前进一步** 3. 当两个指针再次相遇时,当前节点就是环的入口 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfigbvzje1j30ky0bhq3x.jpg) +![](https://p.ipic.vip/fkg8yx.jpg) (图 6) 为什么第二次相遇的点为环的入口? 原因如下: diff --git a/problems/1423.maximum-points-you-can-obtain-from-cards.md b/problems/1423.maximum-points-you-can-obtain-from-cards.md index fb863fcee..b2e03bf46 100644 --- a/problems/1423.maximum-points-you-can-obtain-from-cards.md +++ b/problems/1423.maximum-points-you-can-obtain-from-cards.md @@ -132,4 +132,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/9d1r9i.jpg) diff --git a/problems/1435.jump-game-iv.md b/problems/1435.jump-game-iv.md index b5b1c51a5..419c6e12f 100644 --- a/problems/1435.jump-game-iv.md +++ b/problems/1435.jump-game-iv.md @@ -109,4 +109,4 @@ class Solution: 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/jr6m64.jpg) diff --git a/problems/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md b/problems/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md index f4ceacf3c..b87a0ffb5 100644 --- a/problems/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md +++ b/problems/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md @@ -235,4 +235,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/i071fx.jpg) diff --git a/problems/144.binary-tree-preorder-traversal.md b/problems/144.binary-tree-preorder-traversal.md index 38a93e338..931d822ff 100644 --- a/problems/144.binary-tree-preorder-traversal.md +++ b/problems/144.binary-tree-preorder-traversal.md @@ -42,7 +42,7 @@ https://leetcode-cn.com/problems/binary-tree-preorder-traversal/ > 其他树的非递归遍历可没这么简单 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxumvwfj30zu0nttak.jpg) +![](https://p.ipic.vip/68yby8.jpg) (迭代 VS 递归) @@ -144,4 +144,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/j5i9l1.jpg) diff --git a/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md b/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md index b41a288a0..028aa8382 100644 --- a/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md +++ b/problems/1449.form-largest-integer-with-digits-that-add-up-to-target.md @@ -122,13 +122,13 @@ for i in 1 to N + 1: 那么如果我们不降序遍历会怎么样呢? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubxof5ej30uy0gi758.jpg) +![](https://p.ipic.vip/f97bnh.jpg) 如图橙色部分表示已经遍历的部分,而让我们去用[j - cost[i - 1]] 往前面回溯的时候,实际上回溯的是 dp[i]j - cost[i - 1]],而不是 dp[i - 1]j - cost[i - 1]]。 如果是降序就可以了,如图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubynqxqj30u80fcgmi.jpg) +![](https://p.ipic.vip/ej156c.jpg) 这个明白的话,我们继续思考为什么完全背包就要不降序了呢? @@ -181,12 +181,12 @@ class Solution: 最后贴几个我写过的背包问题,让大家看看历史是多么的相似。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubzm45mj31iq0sotbx.jpg) +![](https://p.ipic.vip/eb5c45.jpg) ([322. 硬币找零(完全背包问题)](https://github.com/azl397985856/leetcode/blob/master/problems/322.coin-change.md)) > 这里内外循环和本题正好是反的,我只是为了"秀技"(好玩),实际上在这里对答案并不影响。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluc31c32j31go0gwq3z.jpg) +![](https://p.ipic.vip/anb9qv.jpg) ([518. 零钱兑换 II](https://github.com/azl397985856/leetcode/blob/master/problems/518.coin-change-2.md)) > 这里内外循环和本题正好是反的,但是这里必须这么做,否则结果是不对的,具体可以点进去链接看我那个题解 @@ -220,4 +220,4 @@ for j in V to 0: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/tjsv0r.jpg) diff --git a/problems/145.binary-tree-postorder-traversal.md b/problems/145.binary-tree-postorder-traversal.md index 451019c41..a47e8ab4e 100644 --- a/problems/145.binary-tree-postorder-traversal.md +++ b/problems/145.binary-tree-postorder-traversal.md @@ -153,4 +153,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/2b7dwe.jpg) diff --git a/problems/147.insertion-sort-list.md b/problems/147.insertion-sort-list.md index c349a4521..f54ee000d 100644 --- a/problems/147.insertion-sort-list.md +++ b/problems/147.insertion-sort-list.md @@ -8,7 +8,7 @@ https://leetcode-cn.com/problems/insertion-sort-list/ 对链表进行插入排序。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkvig9vromg308c050q55.gif) +![](https://p.ipic.vip/h2isi2.gif) ``` 插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。 @@ -143,7 +143,7 @@ class Solution: 如果你上面代码你会了,将 insert 代码整个复制出来就变成大部分人的解法了。不过我还是建议新手按照我的这个模式一步步来,稳扎稳打,不要着急。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkvie3jcz4j315h0dp428.jpg) +![](https://p.ipic.vip/xfidui.jpg) ## 代码 diff --git a/problems/1494.parallel-courses-ii.md b/problems/1494.parallel-courses-ii.md index 016c11711..4892b6141 100644 --- a/problems/1494.parallel-courses-ii.md +++ b/problems/1494.parallel-courses-ii.md @@ -202,4 +202,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/egab5n.jpg) diff --git a/problems/15.3sum.md b/problems/15.3sum.md index 926f30d96..83b505c09 100644 --- a/problems/15.3sum.md +++ b/problems/15.3sum.md @@ -42,7 +42,7 @@ https://leetcode-cn.com/problems/3sum/ 思路如图所示: -![15.3-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyijyb3j30l00e2q3p.jpg) +![15.3-sum](https://p.ipic.vip/p11mp3.jpg) 在这里之所以要排序解决是因为, 我们算法的瓶颈在这里不在于排序,而在于 O(N^2),如果我们瓶颈是排序,就可以考虑别的方式了。 @@ -139,4 +139,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/elf8io.jpg) diff --git a/problems/150.evaluate-reverse-polish-notation.md b/problems/150.evaluate-reverse-polish-notation.md index 1959635fb..09bcbef05 100644 --- a/problems/150.evaluate-reverse-polish-notation.md +++ b/problems/150.evaluate-reverse-polish-notation.md @@ -78,7 +78,7 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ - 将栈顶两个元素出栈运算,将结果压栈 - 重复以上过程直到所有的 token 都处理完毕。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkeeips7ogj30a40gv3z7.jpg) +![](https://p.ipic.vip/jkki9k.jpg) ## 关键点 diff --git a/problems/152.maximum-product-subarray.md b/problems/152.maximum-product-subarray.md index e40e13d38..62fd02b7a 100644 --- a/problems/152.maximum-product-subarray.md +++ b/problems/152.maximum-product-subarray.md @@ -57,7 +57,7 @@ var maxProduct = function (nums) { 前面说了`最小值(比如-20)乘以一个比较小的数(比如-10)可能就会很大` 。因此我们需要同时记录乘积最大值和乘积最小值,然后比较元素和这两个的乘积,去不断更新最大值。当然,我们也可以选择只取当前元素。因此实际上我们的选择有三种,而如何选择就取决于哪个选择带来的价值最大(乘积最大或者最小)。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0be8nej30gr08kjru.jpg) +![](https://p.ipic.vip/b3bls6.jpg) 这种思路的解法由于只需要遍历一次,其时间复杂度是 O(n),代码见下方代码区。 diff --git a/problems/1521.find-a-value-of-a-mysterious-function-closest-to-target.md b/problems/1521.find-a-value-of-a-mysterious-function-closest-to-target.md index c6791b70d..090eab423 100644 --- a/problems/1521.find-a-value-of-a-mysterious-function-closest-to-target.md +++ b/problems/1521.find-a-value-of-a-mysterious-function-closest-to-target.md @@ -4,7 +4,7 @@ https://leetcode-cn.com/problems/find-a-value-of-a-mysterious-function-closest-t ## 题目描述 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmvco08jr1j30hn08owex.jpg) +![](https://p.ipic.vip/1mscqi.jpg) ``` Winston 构造了一个如上所示的函数 func 。他有一个整数数组 arr 和一个整数 target ,他想找到让 |func(arr, l, r) - target| 最小的 l 和 r 。 @@ -65,7 +65,7 @@ Winston 构造了一个如上所示的函数 func 。他有一个整数数组 > 关于这点不熟悉的,也可以看下我的 [【西法带你学算法】一次搞定前缀和](https://lucifer.ren/blog/2020/09/27/atMostK/) -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmvd7r6v4tj306u06g3yl.jpg) +![](https://p.ipic.vip/1sv0hv.jpg) 我们也可以采用同样的思路进行枚举。 @@ -116,4 +116,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/jqnd01.jpg) diff --git a/problems/1526.minimum-number-of-increments-on-subarrays-to-form-a-target-array.md b/problems/1526.minimum-number-of-increments-on-subarrays-to-form-a-target-array.md index 04c4d58e6..1b1d3a29c 100644 --- a/problems/1526.minimum-number-of-increments-on-subarrays-to-form-a-target-array.md +++ b/problems/1526.minimum-number-of-increments-on-subarrays-to-form-a-target-array.md @@ -204,4 +204,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/cwgl6d.jpg) diff --git a/problems/153.find-minimum-in-rotated-sorted-array.md b/problems/153.find-minimum-in-rotated-sorted-array.md index 541d6bc5f..2740ba406 100644 --- a/problems/153.find-minimum-in-rotated-sorted-array.md +++ b/problems/153.find-minimum-in-rotated-sorted-array.md @@ -129,4 +129,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ecns11.jpg) diff --git a/problems/155.min-stack.en.md b/problems/155.min-stack.en.md index c6c88373c..da45264c6 100644 --- a/problems/155.min-stack.en.md +++ b/problems/155.min-stack.en.md @@ -280,7 +280,7 @@ return self. minstack[-1] The intuitive way is to update the minimum value every time you modify the stack (push and pop). Then getMin only needs to return the minimum value we calculated, top can also directly return to the top element of the stack. This approach requires updating the minimum value every time the stack is modified, so the time complexity is O(n). -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucity87j30d609ggls.jpg) +![](https://p.ipic.vip/til0t6.jpg) Is there a more efficient algorithm? The answer is yes. @@ -302,8 +302,8 @@ The previous smallest value is “min-top element of the stack”, we need to up -If the top element of the stack is greater than 0, it means that it has "no effect" on the minimum value, and the previous minimum value is the previous minimum value. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucqck9mj30ji0k1gn0.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucsjh58j30ht0b4mxr.jpg) +![](https://p.ipic.vip/7k050h.jpg) +![](https://p.ipic.vip/8m8mmw.jpg) ### Key points @@ -557,4 +557,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/fmqvj5.jpg) diff --git a/problems/155.min-stack.md b/problems/155.min-stack.md index 119dcbe46..d993c09a5 100644 --- a/problems/155.min-stack.md +++ b/problems/155.min-stack.md @@ -281,7 +281,7 @@ class MinStack: 符合直觉的方法是,每次对栈进行修改操作(push和pop)的时候更新最小值。 然后getMin只需要返回我们计算的最小值即可, top也是直接返回栈顶元素即可。 这种做法每次修改栈都需要更新最小值,因此时间复杂度是O(n). -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucity87j30d609ggls.jpg) +![](https://p.ipic.vip/9g1o0o.jpg) 是否有更高效的算法呢?答案是有的。 @@ -303,8 +303,8 @@ pop或者top的时候: - 如果栈顶元素大于0,说明它对最小值`没有影响`,上一个最小值就是上上个最小值。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucqck9mj30ji0k1gn0.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucsjh58j30ht0b4mxr.jpg) +![](https://p.ipic.vip/fqsua8.jpg) +![](https://p.ipic.vip/ruuhw7.jpg) ### 关键点 @@ -560,4 +560,4 @@ class MinStack: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/rwnkpn.jpg) diff --git a/problems/1558.minimum-numbers-of-function-calls-to-make-target-array.md b/problems/1558.minimum-numbers-of-function-calls-to-make-target-array.md index 18901d69e..eecffb537 100644 --- a/problems/1558.minimum-numbers-of-function-calls-to-make-target-array.md +++ b/problems/1558.minimum-numbers-of-function-calls-to-make-target-array.md @@ -4,7 +4,7 @@ https://leetcode-cn.com/problems/minimum-numbers-of-function-calls-to-make-targe ## 题目描述 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkb30zd602j30fx086aak.jpg) +![](https://p.ipic.vip/2jpne3.jpg) ``` 给你一个与 nums 大小相同且初始值全为 0 的数组 arr ,请你调用以上函数得到整数数组 nums 。 @@ -126,4 +126,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/v806gw.jpg) diff --git a/problems/1574.shortest-subarray-to-be-removed-to-make-array-sorted.md b/problems/1574.shortest-subarray-to-be-removed-to-make-array-sorted.md index 0ef138e25..a25d762af 100644 --- a/problems/1574.shortest-subarray-to-be-removed-to-make-array-sorted.md +++ b/problems/1574.shortest-subarray-to-be-removed-to-make-array-sorted.md @@ -82,7 +82,7 @@ for(int i = 1; i < A.length; i++ ) { 但是显然这只是上界, 并不是正确解。一个显而易见的反例是: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glojn95n9mj30vu0m20uf.jpg) +![](https://p.ipic.vip/2j5gs6.jpg) 如图我们取蓝色部分,而将红色部分删除,答案可能会更小。 @@ -90,7 +90,7 @@ for(int i = 1; i < A.length; i++ ) { 一个可行的思路是初始化两个指针,一个指向头部,一个指向从尾部起第一个拐点(如上图右边蓝色部分的左端点)。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glojtjozp1j310o0s8ace.jpg) +![](https://p.ipic.vip/490p5b.jpg) 假设左指针为 i 右指针为 j,我们只需要不断右移左指针,左移右指针,并根据 i 和 j 的相对大小更新窗口即可。 @@ -175,4 +175,4 @@ public: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/wfci89.jpg) diff --git a/problems/1589.maximum-sum-obtained-of-any-permutation.md b/problems/1589.maximum-sum-obtained-of-any-permutation.md index 8be32a3ea..4b82dc5a6 100644 --- a/problems/1589.maximum-sum-obtained-of-any-permutation.md +++ b/problems/1589.maximum-sum-obtained-of-any-permutation.md @@ -136,4 +136,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/g5385j.jpg) diff --git a/problems/160.Intersection-of-Two-Linked-Lists.en.md b/problems/160.Intersection-of-Two-Linked-Lists.en.md index 4b005f19e..0ce11ddcf 100644 --- a/problems/160.Intersection-of-Two-Linked-Lists.en.md +++ b/problems/160.Intersection-of-Two-Linked-Lists.en.md @@ -67,7 +67,7 @@ return null; -When b reaches the end of the linked list, relocate to the head node of linked list A. The point where the -a, b pointers meet is the starting node of the intersection, otherwise there is no intersection point -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfig7vsvwhj30bs05z3yl.jpg) +![](https://p.ipic.vip/wvm1ah.jpg) (Figure 5) Why must the point where the pointers a and b meet be the starting node of the intersection? Let's prove it: diff --git a/problems/160.Intersection-of-Two-Linked-Lists.md b/problems/160.Intersection-of-Two-Linked-Lists.md index 19e107555..73a9d4369 100644 --- a/problems/160.Intersection-of-Two-Linked-Lists.md +++ b/problems/160.Intersection-of-Two-Linked-Lists.md @@ -67,7 +67,7 @@ return null; - 当 b 到达链表的尾部时,重定位到链表 A 的头结点。 - a, b 指针相遇的点为相交的起始节点,否则没有相交点 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfig7vsvwhj30bs05z3yl.jpg) +![](https://p.ipic.vip/m02u9c.jpg) (图 5) 为什么 a, b 指针相遇的点一定是相交的起始节点? 我们证明一下: diff --git a/problems/1631.path-with-minimum-effort.md b/problems/1631.path-with-minimum-effort.md index 8b6d2e71f..5608686b1 100644 --- a/problems/1631.path-with-minimum-effort.md +++ b/problems/1631.path-with-minimum-effort.md @@ -17,7 +17,7 @@ https://leetcode-cn.com/problems/path-with-minimum-effort/ ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7c0poru6j308z08z0su.jpg) +![](https://p.ipic.vip/qcib1m.jpg) ``` @@ -29,7 +29,7 @@ https://leetcode-cn.com/problems/path-with-minimum-effort/ ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7c0vxq7kj308z08zwel.jpg) +![](https://p.ipic.vip/as0bds.jpg) ``` @@ -40,7 +40,7 @@ https://leetcode-cn.com/problems/path-with-minimum-effort/ ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7c1ckz44j30ej0egaaj.jpg) +![](https://p.ipic.vip/c6cw0y.jpg) ``` diff --git a/problems/1649.create-sorted-array-through-instructions.md b/problems/1649.create-sorted-array-through-instructions.md index dd1e59680..688c56e0f 100644 --- a/problems/1649.create-sorted-array-through-instructions.md +++ b/problems/1649.create-sorted-array-through-instructions.md @@ -135,7 +135,7 @@ nums[l:l] = [instruction] - query(l, r): 查询 [l, r] 范围内的数的个数 - update(x): 将 x 更新到线段树 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmomwhg131j30i90bvq3z.jpg) +![](https://p.ipic.vip/zogfe5.jpg) 因此我们的目标其实就是 min(query(1, instruction - 1), query(instruction + 1, upper)),其中 upper 为 instructions 的最大树。 diff --git a/problems/167.two-sum-ii-input-array-is-sorted.en.md b/problems/167.two-sum-ii-input-array-is-sorted.en.md index 3892ec763..b709f95c9 100644 --- a/problems/167.two-sum-ii-input-array-is-sorted.en.md +++ b/problems/167.two-sum-ii-input-array-is-sorted.en.md @@ -167,4 +167,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0wr0tsj30p00dwt9t.jpg) +![](https://p.ipic.vip/7rnnn5.jpg) diff --git a/problems/167.two-sum-ii-input-array-is-sorted.md b/problems/167.two-sum-ii-input-array-is-sorted.md index a5a359e2c..56ec60c76 100644 --- a/problems/167.two-sum-ii-input-array-is-sorted.md +++ b/problems/167.two-sum-ii-input-array-is-sorted.md @@ -166,4 +166,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0wr0tsj30p00dwt9t.jpg) +![](https://p.ipic.vip/3g9v4q.jpg) diff --git a/problems/1671.minimum-number-of-removals-to-make-mountain-array.md b/problems/1671.minimum-number-of-removals-to-make-mountain-array.md index 1192d5153..5251625e3 100644 --- a/problems/1671.minimum-number-of-removals-to-make-mountain-array.md +++ b/problems/1671.minimum-number-of-removals-to-make-mountain-array.md @@ -63,7 +63,7 @@ arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 根据时间复杂度速查表: -![](https://pic.leetcode-cn.com/1611484348-eWMhVn-008eGmZEly1gmyykugjnaj310m0og401.jpg) +![](https://p.ipic.vip/zf68eo.jpg) > 时间复杂度速查表可以在我的刷题插件中查到。刷题插件可以在我的公众号《力扣加加》回复插件获取。 diff --git a/problems/169.majority-element.en.md b/problems/169.majority-element.en.md index 7f7179d9c..4a5d9dc9a 100644 --- a/problems/169.majority-element.en.md +++ b/problems/169.majority-element.en.md @@ -46,7 +46,7 @@ The principle of the voting algorithm is to eliminate different elements continu The principle behind it is very simple, that is, in the worst case, every number in the non-majority is eliminated from the majority, then the rest is the majority. In other cases, it is obvious that the majority itself is the rest. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7i1c8tj30mz0cjwfk.jpg) +![](https://p.ipic.vip/d87cuw.jpg) ## Analysis of key points @@ -121,4 +121,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/8kh9hh.jpg) diff --git a/problems/169.majority-element.md b/problems/169.majority-element.md index 052d5a20b..9b9404f59 100644 --- a/problems/169.majority-element.md +++ b/problems/169.majority-element.md @@ -45,7 +45,7 @@ https://leetcode-cn.com/problems/majority-element/ 背后的原理非常简单,即最坏的情况下非众数中的每一个数都和众数进行消除,那么剩下的是众数。其他情况则显然剩下的也是众数本身。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7i1c8tj30mz0cjwfk.jpg) +![](https://p.ipic.vip/etszhp.jpg) ## 关键点解析 @@ -141,4 +141,4 @@ class Solution { 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/d7jmss.jpg) diff --git a/problems/1697.checking-existence-of-edge-length-limited-paths.md b/problems/1697.checking-existence-of-edge-length-limited-paths.md index eaca26090..a621599b1 100644 --- a/problems/1697.checking-existence-of-edge-length-limited-paths.md +++ b/problems/1697.checking-existence-of-edge-length-limited-paths.md @@ -16,7 +16,7 @@ https://leetcode-cn.com/problems/checking-existence-of-edge-length-limited-paths 示例 1: ``` -![](https://tva1.sinaimg.cn/large/008eGmZEly1gn3jdi0jdkj307f07ajrf.jpg) +![](https://p.ipic.vip/up5ay9.jpg) ``` 输入:n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,2],[0,2,5]] 输出:[false,true] @@ -25,7 +25,7 @@ https://leetcode-cn.com/problems/checking-existence-of-edge-length-limited-paths 对于第二个查询,有一条路径(0 -> 1 -> 2)两条边都小于 5 ,所以这个查询我们返回 true 。 示例 2: ``` -![](https://tva1.sinaimg.cn/large/008eGmZEly1gn3jdtieo4j30au09yq33.jpg) +![](https://p.ipic.vip/r5fs0e.jpg) ``` 输入:n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,9],[3,4,13]], queries = [[0,4,14],[1,4,13]] diff --git a/problems/17.Letter-Combinations-of-a-Phone-Number.md b/problems/17.Letter-Combinations-of-a-Phone-Number.md index 37f6c952c..4ed03cddc 100644 --- a/problems/17.Letter-Combinations-of-a-Phone-Number.md +++ b/problems/17.Letter-Combinations-of-a-Phone-Number.md @@ -6,7 +6,7 @@ https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 -![image.png](https://assets.leetcode-cn.com/aliyun-lc-upload/original_images/17_telephone_keypad.png) +![image.png](https://p.ipic.vip/4xpxnc.jpg) ``` @@ -290,4 +290,4 @@ N + M 是输入数字的总数 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/srtd7m.jpg) diff --git a/problems/1713.minimum-operations-to-make-a-subsequence.md b/problems/1713.minimum-operations-to-make-a-subsequence.md index 66a770d5e..2e7da2c6e 100644 --- a/problems/1713.minimum-operations-to-make-a-subsequence.md +++ b/problems/1713.minimum-operations-to-make-a-subsequence.md @@ -96,4 +96,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/x2c6v2.jpg) diff --git a/problems/172.factorial-trailing-zeroes.en.md b/problems/172.factorial-trailing-zeroes.en.md index 970896bbb..2aa603b7c 100644 --- a/problems/172.factorial-trailing-zeroes.en.md +++ b/problems/172.factorial-trailing-zeroes.en.md @@ -40,16 +40,16 @@ We need to solve how many zeros are at the end of the result of multiplying thes Through observation, we found that if we want the end of the result to be 0, it must be multiplied by 2 and 5 after decomposing the prime factor. At the same time, after factorization, it is found that the number of 5 is much smaller than 2., Therefore, we only need to solve how many 5s there are after decomposing the prime factor of these n numbers. -![172.factorial-trailing-zeroes-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubdkzp7j30i10970t2.jpg) +![172.factorial-trailing-zeroes-2](https://p.ipic.vip/l75sny.jpg) As shown in the figure, if n is 30, then the result should be the number of red 5s in the figure, which is 7. -![172.factorial-trailing-zeroes-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubf2c3fj30hr0b4aar.jpg) +![172.factorial-trailing-zeroes-1](https://p.ipic.vip/n611xz.jpg) Our result is not directly f(n) = n / 5, for example, if n is 30, there are two 5s in 25. Similarly, if n is 150, there will be 7 such numbers. By observing, we find that the law'f(n) =n/5+n/5^2+n/5^3+n/5^4+n/5^5+. . ` -![172.factorial-trailing-zeroes-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubgxccqj30h3091t9i.jpg) +![172.factorial-trailing-zeroes-3](https://p.ipic.vip/1jtr3h.jpg) If you can find the above rules, it's up to you to implement this formula recursively or cyclically. @@ -150,4 +150,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/f6ptwl.jpg) diff --git a/problems/172.factorial-trailing-zeroes.md b/problems/172.factorial-trailing-zeroes.md index 3b3300afa..e9bb8b33c 100644 --- a/problems/172.factorial-trailing-zeroes.md +++ b/problems/172.factorial-trailing-zeroes.md @@ -41,11 +41,11 @@ https://leetcode-cn.com/problems/factorial-trailing-zeroes/ 同时因数分解之后发现 5 的个数远小于 2,因此我们只需要求解这 n 数字分解质因数之后 一共有多少个 5 即可. -![172.factorial-trailing-zeroes-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubdkzp7j30i10970t2.jpg) +![172.factorial-trailing-zeroes-2](https://p.ipic.vip/hr4mf0.jpg) 如图如果 n 为 30,那么结果应该是图中红色 5 的个数,即 7。 -![172.factorial-trailing-zeroes-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubf2c3fj30hr0b4aar.jpg) +![172.factorial-trailing-zeroes-1](https://p.ipic.vip/b9zcjm.jpg) 我们的结果并不是直接 f(n) = n / 5, 比如 n 为 30, 25 中是有两个 5 的。类似,n 为 150,会有 7 个这样的数字。 @@ -57,7 +57,7 @@ https://leetcode-cn.com/problems/factorial-trailing-zeroes/ 据此得出转移方程:`f(n) = n/5 + n/5^2 + n/5^3 + n/5^4 + n/5^5+..` -![172.factorial-trailing-zeroes-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubgxccqj30h3091t9i.jpg) +![172.factorial-trailing-zeroes-3](https://p.ipic.vip/yzmwpr.jpg) 如果可以发现上面的方程,用递归还是循环实现这个算式就看你的了。 @@ -160,4 +160,4 @@ class Solution { 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你 识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/l7rsgh.jpg) diff --git a/problems/1723.find-minimum-time-to-finish-all-jobs.md b/problems/1723.find-minimum-time-to-finish-all-jobs.md index f857287a9..19cc1d8bc 100644 --- a/problems/1723.find-minimum-time-to-finish-all-jobs.md +++ b/problems/1723.find-minimum-time-to-finish-all-jobs.md @@ -126,7 +126,7 @@ k 二进制位为 1 表示选取任务 k,否则表示不选取任务 k。 \right. $$ --> -![](https://tva1.sinaimg.cn/large/008i3skNly1gs0a7pgkgaj30u204ejrs.jpg) +![](https://p.ipic.vip/47u63j.jpg) 其中 sub 是 j 的子集, sum(sub) 指的是任务情况如 sub 二进制表示那样的完成的**总时间**。 @@ -221,4 +221,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/26rxxs.jpg) diff --git a/problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md b/problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md index 5723e360c..ce1a1ffe0 100644 --- a/problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md +++ b/problems/1737.change-minimum-characters-to-satisfy-one-of-three-conditions.md @@ -169,4 +169,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/43wvae.jpg) diff --git a/problems/1770.maximum-score-from-performing-multiplication-operations.md b/problems/1770.maximum-score-from-performing-multiplication-operations.md index ced8c7f2e..c56388260 100644 --- a/problems/1770.maximum-score-from-performing-multiplication-operations.md +++ b/problems/1770.maximum-score-from-performing-multiplication-operations.md @@ -164,7 +164,7 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/uh3k7s.jpg) ## 其他 diff --git a/problems/1787.make-the-xor-of-all-segments-equal-to-zero.md b/problems/1787.make-the-xor-of-all-segments-equal-to-zero.md index c3f432549..9a86c5149 100644 --- a/problems/1787.make-the-xor-of-all-segments-equal-to-zero.md +++ b/problems/1787.make-the-xor-of-all-segments-equal-to-zero.md @@ -242,4 +242,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/mv7oxw.jpg) diff --git a/problems/1834.single-threaded-cpu.md b/problems/1834.single-threaded-cpu.md index a18eb8d38..4dc77fe1f 100644 --- a/problems/1834.single-threaded-cpu.md +++ b/problems/1834.single-threaded-cpu.md @@ -134,4 +134,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/0yexqu.jpg) diff --git a/problems/1835.find-xor-sum-of-all-pairs-bitwise-and.md b/problems/1835.find-xor-sum-of-all-pairs-bitwise-and.md index 744502439..f59ba8657 100644 --- a/problems/1835.find-xor-sum-of-all-pairs-bitwise-and.md +++ b/problems/1835.find-xor-sum-of-all-pairs-bitwise-and.md @@ -120,4 +120,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/om48o3.jpg) diff --git a/problems/1871.jump-game-vii.md b/problems/1871.jump-game-vii.md index ccaddaeac..be147820b 100644 --- a/problems/1871.jump-game-vii.md +++ b/problems/1871.jump-game-vii.md @@ -258,4 +258,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/tgmjjv.jpg) diff --git a/problems/1872.stone-game-viii.md b/problems/1872.stone-game-viii.md index 8b8de8c3a..31ad4e5a1 100644 --- a/problems/1872.stone-game-viii.md +++ b/problems/1872.stone-game-viii.md @@ -233,4 +233,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/kel64l.jpg) diff --git a/problems/1899.merge-triplets-to-form-target-triplet.md b/problems/1899.merge-triplets-to-form-target-triplet.md index c416cadd5..a202f99bd 100644 --- a/problems/1899.merge-triplets-to-form-target-triplet.md +++ b/problems/1899.merge-triplets-to-form-target-triplet.md @@ -175,4 +175,4 @@ public: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ztxhe1.jpg) diff --git a/problems/19.removeNthNodeFromEndofList.md b/problems/19.removeNthNodeFromEndofList.md index 3eab8cfae..a10762cd5 100644 --- a/problems/19.removeNthNodeFromEndofList.md +++ b/problems/19.removeNthNodeFromEndofList.md @@ -50,7 +50,7 @@ https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ - 将 p 的下一个节点指向下下个节点 -![19.removeNthNodeFromEndOfList](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludrrxbjg30qn0ezajr.gif) +![19.removeNthNodeFromEndOfList](https://p.ipic.vip/gn0tx0.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) diff --git a/problems/190.reverse-bits.en.md b/problems/190.reverse-bits.en.md index f87ab7343..a7b5e45f5 100644 --- a/problems/190.reverse-bits.en.md +++ b/problems/190.reverse-bits.en.md @@ -168,4 +168,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/gi7b0b.jpg) diff --git a/problems/190.reverse-bits.md b/problems/190.reverse-bits.md index d7a813142..744f03586 100644 --- a/problems/190.reverse-bits.md +++ b/problems/190.reverse-bits.md @@ -181,4 +181,4 @@ public: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ty33yx.jpg) diff --git a/problems/1904.the-number-of-full-rounds-you-have-played.md b/problems/1904.the-number-of-full-rounds-you-have-played.md index 7cd583a15..070a51cab 100644 --- a/problems/1904.the-number-of-full-rounds-you-have-played.md +++ b/problems/1904.the-number-of-full-rounds-you-have-played.md @@ -131,4 +131,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/tvysu6.jpg) diff --git a/problems/1906.minimum-absolute-difference-queries.md b/problems/1906.minimum-absolute-difference-queries.md index 1735182e7..40f8209bc 100644 --- a/problems/1906.minimum-absolute-difference-queries.md +++ b/problems/1906.minimum-absolute-difference-queries.md @@ -137,4 +137,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/59w59k.jpg) diff --git a/problems/191.number-of-1-bits.en.md b/problems/191.number-of-1-bits.en.md index a48568068..dd6d4d5fc 100644 --- a/problems/191.number-of-1-bits.en.md +++ b/problems/191.number-of-1-bits.en.md @@ -135,7 +135,7 @@ return count Bit operations can be used to achieve the purpose. For example, an 8-bit integer 21: -![number-of-1-bits](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyhhz7mj308007w0sx.jpg) +![number-of-1-bits](https://p.ipic.vip/5a4ii4.jpg) C++ Code: @@ -168,4 +168,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/xuk9yr.jpg) diff --git a/problems/191.number-of-1-bits.md b/problems/191.number-of-1-bits.md index de028abc7..c0befcc9f 100644 --- a/problems/191.number-of-1-bits.md +++ b/problems/191.number-of-1-bits.md @@ -151,7 +151,7 @@ public class Solution { 可以使用位操作来达到目的。例如 8 位的整数 21: -![number-of-1-bits](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyhhz7mj308007w0sx.jpg) +![number-of-1-bits](https://p.ipic.vip/2p8pm6.jpg) C++ Code: @@ -184,4 +184,4 @@ public: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/7ftvwq.jpg) diff --git a/problems/1970.last-day-where-you-can-still-cross.md b/problems/1970.last-day-where-you-can-still-cross.md index 195b5ac5e..f8368ba27 100644 --- a/problems/1970.last-day-where-you-can-still-cross.md +++ b/problems/1970.last-day-where-you-can-still-cross.md @@ -124,4 +124,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/buz35n.jpg) diff --git a/problems/198.house-robber.en.md b/problems/198.house-robber.en.md index 54d6420a3..18fad9624 100755 --- a/problems/198.house-robber.en.md +++ b/problems/198.house-robber.en.md @@ -48,7 +48,7 @@ Since we always want a larger gain, it's easy to obtain the transition formula: > Note: For the convenience of calculation, we set both dp[0] and dp[1] to be 0. This way, dp[i] is actually for the i-1th house. We can use the following graph to illustrate the above process: -![198.house-robber](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluas8wykj30k00bjdh6.jpg) +![198.house-robber](https://p.ipic.vip/vb22h1.jpg) If we optimize it further, we only need dp[i - 1] and dp[i - 2] when determining each dp[i]. For example, to calculate dp[6], we would only need dp[5] and dp[4], and there's no need to keep dp[3], dp[2], and so on, in memory. diff --git a/problems/198.house-robber.md b/problems/198.house-robber.md index 0736ed389..f51eaa336 100644 --- a/problems/198.house-robber.md +++ b/problems/198.house-robber.md @@ -71,7 +71,7 @@ https://leetcode-cn.com/problems/house-robber/ 上述过程用图来表示的话,是这样的: -![198.house-robber](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluatdk9oj30k00bjdh6.jpg) +![198.house-robber](https://p.ipic.vip/jipwwl.jpg) 我们仔细观察的话,其实我们只需要保证前一个 dp[i - 1] 和 dp[i - 2] 两个变量就好了, 比如我们计算到 i = 6 的时候,即需要计算 dp[6]的时候, 我们需要 dp[5], dp[4],但是我们 @@ -206,4 +206,4 @@ class Solution { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/4uxqo8.jpg) diff --git a/problems/2.add-two-numbers.en.md b/problems/2.add-two-numbers.en.md index ecd31f1f7..1ce835bb7 100644 --- a/problems/2.add-two-numbers.en.md +++ b/problems/2.add-two-numbers.en.md @@ -19,7 +19,7 @@ Explanation: 342 + 465 = 807. Define a new variable `carried` that represents the carry value during the calculation, and a new linked list Traverse the two linked lists from the start to the end simultaneously, and calculate the sum of node value from each linked list. The sum of the result and `carried` would be appended as a new node to the end of the new linked list. -![2.addTwoNumbers](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludjiguqg30qh0eon5c.gif) +![2.addTwoNumbers](https://p.ipic.vip/5nidmb.gif) (Image Reference: https://github.com/MisterBooo/LeetCodeAnimation) diff --git a/problems/2.add-two-numbers.md b/problems/2.add-two-numbers.md index f92c05380..51755135d 100644 --- a/problems/2.add-two-numbers.md +++ b/problems/2.add-two-numbers.md @@ -33,7 +33,7 @@ https://leetcode-cn.com/problems/add-two-numbers/ 设立一个表示进位的变量 carried,建立一个新链表,把输入的两个链表从头往后同时处理,每两个相加,将结果加上 carried 后的值作为一个新节点到新链表后面,并更新 carried 值即可。 -![2.addTwoNumbers](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6u8jwyg30qh0eon5c.gif) +![2.addTwoNumbers](https://p.ipic.vip/budg5i.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) @@ -284,4 +284,4 @@ private: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/uqtfu7.jpg) diff --git a/problems/20.valid-parentheses.md b/problems/20.valid-parentheses.md index 57459099c..0e44ed631 100644 --- a/problems/20.valid-parentheses.md +++ b/problems/20.valid-parentheses.md @@ -73,7 +73,7 @@ https://leetcode-cn.com/problems/valid-parentheses/description 3)若不为对应的左半边括号,反之返回 false -![20.validParentheses](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyb2lpvg30qo0f0n2n.gif) +![20.validParentheses](https://p.ipic.vip/4j38xn.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) @@ -311,4 +311,4 @@ var isValid = function (s) { 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/zl5m7q.jpg) diff --git a/problems/20.valid-parents.en.md b/problems/20.valid-parents.en.md index d6112dfdd..628230a92 100644 --- a/problems/20.valid-parents.en.md +++ b/problems/20.valid-parents.en.md @@ -74,7 +74,7 @@ If you encounter the right half of the brackets, categorize and discuss: 3. If it is not the corresponding left half bracket, return false on the contrary -![20.validParentheses](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyb2lpvg30qo0f0n2n.gif) +![20.validParentheses](https://p.ipic.vip/xdojfe.gif) (Picture from: https://github.com/MisterBooo/LeetCodeAnimation ) @@ -252,4 +252,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ri3f1c.jpg) diff --git a/problems/200.number-of-islands.md b/problems/200.number-of-islands.md index 364e29949..3bb484500 100644 --- a/problems/200.number-of-islands.md +++ b/problems/200.number-of-islands.md @@ -57,7 +57,7 @@ grid[i][j] 的值为 '0' 或 '1' 如图,我们其实就是要求红色区域的个数,换句话说就是求连续区域的个数。 -![200.number-of-islands](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludwu4zlj309y0dgjs0.jpg) +![200.number-of-islands](https://p.ipic.vip/jgv1ll.jpg) 符合直觉的做法是用 DFS 来解: @@ -221,7 +221,7 @@ class Solution: 欢迎关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludxd8jhj31bi0hcq5s.jpg) +![](https://p.ipic.vip/5sm8ho.jpg) ## 相关题目 diff --git a/problems/2007.find-original-array-from-doubled-array.md b/problems/2007.find-original-array-from-doubled-array.md index 4eba11499..cb5217852 100644 --- a/problems/2007.find-original-array-from-doubled-array.md +++ b/problems/2007.find-original-array-from-doubled-array.md @@ -150,4 +150,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/iv6dhk.jpg) diff --git a/problems/2008.maximum-earnings-from-taxi.md b/problems/2008.maximum-earnings-from-taxi.md index afea6bd3c..4f83d3ef3 100644 --- a/problems/2008.maximum-earnings-from-taxi.md +++ b/problems/2008.maximum-earnings-from-taxi.md @@ -120,4 +120,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/gt72lu.jpg) diff --git a/problems/2009.minimum-number-of-operations-to-make-array-continuous.md b/problems/2009.minimum-number-of-operations-to-make-array-continuous.md index dc0fac79b..d1944fe3b 100644 --- a/problems/2009.minimum-number-of-operations-to-make-array-continuous.md +++ b/problems/2009.minimum-number-of-operations-to-make-array-continuous.md @@ -121,4 +121,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/g2h0ww.jpg) diff --git a/problems/2025.maximum-number-of-ways-to-partition-an-array.md b/problems/2025.maximum-number-of-ways-to-partition-an-array.md index e5629f3f2..5b5056be5 100644 --- a/problems/2025.maximum-number-of-ways-to-partition-an-array.md +++ b/problems/2025.maximum-number-of-ways-to-partition-an-array.md @@ -130,4 +130,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/no2re9.jpg) diff --git a/problems/203.remove-linked-list-elements.en.md b/problems/203.remove-linked-list-elements.en.md index 574ae0a59..ced469ea1 100644 --- a/problems/203.remove-linked-list-elements.en.md +++ b/problems/203.remove-linked-list-elements.en.md @@ -121,4 +121,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/uo0v95.jpg) diff --git a/problems/203.remove-linked-list-elements.md b/problems/203.remove-linked-list-elements.md index 3db7f3ca1..c54012808 100644 --- a/problems/203.remove-linked-list-elements.md +++ b/problems/203.remove-linked-list-elements.md @@ -161,4 +161,4 @@ class Solution { 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/rbt63f.jpg) diff --git a/problems/2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.md b/problems/2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.md index 5a8454dac..599cdf174 100644 --- a/problems/2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.md +++ b/problems/2030.smallest-k-length-subsequence-with-occurrences-of-a-letter.md @@ -128,4 +128,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ulyess.jpg) diff --git a/problems/206.reverse-linked-list.md b/problems/206.reverse-linked-list.md index a5ad3cc77..cc44f703b 100644 --- a/problems/206.reverse-linked-list.md +++ b/problems/206.reverse-linked-list.md @@ -277,4 +277,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/in5o20.jpg) diff --git a/problems/208.implement-trie-prefix-tree.md b/problems/208.implement-trie-prefix-tree.md index 3290484cc..14d0a743d 100644 --- a/problems/208.implement-trie-prefix-tree.md +++ b/problems/208.implement-trie-prefix-tree.md @@ -66,7 +66,7 @@ function computeIndex(c) { 其实不管 insert, search 和 startWith 的逻辑都是差不多的,都是从 root 出发, 找到我们需要操作的 child, 然后进行相应操作(添加,修改,返回)。 -![208.implement-trie-prefix-tree-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8zkn7rj30mz0gq406.jpg) +![208.implement-trie-prefix-tree-1](https://p.ipic.vip/zyutt3.jpg) ## 关键点解析 diff --git a/problems/209.minimum-size-subarray-sum.md b/problems/209.minimum-size-subarray-sum.md index e41b9a715..72d154b39 100644 --- a/problems/209.minimum-size-subarray-sum.md +++ b/problems/209.minimum-size-subarray-sum.md @@ -37,7 +37,7 @@ https://leetcode-cn.com/problems/minimum-size-subarray-sum/ 用滑动窗口来记录序列, 每当滑动窗口中的 sum 超过 s, 就去更新最小值,并根据先进先出的原则更新滑动窗口,直至 sum 刚好小于 s -![209.minimum-size-subarray-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4211x3j30my0kxdh3.jpg) +![209.minimum-size-subarray-sum](https://p.ipic.vip/3wsirt.jpg) > 这道题目和 leetcode 3 号题目有点像,都可以用滑动窗口的思路来解决 @@ -133,7 +133,7 @@ public: 欢迎关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu43kcxpj31bi0hcq5s.jpg) +![](https://p.ipic.vip/skdzf4.jpg) ## 扩展 @@ -172,4 +172,4 @@ var minSubArrayLen = function (s, nums) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/z5yy3u.jpg) diff --git a/problems/21.merge-two-sorted-lists.en.md b/problems/21.merge-two-sorted-lists.en.md index 54b2af076..a5ed41bab 100644 --- a/problems/21.merge-two-sorted-lists.en.md +++ b/problems/21.merge-two-sorted-lists.en.md @@ -159,4 +159,4 @@ return prehead. next; If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . It is currently 40K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/7jytuf.jpg) diff --git a/problems/21.merge-two-sorted-lists.md b/problems/21.merge-two-sorted-lists.md index 3804b8545..ae8b24324 100644 --- a/problems/21.merge-two-sorted-lists.md +++ b/problems/21.merge-two-sorted-lists.md @@ -248,4 +248,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/dhb6m3.jpg) diff --git a/problems/2102.sequentially-ordinal-rank-tracker.md b/problems/2102.sequentially-ordinal-rank-tracker.md index d8e60208a..1969d522d 100644 --- a/problems/2102.sequentially-ordinal-rank-tracker.md +++ b/problems/2102.sequentially-ordinal-rank-tracker.md @@ -181,4 +181,4 @@ class SORTracker: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/uv3eyd.jpg) diff --git a/problems/211.add-and-search-word-data-structure-design.md b/problems/211.add-and-search-word-data-structure-design.md index 81a0bc46c..7eacbccbf 100644 --- a/problems/211.add-and-search-word-data-structure-design.md +++ b/problems/211.add-and-search-word-data-structure-design.md @@ -61,7 +61,7 @@ search 中的 word 由 '.' 或小写英文字母组成 关于前缀树,LeetCode 有很多题目。有的是直接考察,让你实现一个前缀树,有的是间接考察,比如本题。前缀树代码见下方,大家之后可以直接当成前缀树的解题模板使用。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwng8wvj30mz0gqdhc.jpg) +![](https://p.ipic.vip/8ujt14.jpg) 由于我们这道题需要考虑特殊字符".",因此我们需要对标准前缀树做一点改造,insert 不做改变,我们只需要改变 search 即可,代码(Python 3): diff --git a/problems/212.word-search-ii.md b/problems/212.word-search-ii.md index 3f84ff5d7..bca9f525c 100644 --- a/problems/212.word-search-ii.md +++ b/problems/212.word-search-ii.md @@ -64,7 +64,7 @@ words = ["oath","pea","eat","rain"] and board = 关于前缀树,可以参考我的[前缀树](../thinkings/trie.md) 专题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua4m3ofj30mz0gqdhc.jpg) +![](https://p.ipic.vip/fgmjpf.jpg) 值得注意的是如果每次 dfs 都使用 startsWith 来判断,那么会超时。我们可以将当前遍历到的 trie 节点 以参数传递到 dfs 中,这样可以进一步减少复杂度。 diff --git a/problems/215.kth-largest-element-in-an-array.md b/problems/215.kth-largest-element-in-an-array.md index 3dab92b95..31fd521ab 100644 --- a/problems/215.kth-largest-element-in-an-array.md +++ b/problems/215.kth-largest-element-in-an-array.md @@ -55,7 +55,7 @@ https://leetcode-cn.com/problems/kth-largest-element-in-an-array/ 扫描一遍数组,最后堆顶就是第`K`大的元素。 直接返回。 例如: -![heap](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwuls8wj312q0u0q7x.jpg) +![heap](https://p.ipic.vip/ki6u36.jpg) *时间复杂度*:`O(n * logk) , n is array length` *空间复杂度*:`O(k)` @@ -80,7 +80,7 @@ Quick Select 类似快排,选取pivot,把小于pivot的元素都移到pivot 如下图: ``` -![quick select](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwvfdvvj30yl0nxwj0.jpg) +![quick select](https://p.ipic.vip/nhqbw0.jpg) *时间复杂度*: - 平均是:`O(n)` diff --git a/problems/219.contains-duplicate-ii.en.md b/problems/219.contains-duplicate-ii.en.md index 18b54125a..48f24069b 100644 --- a/problems/219.contains-duplicate-ii.en.md +++ b/problems/219.contains-duplicate-ii.en.md @@ -137,4 +137,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/hg753q.jpg) diff --git a/problems/219.contains-duplicate-ii.md b/problems/219.contains-duplicate-ii.md index 53c697c5f..16655afc9 100644 --- a/problems/219.contains-duplicate-ii.md +++ b/problems/219.contains-duplicate-ii.md @@ -137,4 +137,4 @@ class Solution { 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ndzy7w.jpg) diff --git a/problems/22.generate-parentheses.md b/problems/22.generate-parentheses.md index a7cfc66ca..3ab855cc7 100644 --- a/problems/22.generate-parentheses.md +++ b/problems/22.generate-parentheses.md @@ -172,4 +172,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/8kgn2o.jpg) diff --git a/problems/220.contains-duplicate-iii.md b/problems/220.contains-duplicate-iii.md index d450b3318..4ad52528c 100644 --- a/problems/220.contains-duplicate-iii.md +++ b/problems/220.contains-duplicate-iii.md @@ -200,4 +200,4 @@ public: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/k13ir3.jpg) diff --git a/problems/2209.minimum-white-tiles-after-covering-with-carpets.md b/problems/2209.minimum-white-tiles-after-covering-with-carpets.md index 322a809be..d7e130769 100644 --- a/problems/2209.minimum-white-tiles-after-covering-with-carpets.md +++ b/problems/2209.minimum-white-tiles-after-covering-with-carpets.md @@ -125,4 +125,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你 识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/d21uo7.jpg) diff --git a/problems/221.maximal-square.md b/problems/221.maximal-square.md index 0db12cf6c..8c125b80e 100644 --- a/problems/221.maximal-square.md +++ b/problems/221.maximal-square.md @@ -34,7 +34,7 @@ https://leetcode-cn.com/problems/maximal-square/ ## 思路 -![221.maximal-square](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludl52xfj30bo09vmxo.jpg) +![221.maximal-square](https://p.ipic.vip/fbnfq5.jpg) 符合直觉的做法是暴力求解处所有的正方形,逐一计算面积,然后记录最大的。这种时间复杂度很高。 @@ -48,13 +48,13 @@ https://leetcode-cn.com/problems/maximal-square/ dp[2][2]等于 1(之前已经计算好了),那么其实这里的瓶颈在于三者的最小值, 即`Min(1, 1, 3)`, 也就是`1`。 那么 dp[3][3] 就等于 `Min(1, 1, 3) + 1`。 -![221.maximal-square](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludlnra9j30an08xt96.jpg) +![221.maximal-square](https://p.ipic.vip/6okd2l.jpg) dp[i - 1][j - 1]我们直接拿到,关键是`往上和往左进行延伸`, 最直观的做法是我们内层加一个循环去做就好了。 但是我们仔细观察一下,其实我们根本不需要这样算。 我们可以直接用 dp[i - 1][j]和 dp[i][j -1]。 具体就是`Min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1`。 -![221.maximal-square](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludm7ilmj30a507sglz.jpg) +![221.maximal-square](https://p.ipic.vip/7xt3ta.jpg) 事实上,这道题还有空间复杂度 O(N)的解法,其中 N 指的是列数。 大家可以去这个[leetcode 讨论](https://leetcode.com/problems/maximal-square/discuss/61803/C%2B%2B-space-optimized-DP)看一下。 diff --git a/problems/226.invert-binary-tree.en.md b/problems/226.invert-binary-tree.en.md index 772f5f2c4..f605e06e2 100644 --- a/problems/226.invert-binary-tree.en.md +++ b/problems/226.invert-binary-tree.en.md @@ -169,4 +169,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/3nffiw.jpg) diff --git a/problems/226.invert-binary-tree.md b/problems/226.invert-binary-tree.md index 97795b04c..9e3ec1e40 100644 --- a/problems/226.invert-binary-tree.md +++ b/problems/226.invert-binary-tree.md @@ -169,4 +169,4 @@ public: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6bt81z.jpg) diff --git a/problems/227.basic-calculator-ii.md b/problems/227.basic-calculator-ii.md index b7046f4a7..e8558079f 100644 --- a/problems/227.basic-calculator-ii.md +++ b/problems/227.basic-calculator-ii.md @@ -308,4 +308,4 @@ if c == ')': 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 38K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/emhakc.jpg) diff --git a/problems/2281.sum-of-total-strength-of-wizards.md b/problems/2281.sum-of-total-strength-of-wizards.md index 407d845ea..244cd8318 100644 --- a/problems/2281.sum-of-total-strength-of-wizards.md +++ b/problems/2281.sum-of-total-strength-of-wizards.md @@ -172,4 +172,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6jaza9.jpg) diff --git a/problems/229.majority-element-ii.md b/problems/229.majority-element-ii.md index b05570e78..4c671ffe8 100644 --- a/problems/229.majority-element-ii.md +++ b/problems/229.majority-element-ii.md @@ -59,9 +59,9 @@ https://leetcode-cn.com/problems/majority-element-ii/ 这里画了一个图,大家可以感受一下: -![229.majority-element-ii-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltygnjljj31400u0ad9.jpg) +![229.majority-element-ii-1](https://p.ipic.vip/geonsr.jpg) -![229.majority-element-ii-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyh2s8jj31400u075n.jpg) +![229.majority-element-ii-1](https://p.ipic.vip/cf2r6u.jpg) ## 关键点解析 @@ -254,4 +254,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/zr18ww.jpg) diff --git a/problems/23.merge-k-sorted-lists.md b/problems/23.merge-k-sorted-lists.md index 9db0f0b58..3f1f5a88f 100644 --- a/problems/23.merge-k-sorted-lists.md +++ b/problems/23.merge-k-sorted-lists.md @@ -46,7 +46,7 @@ https://leetcode-cn.com/problems/merge-k-sorted-lists/ 具体我们可以来看一个动画 -![23.merge-k-sorted-lists](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluds9tu0g30go09ajto.gif) +![23.merge-k-sorted-lists](https://p.ipic.vip/f23z23.gif) (动画来自 https://zhuanlan.zhihu.com/p/61796021 ) @@ -216,4 +216,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/a0rul7.jpg) diff --git a/problems/230.kth-smallest-element-in-a-bst.md b/problems/230.kth-smallest-element-in-a-bst.md index 40c5ec73f..c79e6b912 100644 --- a/problems/230.kth-smallest-element-in-a-bst.md +++ b/problems/230.kth-smallest-element-in-a-bst.md @@ -198,4 +198,4 @@ var kthSmallest = function (root, k) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/00jhxj.jpg) diff --git a/problems/2306.naming-a-company.md b/problems/2306.naming-a-company.md index c30a6033b..c06bcedfb 100644 --- a/problems/2306.naming-a-company.md +++ b/problems/2306.naming-a-company.md @@ -183,4 +183,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/r8633q.jpg) diff --git a/problems/232.implement-queue-using-stacks.en.md b/problems/232.implement-queue-using-stacks.en.md index 15b31b553..912096a8d 100644 --- a/problems/232.implement-queue-using-stacks.en.md +++ b/problems/232.implement-queue-using-stacks.en.md @@ -41,25 +41,25 @@ Let's analyze the process. If you push four numbers into the stack separately `1, 2, 3, 4`, Then the situation of the stack at this time should be: -![](https://tva1.sinaimg.cn/large/008i3skNly1gptsgfuinrj30760dyq38.jpg) +![](https://p.ipic.vip/rabts2.jpg) If you pop or peek according to the requirements of the topic at this time, it should return 1, and 1 is at the bottom of the stack. We cannot operate directly. If we want to return 1, we must first get 2, 3, and 4 out of the stack separately. -![](https://tva1.sinaimg.cn/large/008i3skNly1gptsgtrog1j31yi0jo76y.jpg) +![](https://p.ipic.vip/1jp4io.jpg) However, if we do this, although 1 will return normally, won't 2, 3, and 4 disappear forever? One short answer method is to save 2, 3, and 4 \*\*. And the title said that only the data structure of the stack can be used, so we consider using an additional stack to store the pop-up 2, 3, and 4. -![](https://tva1.sinaimg.cn/large/008i3skNly1gptsh95st5j31jm0u0q5l.jpg) +![](https://p.ipic.vip/obgabr.jpg) (Pop it out and don't throw it away, but save it) The whole process is similar to this: -![](https://tva1.sinaimg.cn/large/008i3skNly1gptshhcxxkj30pg0j0ab3.jpg) +![](https://p.ipic.vip/nycmiu.jpg) For example, at this time, we want to push a 5, then it's probably like this: -![](https://tva1.sinaimg.cn/large/008i3skNly1gptshu24f4j327g0u0n18.jpg) +![](https://p.ipic.vip/qwgovq.jpg) However, this process can also occur in the push stage. diff --git a/problems/232.implement-queue-using-stacks.md b/problems/232.implement-queue-using-stacks.md index 671389019..e8839bd79 100644 --- a/problems/232.implement-queue-using-stacks.md +++ b/problems/232.implement-queue-using-stacks.md @@ -41,25 +41,25 @@ queue.empty(); // 返回 false 假如向栈中分别 push 四个数字 `1, 2, 3, 4`,那么此时栈的情况应该是: -![](https://tva1.sinaimg.cn/large/008i3skNly1gptsgfuinrj30760dyq38.jpg) +![](https://p.ipic.vip/n66w0t.jpg) 如果此时按照题目要求 pop 或者 peek 的话, 应该是返回 1 才对,而 1 在栈底我们无法直接操作。如果想要返回 1,我们首先要将 2,3,4 分别出栈才行。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gptsgtrog1j31yi0jo76y.jpg) +![](https://p.ipic.vip/azksfb.jpg) 然而,如果我们这么做,1 虽然是正常返回了,但是 2,3,4 不就永远消失了么? 一种简答方法就是,将 2,3,4 **存** 起来。而题目又说了,只能使用栈这种数据结构,那么我们考虑使用一个额外的栈来存放弹出的 2,3,4。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gptsh95st5j31jm0u0q5l.jpg) +![](https://p.ipic.vip/qj452a.jpg) (pop 出来不扔掉,而是存起来) 整个过程类似这样: -![](https://tva1.sinaimg.cn/large/008i3skNly1gptshhcxxkj30pg0j0ab3.jpg) +![](https://p.ipic.vip/2x0gn5.jpg) 比如,这个时候,我们想 push 一个 5,那么大概就是这样的: -![](https://tva1.sinaimg.cn/large/008i3skNly1gptshu24f4j327g0u0n18.jpg) +![](https://p.ipic.vip/94xwau.jpg) 然而这一过程,我们也可以发生在 push 阶段。 diff --git a/problems/236.lowest-common-ancestor-of-a-binary-tree.md b/problems/236.lowest-common-ancestor-of-a-binary-tree.md index 4797db905..b4692403e 100644 --- a/problems/236.lowest-common-ancestor-of-a-binary-tree.md +++ b/problems/236.lowest-common-ancestor-of-a-binary-tree.md @@ -13,7 +13,7 @@ https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ ``` -![236.lowest-common-ancestor-of-a-binary-tree](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4oh2jqj305k05aa9z.jpg) +![236.lowest-common-ancestor-of-a-binary-tree](https://p.ipic.vip/eb5q1b.jpg) ``` 示例 1: @@ -59,7 +59,7 @@ p、q 为不同节点且均存在于给定的二叉树中。 对于具体的代码而言就是,我们假设这个树就一个结构,然后尝试去解决,然后在适当地方去递归自身即可。 如下图所示: -![236.lowest-common-ancestor-of-a-binary-tree-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4pf06vj30n00aiq3o.jpg) +![236.lowest-common-ancestor-of-a-binary-tree-2](https://p.ipic.vip/ijmgev.jpg) 我们来看下核心代码: @@ -148,4 +148,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/2qnw3z.jpg) diff --git a/problems/238.product-of-array-except-self.md b/problems/238.product-of-array-except-self.md index c0ed63a26..e749f7bb0 100644 --- a/problems/238.product-of-array-except-self.md +++ b/problems/238.product-of-array-except-self.md @@ -45,7 +45,7 @@ https://leetcode-cn.com/problems/product-of-array-except-self/ 考虑我们先进行一次遍历, 然后维护一个数组,第 i 项代表前 i 个元素(不包括 i)的乘积。 然后我们反向遍历一次,然后维护另一个数组,同样是第 i 项代表前 i 个元素(不包括 i)的乘积。 -![238.product-of-array-except-self](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7zbobsj30n10c9gma.jpg) +![238.product-of-array-except-self](https://p.ipic.vip/jw66wp.jpg) 有意思的是第一个数组和第二个数组的反转(reverse)做乘法(有点像向量运算)就是我们想要的运算。 diff --git a/problems/239.sliding-window-maximum.md b/problems/239.sliding-window-maximum.md index 8b89a5a2d..199901422 100644 --- a/problems/239.sliding-window-maximum.md +++ b/problems/239.sliding-window-maximum.md @@ -113,7 +113,7 @@ class Solution: 经过上面的分析,不难知道双端队列其实是一个递减的一个队列,因此队首的元素一定是最大的。用图来表示就是: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxg29buj30hb0di757.jpg) +![](https://p.ipic.vip/fz6luk.jpg) ## 关键点解析 @@ -185,4 +185,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/61qh2w.jpg) diff --git a/problems/24.swapNodesInPairs.md b/problems/24.swapNodesInPairs.md index 9c503ff35..2a2a1ffd0 100644 --- a/problems/24.swapNodesInPairs.md +++ b/problems/24.swapNodesInPairs.md @@ -8,7 +8,7 @@ https://leetcode-cn.com/problems/swap-nodes-in-pairs/ 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 -![image.png](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg) +![image.png](https://p.ipic.vip/cntkb1.jpg) ``` 示例 1: @@ -52,7 +52,7 @@ https://leetcode-cn.com/problems/swap-nodes-in-pairs/ 7. current 移动两格 8. 重复 -![24.swap-nodes-in-pairs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6v237kg30qk0evqbw.gif) +![24.swap-nodes-in-pairs](https://p.ipic.vip/5vvrv4.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) @@ -207,4 +207,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/fi1yyu.jpg) diff --git a/problems/240.search-a-2-d-matrix-ii.md b/problems/240.search-a-2-d-matrix-ii.md index c048e6e1f..00c52cab6 100644 --- a/problems/240.search-a-2-d-matrix-ii.md +++ b/problems/240.search-a-2-d-matrix-ii.md @@ -44,7 +44,7 @@ https://leetcode-cn.com/problems/search-a-2d-matrix-ii/ 有没有时间复杂度更好的做法呢? 答案是有,那就是充分运用矩阵的特性(横向纵向都递增), 我们可以从角落(左下或者右上)开始遍历,这样时间复杂度是 O(m + n). -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlub9dbyij30ft0b43zd.jpg) +![](https://p.ipic.vip/yaajgz.jpg) 其中蓝色代表我们选择的起点元素, 红色代表目标元素。 @@ -126,4 +126,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/j14g18.jpg) diff --git a/problems/25.reverse-nodes-in-k-groups-en.md b/problems/25.reverse-nodes-in-k-groups-en.md index fa717e331..22740b254 100644 --- a/problems/25.reverse-nodes-in-k-groups-en.md +++ b/problems/25.reverse-nodes-in-k-groups-en.md @@ -45,7 +45,7 @@ curr = temp; For example(as below pic): reverse the whole linked list `1->2->3->4->null` -> `4->3->2->1->null` -![reverse linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty7t8i8j31400u0ahc.jpg) +![reverse linked list](https://p.ipic.vip/ajewar.jpg) Here Reverse each group(`k nodes`): @@ -63,13 +63,13 @@ Here Reverse each group(`k nodes`): As below pic show steps 4 and 5, reverse linked list in range `(start, end)`: -![reverse linked list range in (start, end)](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty8tkv0j30zd0qxjtg.jpg) +![reverse linked list range in (start, end)](https://p.ipic.vip/4khz8w.jpg) For example(as below pic),`head=[1,2,3,4,5,6,7,8], k = 3` -![reverse k nodes in linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty9776uj312u0u0nnt.jpg) +![reverse k nodes in linked list](https://p.ipic.vip/o04jk9.jpg) >**NOTE**: Usually we create a `dummy node` to solve linked list problem, because head node may be changed during operation. diff --git a/problems/25.reverse-nodes-in-k-groups.md b/problems/25.reverse-nodes-in-k-groups.md index a2d972666..de3b65afd 100644 --- a/problems/25.reverse-nodes-in-k-groups.md +++ b/problems/25.reverse-nodes-in-k-groups.md @@ -59,7 +59,7 @@ curr = temp; 举例如图:翻转整个链表 `1->2->3->4->null` -> `4->3->2->1->null` -![reverse linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwz9weoj31400u0ahc.jpg) +![reverse linked list](https://p.ipic.vip/qulz9a.jpg) 这里是对每一组(`k个nodes`)进行翻转, @@ -77,11 +77,11 @@ curr = temp; 如图所示 步骤 4 和 5: 翻转区间链表区间`(start, end)` -![reverse linked list range in (start, end)](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx146hoj30zd0qxjtg.jpg) +![reverse linked list range in (start, end)](https://p.ipic.vip/5cga6g.jpg) 举例如图,`head=[1,2,3,4,5,6,7,8], k = 3` -![reverse k nodes in linked list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx3k8x2j312u0u0nnt.jpg) +![reverse k nodes in linked list](https://p.ipic.vip/7whudf.jpg) > **NOTE**: 一般情况下对链表的操作,都有可能会引入一个新的`dummy node`,因为`head`有可能会改变。这里`head 从1->3`, > `dummy (List(0))`保持不变。 @@ -336,4 +336,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/urt7jp.jpg) diff --git a/problems/26.remove-duplicates-from-sorted-array.en.md b/problems/26.remove-duplicates-from-sorted-array.en.md index d1551850e..d53b6972f 100644 --- a/problems/26.remove-duplicates-from-sorted-array.en.md +++ b/problems/26.remove-duplicates-from-sorted-array.en.md @@ -70,7 +70,7 @@ Use the speed pointer to record the traversed coordinates. -When the fast pointer walks through the entire array, the current coordinates of the slow pointer plus 1 are the number of different numbers in the array. -![26.remove-duplicates-from-sorted-array](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucxqaoyg30qg0esju1.gif) +![26.remove-duplicates-from-sorted-array](https://p.ipic.vip/ooxtkv.gif) (Picture from: https://github.com/MisterBooo/LeetCodeAnimation ) @@ -159,4 +159,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucyn5dcj30p00dwt9t.jpg) +![](https://p.ipic.vip/yrgnnu.jpg) diff --git a/problems/26.remove-duplicates-from-sorted-array.md b/problems/26.remove-duplicates-from-sorted-array.md index e89a9c968..d3b5825e5 100644 --- a/problems/26.remove-duplicates-from-sorted-array.md +++ b/problems/26.remove-duplicates-from-sorted-array.md @@ -69,7 +69,7 @@ for (int i = 0; i < len; i++) { - 当快指针走完整个数组后,慢指针当前的坐标加 1 就是数组中不同数字的个数 -![26.remove-duplicates-from-sorted-array](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucxqaoyg30qg0esju1.gif) +![26.remove-duplicates-from-sorted-array](https://p.ipic.vip/mwo1eg.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) @@ -176,4 +176,4 @@ Java Code: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucyn5dcj30p00dwt9t.jpg) +![](https://p.ipic.vip/2mf5xs.jpg) diff --git a/problems/263.ugly-number.en.md b/problems/263.ugly-number.en.md index dfb85fb16..ce3e2cdff 100644 --- a/problems/263.ugly-number.en.md +++ b/problems/263.ugly-number.en.md @@ -47,7 +47,7 @@ The input will not exceed the range of 32−bit signed integers: [-231, 231-1]. The title requires that a number be given to determine whether it is an ”ugly number". An ugly number refers to a positive integer that contains only a prime factor of 2,3,5. -![263.ugly-number](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxf68kej30hh09fdgd.jpg) +![263.ugly-number](https://p.ipic.vip/a8i6ve.jpg) By definition, we divide a given number by 2, 3, and 5 (the order does not matter) until it cannot be divisible. If you get 1, it means that all factors are 2 or 3 or 5. If it is not 1, it is not an ugly number. @@ -170,4 +170,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ff467o.jpg) diff --git a/problems/263.ugly-number.md b/problems/263.ugly-number.md index 3865118d9..b4fd659ce 100644 --- a/problems/263.ugly-number.md +++ b/problems/263.ugly-number.md @@ -47,7 +47,7 @@ https://leetcode-cn.com/problems/ugly-number/ 题目要求给定一个数字,判断是否为“丑陋数”(ugly number), 丑陋数是指只包含质因子 2, 3, 5 的正整数。 -![263.ugly-number](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxf68kej30hh09fdgd.jpg) +![263.ugly-number](https://p.ipic.vip/hid8a0.jpg) 根据定义,我们将给定数字除以 2、3、5(顺序无所谓),直到无法整除。 如果得到 1,说明是所有因子都是 2 或 3 或 5,如果不是 1,则不是丑陋数。 @@ -170,4 +170,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6y6avj.jpg) diff --git a/problems/279.perfect-squares.md b/problems/279.perfect-squares.md index dfdf825e0..b4fcfc3bd 100644 --- a/problems/279.perfect-squares.md +++ b/problems/279.perfect-squares.md @@ -149,4 +149,4 @@ var numSquares = function (n) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/j2dm9k.jpg) diff --git a/problems/283.move-zeroes.en.md b/problems/283.move-zeroes.en.md index 8ada58b94..947143cc4 100644 --- a/problems/283.move-zeroes.en.md +++ b/problems/283.move-zeroes.en.md @@ -146,4 +146,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/rd4o8s.jpg) diff --git a/problems/283.move-zeroes.md b/problems/283.move-zeroes.md index 268fbdcc4..e173445ea 100644 --- a/problems/283.move-zeroes.md +++ b/problems/283.move-zeroes.md @@ -145,4 +145,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vhvrtg.jpg) diff --git a/problems/29.divide-two-integers.md b/problems/29.divide-two-integers.md index 1c34c6164..282991272 100644 --- a/problems/29.divide-two-integers.md +++ b/problems/29.divide-two-integers.md @@ -63,7 +63,7 @@ return count; 这种做法简单直观,但是性能却比较差. 下面来介绍一种性能更好的方法。 -![29.divide-two-integers](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluakjnbkj30n20lbjss.jpg) +![29.divide-two-integers](https://p.ipic.vip/82bhio.jpg) 通过上面这样的分析,我们直到可以使用二分法来解决,性能有很大的提升。 @@ -230,4 +230,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/84mlor.jpg) diff --git a/problems/295.find-median-from-data-stream.md b/problems/295.find-median-from-data-stream.md index 4757b196d..f20a2e9a4 100644 --- a/problems/295.find-median-from-data-stream.md +++ b/problems/295.find-median-from-data-stream.md @@ -83,11 +83,11 @@ function findMedian(a) { 比如对于[1,2,3] 求中位数: -![295.find-median-from-data-stream-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty0myeij30n0064t8x.jpg) +![295.find-median-from-data-stream-1](https://p.ipic.vip/o7xgjv.jpg) 再比如对于[1,2,3, 4] 求中位数: -![295.find-median-from-data-stream-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty1ld04j30mx06ljrm.jpg) +![295.find-median-from-data-stream-2](https://p.ipic.vip/94jy7y.jpg) ## 关键点解析 @@ -219,7 +219,7 @@ this.heap.shift(null); 其实就是为了存储的数据从 1 开始,这样方便计算。 即对于下标为 i 的元素, `i >> 1` 一定是父节点的下标。 -![295.find-median-from-data-stream-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty4xqrrj30n706z3yu.jpg) +![295.find-median-from-data-stream-3](https://p.ipic.vip/vfni9p.jpg) > 这是因为我用满二叉树来存储的堆 diff --git a/problems/297.serialize-and-deserialize-binary-tree.md b/problems/297.serialize-and-deserialize-binary-tree.md index 705da18fc..6443f50b1 100644 --- a/problems/297.serialize-and-deserialize-binary-tree.md +++ b/problems/297.serialize-and-deserialize-binary-tree.md @@ -68,7 +68,7 @@ class Codec: 如果我用一个箭头表示节点的父子关系,箭头指向节点的两个子节点,那么大概是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqb8mcsv7j31z60sggrm.jpg) +![](https://p.ipic.vip/bmlx4h.jpg) 我们刚才提到了: @@ -107,13 +107,13 @@ class Codec: 但是上面的代码是不对的,因为我们序列化的时候其实不是完全二叉树,这也是上面我埋下的伏笔。因此遇到类似这样的 case 就会挂: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqcfujvv4j315s0u078j.jpg) +![](https://p.ipic.vip/i22124.jpg) 这也是我前面说”上面代码的序列化并不是一颗完全二叉树“的原因。 其实这个很好解决, 核心还是上面我画的那种图: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqb8mcsv7j31z60sggrm.jpg) +![](https://p.ipic.vip/bmlx4h.jpg) 其实我们可以: diff --git a/problems/3.longest-substring-without-repeating-characters.md b/problems/3.longest-substring-without-repeating-characters.md index f81e4f104..3a53e7791 100644 --- a/problems/3.longest-substring-without-repeating-characters.md +++ b/problems/3.longest-substring-without-repeating-characters.md @@ -55,7 +55,7 @@ https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 5. 最后返回 res 即可; -![3.longestSubstringWithoutRepeatingCharacters](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubou8hhg30no0dbjvw.gif) +![3.longestSubstringWithoutRepeatingCharacters](https://p.ipic.vip/i2ybbf.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) @@ -153,4 +153,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/lv54lv.jpg) diff --git a/problems/30.substring-with-concatenation-of-all-words.md b/problems/30.substring-with-concatenation-of-all-words.md index c7df34e56..d3879907c 100644 --- a/problems/30.substring-with-concatenation-of-all-words.md +++ b/problems/30.substring-with-concatenation-of-all-words.md @@ -135,4 +135,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/bbl6tw.jpg) diff --git a/problems/301.remove-invalid-parentheses.md b/problems/301.remove-invalid-parentheses.md index d8cf7fda0..fca3ba6fb 100644 --- a/problems/301.remove-invalid-parentheses.md +++ b/problems/301.remove-invalid-parentheses.md @@ -44,7 +44,7 @@ https://leetcode-cn.com/problems/remove-invalid-parentheses/ 而且由于题目要求是要删除最少的小括号,因此我们的思路是使用广度优先遍历,而不是深度有限的遍历。 -![301.remove-invalid-parentheses](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlude6ok2j30s90fctaa.jpg) +![301.remove-invalid-parentheses](https://p.ipic.vip/sm267s.jpg) > 没有动图,请脑补 diff --git a/problems/31.next-permutation.md b/problems/31.next-permutation.md index e574f55e8..198d3ba24 100644 --- a/problems/31.next-permutation.md +++ b/problems/31.next-permutation.md @@ -37,11 +37,11 @@ https://leetcode-cn.com/problems/next-permutation/ 让我们先回溯一次,即思考最后一个数字是如何被添加的。 -![31.next-permutation-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4tmf9vj30d204r74f.jpg) +![31.next-permutation-2](https://p.ipic.vip/h1ecnu.jpg) 由于这个时候可以选择的元素只有 2,我们无法组成更大的排列,我们继续回溯,直到如图: -![31.next-permutation-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4ukjgej30go07imxq.jpg) +![31.next-permutation-3](https://p.ipic.vip/otz7zv.jpg) 我们发现我们可以交换 4 和 2 就会变小,因此我们不能进行交换。 @@ -52,7 +52,7 @@ https://leetcode-cn.com/problems/next-permutation/ 两种交换都能使得结果更大,但是 和 2 交换能够使得增值最小,也就是题目中的下一个更大的效果。因此我们 1 和 2 进行交换。 -![31.next-permutation-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4vhrisj30h00cmwfn.jpg) +![31.next-permutation-4](https://p.ipic.vip/ddqcg7.jpg) 还需要继续往高位看么?不需要,因为交换高位得到的增幅一定比交换低位大,这是一个贪心的思想。 diff --git a/problems/312.burst-balloons.md b/problems/312.burst-balloons.md index c64abec70..1e54bfc41 100644 --- a/problems/312.burst-balloons.md +++ b/problems/312.burst-balloons.md @@ -115,7 +115,7 @@ var maxCoins = function (nums) { 因为我们最终要求的答案是 dp[0][n + 1],就是戳破虚拟气球之间的所有气球获得的最大值。当 i == j 时,i 和 j 之间是没有气球的,所以枚举的状态很明显是 dp table 的左上部分,也就是 j 大于 i,如下图所示,只给出一部分方便思考。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwkpbhyj30lk0aoaa9.jpg) +![](https://p.ipic.vip/8ugnau.jpg) > 图有错误。图中 dp[k][i] 应该是 dp[i][k],dp[j][k] 应该是 dp[k][j] diff --git a/problems/32.longest-valid-parentheses.md b/problems/32.longest-valid-parentheses.md index 736216232..b9dc90d1b 100644 --- a/problems/32.longest-valid-parentheses.md +++ b/problems/32.longest-valid-parentheses.md @@ -290,7 +290,7 @@ s = '(())())' 4. 根据第 3 条规则来计算的话, 我们发现 dp[5]=0, dp[6]=2, 但是显然, dp[6]应该为 6 才对, 但是我们发现可以将"(())"和"()"进行拼接, 即: dp[i] += dp[i-dp[i]], 即: dp[6] = 2 + dp[6-2] = 2 + dp[4] = 6 根据以上规则, 我们求解 dp 数组的结果为: [0, 0, 0, 2, 4, 0, 6, 0], 其中最长有效括号对的长度为 6. 以下为图解: -![32.longest-valid-parentheses](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8oq5vxj30pn0cb0vo.jpg) +![32.longest-valid-parentheses](https://p.ipic.vip/u0te4a.jpg) ### 代码 @@ -365,4 +365,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/8hx4j5.jpg) diff --git a/problems/322.coin-change.md b/problems/322.coin-change.md index d2940c1d1..80c2db392 100644 --- a/problems/322.coin-change.md +++ b/problems/322.coin-change.md @@ -96,7 +96,7 @@ eg: 对于 [1,2,5] 组成 11 块 以 coins = [1,2,3], amount = 6 来说,我们可以画出如下的递归树。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1goil47ztakj31jk0nm791.jpg) +![](https://p.ipic.vip/3vjmts.jpg) (图片来自https://leetcode.com/problems/coin-change/solution/) diff --git a/problems/324.wiggle-sort-ii.md b/problems/324.wiggle-sort-ii.md index 406a7415c..f4bdae1d4 100644 --- a/problems/324.wiggle-sort-ii.md +++ b/problems/324.wiggle-sort-ii.md @@ -110,4 +110,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vwyqn5.jpg) diff --git a/problems/328.odd-even-linked-list.md b/problems/328.odd-even-linked-list.md index 2caaa2cd5..2ac336b9b 100644 --- a/problems/328.odd-even-linked-list.md +++ b/problems/328.odd-even-linked-list.md @@ -140,4 +140,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/f3xfkx.jpg) diff --git a/problems/33.search-in-rotated-sorted-array.md b/problems/33.search-in-rotated-sorted-array.md index 07cdc9487..7dd7e0ed0 100644 --- a/problems/33.search-in-rotated-sorted-array.md +++ b/problems/33.search-in-rotated-sorted-array.md @@ -73,9 +73,9 @@ nums 肯定会在某个点上旋转 我们以([6,7,8,1,2,3,4,5], 4)为例讲解一下: -![search-in-rotated-sorted-array-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucavmv8j30if0b03zp.jpg) +![search-in-rotated-sorted-array-1](https://p.ipic.vip/a1vhqv.jpg) -![search-in-rotated-sorted-array-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucdam5mj30gx0i2jt8.jpg) +![search-in-rotated-sorted-array-1](https://p.ipic.vip/s6qy3v.jpg) ## 关键点解析 @@ -179,4 +179,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/yvd35x.jpg) diff --git a/problems/330.patching-array.md b/problems/330.patching-array.md index 07c07e659..8ce234bfd 100644 --- a/problems/330.patching-array.md +++ b/problems/330.patching-array.md @@ -120,4 +120,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/s7ko8b.jpg) diff --git a/problems/331.verify-preorder-serialization-of-a-binary-tree.md b/problems/331.verify-preorder-serialization-of-a-binary-tree.md index dd4ef0c9c..013c48b22 100644 --- a/problems/331.verify-preorder-serialization-of-a-binary-tree.md +++ b/problems/331.verify-preorder-serialization-of-a-binary-tree.md @@ -118,4 +118,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/tyz0xf.jpg) diff --git a/problems/334.increasing-triplet-subsequence.md b/problems/334.increasing-triplet-subsequence.md index 1bce7a343..734d03985 100644 --- a/problems/334.increasing-triplet-subsequence.md +++ b/problems/334.increasing-triplet-subsequence.md @@ -43,7 +43,7 @@ https://leetcode-cn.com/problems/increasing-triplet-subsequence/ 因此我们的做法可以是从左到右依次遍历,然后维护三个变量,分别记录最小值,第二小值,第三小值。只要我们能够填满这三个变量就返回 true,否则返回 false。 -![334.increasing-triplet-subsequence](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu86293pj30n30jdabm.jpg) +![334.increasing-triplet-subsequence](https://p.ipic.vip/swvj6t.jpg) ## 关键点解析 @@ -104,4 +104,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/eyot5z.jpg) diff --git a/problems/335.self-crossing.md b/problems/335.self-crossing.md index 533d1ded9..852e0a262 100644 --- a/problems/335.self-crossing.md +++ b/problems/335.self-crossing.md @@ -58,24 +58,24 @@ https://leetcode-cn.com/problems/self-crossing/ 1. 我们画的圈不断增大。 2. 我们画的圈不断减少。 -![](https://pic.leetcode-cn.com/1635437888-QNrRzh-007S8ZIlly1ghltxh0sygj30te1dajvv.jpg) +![](https://p.ipic.vip/citpjp.jpg) (有没有感觉像迷宫?) 这样我们会发现,其实我们画最新一笔的时候,并不是之前画的所有的都需要考虑,我们只需要最近的几个就可以了,实际上是最近的五个,不过不知道也没关系,我们稍后会讲解。 -![](https://pic.leetcode-cn.com/1635437888-QiEWyD-007S8ZIlly1ghltxhyhumj30to0lamyt.jpg) +![](https://p.ipic.vip/w50xpw.jpg) 红色部分指的是我们需要考虑的,而剩余没有被红色标注的部分则无需考虑。不是因为我们无法与之相交,而是我们`一旦与之相交,则必然我们也一定会与红色标记部分相交`。 然而我们画的方向也是不用考虑的。比如我当前画的方向是从左到右,那和我画的方向是从上到下有区别么?在这里是没区别的,不信我帮你将上图顺时针旋转 90 度看一下: -![](https://pic.leetcode-cn.com/1635437888-phhprI-007S8ZIlly1ghltxjatzhj30mk1cwdk7.jpg) +![](https://p.ipic.vip/dpebpv.jpg) 方向对于我们考虑是否相交没有差别。 当我们仔细思考的时候,会发现其实相交的情况只有以下几种: -![](https://pic.leetcode-cn.com/1635437888-JuQzXp-007S8ZIlly1ghltxkbce9j30ro0o676d.jpg) +![](https://p.ipic.vip/5v5q7o.jpg) > 图有误,第一种和第二种是同一种情况,换个角度看一样了。文字解释和代码已经更正 @@ -134,4 +134,4 @@ class Solution: 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/johr3h.jpg) diff --git a/problems/337.house-robber-iii.md b/problems/337.house-robber-iii.md index eda57cc0d..e9acfbfe2 100644 --- a/problems/337.house-robber-iii.md +++ b/problems/337.house-robber-iii.md @@ -201,4 +201,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludu08hcj30p00dwt9t.jpg) +![](https://p.ipic.vip/9n0849.jpg) diff --git a/problems/342.power-of-four.en.md b/problems/342.power-of-four.en.md index b124d04e9..9e8a4d70c 100644 --- a/problems/342.power-of-four.en.md +++ b/problems/342.power-of-four.en.md @@ -45,7 +45,7 @@ But this question has a follow up: "Can you do it without loops/recursion”. Th Let's take a look at what the power of 4 looks like with a binary representation. -![263.342.power-of-four-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua1uaopj30j009iwf1.jpg) +![263.342.power-of-four-1](https://p.ipic.vip/kbm3oz.jpg) Found the law: The binary representation of a power of 4 means that the position of 1 is in the odd position (and not in the lowest position), and the other positions are 0. @@ -63,13 +63,13 @@ This can be used as a question of thinking, let's think about it. For the second point, we can take a special number. For this special number, the odd position is 1, and the even position is 0, and then with this special number `Sum", if it is equal to itself, then there is no doubt that this 1 is no longer in an even position, but must be in an odd position, because if it is in an even position, the result of "sum" is 0. The title requires that n is a 32-bit signed integer, so our special number should be `010101010101010101010101010101` (no need to count, a total of 32 digits). -![263.342.power-of-four-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua2pq5hj30fi0b0q41.jpg) +![263.342.power-of-four-2](https://p.ipic.vip/r7zocl.jpg) As shown in the figure above, 64 is summed with this special number, and the result is itself. 8 is the power of 2, but it is not the power of 4. The result of our search is 0. In order to reflect our own grid, we can use a calculator to find a number with a relatively high grid. Here I chose hexadecimal, and the result is `0x55555555`. -![263.342.power-of-four](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua3mzibj30b20d70ua.jpg) +![263.342.power-of-four](https://p.ipic.vip/h15420.jpg) See the code area below for the code. @@ -149,4 +149,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/dzt82z.jpg) diff --git a/problems/342.power-of-four.md b/problems/342.power-of-four.md index 89bc3d0f3..fa217bd64 100644 --- a/problems/342.power-of-four.md +++ b/problems/342.power-of-four.md @@ -44,7 +44,7 @@ return num == 1; 我们先来看下,4 的幂次方用 2 进制表示是什么样的. -![263.342.power-of-four-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua1uaopj30j009iwf1.jpg) +![263.342.power-of-four-1](https://p.ipic.vip/ntu60a.jpg) 发现规律: 4 的幂次方的二进制表示 1 的位置都是在奇数位(且不在最低位),其他位置都为 0 @@ -63,13 +63,13 @@ return num == 1; `求与`, 如果等于本身,那么毫无疑问,这个 1 不再偶数位置,一定在奇数位置,因为如果在偶数位置,`求与`的结果就是 0 了 题目要求 n 是 32 位有符号整形,那么我们的特殊数字就应该是`01010101010101010101010101010101`(不用数了,一共 32 位)。 -![263.342.power-of-four-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua2pq5hj30fi0b0q41.jpg) +![263.342.power-of-four-2](https://p.ipic.vip/nii9nv.jpg) 如上图,64 和这个特殊数字求与,得到的是本身。 8 是 2 的次方,但是不是 4 的次方,我们求与结果就是 0 了。 为了体现自己的逼格,我们可以使用计算器,来找一个逼格比较高的数字,这里我选了十六进制,结果是`0x55555555`。 -![263.342.power-of-four](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua3mzibj30b20d70ua.jpg) +![263.342.power-of-four](https://p.ipic.vip/nfdw3v.jpg) 代码见下方代码区。 @@ -149,4 +149,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6125vr.jpg) diff --git a/problems/343.integer-break.md b/problems/343.integer-break.md index 4ddad429c..d5a03dd45 100644 --- a/problems/343.integer-break.md +++ b/problems/343.integer-break.md @@ -69,10 +69,10 @@ Ok,下面来讲下`我是如何解这道题的`。 这道题抽象一下就是: 令: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludn2s3wj305o03cgle.jpg) +![](https://p.ipic.vip/8292qs.jpg) (图 1) 求: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludny0u2j305o036wea.jpg) +![](https://p.ipic.vip/5ouhce.jpg) (图 2) ## 第一直觉 @@ -90,7 +90,7 @@ Ok,下面来讲下`我是如何解这道题的`。 用数学公式表示就是: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludoulynj30co03ydfo.jpg) +![](https://p.ipic.vip/ahfho6.jpg) (图 3) 截止目前,是一点点数学 + 一点点递归,我们继续往下看。现在问题是不是就很简单啦?直接翻译图三为代码即可,我们来看下这个时候的代码: @@ -107,7 +107,7 @@ class Solution: 毫无疑问,超时了。原因很简单,就是算法中包含了太多的重复计算。如果经常看我的题解的话,这句话应该不陌生。我随便截一个我之前讲过这个知识点的图。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludph6m5j313p0u00we.jpg) +![](https://p.ipic.vip/s7ua7h.jpg) (图 4) > 原文链接:https://github.com/azl397985856/leetcode/blob/master/thinkings/dynamic-programming.md @@ -139,7 +139,7 @@ class Solution: 如图 4,我们的思考方式是从顶向下,这符合人们思考问题的方式。将其改造成如下图的自底向上方式就是动态规划。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludra48hj31eq0r0gp1.jpg) +![](https://p.ipic.vip/rus34y.jpg) (图 5) 现在再来看下文章开头的代码: diff --git a/problems/349.intersection-of-two-arrays.en.md b/problems/349.intersection-of-two-arrays.en.md index 02cddf5b1..9439a3f61 100644 --- a/problems/349.intersection-of-two-arrays.en.md +++ b/problems/349.intersection-of-two-arrays.en.md @@ -107,4 +107,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/i7vosc.jpg) diff --git a/problems/349.intersection-of-two-arrays.md b/problems/349.intersection-of-two-arrays.md index e4c77cdc7..e9742d515 100644 --- a/problems/349.intersection-of-two-arrays.md +++ b/problems/349.intersection-of-two-arrays.md @@ -107,4 +107,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/3yad4m.jpg) diff --git a/problems/365.water-and-jug-problem.md b/problems/365.water-and-jug-problem.md index 9759a4b11..b87f820b3 100644 --- a/problems/365.water-and-jug-problem.md +++ b/problems/365.water-and-jug-problem.md @@ -207,4 +207,4 @@ def GCD(a, b): 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/tgcuwh.jpg) diff --git a/problems/371.sum-of-two-integers.en.md b/problems/371.sum-of-two-integers.en.md index b6d9f0d26..6f1bb7e45 100644 --- a/problems/371.sum-of-two-integers.en.md +++ b/problems/371.sum-of-two-integers.en.md @@ -35,11 +35,11 @@ Addition and subtraction cannot be used to find addition. We can only think from Since "XOR" is `the same bit is 0, different bit is 1`, we can think of XOR as a kind of addition and subtraction without carry. -![371.sum-of-two-integers-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud9y5phj30eu0b8jro.jpg) +![371.sum-of-two-integers-1](https://p.ipic.vip/td5ndr.jpg) Since 'and`are`if all bits are 1, then bits are 1, otherwise bits are 0`, we can shift one bit to the left after the sum to indicate carry. -![371.sum-of-two-integers-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludauj6aj30ev0f00t5.jpg) +![371.sum-of-two-integers-2](https://p.ipic.vip/2fvfdy.jpg) Then we can solve the above two meta-calculations recursively. The end condition of recursion is that one of them is 0, and we return the other directly. @@ -146,4 +146,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/uus3jb.jpg) diff --git a/problems/371.sum-of-two-integers.md b/problems/371.sum-of-two-integers.md index 6b8cfd6cb..456ebac8c 100644 --- a/problems/371.sum-of-two-integers.md +++ b/problems/371.sum-of-two-integers.md @@ -35,11 +35,11 @@ https://leetcode-cn.com/problems/sum-of-two-integers/ 由于`异或`是`相同则位0,不同则位1`,因此我们可以把异或看成是一种不进位的加减法。 -![371.sum-of-two-integers-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud9y5phj30eu0b8jro.jpg) +![371.sum-of-two-integers-1](https://p.ipic.vip/ew4ycn.jpg) 由于`与`是`全部位1则位1,否则位0`,因此我们可以求与之后左移一位来表示进位。 -![371.sum-of-two-integers-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludauj6aj30ev0f00t5.jpg) +![371.sum-of-two-integers-2](https://p.ipic.vip/oaiu0w.jpg) 然后我们对上述两个元算结果递归求解即可。 递归的结束条件就是其中一个为 0,我们直接返回另一个。 @@ -146,4 +146,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vbjbs5.jpg) diff --git a/problems/378.kth-smallest-element-in-a-sorted-matrix.md b/problems/378.kth-smallest-element-in-a-sorted-matrix.md index 7025a4de5..f6a08376a 100644 --- a/problems/378.kth-smallest-element-in-a-sorted-matrix.md +++ b/problems/378.kth-smallest-element-in-a-sorted-matrix.md @@ -48,7 +48,7 @@ k = 8, 最普通的二分法是有序数组中查找指定值(或者说满足某个条件的值)这种思路比较直接,但是对于这道题目是二维矩阵,而不是一维数组,因此这种二分思想就行不通了。 -![378.kth-smallest-element-in-a-sorted-matrix-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyc87pwj30gb03u0sx.jpg) +![378.kth-smallest-element-in-a-sorted-matrix-1](https://p.ipic.vip/omwt5h.jpg) (普通的一维二分法) @@ -56,11 +56,11 @@ k = 8, - 我们能够找到矩阵中最大的元素(右下角)和最小的元素(左上角)。接下来我们可以求出**值的中间**,而不是上面那种普通二分法的索引的中间。 -![378.kth-smallest-element-in-a-sorted-matrix-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyd2629j30ch05faaa.jpg) +![378.kth-smallest-element-in-a-sorted-matrix-3](https://p.ipic.vip/zbw2k2.jpg) - 找到中间值之后,我们可以拿这个值去计算有多少元素是小于等于它的。具体方式就是比较行的最后一列,如果中间值比最后一列大,说明中间元素肯定大于这一行的所有元素。 否则我们从后往前遍历直到不大于。 -![378.kth-smallest-element-in-a-sorted-matrix-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyeslbij30by06awep.jpg) +![378.kth-smallest-element-in-a-sorted-matrix-2](https://p.ipic.vip/h86vm0.jpg) - 上一步我们会计算一个 count,我们拿这个 count 和 k 进行比较 @@ -76,7 +76,7 @@ k = 8, 整个计算过程是这样的: -![378.kth-smallest-element-in-a-sorted-matrix-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyfm2okj30je0fq0uf.jpg) +![378.kth-smallest-element-in-a-sorted-matrix-4](https://p.ipic.vip/792z0f.jpg) 这里有一个大家普遍都比较疑惑的点,就是“能够确保最终我们找到的元素一定在矩阵中么?” @@ -232,4 +232,4 @@ public: - [240.search-a-2-d-matrix-ii](./240.search-a-2-d-matrix-ii.md) -大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 47K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 47K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://p.ipic.vip/5nvmcp.jpg) diff --git a/problems/380.insert-delete-getrandom-o1.md b/problems/380.insert-delete-getrandom-o1.md index f86071330..703cf4d65 100644 --- a/problems/380.insert-delete-getrandom-o1.md +++ b/problems/380.insert-delete-getrandom-o1.md @@ -80,17 +80,17 @@ randomSet.getRandom(); 以依次【1,2,3,4】之后为初始状态,那么此时状态是这样的: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjm9sg9olj30pg11wwiu.jpg) +![](https://p.ipic.vip/0m8rj9.jpg) 而当要插入一个新的 5 的时候, 我们只需要分别向数组末尾和哈希表中插入这条记录即可。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjmanhni6j30ka126tdm.jpg) +![](https://p.ipic.vip/scno98.jpg) 而删除的时候稍微有一点复杂,我们需要交换需要删除的数和数组末尾,并约定数组末尾的 n 项是被删除过的。(其中 n 为删除次数) > 有没有像力扣的原题**删除重复数字**? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjmbib4v5j30z60u049j.jpg) +![](https://p.ipic.vip/nob4bk.jpg) ## 关键点解析 @@ -169,4 +169,4 @@ class RandomizedSet: 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7yk9v4j30p00dwt9t.jpg) +![](https://p.ipic.vip/plglu2.jpg) diff --git a/problems/385.mini-parser.md b/problems/385.mini-parser.md index a4799ea7a..6256d1bd5 100644 --- a/problems/385.mini-parser.md +++ b/problems/385.mini-parser.md @@ -174,4 +174,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/fq18d0.jpg) diff --git a/problems/39.combination-sum.md b/problems/39.combination-sum.md index 9837b114f..e39d19214 100644 --- a/problems/39.combination-sum.md +++ b/problems/39.combination-sum.md @@ -61,7 +61,7 @@ candidate 中的每个元素都是独一无二的。 我们先来看下通用解法的解题思路,我画了一张图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2o05lsj31190u0jw4.jpg) +![](https://p.ipic.vip/rqqh32.jpg) > 每一层灰色的部分,表示当前有哪些节点是可以选择的, 红色部分则是选择路径。1,2,3,4,5,6 则分别表示我们的 6 个子集。 diff --git a/problems/394.decode-string.md b/problems/394.decode-string.md index b74fd0a87..4ca3b1108 100644 --- a/problems/394.decode-string.md +++ b/problems/394.decode-string.md @@ -65,27 +65,27 @@ https://leetcode-cn.com/problems/decode-string/ 拿题目给的例子`s = "3[a2[c]]"` 来说: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfghoy69l3j30ga03g3yq.jpg) +![](https://p.ipic.vip/1v2ath.jpg) 在遇到 ` 】` 之前,我们不断执行压栈操作: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx294t8jj30720bi0sv.jpg) +![](https://p.ipic.vip/16mkot.jpg) 当遇到 `】`的时候,说明我们应该出栈了,不断出栈知道对应的`【`,这中间的就是 repeatStr。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx2hbfe4j30m20b274q.jpg) +![](https://p.ipic.vip/en4ews.jpg) 但是要重复几次呢? 我们需要继续出栈,直到非数字为止,这个数字我们记录为 repeatCount。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glwx2m76t2j30ur0akt9i.jpg) +![](https://p.ipic.vip/r0kuvi.jpg) 而最终的字符串就是 repeatCount 个 repeatStr 拼接的形式。 **并将其看成一个字母压入栈中**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfghxjk5ejj310g0dt41r.jpg) +![](https://p.ipic.vip/co3wz7.jpg) 继续,后面的逻辑是一样的: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfgi1jhwb3j30uv09q0vd.jpg) +![](https://p.ipic.vip/5rssug.jpg) (最终图) @@ -165,4 +165,4 @@ class Solution: 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/simhwk.jpg) diff --git a/problems/4.median-of-two-sorted-arrays.md b/problems/4.median-of-two-sorted-arrays.md index 0e7572ab9..7ae5dcc49 100644 --- a/problems/4.median-of-two-sorted-arrays.md +++ b/problems/4.median-of-two-sorted-arrays.md @@ -48,7 +48,7 @@ nums2 = [3, 4] 如下图: -![中位数概念](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyup7ixj310w0eote4.jpg) +![中位数概念](https://p.ipic.vip/y62nbx.jpg) 知道了概念,我们先来看下如何使用暴力法来解决。 @@ -68,7 +68,7 @@ nums2 = [3, 4] > 整个过程类似归并排序的合并过程 Merge 的过程如下图。 -![暴力法图解](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltywjka3j30sm13w4ba.jpg) +![暴力法图解](https://p.ipic.vip/xksgul.jpg) 时间复杂度和空间复杂度都是`O(m+n)`, 不符合题中给出`O(log(m+n))`时间复杂度的要求。 @@ -195,15 +195,15 @@ else: 比如对数组 A 的做 partition 的位置是区间`[0,m]` 如图: -![详细算法图解](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyypek2j30o6166qmc.jpg) +![详细算法图解](https://p.ipic.vip/og35ih.jpg) 下图给出几种不同情况的例子(注意但左边或者右边没有元素的时候,左边用`INF_MIN`,右边用`INF_MAX`表示左右的元素: -![实例解析](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyzwjqej31bo0rq1it.jpg) +![实例解析](https://p.ipic.vip/zinoty.jpg) 下图给出具体做的 partition 解题的例子步骤, -![更详细的实例解析](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltz2832yj30u011g7ru.jpg) +![更详细的实例解析](https://p.ipic.vip/rqfle1.jpg) 这个算法关键在于: @@ -377,4 +377,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/r8viss.jpg) diff --git a/problems/40.combination-sum-ii.md b/problems/40.combination-sum-ii.md index 87e924561..1b61c942f 100644 --- a/problems/40.combination-sum-ii.md +++ b/problems/40.combination-sum-ii.md @@ -54,7 +54,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 我们先来看下通用解法的解题思路,我画了一张图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2o05lsj31190u0jw4.jpg) +![](https://p.ipic.vip/uivnzh.jpg) > 每一层灰色的部分,表示当前有哪些节点是可以选择的, 红色部分则是选择路径。1,2,3,4,5,6 则分别表示我们的 6 个子集。 @@ -222,4 +222,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vyqn2v.jpg) diff --git a/problems/401.binary-watch.en.md b/problems/401.binary-watch.en.md index 1581654aa..d2f264b60 100644 --- a/problems/401.binary-watch.en.md +++ b/problems/401.binary-watch.en.md @@ -10,7 +10,7 @@ The binary watch has 4 LEDS on the top to represent the hour (0-11), and the 6 L Each LED represents a 0 or 1, and the lowest position is on the right. ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5szmnbinj31400u0tra.jpg) +![](https://p.ipic.vip/47z3vd.jpg) ``` For example, the binary watch above reads “3:25”. diff --git a/problems/401.binary-watch.md b/problems/401.binary-watch.md index b04c6bdc7..6172aa863 100644 --- a/problems/401.binary-watch.md +++ b/problems/401.binary-watch.md @@ -9,7 +9,7 @@ https://leetcode-cn.com/problems/binary-watch/ 每个 LED 代表一个 0 或 1,最低位在右侧。 ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5szmnbinj31400u0tra.jpg) +![](https://p.ipic.vip/tkf45f.jpg) ``` 例如,上面的二进制手表读取 “3:25”。 diff --git a/problems/416.partition-equal-subset-sum.md b/problems/416.partition-equal-subset-sum.md index f17540a5e..f5ca2a680 100644 --- a/problems/416.partition-equal-subset-sum.md +++ b/problems/416.partition-equal-subset-sum.md @@ -307,4 +307,4 @@ var change = function (amount, coins) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6icaaz.jpg) diff --git a/problems/42.trapping-rain-water.en.md b/problems/42.trapping-rain-water.en.md index 363e173c4..19a75336f 100755 --- a/problems/42.trapping-rain-water.en.md +++ b/problems/42.trapping-rain-water.en.md @@ -6,7 +6,7 @@ https://leetcode.com/problems/trapping-rain-water/description/ > Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. -![42.trapping-rain-water-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2p6pzfj30bg04hmx3.jpg) +![42.trapping-rain-water-1](https://p.ipic.vip/f2gqbu.jpg) > The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! @@ -225,4 +225,4 @@ For more solutions, visit my [LeetCode Solution Repo](https://github.com/azl3979 Follow my WeChat official account 力扣加加, which has lots of graphic solutions and teaches you how to recognize problem patterns to solve problems with efficiency. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu2wi41cj30p00dwt9t.jpg) +![](https://p.ipic.vip/w9d2t2.jpg) diff --git a/problems/42.trapping-rain-water.md b/problems/42.trapping-rain-water.md index f136004e5..040f663f5 100755 --- a/problems/42.trapping-rain-water.md +++ b/problems/42.trapping-rain-water.md @@ -10,7 +10,7 @@ https://leetcode-cn.com/problems/trapping-rain-water/ ``` -![42.trapping-rain-water-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8i4s97j30bg04hmx3.jpg) +![42.trapping-rain-water-1](https://p.ipic.vip/cghgbn.jpg) ``` 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。 @@ -318,4 +318,4 @@ class Solution 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8mu8kwj30p00dwt9t.jpg) +![](https://p.ipic.vip/ywgwz4.jpg) diff --git a/problems/424.longest-repeating-character-replacement.md b/problems/424.longest-repeating-character-replacement.md index d0d91c801..7b19f3937 100644 --- a/problems/424.longest-repeating-character-replacement.md +++ b/problems/424.longest-repeating-character-replacement.md @@ -133,4 +133,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/qceyie.jpg) diff --git a/problems/437.path-sum-iii.en.md b/problems/437.path-sum-iii.en.md index 80cbba137..c78ca09c7 100644 --- a/problems/437.path-sum-iii.en.md +++ b/problems/437.path-sum-iii.en.md @@ -98,11 +98,11 @@ In addition, I drew a picture, I believe you will understand after reading it. When we execute to the bottom: -![437.path-sum-iii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludenaf3j30l60cyta7.jpg) +![437.path-sum-iii](https://p.ipic.vip/ukku3e.jpg) Then go back up: -![437.path-sum-iii-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludf311tj30ii0bp0ty.jpg) +![437.path-sum-iii-2](https://p.ipic.vip/zl3kb7.jpg) It is easy to see that our hashmap should not have the record of the first picture, so it needs to be subtracted. @@ -171,4 +171,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vathfp.jpg) diff --git a/problems/437.path-sum-iii.md b/problems/437.path-sum-iii.md index 48f1eb45e..d1329eacc 100644 --- a/problems/437.path-sum-iii.md +++ b/problems/437.path-sum-iii.md @@ -100,11 +100,11 @@ var pathSum = function (root, sum) { 当我们执行到底部的时候: -![437.path-sum-iii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludenaf3j30l60cyta7.jpg) +![437.path-sum-iii](https://p.ipic.vip/qd3vcn.jpg) 接着往上回溯: -![437.path-sum-iii-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludf311tj30ii0bp0ty.jpg) +![437.path-sum-iii-2](https://p.ipic.vip/3xnb5f.jpg) 很容易看出,我们的 hashmap 不应该有第一张图的那个记录了,因此需要减去。 @@ -207,4 +207,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/7v768z.jpg) diff --git a/problems/445.add-two-numbers-ii.md b/problems/445.add-two-numbers-ii.md index b245dcb7f..cf558987a 100644 --- a/problems/445.add-two-numbers-ii.md +++ b/problems/445.add-two-numbers-ii.md @@ -283,4 +283,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/f0dl5y.jpg) diff --git a/problems/454.4-Sum-ii.en.md b/problems/454.4-Sum-ii.en.md index 4d5144ac2..4a5b21d8a 100644 --- a/problems/454.4-Sum-ii.en.md +++ b/problems/454.4-Sum-ii.en.md @@ -34,7 +34,7 @@ My idea is to separate these four lists into two groups and combine them two by As the picture shows: -![454.4-sum-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5lpuodj30le0deab3.jpg) +![454.4-sum-ii](https://p.ipic.vip/c8uc1i.jpg) Now that we got two `hashTable`, and the result would appear with some basic calculations. diff --git a/problems/454.4-sum-ii.md b/problems/454.4-sum-ii.md index 4b2fd033a..e9a7e7e1c 100644 --- a/problems/454.4-sum-ii.md +++ b/problems/454.4-sum-ii.md @@ -46,7 +46,7 @@ D = [ 0, 2] 如图: -![454.4-sum-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludk9vnxj30le0deab3.jpg) +![454.4-sum-ii](https://p.ipic.vip/4zdbc1.jpg) 这个时候我们得到了两个`hashTable`, 我们只需要进行简单的数学运算就可以得到结果。 @@ -118,4 +118,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/pj7k2p.jpg) diff --git a/problems/455.AssignCookies.en.md b/problems/455.AssignCookies.en.md index 0d23597aa..e01ec724e 100644 --- a/problems/455.AssignCookies.en.md +++ b/problems/455.AssignCookies.en.md @@ -101,4 +101,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/z49yum.jpg) diff --git a/problems/455.AssignCookies.md b/problems/455.AssignCookies.md index 00b7fc194..687e51e07 100644 --- a/problems/455.AssignCookies.md +++ b/problems/455.AssignCookies.md @@ -116,5 +116,5 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/p9c84i.jpg) diff --git a/problems/456.132-pattern.md b/problems/456.132-pattern.md index 3b59b5686..4a30771d2 100644 --- a/problems/456.132-pattern.md +++ b/problems/456.132-pattern.md @@ -109,4 +109,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/2xb5cd.jpg) diff --git a/problems/457.circular-array-loop.md b/problems/457.circular-array-loop.md index cad9a838e..33a17a3ab 100644 --- a/problems/457.circular-array-loop.md +++ b/problems/457.circular-array-loop.md @@ -216,4 +216,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/qn10tk.jpg) diff --git a/problems/460.lfu-cache.md b/problems/460.lfu-cache.md index 7ebc66998..09faa2fb0 100644 --- a/problems/460.lfu-cache.md +++ b/problems/460.lfu-cache.md @@ -69,7 +69,7 @@ cache.get(4); // 返回 4 没有就新建 doublylinkedlist(head, tail), 把 node1 插入 doublylinkedlist head->next = node1. 如下图, ``` -![460.lfu-cache-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4z6wr1j314x0u0q61.jpg) +![460.lfu-cache-1](https://p.ipic.vip/5keys7.jpg) ``` 2. put(2, 2), - 首先查找 nodeMap 中有没有 key=2 对应的 value, @@ -78,7 +78,7 @@ cache.get(4); // 返回 4 没有就新建 doublylinkedlist(head, tail), 把 node2 插入 doublylinkedlist head->next = node2. 如下图, ``` -![460.lfu-cache-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4zqh7wj314k0u0adh.jpg) +![460.lfu-cache-2](https://p.ipic.vip/e1l43k.jpg) ``` 3. get(1), - 首先查找 nodeMap 中有没有 key=1 对应的 value,nodeMap:{[1, node1], [2, node2]}, @@ -87,7 +87,7 @@ cache.get(4); // 返回 4 - 更新 freqMap,插入 freq=2,node1 如下图, ``` -![460.lfu-cache-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu53jotrj313p0u0tdi.jpg) +![460.lfu-cache-3](https://p.ipic.vip/pgt0c5.jpg) ``` 4. put(3, 3), - 判断 cache 的 capacity,已满,需要淘汰使用次数最少的元素,找到最小的 freq=1,删除双链表 tail node.prev @@ -98,7 +98,7 @@ cache.get(4); // 返回 4 没有就新建 doublylinkedlist(head, tail), 把 node3 插入 doublylinkedlist head->next = node3. 如下图, ``` -![460.lfu-cache-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu54ffzrj313l0qwdkf.jpg) +![460.lfu-cache-4](https://p.ipic.vip/qh7eg2.jpg) ``` 5. get(2) - 查找 nodeMap,如果没有对应的 key 的 value,返回 -1。 @@ -110,7 +110,7 @@ cache.get(4); // 返回 4 - 更新 freqMap,插入 freq=2,node3 如下图, ``` -![460.lfu-cache-5](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu54r66gj31380r1af4.jpg) +![460.lfu-cache-5](https://p.ipic.vip/i18s52.jpg) ``` 7. put(4, 4), - 判断 cache 的 capacity,已满,需要淘汰使用次数最少的元素,找到最小的 freq=1,删除双链表 tail node.prev @@ -121,7 +121,7 @@ cache.get(4); // 返回 4 没有就新建 doublylinkedlist(head, tail), 把 node4 插入 doublylinkedlist head->next = node4. 如下图, ``` -![460.lfu-cache-6](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu558d63j317s0trgrk.jpg) +![460.lfu-cache-6](https://p.ipic.vip/0mz6kw.jpg) ``` 8. get(1) - 查找 nodeMap,如果没有对应的 key 的 value,返回 -1。 @@ -133,7 +133,7 @@ cache.get(4); // 返回 4 - 更新 freqMap,插入 freq=3,node3 如下图, ``` -![460.lfu-cache-7](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu56bzvhj313u0u0q7w.jpg) +![460.lfu-cache-7](https://p.ipic.vip/2pter5.jpg) ``` 10. get(4) - 首先查找 nodeMap 中有没有 key=4 对应的 value,nodeMap:{[4, node4], [3, node3]}, @@ -142,7 +142,7 @@ cache.get(4); // 返回 4 - 更新 freqMap,插入 freq=2,node4 如下图, ``` -![460.lfu-cache-8](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu57axl0j314y0tc45n.jpg) +![460.lfu-cache-8](https://p.ipic.vip/u6reol.jpg) ## 关键点分析 用两个`Map`分别保存 `nodeMap {key, node}` 和 `freqMap{frequent, DoublyLinkedList}`。 diff --git a/problems/464.can-i-win.md b/problems/464.can-i-win.md index a62e0fff5..933852809 100644 --- a/problems/464.can-i-win.md +++ b/problems/464.can-i-win.md @@ -74,7 +74,7 @@ def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool: 为了方便大家理解,我画了一个逻辑树: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glr0529zbcj30nf0d0my8.jpg) +![](https://p.ipic.vip/vqo4yw.jpg) 接下来,我们写代码遍历这棵树即可。 @@ -108,11 +108,11 @@ class Solution: 如果使用值传递,对应是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glr0k15pucj30pi0fugnn.jpg) +![](https://p.ipic.vip/yi6r3v.jpg) 如果在每次递归返回的是时候主动回溯状态,对应是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glr0lavj37j30oa0gzjtp.jpg) +![](https://p.ipic.vip/d0hiks.jpg) 注意图上的蓝色的新增的线,他们表示递归返回的过程。我们需要在返回的过程**撤销选择**。比如我选了数组 2, 递归返回的时候再把数字 2 从 set 中移除。 @@ -169,7 +169,7 @@ class Solution: 如下图,两个 set 应该一样,但是遍历的结果顺序可能不同,如果不排序就可能有错误。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glr1bbec2bj30jn07vdgm.jpg) +![](https://p.ipic.vip/xinbk3.jpg) 至此,问题的关键基本上锁定为找到一个**可以序列化且容量大大减少的数据结构**来存是不是就可行了? @@ -196,7 +196,7 @@ class Solution: 这个不难。 比如我要模拟 picked.add(n),只要将 picked 第 n 为置为 1 就行。也就是说 1 表示在集合中,0 表示不在。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glr1mim1poj30pl04ujrw.jpg) +![](https://p.ipic.vip/s4a6v5.jpg) 使用**或运算和位移运算**可以很好的完成这个需求。 @@ -208,7 +208,7 @@ class Solution: 指的是 1 的二进制表示全体左移 a 位, 右移也是同理 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glr1paz9e7j31bo0f40uz.jpg) +![](https://p.ipic.vip/3egnrr.jpg) **| 操作** @@ -424,4 +424,4 @@ var canIWin = function (maxChoosableInteger, desiredTotal) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/21t1qb.jpg) diff --git a/problems/470.implement-rand10-using-rand7.md b/problems/470.implement-rand10-using-rand7.md index 126e9e71e..e4ade9f61 100644 --- a/problems/470.implement-rand10-using-rand7.md +++ b/problems/470.implement-rand10-using-rand7.md @@ -121,4 +121,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/rw499k.jpg) diff --git a/problems/472.concatenated-words.md b/problems/472.concatenated-words.md index bc08f15ff..6aac0f379 100644 --- a/problems/472.concatenated-words.md +++ b/problems/472.concatenated-words.md @@ -48,7 +48,7 @@ https://leetcode-cn.com/problems/concatenated-words/ 我们构造的前缀树大概是这样的: -![472.concatenated-words.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluaqn7pmj310g0u0wj5.jpg) +![472.concatenated-words.png](https://p.ipic.vip/5dsmsk.jpg) 问题的关键在于第二步中的**查找每一个单词有几个单词表中的单词组成**。 其实如果你了解前缀树的话应该不难写出来。 比如查找 catsdogcats: @@ -151,4 +151,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/hbhj4l.jpg) diff --git a/problems/473.matchsticks-to-square.md b/problems/473.matchsticks-to-square.md index 981607085..420b27f32 100644 --- a/problems/473.matchsticks-to-square.md +++ b/problems/473.matchsticks-to-square.md @@ -113,4 +113,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/dvadiy.jpg) diff --git a/problems/474.ones-and-zeros-en.md b/problems/474.ones-and-zeros-en.md index 43036fa35..22de58944 100644 --- a/problems/474.ones-and-zeros-en.md +++ b/problems/474.ones-and-zeros-en.md @@ -112,7 +112,7 @@ DP formula: For example: -![ones and zeros 2d dp](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxrhvyrj31400u0h2m.jpg) +![ones and zeros 2d dp](https://p.ipic.vip/nfeo4u.jpg) #### - *Time Complexity:* `O(l * m * n) - l is strs length,m is number of 0,n number of 1` diff --git a/problems/48.rotate-image.md b/problems/48.rotate-image.md index 3b17fdee7..0fd391d6e 100644 --- a/problems/48.rotate-image.md +++ b/problems/48.rotate-image.md @@ -66,7 +66,7 @@ https://leetcode-cn.com/problems/rotate-image/ 通过观察发现,我们只需要将第 i 行变成第 n - i - 1 列, 因此我们只需要保存一个原有矩阵,然后按照这个规律一个个更新即可。 -![48.rotate-image-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlty9mstdj30n0081q36.jpg) +![48.rotate-image-1](https://p.ipic.vip/h3kw2a.jpg) 代码: @@ -90,7 +90,7 @@ var rotate = function (matrix) { 事实上有一个更加巧妙的做法,我们可以巧妙地利用对称轴旋转达到我们的目的,如图,我们先进行一次以对角线为轴的翻转,然后 再进行一次以水平轴心线为轴的翻转即可。 -![48.rotate-image-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyaj6f1j30my0aegma.jpg) +![48.rotate-image-2](https://p.ipic.vip/b57zdr.jpg) 这种做法的时间复杂度是 O(n^2) ,空间复杂度是 O(1) @@ -188,4 +188,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/nkh04i.jpg) diff --git a/problems/480.sliding-window-median.md b/problems/480.sliding-window-median.md index 00ba76cc0..61ac43d0e 100644 --- a/problems/480.sliding-window-median.md +++ b/problems/480.sliding-window-median.md @@ -101,4 +101,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/kx3tot.jpg) diff --git a/problems/488.zuma-game.md b/problems/488.zuma-game.md index fd256bf25..f2ce00ac7 100644 --- a/problems/488.zuma-game.md +++ b/problems/488.zuma-game.md @@ -64,13 +64,13 @@ https://leetcode-cn.com/problems/zuma-game/ 因此我们只需要两个指针记录连续相同颜色球的位置,如果可以消除,消除即可。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjfehgw7lnj31880fydkr.jpg) +![](https://p.ipic.vip/j2f0e1.jpg) 如图,我们记录了连续红球的位置, 如果手上有红球, 则可以尝试将其清除,这一次决策就是回溯树(决策树)的一个分支。之后我们会撤回到这个决策分支, 尝试其他可行的决策分支。 以 board = RRBBRR , hand 为 RRBB 为例,其决策树为: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjfg7kykx3j30u00wc46o.jpg) +![](https://p.ipic.vip/wu8d71.jpg) 其中虚线表示无需手动干预,系统自动消除。叶子节点末尾的黄色表示全部消除需要的手球个数。路径上的文字后面的数字表示此次消除需要的手球个数 @@ -91,7 +91,7 @@ while i < len(board): i = j ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjfegz0iwvj316e0my43t.jpg) +![](https://p.ipic.vip/e1ix28.jpg) 具体算法: @@ -148,4 +148,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 36K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/sepme7.jpg) diff --git a/problems/49.group-anagrams.md b/problems/49.group-anagrams.md index 074481aa7..86af55a9a 100644 --- a/problems/49.group-anagrams.md +++ b/problems/49.group-anagrams.md @@ -70,7 +70,7 @@ var groupAnagrams = function (strs) { 然后我们给每一个字符一个固定的数组下标,然后我们只需要更新每个字符出现的次数。 最后形成的 counts 数组如果一致,则说明他们可以通过 交换顺序得到。这种算法空间复杂度 O(n), 时间复杂度 O(n \* k), n 为数组长度,k 为字符串的平均长度. -![49.group-anagrams](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlubhv58qj30n209474l.jpg) +![49.group-anagrams](https://p.ipic.vip/c8c462.jpg) 实际上,这就是桶排序的基本思想。 很多题目都用到了这种思想, 读者可以留心一下。 @@ -168,4 +168,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/jpeo9h.jpg) diff --git a/problems/493.reverse-pairs.md b/problems/493.reverse-pairs.md index cdd8d5dc1..adb5ef743 100644 --- a/problems/493.reverse-pairs.md +++ b/problems/493.reverse-pairs.md @@ -123,7 +123,7 @@ class Solution(object): 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/kklv2v.jpg) 对于具体的排序过程我们偷懒直接使用了语言内置的方法 sorted,这在很多时候是有用的,即使你是参加面试,这种方式通常也是允许的。省略非核心的考点,可以使得问题更加聚焦,这是一种解决问题的思路,在工作中也很有用。 diff --git a/problems/494.target-sum.md b/problems/494.target-sum.md index ae72ae2ee..905ea1efd 100644 --- a/problems/494.target-sum.md +++ b/problems/494.target-sum.md @@ -49,13 +49,13 @@ https://leetcode-cn.com/problems/target-sum/ 题目是给定一个数组,让你在数字前面添加 `+`或者`-`,使其和等于 target. -![494.target-sum](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzih10wj30is07ojrv.jpg) +![494.target-sum](https://p.ipic.vip/1e3oiz.jpg) 暴力法的时间复杂度是指数级别的,因此我们不予考虑。我们需要换种思路. 我们将最终的结果分成两组,一组是我们添加了`+`的,一组是我们添加了`-`的。 -![494.target-sum-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzpo2ptj30mk05ijrp.jpg) +![494.target-sum-2](https://p.ipic.vip/fq8fcg.jpg) 如上图,问题转化为如何求其中一组,我们不妨求前面标`+`的一组 @@ -63,7 +63,7 @@ https://leetcode-cn.com/problems/target-sum/ 通过进一步分析,我们得到了这样的关系: -![494.target-sum-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltzz1l61j30ks06xwfi.jpg) +![494.target-sum-3](https://p.ipic.vip/0rs2q3.jpg) 因此问题转化为,求解`sumCount(nums, target)`,即 nums 数组中能够组成 target 的总数一共有多少种,这是一道我们之前做过的题目,使用动态规划可以解决。 @@ -117,7 +117,7 @@ var findTargetSumWays = function (nums, S) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/rruyyb.jpg) ## 扩展 diff --git a/problems/5.longest-palindromic-substring.md b/problems/5.longest-palindromic-substring.md index b21b1ae64..79c592afc 100644 --- a/problems/5.longest-palindromic-substring.md +++ b/problems/5.longest-palindromic-substring.md @@ -30,15 +30,15 @@ https://leetcode-cn.com/problems/longest-palindromic-substring/ 这是一道最长回文的题目,要我们求出给定字符串的最大回文子串。 -![5.longest-palindromic-substring](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluamgzr3j30c10690sv.jpg) +![5.longest-palindromic-substring](https://p.ipic.vip/x26vx1.jpg) 解决这类问题的核心思想就是两个字“延伸”,具体来说**如果在一个不是回文字符串的字符串两端添加任何字符,或者在回文串左右分别加不同的字符,得到的一定不是回文串** -![5.longest-palindromic-substring-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluanbu9aj30fy07b3yt.jpg) +![5.longest-palindromic-substring-2](https://p.ipic.vip/3mt0s7.jpg) base case 就是一个字符(轴对称点是本身),或者两个字符(轴对称点是介于两者之间的虚拟点)。 -![5.longest-palindromic-substring-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluanwnirj30eh09l3yt.jpg) +![5.longest-palindromic-substring-3](https://p.ipic.vip/6je4r5.jpg) 事实上,上面的分析已经建立了大问题和小问题之间的关联,基于此,我们可以建立动态规划模型。 @@ -168,4 +168,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/mfppv5.jpg) diff --git a/problems/50.pow-x-n.md b/problems/50.pow-x-n.md index 7714612af..c156cac13 100644 --- a/problems/50.pow-x-n.md +++ b/problems/50.pow-x-n.md @@ -172,9 +172,9 @@ public: 以 x 的 10 次方举例。10 的 2 进制是 1010,然后用 2 进制转 10 进制的方法把它展成 2 的幂次的和。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwqu7tfj30t802mq2z.jpg) +![](https://p.ipic.vip/rlvci4.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwspa8lj30xp0u0dj7.jpg) +![](https://p.ipic.vip/25n2y0.jpg) 因此我们的算法就是: @@ -183,7 +183,7 @@ public: - 将 n 的二进制表示中`1的位置`pick 出来。比如 n 的第 i 位为 1,那么就将 x^i pick 出来。 - 将 pick 出来的结果相乘 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwt3awfj30vq0hcabp.jpg) +![](https://p.ipic.vip/09kvxz.jpg) 这里有两个问题: @@ -230,8 +230,8 @@ class Solution: - [458.可怜的小猪](https://leetcode-cn.com/problems/poor-pigs/description/) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwu19yqj30wn0u0abv.jpg) +![](https://p.ipic.vip/izl7mu.jpg) 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/3p3tiz.jpg) diff --git a/problems/504.base-7.md b/problems/504.base-7.md index acc609018..29fe82aa4 100644 --- a/problems/504.base-7.md +++ b/problems/504.base-7.md @@ -43,7 +43,7 @@ https://leetcode-cn.com/problems/base-7/ 将此过冲的余数**反序**就是答案了。图解: -![](https://tva1.sinaimg.cn/large/008eGmZEly1goaco026g7j30pe0zb40r.jpg) +![](https://p.ipic.vip/pd45gi.jpg) (图片来自网络) 如图,4312 的 7 进制就是 15400。 @@ -112,4 +112,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ufnthm.jpg) diff --git a/problems/516.longest-palindromic-subsequence.md b/problems/516.longest-palindromic-subsequence.md index 82e36da08..d0b6d25f9 100644 --- a/problems/516.longest-palindromic-subsequence.md +++ b/problems/516.longest-palindromic-subsequence.md @@ -52,14 +52,14 @@ s 只包含小写英文字母 这是一道最长回文的题目,要我们求出给定字符串的最大回文子序列。 -![516.longest-palindromic-subsequence-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltykreoxj30de06ct8w.jpg) +![516.longest-palindromic-subsequence-1](https://p.ipic.vip/ohyv8s.jpg) 解决这类问题的核心思想就是两个字“延伸”,具体来说 - 如果一个字符串是回文串,那么在它左右分别加上一个相同的字符,那么它一定还是一个回文串,因此`回文长度增加2` - 如果一个字符串不是回文串,或者在回文串左右分别加不同的字符,得到的一定不是回文串,因此`回文长度不变,我们取[i][j-1]和[i+1][j]的较大值` -![516.longest-palindromic-subsequence-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyldw9mj30eb09hq3a.jpg) +![516.longest-palindromic-subsequence-2](https://p.ipic.vip/xkfnid.jpg) 事实上,上面的分析已经建立了大问题和小问题之间的关联, 基于此,我们可以建立动态规划模型。 @@ -77,7 +77,7 @@ if (s[i] === s[j]) { base case 就是一个字符(轴对称点是本身) -![516.longest-palindromic-subsequence-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltylrdezj30bk051dfq.jpg) +![516.longest-palindromic-subsequence-3](https://p.ipic.vip/y896jj.jpg) ## 关键点 @@ -166,4 +166,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/c0d75t.jpg) diff --git a/problems/518.coin-change-2.md b/problems/518.coin-change-2.md index 372f7a1cb..5711d8c04 100644 --- a/problems/518.coin-change-2.md +++ b/problems/518.coin-change-2.md @@ -88,7 +88,7 @@ return dp[dp.length - 1][coins.length]; - 当我们选择一维数组去解的时候,内外循环将会对结果造成影响 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluafxrm4j30j00bdmxx.jpg) +![](https://p.ipic.vip/sdvm3a.jpg) eg: @@ -210,4 +210,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vldrtr.jpg) diff --git a/problems/52.N-Queens-II.md b/problems/52.N-Queens-II.md index 53ee16617..6918e57ba 100644 --- a/problems/52.N-Queens-II.md +++ b/problems/52.N-Queens-II.md @@ -5,7 +5,7 @@ https://leetcode-cn.com/problems/n-queens-ii/ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 -![image.png](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/8-queens.png) +![image.png](https://p.ipic.vip/vnynhl.png) ``` @@ -99,4 +99,4 @@ const totalNQueens = function (n) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/nz33zy.jpg) diff --git a/problems/525.contiguous-array.md b/problems/525.contiguous-array.md index 137bcb732..3bfb8cae3 100644 --- a/problems/525.contiguous-array.md +++ b/problems/525.contiguous-array.md @@ -85,4 +85,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/s53udc.jpg) diff --git a/problems/5254.selling-pieces-of-wood.md b/problems/5254.selling-pieces-of-wood.md index 24c539055..8586c58b7 100644 --- a/problems/5254.selling-pieces-of-wood.md +++ b/problems/5254.selling-pieces-of-wood.md @@ -128,4 +128,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/m9itjv.jpg) diff --git a/problems/53.maximum-sum-subarray-cn.en.md b/problems/53.maximum-sum-subarray-cn.en.md index 77f1b530f..74687e69f 100644 --- a/problems/53.maximum-sum-subarray-cn.en.md +++ b/problems/53.maximum-sum-subarray-cn.en.md @@ -99,7 +99,7 @@ There are three situations where the position of the largest sub-sequence sum is The sum of the largest sub-sequences in the three cases is obtained separately, and the largest value of the three is the sum of the largest sub-sequences. For example, as shown in the figure below: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwlo6jpj31400u0acg.jpg) +![](https://p.ipic.vip/8i530l.jpg) **Complexity analysis** @@ -127,7 +127,7 @@ From the state transition equation, we only focus on the value of the previous s - `maxSum = max(currMaxSum, maxSum)` As shown in the figure: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwmokuuj30pj0h20te.jpg) +![](https://p.ipic.vip/1l599b.jpg) **Complexity analysis** diff --git a/problems/53.maximum-sum-subarray-cn.md b/problems/53.maximum-sum-subarray-cn.md index aa6bd2562..ef0e3e20a 100644 --- a/problems/53.maximum-sum-subarray-cn.md +++ b/problems/53.maximum-sum-subarray-cn.md @@ -100,7 +100,7 @@ https://leetcode-cn.com/problems/maximum-subarray/ 分别求出三种情况下最大子序列和,三者中最大值即为最大子序列和。 举例说明,如下图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwlo6jpj31400u0acg.jpg) +![](https://p.ipic.vip/kg4yvp.jpg) **复杂度分析** @@ -128,7 +128,7 @@ https://leetcode-cn.com/problems/maximum-subarray/ - `maxSum = max(currMaxSum, maxSum)` 如图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwmokuuj30pj0h20te.jpg) +![](https://p.ipic.vip/f5g6y3.jpg) **复杂度分析** @@ -415,4 +415,4 @@ function LSS(list) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/w6dpse.jpg) diff --git a/problems/53.maximum-sum-subarray-en.md b/problems/53.maximum-sum-subarray-en.md index 029d7ff4e..d3dd9acac 100644 --- a/problems/53.maximum-sum-subarray-en.md +++ b/problems/53.maximum-sum-subarray-en.md @@ -91,7 +91,7 @@ The maximum sum is `max(left, right, crossMaxSum)` For example, `nums=[-2,1,-3,4,-1,2,1,-5,4]` -![maximum subarray sum divide conquer](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxdvaw8j31400u07ps.jpg) +![maximum subarray sum divide conquer](https://p.ipic.vip/5rhfk8.jpg) #### Complexity Analysis @@ -118,7 +118,7 @@ From above DP formula, notice only need to access its previous element at each s - `maxSum = max(currMaxSum, maxSum)` As below pic: -![maximum subarray sum dp](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxeq0b6j30pj0h2abm.jpg) +![maximum subarray sum dp](https://p.ipic.vip/3cmh1k.jpg) #### Complexity Analysis - *Time Complexity:* `O(n) - n array length` diff --git a/problems/547.friend-circles-en.md b/problems/547.friend-circles-en.md index dfe634bc2..ed28b495f 100644 --- a/problems/547.friend-circles-en.md +++ b/problems/547.friend-circles-en.md @@ -43,7 +43,7 @@ this problem become to find number of connected components in a undirected graph For example, how to transfer Adjacency Matrix into a graph problem. As below pic: -![adjacency matrix](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud2fq7sj31bh0n4jub.jpg) +![adjacency matrix](https://p.ipic.vip/3ab9h1.jpg) Connected components in a graph problem usually can be solved using *DFS*, *BFS*, *Union-Find*. @@ -56,7 +56,7 @@ Below we will explain details on each approach. as below pic show *DFS* traverse process: -![friend circle DFS](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud6st4nj30u01400x8.jpg) +![friend circle DFS](https://p.ipic.vip/j284f4.jpg) #### Complexity Analysis - *Time Complexity:* `O(n*n) - n is the number of students, traverse n*n matrix` @@ -70,7 +70,7 @@ as below pic show *DFS* traverse process: as below pic show *BFS* (Level traverse) traverse process: -![friend circle BFS](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud7pt1xj30u0140tdc.jpg) +![friend circle BFS](https://p.ipic.vip/ajoi85.jpg) #### Complexity Analysis - *Time Complexity:* `O(n*n) - n is the number of students, traverse n*n matrix` @@ -93,7 +93,7 @@ To know more details and implementations, see further reading lists. as below Union-Find approach process: -![friend circle union-find](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud9heh4j31400u013q.jpg) +![friend circle union-find](https://p.ipic.vip/wz5b3h.jpg) > **Note:** here using weighted-union-find to avoid Union and Find take `O(n)` in the worst case. diff --git a/problems/547.number-of-provinces.md b/problems/547.number-of-provinces.md index 7b31f0f90..7ba71cc36 100644 --- a/problems/547.number-of-provinces.md +++ b/problems/547.number-of-provinces.md @@ -104,4 +104,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 36K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/3f6d57.jpg) diff --git a/problems/55.jump-game.md b/problems/55.jump-game.md index 39a2e96b3..c7116ebb6 100644 --- a/problems/55.jump-game.md +++ b/problems/55.jump-game.md @@ -142,4 +142,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/9n0m59.jpg) diff --git a/problems/56.merge-intervals.md b/problems/56.merge-intervals.md index 31ce798ae..a46983fd8 100644 --- a/problems/56.merge-intervals.md +++ b/problems/56.merge-intervals.md @@ -141,10 +141,10 @@ class Solution: 另外下面的图是我思考时候的草图,红色表示需要删除的区间,灰色是题目给的区间。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gouxm8ur4nj30sp0a0t8x.jpg) +![](https://p.ipic.vip/exqstp.jpg) > 注意是 if 不是 else if 。 否则的话,你的判断会很多。 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/laky7s.jpg) diff --git a/problems/560.subarray-sum-equals-k.en.md b/problems/560.subarray-sum-equals-k.en.md index 31141e4e9..b137ea301 100644 --- a/problems/560.subarray-sum-equals-k.en.md +++ b/problems/560.subarray-sum-equals-k.en.md @@ -71,7 +71,7 @@ Algorithm: Here is a graph demonstrating this algorithm in the case of `nums = [1,2,3,3,0,3,4,2], k = 6`. -![560.subarray-sum-equals-k](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6cdcbrj30lt0edabn.jpg) +![560.subarray-sum-equals-k](https://p.ipic.vip/tsms2q.jpg) When we are at `nums[3]`, the hashmap is as the picture shows, and `count` is 2 by this time. `[1, 2, 3]` accounts for one of the count, and `[3, 3]` accounts for another. diff --git a/problems/560.subarray-sum-equals-k.md b/problems/560.subarray-sum-equals-k.md index a0452f4ac..befb345bd 100644 --- a/problems/560.subarray-sum-equals-k.md +++ b/problems/560.subarray-sum-equals-k.md @@ -78,7 +78,7 @@ class Solution: 语言比较难以解释,我画了一个图来演示 nums = [1,2,3,3,0,3,4,2], k = 6 的情况。 -![560.subarray-sum-equals-k](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu77udnrj30lt0edabn.jpg) +![560.subarray-sum-equals-k](https://p.ipic.vip/1j2rkm.jpg) 如图,当访问到 nums[3]的时候,hashmap 如图所示,这个时候 count 为 2. 其中之一是[1,2,3],这个好理解。还有一个是[3,3]. diff --git a/problems/5640.maximum-xor-with-an-element-from-array.md b/problems/5640.maximum-xor-with-an-element-from-array.md index 0d6958c90..fa282d067 100644 --- a/problems/5640.maximum-xor-with-an-element-from-array.md +++ b/problems/5640.maximum-xor-with-an-element-from-array.md @@ -60,9 +60,9 @@ PS:使用 JS 可以平方复杂度直接莽过。不过这个数据范围平 以 nums[0,1,2,3,4], x 为 9 为例,给大家讲解一下核心原理。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm2t6qgo9lj30zy0fcwf7.jpg) +![](https://p.ipic.vip/78x7pl.jpg) -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm2t6yvkuyj31ye0q8adv.jpg) +![](https://p.ipic.vip/yfoe80.jpg) 具体算法: diff --git a/problems/57.insert-interval.md b/problems/57.insert-interval.md index 00997fd8c..de3b30922 100644 --- a/problems/57.insert-interval.md +++ b/problems/57.insert-interval.md @@ -145,4 +145,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/4dn4ww.jpg) diff --git a/problems/575.distribute-candies.en.md b/problems/575.distribute-candies.en.md index d79d80ce5..bb044e907 100644 --- a/problems/575.distribute-candies.en.md +++ b/problems/575.distribute-candies.en.md @@ -40,7 +40,7 @@ Since the candies are even, we only need to make the same number of candies for Consider two situations: -![575.distribute-candies](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucvt9rcj30kw09pmy6.jpg) +![575.distribute-candies](https://p.ipic.vip/e1ejqa.jpg) -If the types of candies are greater than n / 2 (the number of types of candies is n), the most types of candies that my sister can get should be`n /2` (because my sister only has n /2 candies). -The number of types of candies is less than n /2. The types of candies that my sister can get can be the number of types of candies (there are so many types of candies in themselves). @@ -90,4 +90,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/dvolyl.jpg) diff --git a/problems/575.distribute-candies.md b/problems/575.distribute-candies.md index c2ca0a35e..8c5dfd1e5 100644 --- a/problems/575.distribute-candies.md +++ b/problems/575.distribute-candies.md @@ -40,7 +40,7 @@ https://leetcode-cn.com/problems/distribute-candies/ 考虑两种情况: -![575.distribute-candies](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucvt9rcj30kw09pmy6.jpg) +![575.distribute-candies](https://p.ipic.vip/ggk8wu.jpg) - 如果糖果种类大于 n / 2(糖果种类数为 n),妹妹最多可以获得的糖果种类应该是`n / 2`(因为妹妹只有 n / 2 个糖). - 糖果种类数小于 n / 2, 妹妹能够得到的糖果种类可以是糖果的种类数(糖果种类本身就这么多). @@ -90,4 +90,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/w2vk1g.jpg) diff --git a/problems/5775.minimum-skips-to-arrive-at-meeting-on-time.md b/problems/5775.minimum-skips-to-arrive-at-meeting-on-time.md index 521da1e8f..d9ce6dc50 100644 --- a/problems/5775.minimum-skips-to-arrive-at-meeting-on-time.md +++ b/problems/5775.minimum-skips-to-arrive-at-meeting-on-time.md @@ -157,4 +157,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/fjmxbs.jpg) diff --git a/problems/5935.find-good-days-to-rob-the-bank.md b/problems/5935.find-good-days-to-rob-the-bank.md index 98253dca0..149a384f0 100644 --- a/problems/5935.find-good-days-to-rob-the-bank.md +++ b/problems/5935.find-good-days-to-rob-the-bank.md @@ -128,4 +128,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6k17en.jpg) diff --git a/problems/5936.detonate-the-maximum-bombs.md b/problems/5936.detonate-the-maximum-bombs.md index ed28a0e0b..15fcbe0c5 100644 --- a/problems/5936.detonate-the-maximum-bombs.md +++ b/problems/5936.detonate-the-maximum-bombs.md @@ -136,4 +136,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/03vm8z.jpg) diff --git a/problems/5965.intervals-between-identical-elements.md b/problems/5965.intervals-between-identical-elements.md index c256c0ade..bfabd3bfa 100644 --- a/problems/5965.intervals-between-identical-elements.md +++ b/problems/5965.intervals-between-identical-elements.md @@ -131,4 +131,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/huy5gr.jpg) diff --git a/problems/5999.count-good-triplets-in-an-array.md b/problems/5999.count-good-triplets-in-an-array.md index 19a8170c0..ed51bff1a 100644 --- a/problems/5999.count-good-triplets-in-an-array.md +++ b/problems/5999.count-good-triplets-in-an-array.md @@ -132,4 +132,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/7rretn.jpg) diff --git a/problems/60.permutation-sequence.md b/problems/60.permutation-sequence.md index 1d4a4df89..526484677 100644 --- a/problems/60.permutation-sequence.md +++ b/problems/60.permutation-sequence.md @@ -109,4 +109,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/qabj8z.jpg) diff --git a/problems/611.valid-triangle-number.md b/problems/611.valid-triangle-number.md index 1ed55e1b1..992d2250a 100644 --- a/problems/611.valid-triangle-number.md +++ b/problems/611.valid-triangle-number.md @@ -159,4 +159,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud0qh2oj30p00dwt9t.jpg) +![](https://p.ipic.vip/ulcce7.jpg) diff --git a/problems/62.unique-paths.md b/problems/62.unique-paths.md index aa65ccab3..0fc4aac1f 100644 --- a/problems/62.unique-paths.md +++ b/problems/62.unique-paths.md @@ -14,7 +14,7 @@ https://leetcode-cn.com/problems/unique-paths/ ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludgx4b6j30b40533yf.jpg) +![](https://p.ipic.vip/edxo2e.jpg) ``` 例如,上图是一个7 x 3 的网格。有多少可能的路径? @@ -59,14 +59,14 @@ https://leetcode-cn.com/problems/unique-paths/ 首先这道题可以用排列组合的解法来解,需要一点高中的知识。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1giwviy6wj6j32b80u0792.jpg) +![](https://p.ipic.vip/yyyfdk.jpg) 而这道题我们也可以用动态规划来解。其实这是一道典型的适合使用动态规划解决的题目,它和爬楼梯等都属于动态规划中最简单的题目,因此也经常会被用于面试之中。 读完题目你就能想到动态规划的话,建立模型并解决恐怕不是难事。其实我们很容易看出,由于机器人只能右移动和下移动, 因此第[i, j]个格子的总数应该等于[i - 1, j] + [i, j -1], 因为第[i,j]个格子一定是从左边或者上面移动过来的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludhu8vpj304z07ga9z.jpg) +![](https://p.ipic.vip/c48wi8.jpg) 这不就是二维平面的爬楼梯么?和爬楼梯又有什么不同呢? @@ -93,7 +93,7 @@ class Solution: 由于 dp[i][j] 只依赖于左边的元素和上面的元素,因此空间复杂度可以进一步优化, 优化到 O(n). -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludigqo6j30gr09waaq.jpg) +![](https://p.ipic.vip/1wo20j.jpg) 具体代码请查看代码区。 diff --git a/problems/63.unique-paths-ii.md b/problems/63.unique-paths-ii.md index 2bdea2eef..500bc6968 100644 --- a/problems/63.unique-paths-ii.md +++ b/problems/63.unique-paths-ii.md @@ -13,7 +13,7 @@ https://leetcode-cn.com/problems/unique-paths-ii/ 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径? ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludv12xej30b40533yf.jpg) +![](https://p.ipic.vip/r355vn.jpg) ``` 网格中的障碍物和空位置分别用 1 和 0 来表示。 @@ -55,7 +55,7 @@ https://leetcode-cn.com/problems/unique-paths-ii/ 读完题目你就能想到动态规划的话,建立模型并解决恐怕不是难事。其实我们很容易看出,由于机器人只能右移动和下移动, 因此第[i, j]个格子的总数应该等于[i - 1, j] + [i, j -1], 因为第[i,j]个格子一定是从左边或者上面移动过来的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludvgtpxj304z07ga9z.jpg) +![](https://p.ipic.vip/ww9sxm.jpg) dp[i][j] 表示 到格子 obstacleGrid[i - 1][j - 1] 的所有路径数。 @@ -113,7 +113,7 @@ class Solution: 由于 dp[i][j] 只依赖于左边的元素和上面的元素,因此空间复杂度可以进一步优化, 优化到 O(n). -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludvwao6j30gr09waaq.jpg) +![](https://p.ipic.vip/yocls5.jpg) 具体代码请查看代码区。 @@ -184,4 +184,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/0n7ygz.jpg) diff --git a/problems/65.valid-number.md b/problems/65.valid-number.md index 1803399c7..2be49d5b3 100644 --- a/problems/65.valid-number.md +++ b/problems/65.valid-number.md @@ -154,7 +154,7 @@ class Solution: ### 思路 -![](https://tva1.sinaimg.cn/large/008i3skNly1gq49ny3fb6j319s0u0k0q.jpg) +![](https://p.ipic.vip/tzx4ia.jpg) 对于状态机,我们需要解决的就是: @@ -255,4 +255,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/57bg3l.jpg) diff --git a/problems/661.image-smoother.md b/problems/661.image-smoother.md index 23fede05a..e1af1cc05 100644 --- a/problems/661.image-smoother.md +++ b/problems/661.image-smoother.md @@ -69,7 +69,7 @@ n == img[i].length 的[刷题插件]([力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/)的模板 ,没改直接用的。 -![image.png](https://pic.leetcode-cn.com/1648089491-zsRKDU-image.png) +![image.png](https://p.ipic.vip/ix9mh7.png) ## 关键点 diff --git a/problems/664.strange-printer.md b/problems/664.strange-printer.md index 0cb29b8fa..8bf738b2a 100644 --- a/problems/664.strange-printer.md +++ b/problems/664.strange-printer.md @@ -156,4 +156,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/0j9rlh.jpg) diff --git a/problems/665.non-decreasing-array.md b/problems/665.non-decreasing-array.md index a247501ae..8d842dc21 100644 --- a/problems/665.non-decreasing-array.md +++ b/problems/665.non-decreasing-array.md @@ -117,4 +117,4 @@ class Solution(object): 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/wnfyw8.jpg) diff --git a/problems/673.number-of-longest-increasing-subsequence.md b/problems/673.number-of-longest-increasing-subsequence.md index d80202fd7..84de78b1c 100644 --- a/problems/673.number-of-longest-increasing-subsequence.md +++ b/problems/673.number-of-longest-increasing-subsequence.md @@ -110,4 +110,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud0qh2oj30p00dwt9t.jpg) +![](https://p.ipic.vip/9annlp.jpg) diff --git a/problems/679.24-game.md b/problems/679.24-game.md index ffdb155a3..3e110625c 100644 --- a/problems/679.24-game.md +++ b/problems/679.24-game.md @@ -78,4 +78,4 @@ for x in [a+b, a-b, a*b, b and a/b]) 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/bm42zq.jpg) diff --git a/problems/686.repeated-string-match.md b/problems/686.repeated-string-match.md index 167c764ba..85ba59ef6 100644 --- a/problems/686.repeated-string-match.md +++ b/problems/686.repeated-string-match.md @@ -128,4 +128,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlud0qh2oj30p00dwt9t.jpg) +![](https://p.ipic.vip/2m42ja.jpg) diff --git a/problems/710.random-pick-with-blacklist.md b/problems/710.random-pick-with-blacklist.md index 47fc6c0c4..5d26d6e65 100644 --- a/problems/710.random-pick-with-blacklist.md +++ b/problems/710.random-pick-with-blacklist.md @@ -117,4 +117,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/9pr94u.jpg) diff --git a/problems/714.best-time-to-buy-and-sell-stock-with-transaction-fee.md b/problems/714.best-time-to-buy-and-sell-stock-with-transaction-fee.md index 3e520e9c5..97a095cef 100644 --- a/problems/714.best-time-to-buy-and-sell-stock-with-transaction-fee.md +++ b/problems/714.best-time-to-buy-and-sell-stock-with-transaction-fee.md @@ -196,4 +196,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/mzctl2.jpg) diff --git a/problems/715.range-module.md b/problems/715.range-module.md index 06481274d..fab601c3a 100644 --- a/problems/715.range-module.md +++ b/problems/715.range-module.md @@ -95,15 +95,15 @@ class RangeModule(object): - 如何查询某一个区间 [s, e] 是否被跟踪呢?我们只需要将 s, e 分别在数组中查一下。如果 s 和 e 都是**同一个奇数坐标**即可。 - 插入和删除也是一样。先将 s, e 分别在数组中查一下,假设我们查到的分别为 i 和 j,接下来使用 [i, j] 更新原有区间即可。 -![示例1](https://tva1.sinaimg.cn/large/008eGmZEly1gmjs9au58kj30pm0n6gnq.jpg) +![示例1](https://p.ipic.vip/vmnsi6.jpg) -![示例2](https://tva1.sinaimg.cn/large/008eGmZEly1gmjsbe2nkdj30j80h075s.jpg) +![示例2](https://p.ipic.vip/y8ii0o.jpg) 使用不同颜色区分不同的区间,当我们要查 [3,9] 的时候。实线圈表示我们查到的索引,黑色的框框表示我们需要更新的区间。 区间更新逻辑如下: -![区间更新逻辑](https://tva1.sinaimg.cn/large/008eGmZEly1gmjs8sbyo6j31ak0qatep.jpg) +![区间更新逻辑](https://p.ipic.vip/ovosah.jpg) ## 关键点解析 @@ -162,4 +162,4 @@ addRange 和 removeRange 中使用 bisect_left 找到左端点 l,使用 bisect 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0yircgj30p00dwt9t.jpg) +![](https://p.ipic.vip/a21tbf.jpg) diff --git a/problems/718.maximum-length-of-repeated-subarray.md b/problems/718.maximum-length-of-repeated-subarray.md index 7cdf6a84a..10f1d8016 100644 --- a/problems/718.maximum-length-of-repeated-subarray.md +++ b/problems/718.maximum-length-of-repeated-subarray.md @@ -87,4 +87,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0yircgj30p00dwt9t.jpg) +![](https://p.ipic.vip/2h3f8q.jpg) diff --git a/problems/721.accounts-merge.md b/problems/721.accounts-merge.md index e4bde5c2d..e304deffa 100644 --- a/problems/721.accounts-merge.md +++ b/problems/721.accounts-merge.md @@ -83,4 +83,4 @@ class Solution: 欢迎关注我的公众号《脑洞前端》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlucengjhj31bi0hcq5s.jpg) +![](https://p.ipic.vip/zkaxzw.jpg) diff --git a/problems/726.number-of-atoms.md b/problems/726.number-of-atoms.md index 62ae2c3b4..c956b852e 100644 --- a/problems/726.number-of-atoms.md +++ b/problems/726.number-of-atoms.md @@ -130,4 +130,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vgf8uk.jpg) diff --git a/problems/73.set-matrix-zeroes.md b/problems/73.set-matrix-zeroes.md index c6de38210..3b47187ba 100644 --- a/problems/73.set-matrix-zeroes.md +++ b/problems/73.set-matrix-zeroes.md @@ -58,7 +58,7 @@ https://leetcode-cn.com/problems/set-matrix-zeroes/ 符合直觉的想法是,使用一个 m + n 的数组来表示每一行每一列是否”全部是 0“, 先遍历一遍去构建这样的 m + n 数组,然后根据这个 m + n 数组去修改 matrix 即可。 -![73.set-matrix-zeroes-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwwel2wj30gs0c5t9c.jpg) +![73.set-matrix-zeroes-1](https://p.ipic.vip/5o5ley.jpg) 这样的时间复杂度 O(m \* n), 空间复杂度 O(m + n). @@ -116,7 +116,7 @@ var setZeroes = function (matrix) { - 根据第一行第一列的数据,更新 matrix - 最后根据我们最开始记录的”第一行和第一列是否全是 0“去更新第一行和第一列即可 -![73.set-matrix-zeroes-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwxfcj5j30ka08xjrv.jpg) +![73.set-matrix-zeroes-2](https://p.ipic.vip/55w5t6.jpg) ## 关键点 @@ -269,7 +269,7 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/uwj0o9.jpg) ## 扩展 diff --git a/problems/735.asteroid-collision.md b/problems/735.asteroid-collision.md index b1efb131f..f821e70e1 100644 --- a/problems/735.asteroid-collision.md +++ b/problems/735.asteroid-collision.md @@ -124,4 +124,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/krao83.jpg) diff --git a/problems/75.sort-colors.md b/problems/75.sort-colors.md index 59b7ef119..4a2c3685d 100644 --- a/problems/75.sort-colors.md +++ b/problems/75.sort-colors.md @@ -49,7 +49,7 @@ https://leetcode-cn.com/problems/sort-colors/ 这种思路的时间复杂度:$O(n)$,需要遍历数组两次(Two pass)。 -![image](https://tva1.sinaimg.cn/large/0081Kckwly1gl0hievmxyj30kl0c1t9m.jpg) +![image](https://p.ipic.vip/fx8w93.jpg) ## 解法二 - 挡板法 @@ -69,29 +69,29 @@ https://leetcode-cn.com/problems/sort-colors/ 形象地来说地话就是有两个挡板,这两个挡板具体在哪事先我们不知道,我们的目标就是移 动挡板到合适位置,并且使得挡板间的每一部分都是同一个的颜色。 -![image](https://tva1.sinaimg.cn/large/0081Kckwly1gl0hihivldj31660u0wnb.jpg) +![image](https://p.ipic.vip/42zkeh.jpg) 还是以题目给的样例来说,初始化挡板位置为最左侧和最右侧: -![image](https://tva1.sinaimg.cn/large/0081Kckwly1gl0hijbh5nj31h80h475x.jpg) +![image](https://p.ipic.vip/z76kvp.jpg) 读取第一个元素是 2,它应该在右边,那么我们移动右边地挡板,使得 2 跑到挡板的右边 。 -![image](https://tva1.sinaimg.cn/large/0081Kckwly1gl0hikpnjhj31s80j4421.jpg) +![image](https://p.ipic.vip/xrtyee.jpg) > 带有背景色的圆圈 1 是第一步的意思。 并将其和移动挡板后挡板右侧地元素进行一次交换,这意味着“被移动挡板右侧元素已就位 ”。 -![image](https://tva1.sinaimg.cn/large/0081Kckwly1gl0himlg5zj31iu0j8mz8.jpg) +![image](https://p.ipic.vip/s2ylwf.jpg) 。。。 整个过程大概是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gl0himzyeaj310m0l2wfs.jpg) +![](https://p.ipic.vip/t06pjb.jpg) 这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。空间复杂度为 $O(1),因为我们 没有使用额外的空间。 @@ -181,4 +181,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我 的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gl0hinyr5cj30p00dwt9t.jpg) +![](https://p.ipic.vip/ounerm.jpg) diff --git a/problems/768.max-chunks-to-make-sorted-ii.md b/problems/768.max-chunks-to-make-sorted-ii.md index c7049383a..55e49c2b5 100644 --- a/problems/768.max-chunks-to-make-sorted-ii.md +++ b/problems/768.max-chunks-to-make-sorted-ii.md @@ -43,11 +43,11 @@ arr[i]的大小在[0, 10**8]之间。 这里可以使用类似计数排序的技巧来完成。以题目给的 [2,1,3,4,4] 来说: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkejc54ecwj30dh037mx6.jpg) +![](https://p.ipic.vip/9s7x67.jpg) 可以先计数,比如用一个数组来计数,其中数组的索引表示值,数组的值表示其对应的出现次数。比如上面,除了 4 出现了两次,其他均出现一次,因此 count 就是 [0,1,1,1,2]。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkejnxnxx9j30h9052glw.jpg) +![](https://p.ipic.vip/ejv08v.jpg) 其中 counts[4] 就是 2,表示的就是 4 这个值出现了两次。 @@ -61,7 +61,7 @@ arr[i]的大小在[0, 10**8]之间。 如果我把一个数组打乱,然后排序,得到的数组一定是确定的,即不管你怎么打乱排好序都是一个确定的有序序列。这个论点的正确性是毋庸置疑的。而实际上,一个数组无论怎么打乱,其计数结果也是确定的,这也是毋庸置疑的。反之,如果是两个不同的数组,打乱排序后的结果一定是不同的,计数也是同理。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkejta7rb5j30dm07baad.jpg) +![](https://p.ipic.vip/i9mrda.jpg) (这两个数组排序后的结果以及计数信息是一致的) 因此我们的算法有了: @@ -189,7 +189,7 @@ class Solution(object): 不过这还不够,我们要把思路逆转! -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkekhonycnj30zk0i0782.jpg) +![](https://p.ipic.vip/c5fts0.jpg) > 这是《逆转裁判》 中经典的台词, 主角在深处绝境的时候,会突然冒出这句话,从而逆转思维,寻求突破口。 diff --git a/problems/78.subsets-en.md b/problems/78.subsets-en.md index dcafa79fc..d49ad860c 100644 --- a/problems/78.subsets-en.md +++ b/problems/78.subsets-en.md @@ -33,7 +33,7 @@ Actually, there is a general approach to solve problems similar to this one -- b Given a picture as followed, let's start with problem-solving ideas of this general solution. -![backtrack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu75m5n4j30n20nptas.jpg) +![backtrack](https://p.ipic.vip/n9c7lm.jpg) See Code Template details below. diff --git a/problems/78.subsets.md b/problems/78.subsets.md index c4c7793ab..f574715f2 100644 --- a/problems/78.subsets.md +++ b/problems/78.subsets.md @@ -160,4 +160,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/tv8mab.jpg) diff --git a/problems/79.word-search-en.md b/problems/79.word-search-en.md index b1910c371..9316bd031 100644 --- a/problems/79.word-search-en.md +++ b/problems/79.word-search-en.md @@ -40,7 +40,7 @@ board, word:`SEE` as below pic: as below pic: ``` -![word search 1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxkrpz0j30zh0bita5.jpg) +![word search 1](https://p.ipic.vip/8zz0rc.jpg) Staring position(1,0), check whether adjacent cells match word next letter `E`. ``` @@ -52,7 +52,7 @@ Staring position(1,0), check whether adjacent cells match word next letter ` as below pic: ``` -![word search 2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxldusrj30wk0fqdhj.jpg) +![word search 2](https://p.ipic.vip/tb0bac.jpg) Didn't find matching from starting position, so ``` @@ -61,7 +61,7 @@ Didn't find matching from starting position, so as below pic: ``` -![word search 3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxm8h3zj30xm0czdha.jpg) +![word search 3](https://p.ipic.vip/hl7jpa.jpg) New starting position(1,3),check whether adjacent cells match word next letter `E`. ``` @@ -73,7 +73,7 @@ New starting position(1,3),check whether adjacent cells match word next as below pic: ``` -![word search 4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxn5k3xj30yi0ebq4o.jpg) +![word search 4](https://p.ipic.vip/429a6k.jpg) Start position(0,3), DFS,check whether adjacent cells match word next letter `E` ``` @@ -85,7 +85,7 @@ Start position(0,3), DFS,check whether adjacent cells match word next lett as below pic: ``` -![word search 5](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxnk3ldj30tr0blq43.jpg) +![word search 5](https://p.ipic.vip/5b5gqz.jpg) Start from position(0,3)not matching word, start position (2, 3) DFS search: ``` @@ -98,10 +98,10 @@ Start from position(0,3)not matching word, start position (2, 3) DFS searc as below pic: ``` -![word search 6](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxnznr2j30v50chmyf.jpg) +![word search 6](https://p.ipic.vip/dhe0zh.jpg) Found match with word, return `True`. -![word search 7](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltxowzgej30y90bo3zv.jpg) +![word search 7](https://p.ipic.vip/am0nll.jpg) #### Complexity Analysis - *Time Complexity:* `O(m*n) - m is number of board rows, n is number of board columns ` diff --git a/problems/79.word-search.md b/problems/79.word-search.md index 8d59cc1e4..2f83ebfb1 100644 --- a/problems/79.word-search.md +++ b/problems/79.word-search.md @@ -61,7 +61,7 @@ board 和 word 中只包含大写和小写英文字母。 如下图: ``` -![word search 1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9qqyy1j31200cj0ue.jpg) +![word search 1](https://p.ipic.vip/9v5dpx.jpg) 起始位置(1,0),判断相邻的字符是否匹配单词下一个字符 `E`. @@ -75,7 +75,7 @@ board 和 word 中只包含大写和小写英文字母。 如下图: ``` -![word search 2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9s2be3j30wk0fqdhj.jpg) +![word search 2](https://p.ipic.vip/dlg33a.jpg) 由于从起始位置 DFS 都不满足条件,所以 @@ -86,7 +86,7 @@ board 和 word 中只包含大写和小写英文字母。 如下图: ``` -![word search 3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9tzqn3j30xm0czdha.jpg) +![word search 3](https://p.ipic.vip/v95ixt.jpg) 起始位置(1,3),判断相邻的字符是否匹配单词下一个字符 `E`. @@ -100,7 +100,7 @@ board 和 word 中只包含大写和小写英文字母。 如下图: ``` -![word search 4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9vdrm5j30yi0ebq4o.jpg) +![word search 4](https://p.ipic.vip/w8pgef.jpg) 位置(0,3)满足条件,继续 DFS,判断相邻的字符是否匹配单词下一个字符 `E` @@ -114,7 +114,7 @@ board 和 word 中只包含大写和小写英文字母。 如下图 ``` -![word search 5](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9w7rchj30tr0blq43.jpg) +![word search 5](https://p.ipic.vip/qc9syj.jpg) 从位置(0,3)DFS 不满足条件,继续位置(2,3)DFS 搜索 @@ -129,10 +129,10 @@ board 和 word 中只包含大写和小写英文字母。 如下图: ``` -![word search 6](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9x8av2j30v50chmyf.jpg) +![word search 6](https://p.ipic.vip/unor7j.jpg) 单词匹配完成,满足条件,返回 `True`. -![word search 7](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu9yl94uj30y90bo3zv.jpg) +![word search 7](https://p.ipic.vip/619on0.jpg) #### 复杂度分析 @@ -283,4 +283,4 @@ var exist = function (board, word) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/s2wayh.jpg) diff --git a/problems/790.domino-and-tromino-tiling.md b/problems/790.domino-and-tromino-tiling.md index 753aa4f03..da75e7c2b 100644 --- a/problems/790.domino-and-tromino-tiling.md +++ b/problems/790.domino-and-tromino-tiling.md @@ -48,9 +48,9 @@ N  的范围是 [1, 1000] 以这道题来说,所有可能的情况无非就是以下 6 种: -![](https://tva1.sinaimg.cn/large/008eGmZEly1gnqf5s4jhaj30h80qijsk.jpg) +![](https://p.ipic.vip/9wyy2o.jpg) -![](https://tva1.sinaimg.cn/large/008eGmZEly1gnqf6033vrj30du0qswfn.jpg) +![](https://p.ipic.vip/q04uxe.jpg) 而题目要求的是**刚好铺满** 2 \* N 的情况的总的可能数。 @@ -165,4 +165,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/4nga7b.jpg) diff --git a/problems/799.champagne-tower.md b/problems/799.champagne-tower.md index dccc5458e..42e4a1e6c 100644 --- a/problems/799.champagne-tower.md +++ b/problems/799.champagne-tower.md @@ -47,7 +47,7 @@ query_glass 和query_row 的范围 [0, 99]。 由题目可知杯子的数目是第一行一个,第二行两个。。。第 i 行 i 个 (i >= 1)。因此建立一个二维数组即可。为了简单,我们可以建立一个大小为 R _ R 的二维矩阵 A ,其中 R 为香槟塔的高度。虽然这样的建立方式会造成一半的空间浪费。但是题目的条件是** query_glass 和 query_row 的范围 [0, 99]**,因此即便如此问题也不大。当然你也可以直接开辟一个 100 _ 100 的矩阵。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gnobpdiwqrj30mw0l6wjw.jpg) +![](https://p.ipic.vip/8hyeny.jpg) (用 R \* R 的二维矩阵 A 进行模拟,如图虚线的部分是没有被使用的空间,也就是”浪费“的空间) 接下来,我们只需要按照题目描述进行模拟即可。具体来说: @@ -101,4 +101,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/4576v1.jpg) diff --git a/problems/80.remove-duplicates-from-sorted-array-ii.md b/problems/80.remove-duplicates-from-sorted-array-ii.md index ab2cf0def..b84110714 100644 --- a/problems/80.remove-duplicates-from-sorted-array-ii.md +++ b/problems/80.remove-duplicates-from-sorted-array-ii.md @@ -56,7 +56,7 @@ for (int i = 0; i < len; i++) { ”删除排序“类题目截止到现在(2020-1-15)一共有四道题: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0jxjeej30x60cedh0.jpg) +![](https://p.ipic.vip/vclvkl.jpg) 这道题是[26.remove-duplicates-from-sorted-array](./26.remove-duplicates-from-sorted-array.md) 的进阶版本,唯一的不同是不再是全部元素唯一,而是全部元素不超过 2 次。实际上这种问题可以更抽象一步,即“删除排序数组中的重复项,使得相同数字最多出现 k 次” 。 那么这道题 k 就是 2, 26.remove-duplicates-from-sorted-array 的 k 就是 1。 @@ -72,9 +72,9 @@ for (int i = 0; i < len; i++) { 图解(红色的两个数字,表示我们需要比较的两个数字): -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0p8ea3j30n10hpmy4.jpg) +![](https://p.ipic.vip/jl0f21.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0r18z0j30ga088mxh.jpg) +![](https://p.ipic.vip/m5hj2d.jpg) ## 关键点分析 @@ -135,12 +135,12 @@ public: - 82. 删除排序链表中的重复元素 II -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0s4jb3j31lq0tgq7m.jpg) +![](https://p.ipic.vip/ojw569.jpg) - 83. 删除排序链表中的重复元素 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0vrcvlj318c0se0wm.jpg) +![](https://p.ipic.vip/g3vnho.jpg) 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/7xxxeg.jpg) diff --git a/problems/801.minimum-swaps-to-make-sequences-increasing.md b/problems/801.minimum-swaps-to-make-sequences-increasing.md index ce93b67a2..4e6809fca 100644 --- a/problems/801.minimum-swaps-to-make-sequences-increasing.md +++ b/problems/801.minimum-swaps-to-make-sequences-increasing.md @@ -172,4 +172,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/d35xen.jpg) diff --git a/problems/805.split-array-with-same-average.md b/problems/805.split-array-with-same-average.md index 174f36244..97c4dd085 100644 --- a/problems/805.split-array-with-same-average.md +++ b/problems/805.split-array-with-same-average.md @@ -150,4 +150,4 @@ class Solution(object): 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/elnjpp.jpg) diff --git a/problems/816.ambiguous-coordinates.md b/problems/816.ambiguous-coordinates.md index ea126bd25..e2f317694 100644 --- a/problems/816.ambiguous-coordinates.md +++ b/problems/816.ambiguous-coordinates.md @@ -139,4 +139,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/066kzt.jpg) diff --git a/problems/820.short-encoding-of-words.md b/problems/820.short-encoding-of-words.md index 396d1b5a8..23849a70e 100644 --- a/problems/820.short-encoding-of-words.md +++ b/problems/820.short-encoding-of-words.md @@ -47,7 +47,7 @@ https://leetcode-cn.com/problems/short-encoding-of-words/ 下面的代码看起来复杂,但是很多题目我都是用这个模板,稍微调整下细节就能 AC。我这里总结了一套[前缀树专题](https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md) -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx4t6x9j30nm0703z0.jpg) +![image.png](https://p.ipic.vip/s3jqae.jpg) 前缀树的 api 主要有以下几个: @@ -59,7 +59,7 @@ https://leetcode-cn.com/problems/short-encoding-of-words/ 一个前缀树大概是这个样子: -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx5uzkkj30mz0gqwgc.jpg) +![image.png](https://p.ipic.vip/bnlvyh.jpg) 如图每一个节点存储一个字符,然后外加一个控制信息表示是否是单词结尾,实际使用过程可能会有细微差别,不过变化不大。 @@ -129,7 +129,7 @@ class Solution: 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx6qyuoj30p00dwt9t.jpg) +![](https://p.ipic.vip/8ffgif.jpg) ## 相关题目 diff --git a/problems/821.shortest-distance-to-a-character.en.md b/problems/821.shortest-distance-to-a-character.en.md index 4c4ca1901..73be65442 100644 --- a/problems/821.shortest-distance-to-a-character.en.md +++ b/problems/821.shortest-distance-to-a-character.en.md @@ -29,7 +29,7 @@ This question is for us to ask for the closest distance to the target character I drew a picture for everyone to understand: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gka46lqwlej30rc0f2tae.jpg) +![](https://p.ipic.vip/r11lwm.jpg) For example, if we want to find the nearest character e of the first character l, the intuitive idea is to search from left to right, stop when we encounter the character e, compare the distances on both sides, and take a smaller one. As shown in the figure above, l is 3 and c is 2. diff --git a/problems/821.shortest-distance-to-a-character.md b/problems/821.shortest-distance-to-a-character.md index 3a934366c..7f390b346 100644 --- a/problems/821.shortest-distance-to-a-character.md +++ b/problems/821.shortest-distance-to-a-character.md @@ -29,7 +29,7 @@ https://leetcode-cn.com/problems/shortest-distance-to-a-character 我画了个图方便大家理解: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gka46lqwlej30rc0f2tae.jpg) +![](https://p.ipic.vip/l1pccw.jpg) 比如我们要找第一个字符 l 的最近的字符 e,直观的想法就是向左向右分别搜索,遇到字符 e 就停止,比较两侧的距离,并取较小的即可。如上图,l 就是 3,c 就是 2。 diff --git a/problems/838.push-dominoes.md b/problems/838.push-dominoes.md index 738f89928..a95919e24 100644 --- a/problems/838.push-dominoes.md +++ b/problems/838.push-dominoes.md @@ -134,4 +134,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ks9a4p.jpg) diff --git a/problems/84.largest-rectangle-in-histogram.md b/problems/84.largest-rectangle-in-histogram.md index 705f33500..1cb46c2b7 100644 --- a/problems/84.largest-rectangle-in-histogram.md +++ b/problems/84.largest-rectangle-in-histogram.md @@ -8,11 +8,11 @@ https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ 求在该柱状图中,能够勾勒出来的矩形的最大面积。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx8sr4uj305805odfn.jpg) +![](https://p.ipic.vip/3ds3wy.jpg) 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为  [2,1,5,6,2,3]。  -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx9kgd2j305805oa9z.jpg) +![](https://p.ipic.vip/y52e0e.jpg) 图中阴影部分为所能勾勒出的最大矩形面积,其面积为  10  个单位。 @@ -221,4 +221,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/jpjwki.jpg) diff --git a/problems/85.maximal-rectangle.md b/problems/85.maximal-rectangle.md index d9ec93d07..01bf0a675 100644 --- a/problems/85.maximal-rectangle.md +++ b/problems/85.maximal-rectangle.md @@ -49,7 +49,7 @@ https://leetcode-cn.com/problems/maximal-rectangle/ 我们逐行扫描得到 `84. 柱状图中最大的矩形` 中的 heights 数组: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu7999xyj30t21cgtcn.jpg) +![](https://p.ipic.vip/hr0r1n.jpg) 这样我们就可以使用`84. 柱状图中最大的矩形` 中的解法来进行了,这里我们使用单调栈来解。 diff --git a/problems/86.partition-list.md b/problems/86.partition-list.md index ba4b10eb2..9e4a5b62b 100644 --- a/problems/86.partition-list.md +++ b/problems/86.partition-list.md @@ -37,7 +37,7 @@ https://leetcode-cn.com/problems/partition-list/ 遍历结束后,将 dummyHead2 插入到 dummyHead1 后面 -![86.partition-list](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlua0z1b2g30qq0f1qg9.gif) +![86.partition-list](https://p.ipic.vip/gvhme6.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) @@ -161,4 +161,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vocldc.jpg) diff --git a/problems/87.scramble-string.md b/problems/87.scramble-string.md index d05f4e64b..f38584b85 100644 --- a/problems/87.scramble-string.md +++ b/problems/87.scramble-string.md @@ -157,4 +157,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/nsfsko.jpg) diff --git a/problems/873.length-of-longest-fibonacci-subsequence.md b/problems/873.length-of-longest-fibonacci-subsequence.md index e329ef225..2573ad0ad 100644 --- a/problems/873.length-of-longest-fibonacci-subsequence.md +++ b/problems/873.length-of-longest-fibonacci-subsequence.md @@ -112,4 +112,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/1n3mq5.jpg) diff --git a/problems/874.walking-robot-simulation.en.md b/problems/874.walking-robot-simulation.en.md index c6395470f..f49eead30 100644 --- a/problems/874.walking-robot-simulation.en.md +++ b/problems/874.walking-robot-simulation.en.md @@ -73,7 +73,7 @@ Next, let's “translate” the topic. In order to make the code writing simple, I established a cartesian coordinate system. Use'the degree of angle between the orientation of the robot and the positive direction of the x-axis` as the enumeration value, and this degree is`0<=deg<360`. It is not difficult for us to know, in fact, this value is`0`, `90`,`180`,`270` Four values. Then when it is 0 degrees, we only need to keep x + 1, when it is 90 degrees, we keep y + 1, and so on. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu040owij31020r8gos.jpg) +![](https://p.ipic.vip/idg3qd.jpg) ## Analysis of key points @@ -134,4 +134,4 @@ For more questions, please visit my LeetCode questions warehouse:https://github. Pay attention to the official account, work hard to restore the problem-solving ideas in clear and straightforward language, and there are a large number of diagrams to teach you how to recognize routines and brush questions efficiently. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/iym2m5.jpg) diff --git a/problems/874.walking-robot-simulation.md b/problems/874.walking-robot-simulation.md index a11883038..98991f4ce 100644 --- a/problems/874.walking-robot-simulation.md +++ b/problems/874.walking-robot-simulation.md @@ -73,7 +73,7 @@ https://leetcode-cn.com/problems/walking-robot-simulation/submissions/ 为了代码书写简单,我建立了一个直角坐标系。用`机器人的朝向和 x 轴正方向的夹角度数`来作为枚举值,并且这个度数是 `0 <= deg < 360`。我们不难知道,其实这个取值就是`0`, `90`,`180`,`270` 四个值。那么当 0 度的时候,我们只需要不断地 x+1,90 度的时候我们不断地 y + 1 等等。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu040owij31020r8gos.jpg) +![](https://p.ipic.vip/gyi1zg.jpg) ## 关键点解析 @@ -134,4 +134,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/px0kxt.jpg) diff --git a/problems/875.koko-eating-bananas.md b/problems/875.koko-eating-bananas.md index 209d5fd58..29a79ed97 100644 --- a/problems/875.koko-eating-bananas.md +++ b/problems/875.koko-eating-bananas.md @@ -52,7 +52,7 @@ piles.length <= H <= 10^9 这道题如果能看出来是二分法解决,那么其实很简单。为什么它是二分问题呢?我这里画了个图,我相信你看了就明白了。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4rmzwcj30q00lv40j.jpg) +![](https://p.ipic.vip/txu7bp.jpg) > 香蕉堆的香蕉个数上限是 10^9, 珂珂这也太能吃了吧? @@ -212,4 +212,4 @@ public int binarySearchRight(int[] nums, int target) { 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4sl5v4j30p00dwt9t.jpg) +![](https://p.ipic.vip/nojq1y.jpg) diff --git a/problems/88.merge-sorted-array.en.md b/problems/88.merge-sorted-array.en.md index 42e539a42..be32e74cc 100644 --- a/problems/88.merge-sorted-array.en.md +++ b/problems/88.merge-sorted-array.en.md @@ -105,9 +105,9 @@ As shown in the figure: -Red represents the element currently being compared -Green represents the element that is already in place -![88.merge-sorted-array-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludbcompj30h00n10tj.jpg) -![88.merge-sorted-array-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludbuxg8j30dv08l0sv.jpg) -![88.merge-sorted-array-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludcsa7oj30ca077wek.jpg) +![88.merge-sorted-array-1](https://p.ipic.vip/facbuu.jpg) +![88.merge-sorted-array-2](https://p.ipic.vip/8huv7c.jpg) +![88.merge-sorted-array-3](https://p.ipic.vip/h2lnwm.jpg) ## Analysis of key points @@ -234,4 +234,4 @@ If you have any comments on this, please leave me a message. I will check the an You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/cqbfns.jpg) diff --git a/problems/88.merge-sorted-array.md b/problems/88.merge-sorted-array.md index 19574dd5e..45e4324e2 100644 --- a/problems/88.merge-sorted-array.md +++ b/problems/88.merge-sorted-array.md @@ -104,9 +104,9 @@ function merge(nums1, nums2) { - 红色代表当前正在进行比较的元素 - 绿色代表已经就位的元素 -![88.merge-sorted-array-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludbcompj30h00n10tj.jpg) -![88.merge-sorted-array-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludbuxg8j30dv08l0sv.jpg) -![88.merge-sorted-array-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghludcsa7oj30ca077wek.jpg) +![88.merge-sorted-array-1](https://p.ipic.vip/vkiwwv.jpg) +![88.merge-sorted-array-2](https://p.ipic.vip/uaep0y.jpg) +![88.merge-sorted-array-3](https://p.ipic.vip/5x29zr.jpg) ## 关键点解析 @@ -233,4 +233,4 @@ class Solution: 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/sqb4n7.jpg) diff --git a/problems/886.possible-bipartition.md b/problems/886.possible-bipartition.md index 43c1c35a9..21e9b19a6 100644 --- a/problems/886.possible-bipartition.md +++ b/problems/886.possible-bipartition.md @@ -67,11 +67,11 @@ for a, b in dislikes: graph[b - 1][a - 1] = 1 ``` -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5nd1cij30eo0d2tcg.jpg) +![image.png](https://p.ipic.vip/m9h3nn.jpg) 同时可以用 hashmap 或者数组存储 N 个人的分组情况, 业界关于这种算法一般叫染色法,因此我们命名为 colors,其实对应的本题叫 groups 更合适。 -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu5rtfpcj308s032wf6.jpg) +![image.png](https://p.ipic.vip/ioq7cv.jpg) 我们用: @@ -85,13 +85,13 @@ for a, b in dislikes: - 遍历每一个人,尝试给他们进行分组,比如默认分配组 1. -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu6151fkj30bj05m3zb.jpg) +![image.png](https://p.ipic.vip/96dtvd.jpg) - 然后遍历这个人讨厌的人,尝试给他们分另外一组,如果不可以分配另外一组,则返回 False 那问题的关键在于如何判断“不可以分配另外一组”呢? -![image.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu64l20mj313h0kd77i.jpg) +![image.png](https://p.ipic.vip/3hazb3.jpg) 实际上,我们已经用 colors 记录了分组信息,对于每一个人如果分组确定了,我们就更新 colors,那么对于一个人如果分配了一个组,并且他讨厌的人也被分组之后,**分配的组和它只能是一组**,那么“就是不可以分配另外一组”。 diff --git a/problems/887.super-egg-drop.md b/problems/887.super-egg-drop.md index 788f45a34..a86ed24f3 100644 --- a/problems/887.super-egg-drop.md +++ b/problems/887.super-egg-drop.md @@ -56,12 +56,12 @@ 假如有 2 个鸡蛋,6 层楼。 我们应该先从哪层楼开始扔呢?想了一会,没有什么好的办法。我们来考虑使用暴力的手段。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk793ken5j30zi0fidhu.jpg) +![](https://p.ipic.vip/120oh0.jpg) (图 1. 这种思路是不对的) 既然我不知道先从哪层楼开始扔是最优的,那我就依次模拟从第 1,第 2。。。第 6 层扔。每一层楼丢鸡蛋,都有两种可能,碎或者不碎。由于是最坏的情况,因此我们需要模拟两种情况,并取两种情况中的扔次数的较大值(较大值就是最坏情况)。 然后我们从六种扔法中选择最少次数的即可。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk7a7q9h5j32bo0jutfj.jpg) +![](https://p.ipic.vip/5vz4r2.jpg) (图 2. 应该是这样的) 而每一次选择从第几层楼扔之后,剩下的问题似乎是一个规模变小的同样问题。嗯哼?递归? @@ -120,19 +120,19 @@ class Solution: 那只好 bottom-up(动态规划)啦。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk77gt74aj310d0u0adb.jpg) +![](https://p.ipic.vip/gnmqq1.jpg) (图 3) 我将上面的过程简写成如下形式: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk78qrz6yj316s09k75o.jpg) +![](https://p.ipic.vip/m4ruew.jpg) (图 4) 与其递归地进行这个过程,我们可以使用迭代的方式。 相比于上面的递归式,减少了栈开销。然而两者有着很多的相似之处。 如果说递归是用函数调用来模拟所有情况, 那么动态规划就是用表来模拟。我们知道所有的情况,无非就是 N 和 K 的所有组合,我们怎么去枚举 K 和 N 的所有组合? 当然是套两层循环啦! -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk7d63dfoj31qw0s2dkw.jpg) +![](https://p.ipic.vip/o91aox.jpg) (图 5. 递归 vs 迭代) 如上,你将 dp[i][j] 看成 superEggDrop(i, j),是不是和递归是一摸一样? @@ -176,7 +176,7 @@ class Solution: 总结一下,上面的解题方法思路是: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk7arzmn3j30pa0nemzo.jpg) +![](https://p.ipic.vip/ynsszu.jpg) 然而这样还是不能 AC。这正是这道题困难的地方。 **一道题目往往有不止一种状态转移方程,而不同的状态转移方程往往性能是不同的。** @@ -184,7 +184,7 @@ class Solution: 把思路逆转! -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk7m9z3elj30zk0i01kx.jpg) +![](https://p.ipic.vip/jtgl7i.jpg) > 这是《逆转裁判》 中经典的台词, 主角在深处绝境的时候,会突然冒出这句话,从而逆转思维,寻求突破口。 @@ -290,4 +290,4 @@ var superEggDrop = function (K, N) { 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ln4btk.jpg) diff --git a/problems/895.maximum-frequency-stack.md b/problems/895.maximum-frequency-stack.md index 29ef58663..3624c2773 100644 --- a/problems/895.maximum-frequency-stack.md +++ b/problems/895.maximum-frequency-stack.md @@ -90,21 +90,21 @@ pop() -> 返回 4 。 - 使用 fraq 来存储对应的数字出现次数。key 是数字,value 频率 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluav001bj30d00la74y.jpg) +![](https://p.ipic.vip/up540g.jpg) - 由于题目限制“如果最频繁的元素不只一个,则移除并返回最接近栈顶的元素。”,我们考虑使用栈来维护一个频率表 fraq_stack。key 是频率,value 是数字组成的栈。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlub1bwg0j30k20i8gnh.jpg) +![](https://p.ipic.vip/v0g57i.jpg) - 同时用 max_fraq 记录当前的最大频率值。 - 第一次 pop 的时候,我们最大的频率是 3。由 fraq_stack 知道我们需要 pop 掉 5。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlub2e82vj31160nan00.jpg) +![](https://p.ipic.vip/ddem9w.jpg) - 之后 pop 依次是这样的(红色数字表示顺序) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlub3rxt5j30pk0kitb7.jpg) +![](https://p.ipic.vip/8qb2qr.jpg) ## 关键点解析 @@ -153,4 +153,4 @@ class FreqStack: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/co2602.jpg) diff --git a/problems/898.bitwise-ors-of-subarrays.md b/problems/898.bitwise-ors-of-subarrays.md index 4a7eb0979..6b7b8e73e 100644 --- a/problems/898.bitwise-ors-of-subarrays.md +++ b/problems/898.bitwise-ors-of-subarrays.md @@ -129,4 +129,4 @@ class Solution(object): 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/aadyah.jpg) diff --git a/problems/90.subsets-ii-en.md b/problems/90.subsets-ii-en.md index e6b8c3b3c..c90f3cfd2 100644 --- a/problems/90.subsets-ii-en.md +++ b/problems/90.subsets-ii-en.md @@ -31,7 +31,7 @@ Actually, there is a general approach to solve problems similar to this one -- b Given a picture as followed, let's start with problem-solving ideas of this general solution. -![backtrack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu843pcgj30n20nptas.jpg) +![backtrack](https://p.ipic.vip/uc0i4j.jpg) See Code Template details below. diff --git a/problems/909.snakes-and-ladders.md b/problems/909.snakes-and-ladders.md index 1cde803c9..07bde9e06 100644 --- a/problems/909.snakes-and-ladders.md +++ b/problems/909.snakes-and-ladders.md @@ -143,4 +143,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/bg9q5n.jpg) diff --git a/problems/91.decode-ways.md b/problems/91.decode-ways.md index 4fc01da87..dfe66c91b 100644 --- a/problems/91.decode-ways.md +++ b/problems/91.decode-ways.md @@ -174,4 +174,4 @@ public: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/lzwtjp.jpg) diff --git a/problems/912.sort-an-array.md b/problems/912.sort-an-array.md index cf0ac12d7..061e25ecf 100644 --- a/problems/912.sort-an-array.md +++ b/problems/912.sort-an-array.md @@ -64,7 +64,7 @@ https://leetcode-cn.com/problems/sort-an-array/ 这样一次遍历,我们统计出了所有的数字出现的位置和次数。 我们再来一次遍历,将其输出到即可。 -![sort-an-array-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8a5bvij30mz0dedgw.jpg) +![sort-an-array-1](https://p.ipic.vip/e7udc2.jpg) ### 解法二 - 快速排序 @@ -80,7 +80,7 @@ https://leetcode-cn.com/problems/sort-an-array/ - 数组中间的元素(我采用的是这种,大家可以尝试下别的) - 数组随机一项元素 -![sort-an-array-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu8b9s1vj30jj08oaau.jpg) +![sort-an-array-2](https://p.ipic.vip/our3bd.jpg) (图片来自: https://www.geeksforgeeks.org/quick-sort/) diff --git a/problems/92.reverse-linked-list-ii.md b/problems/92.reverse-linked-list-ii.md index 6aea16617..5e5b6bfc7 100644 --- a/problems/92.reverse-linked-list-ii.md +++ b/problems/92.reverse-linked-list-ii.md @@ -42,7 +42,7 @@ https://leetcode-cn.com/problems/reverse-linked-list-ii/ 这样我们就可以把反转后的那一小段链表加入到原链表中 -![92.reverse-linked-list-ii](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwozbgug30qk0ev4bt.gif) +![92.reverse-linked-list-ii](https://p.ipic.vip/co1bh5.gif) (图片来自网络) @@ -259,4 +259,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltwptvcgj30p00dwt9t.jpg) +![](https://p.ipic.vip/1xoxdp.jpg) diff --git a/problems/932.beautiful-array.md b/problems/932.beautiful-array.md index 5d39cc148..3df017eb3 100644 --- a/problems/932.beautiful-array.md +++ b/problems/932.beautiful-array.md @@ -110,4 +110,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6t8exw.jpg) diff --git a/problems/935.knight-dialer.md b/problems/935.knight-dialer.md index 334bb70d5..c83b9833e 100644 --- a/problems/935.knight-dialer.md +++ b/problems/935.knight-dialer.md @@ -9,7 +9,7 @@ https://leetcode-cn.com/problems/knight-dialer/ ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu303ibcj305305p744.jpg) +![](https://p.ipic.vip/iswthc.jpg) ```          @@ -126,4 +126,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0yircgj30p00dwt9t.jpg) +![](https://p.ipic.vip/bvo6h6.jpg) diff --git a/problems/94.binary-tree-inorder-traversal.md b/problems/94.binary-tree-inorder-traversal.md index 37de12718..f21d6a6ce 100644 --- a/problems/94.binary-tree-inorder-traversal.md +++ b/problems/94.binary-tree-inorder-traversal.md @@ -47,7 +47,7 @@ https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ - 再将当前指针移到其右子节点上,若存在右子节点,则在下次循环时又可将其所有左子结点压入栈中, 重复上步骤 -![94.binary-tree-inorder-traversal](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4qkvu0g30qp0eywoh.gif) +![94.binary-tree-inorder-traversal](https://p.ipic.vip/mp4k3r.gif) (图片来自: https://github.com/MisterBooo/LeetCodeAnimation) @@ -209,4 +209,4 @@ class Solution { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/391x85.jpg) diff --git a/problems/947.most-stones-removed-with-same-row-or-column.md b/problems/947.most-stones-removed-with-same-row-or-column.md index 60c6f293f..f5449bfa8 100644 --- a/problems/947.most-stones-removed-with-same-row-or-column.md +++ b/problems/947.most-stones-removed-with-same-row-or-column.md @@ -58,11 +58,11 @@ n 块石头放置在二维平面中的一些整数坐标点上。每个坐标点 继续分析下题目。 题目的意思是任意一个石头可以消除和它同行和同列的其他石子。于是我就想象出了下面这样一幅图,其中红色的方块表示石子,方块的连线表示离得最近的可以消除的石子。实际上,一个石子除了可以消除图中线条直接相连的石子,还可以消除邻居的邻居。**这提示我们使用并查集维护这种联通关系**,联通的依据自然就是列或者行一样。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gq8w3hl3hfj30o00li0ts.jpg) +![](https://p.ipic.vip/0g23sy.jpg) 上面是一个全联通的图。如下是有两个联通域的图。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gq8w3os5ngj30og0lwmxw.jpg) +![](https://p.ipic.vip/9ysm39.jpg) 有了上面的知识,其实就可以将石子全部建立并查集的联系,并计算联通子图的个数。答案就是 n - 联通子图的个数,其中 n 为 stones 的长度。 @@ -84,13 +84,13 @@ return n - uf.cnt 答案是肯定的。其实上面我提到了这道题也可使用 DFS 和 BFS 的方式来做。如果你使用 DFS 的方式来做,会发现其实 **DFS 路径的取反就是消除的顺序**,当然消除的顺序不唯一,因为遍历访问联通子图的序列并不唯一。 如果题目要求我们求移除顺序,那我们可以考虑使用 DFS 来做,同时记录路径信息即可。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gq8w9ocdcmj315a0ni406.jpg) +![](https://p.ipic.vip/b62ori.jpg) 使用遍历的方式(BFS 或者 DFS),由于每次访问一个石子都需要使用 visited 来记录访问信息防止环的产生,因此 visited 的逆序也是一个可行的移除顺序。不过这要求你的 visited 的是有序的。实现的方法有很多,有点偏题了,这里就不赘述了。 实际上,上面的并查集代码仍然可以优化。上面的思路是直接将点作为并查集的联通条件。实际上,我们可以将点的横纵坐标分别作为联通条件。即如果横坐标相同的联通到一个子图,纵坐标相同的联通到一个子图。如下图: -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmoaaz1j13j317v0u0teu.jpg) +![](https://p.ipic.vip/z3q3o8.jpg) 为了达到这个模板,我们不能再初始化的时候计算联通域数量了,即不能像上面那样 `uf = UF(n)`(此时联通域个数为 n)。因为横坐标,纵坐标分别有多少不重复的我们是不知道的,一种思路是先计算出**横坐标,纵坐标分别有多少不重复的**。这当然可以,还有一种思路是在 find 过程中计算,这样 one pass 即可完成,具体见下方代码区。 diff --git a/problems/959.regions-cut-by-slashes.md b/problems/959.regions-cut-by-slashes.md index 04d3e513b..dbc8cf953 100644 --- a/problems/959.regions-cut-by-slashes.md +++ b/problems/959.regions-cut-by-slashes.md @@ -94,7 +94,7 @@ grid[i][j] 是 '/'、'\'、或 ' '。 使用并查集可以将网格按照如下方式进行逻辑上的划分,之所以进行如下划分的原因是一个网格最多只能被分成如下四个部分,而并查集的处理过程是**合并**,因此初始状态需要是一个个孤立的点,每一个点初始都是一个独立的联通区域。这在我下方代码的初始化过程有所体现。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmzs7bgds1j30e80cyab0.jpg) +![](https://p.ipic.vip/wjwapk.jpg) > 编号方式无所谓,你可以按照你的喜好编号。不过编号方式改变了,代码要做相应微调。 @@ -211,7 +211,7 @@ class Solution: > 4 X 4 以及更多的格子也是可以的,但没有必要了,那样只会徒增时间和空间。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmzt2uxx4nj31m30u04qq.jpg) +![](https://p.ipic.vip/xigtq7.jpg) ### 代码 @@ -265,4 +265,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0yircgj30p00dwt9t.jpg) +![](https://p.ipic.vip/isnd7j.jpg) diff --git a/problems/975.odd-even-jump.md b/problems/975.odd-even-jump.md index 816c5f07e..e07f94a6b 100644 --- a/problems/975.odd-even-jump.md +++ b/problems/975.odd-even-jump.md @@ -221,4 +221,4 @@ return ans 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0yircgj30p00dwt9t.jpg) +![](https://p.ipic.vip/7qxoqa.jpg) diff --git a/problems/978.longest-turbulent-subarray.md b/problems/978.longest-turbulent-subarray.md index 6033691a2..94d21100f 100644 --- a/problems/978.longest-turbulent-subarray.md +++ b/problems/978.longest-turbulent-subarray.md @@ -92,4 +92,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0yircgj30p00dwt9t.jpg) +![](https://p.ipic.vip/srpstu.jpg) diff --git a/problems/98.validate-binary-search-tree.md b/problems/98.validate-binary-search-tree.md index 4c519c6ae..be323bca2 100644 --- a/problems/98.validate-binary-search-tree.md +++ b/problems/98.validate-binary-search-tree.md @@ -363,4 +363,4 @@ function valid(root, min = -Infinity, max = Infinity) { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/pkle97.jpg) diff --git a/problems/987.vertical-order-traversal-of-a-binary-tree.md b/problems/987.vertical-order-traversal-of-a-binary-tree.md index 1423ecc19..45c446da0 100644 --- a/problems/987.vertical-order-traversal-of-a-binary-tree.md +++ b/problems/987.vertical-order-traversal-of-a-binary-tree.md @@ -52,7 +52,7 @@ https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree 我们先来简化一下问题。假如题目没有`从上到下的顺序报告结点的值(Y 坐标递减)`,甚至也没有`如果两个结点位置相同,则首先报告的结点值较小。` 的限制。是不是就比较简单了? -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkteha9unvj30mw0iedh9.jpg) +![](https://p.ipic.vip/gkw801.jpg) 如上图,我们只需要进行一次搜索,不妨使用 DFS(没有特殊理由,我一般都是 DFS),将节点存储到一个哈希表中,其中 key 为节点的 x 值,value 为横坐标为 x 的节点值列表(不妨用数组表示)。形如: diff --git a/problems/995.minimum-number-of-k-consecutive-bit-flips.md b/problems/995.minimum-number-of-k-consecutive-bit-flips.md index 3faab7964..ce3b0d5d5 100644 --- a/problems/995.minimum-number-of-k-consecutive-bit-flips.md +++ b/problems/995.minimum-number-of-k-consecutive-bit-flips.md @@ -222,4 +222,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/3992tg.jpg) diff --git a/problems/997.find-the-town-judge.md b/problems/997.find-the-town-judge.md index 5eb77e402..4727b4d02 100644 --- a/problems/997.find-the-town-judge.md +++ b/problems/997.find-the-town-judge.md @@ -138,4 +138,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/iy282q.jpg) diff --git a/problems/Every-Sublist-Min-Sum.md b/problems/Every-Sublist-Min-Sum.md index ba83d6a54..f13517e9b 100644 --- a/problems/Every-Sublist-Min-Sum.md +++ b/problems/Every-Sublist-Min-Sum.md @@ -100,4 +100,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 46K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/pjrcm6.jpg) diff --git a/problems/Ticket-Order.md b/problems/Ticket-Order.md index 19987e448..719ff1131 100644 --- a/problems/Ticket-Order.md +++ b/problems/Ticket-Order.md @@ -74,7 +74,7 @@ class Solution: 这里我直接用的别人画好的图进行说明。 -![](https://tva1.sinaimg.cn/large/008i3skNly1graynsnv0aj31og0u0n3h.jpg) +![](https://p.ipic.vip/cn3s63.jpg) - 图中的 ps 就是我说的 a1 - 图中的 $(n - j) * (ai - 1)$ 就是我的 a2 diff --git a/problems/binode-lcci.en.md b/problems/binode-lcci.en.md index ecb0f6e65..20eb520e0 100644 --- a/problems/binode-lcci.en.md +++ b/problems/binode-lcci.en.md @@ -50,15 +50,15 @@ If you understand the above content, then let's get to the point. Among them, green is the connection we want to add, and black is the original connection. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj0zk657mmj30qq0doabd.jpg) +![](https://p.ipic.vip/y2qhfk.jpg) Let's look at a more complicated one: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj0zl95r69j31040m6tbc.jpg) +![](https://p.ipic.vip/w0oy7x.jpg) In fact, no matter how complicated it is. We only need to perform a mid-sequence traversal once, and record the precursor nodes at the same time. Then you can modify the pointers of the precursor node and the current node. The whole process is as if the linked list is reversed. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjufqa8pk7j30dm07cwev.jpg) +![](https://p.ipic.vip/prjau5.jpg) Core code (assuming we have calculated the pre correctly): @@ -136,4 +136,4 @@ return self. ans If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/70qh9q.jpg) diff --git a/problems/binode-lcci.md b/problems/binode-lcci.md index b440aaf5a..386ce5b3b 100644 --- a/problems/binode-lcci.md +++ b/problems/binode-lcci.md @@ -51,15 +51,15 @@ https://leetcode-cn.com/problems/binode-lcci/ 其中绿色是我们要增加的连线,而黑色是是原本的连线。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj0zk657mmj30qq0doabd.jpg) +![](https://p.ipic.vip/91t658.gif) 我们再来看一个复杂一点的: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj0zl95r69j31040m6tbc.jpg) +![](https://p.ipic.vip/v4jgm0.jpg) 实际上,不管多么复杂。 我们只需要进行一次**中序遍历**,同时记录前驱节点。然后修改前驱节点和当前节点的指针即可,整个过程就好像是链表反转。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjufqa8pk7j30dm07cwev.jpg) +![](https://p.ipic.vip/rizxay.jpg) 核心代码(假设 pre 我们已经正确计算出了): @@ -137,4 +137,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/7nkycx.jpg) diff --git a/problems/consecutive-wins.md b/problems/consecutive-wins.md index fc3d94bb6..b5dbefdf7 100644 --- a/problems/consecutive-wins.md +++ b/problems/consecutive-wins.md @@ -54,7 +54,7 @@ Here are the ways in which we can win 2 or fewer times consecutively: 用图来表示就是如下的样子: -![图采用力扣加加刷题插件制作](https://tva1.sinaimg.cn/large/008eGmZEly1gotv04k40uj30jg0glmzp.jpg) +![图采用力扣加加刷题插件制作](https://p.ipic.vip/kwdjfk.jpg) 不是一般性,我们可以得出如下的转移方程: diff --git a/problems/get-kth-magic-number-lcci.md b/problems/get-kth-magic-number-lcci.md index 43fcc7708..b7d9df1ab 100644 --- a/problems/get-kth-magic-number-lcci.md +++ b/problems/get-kth-magic-number-lcci.md @@ -89,4 +89,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 36K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ts7jth.jpg) diff --git a/problems/lcp20.meChtZ.md b/problems/lcp20.meChtZ.md index 6e5f9c249..893794078 100644 --- a/problems/lcp20.meChtZ.md +++ b/problems/lcp20.meChtZ.md @@ -69,7 +69,7 @@ https://leetcode-cn.com/problems/meChtZ/ 那么如何模拟呢?这里的模拟思路其实和回溯是一样的。我们可以使用递归控制一个变量,递归函数内部控制另外一个变量。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gn7w8lgdi2j30v80iggmi.jpg) +![](https://p.ipic.vip/7tk8cm.jpg) 具体来说,我们可以用递归控制当前位置这一变量,递归函数内部循环遍历 jumps。自然语言表达就是**对于每一个位置 pos,我们都可以选择我先走一步(之后怎么走不管)到终点或者先乘坐一个公交车(之后怎么走不管)到终点**。 @@ -145,4 +145,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/5yfrpj.jpg) diff --git a/problems/lcp21.Za25hA.md b/problems/lcp21.Za25hA.md index 6cb8943e9..10c86cb19 100644 --- a/problems/lcp21.Za25hA.md +++ b/problems/lcp21.Za25hA.md @@ -170,4 +170,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/aqenb3.jpg) diff --git a/problems/max-black-square-lcci.md b/problems/max-black-square-lcci.md index 60f8959ac..8e407b8ec 100644 --- a/problems/max-black-square-lcci.md +++ b/problems/max-black-square-lcci.md @@ -50,7 +50,7 @@ matrix.length == matrix[0].length <= 200 如下图,红色部分就是答案。只需要保证边全部是 0 就好了,所以里面有一个 1 无所谓的。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glolibd04ij30z90u0n10.jpg) +![](https://p.ipic.vip/8ty63s.jpg) 我们不妨从局部入手,看能不能打开思路。 @@ -60,11 +60,11 @@ matrix.length == matrix[0].length <= 200 在上面的例子中,不难看出其最大黑方阵不会超过 min(4, 5)。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glolvo257pj30yr0u0780.jpg) +![](https://p.ipic.vip/fq45s1.jpg) 那答案直接就是 4 么? 对于这种情况是的, 但是也存在其他情况。比如: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glolynuddvj30u00umtcx.jpg) +![](https://p.ipic.vip/vns704.jpg) 因此解空间上界虽然是 4,但是下界仍然可能为 1。 @@ -97,7 +97,7 @@ matrix.length == matrix[0].length <= 200 看一下图或许好理解一点。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glon25oegxj310f0u042g.jpg) +![](https://p.ipic.vip/anlw6c.jpg) 如上图就是尝试 2 是否可行,如果可行,我们继续**得寸进尺**,直到不可行或者到上界。 @@ -112,7 +112,7 @@ matrix.length == matrix[0].length <= 200 比如上面提到的向上向左探测的过程,如果上面和左面格子的扩展结果已经计算出来了,那么直接用就行了,这部分延伸的复杂度可以降低到 $O(1)$。因此不难看出, 当前格子的计算依赖于左侧和上方格子,因此使用**从左到右从上到下扫描矩阵** 是正确的选择,因为我们需要在遍历当当前格子的时候**左侧和上方格子的结果已经被计算出来了**。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gloo8fqjxwj318w0u07dd.jpg) +![](https://p.ipic.vip/lakbpv.jpg) 1. (4,5) 找到上方相邻的格子,如果是 1 直接返回。 2. 如果上方格子值是 0 ,去 memo 中查询。 diff --git a/problems/sub-sort-lcci.md b/problems/sub-sort-lcci.md index 59a8afb22..03cdead69 100644 --- a/problems/sub-sort-lcci.md +++ b/problems/sub-sort-lcci.md @@ -86,4 +86,4 @@ class Solution: 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/3apc01.jpg) diff --git a/selected/LCS.md b/selected/LCS.md index c7c492e43..4fd31800a 100644 --- a/selected/LCS.md +++ b/selected/LCS.md @@ -185,7 +185,7 @@ https://leetcode-cn.com/problems/uncrossed-lines/description/ 示例 1: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ggbkku13xuj315x0u0abp.jpg) +![](https://p.ipic.vip/dumeqf.jpg) 输入:A = [1,4,2], B = [1,2,4] 输出:2 @@ -257,4 +257,4 @@ class Solution: 更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/epq5vl.jpg) diff --git a/selected/LIS.md b/selected/LIS.md index c7f122a22..702cbebcc 100644 --- a/selected/LIS.md +++ b/selected/LIS.md @@ -38,7 +38,7 @@ https://leetcode-cn.com/problems/longest-increasing-subsequence 题目的意思是让我们从给定数组中挑选若干数字,这些数字满足: `如果 i < j 则 nums[i] < nums[j]`。问:一次可以挑选最多满足条件的数字是多少个。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyyu7187bj31ku0igq6f.jpg) +![](https://p.ipic.vip/7tda84.jpg) 这种子序列求极值的题目,应该要考虑到贪心或者动态规划。这道题贪心是不可以的,我们考虑动态规划。 @@ -51,21 +51,21 @@ https://leetcode-cn.com/problems/longest-increasing-subsequence 第一种定义方式虽然需要比较不同的 dp[i] 从而获得结果,但是我们可以在循环的时候顺便得出,对复杂度不会有影响,只是代码多了一点而已。因此我们**选择第一种建模方式**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyyz18gu6j31t40dy77l.jpg) +![](https://p.ipic.vip/itmnki.jpg) 由于 dp[j] 中一定会包括 j,且以 j 结尾, 那么 nums[j] 一定是其所形成的序列中最大的元素,那么如果位于其后(意味着 i > j)的 nums[i] > nums[j],那么 nums[i] 一定能够融入 dp[j] 从而形成更大的序列,这个序列的长度是 dp[j] + 1。因此状态转移方程就有了:`dp[i] = dp[j] + 1 (其中 i > j, nums[i] > nums[j])` 以 `[10, 9, 2, 5, 3, 7, 101, 18]` 为例,当我们计算到 dp[5]的时候,我们需要往回和 0,1,2,3,4 进行比较。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfzzp18iyej311i0o8dk8.jpg) +![](https://p.ipic.vip/iro5el.jpg) 具体的比较内容是: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfzzqeaen1j30um0fwwhd.jpg) +![](https://p.ipic.vip/802b59.jpg) 最后从三个中选一个最大的 + 1 赋给 dp[5]即可。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfzzt54n5wj30ys05g74x.jpg) +![](https://p.ipic.vip/kcy9j7.jpg) **记住这个状态转移方程,后面我们还会频繁用到。** @@ -138,7 +138,7 @@ https://leetcode-cn.com/problems/non-overlapping-intervals/ 如果对区间按照起点或者终点进行排序,那么就转化为上面的最长递增子序列问题了。和上面问题不同的是,由于是一个区间。因此实际上,我们是需要拿**后面的开始时间**和**前面的结束时间**进行比较。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfyzp8n59cj31000a2jse.jpg) +![](https://p.ipic.vip/a6eh13.jpg) 而由于: @@ -296,7 +296,7 @@ class Solution: 大家想看效率高的,其实也不难。 LIS 也可以用 **贪心 + 二分** 达到不错的效率。代码如下: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gl6ajh887vj31zc0gmae6.jpg) +![](https://p.ipic.vip/zt3tzj.jpg) 代码文字版如下: @@ -492,4 +492,4 @@ class Solution: 更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 38K star 啦。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/ninoev.jpg) diff --git a/selected/LSS.md b/selected/LSS.md index 2bed9d6ad..8e9b96322 100644 --- a/selected/LSS.md +++ b/selected/LSS.md @@ -121,7 +121,7 @@ class Solution: 举例说明,如下图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gds543yp2cj31400u0myf.jpg) +![](https://p.ipic.vip/sc2mro.jpg) (by [snowan](https://github.com/snowan)) 这种做法的时间复杂度为 O(N\*logN), 空间复杂度为 O(1)。 @@ -243,7 +243,7 @@ class Solution: 举例说明,如下图: -![53.maximum-sum-subarray-dp.png](https://tva1.sinaimg.cn/large/007S8ZIlly1gds544xidoj30pj0h2wew.jpg) +![53.maximum-sum-subarray-dp.png](https://p.ipic.vip/x9jn5o.jpg) (by [snowan](https://github.com/snowan)) 这种算法的时间复杂度 O(N), 空间复杂度为 O(1) diff --git a/selected/a-deleted.md b/selected/a-deleted.md index 18106102f..c4ce54924 100644 --- a/selected/a-deleted.md +++ b/selected/a-deleted.md @@ -52,7 +52,7 @@ num 不会包含任何前导零。 以题目中的 `num = 1432219, k = 3` 为例,我们需要返回一个长度为 4 的字符串,问题在于: 我们怎么才能求出这四个位置依次是什么呢? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfr0o3bz8aj30ya0he75v.jpg) +![](https://p.ipic.vip/stdrvp.jpg) (图 1) @@ -76,19 +76,19 @@ num 不会包含任何前导零。 以题目中的 `num = 1432219, k = 3` 为例的图解过程如下: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfr3me4mltj30u00xjgp5.jpg) +![](https://p.ipic.vip/8jxf63.jpg) (图 2) 由于没有左侧相邻元素,因此**没办法丢弃**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfr3p4idahj30sk116dj7.jpg) +![](https://p.ipic.vip/zi6ehp.jpg) (图 3) 由于 4 比左侧相邻的 1 大。如果选择丢弃左侧的 1,那么会使得剩下的数字更大(开头的数从 1 变成了 4)。因此我们仍然选择**不丢弃**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfr3rtp1b1j30tk12etcr.jpg) +![](https://p.ipic.vip/pfq2jw.jpg) (图 4) @@ -107,7 +107,7 @@ num 不会包含任何前导零。 上面的思路可行,但是稍显复杂。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfk7m9z3elj30zk0i01kx.jpg) +![](https://p.ipic.vip/oeib5j.jpg) (图 5) 我们需要把思路逆转过来。刚才我的关注点一直是**丢弃**,题目要求我们丢弃 k 个。反过来说,不就是让我们保留 $n - k$ 个元素么?其中 n 为数字长度。 那么我们只需要按照上面的方法遍历完成之后,再截取前**n - k**个元素即可。 @@ -290,7 +290,7 @@ k = 3 实际上这个过程有点类似`归并排序`中的**治**,而上面我们分别计算 num1 和 num2 的最大数的过程类似`归并排序`中的**分**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfruuvyrn5j31mk0i8414.jpg) +![](https://p.ipic.vip/5sx28e.jpg) (图 6) 代码: @@ -324,7 +324,7 @@ A < B # False 以合并 [6] 和 [9,5,8,3] 为例,图解过程如下: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfruxjfwlhj31cu0u07c0.jpg) +![](https://p.ipic.vip/1tuzsh.jpg) (图 7) 具体算法: @@ -381,4 +381,4 @@ class Solution: 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/a6klat.jpg) diff --git a/selected/atMostK.md b/selected/atMostK.md index 8d955b174..d057e999c 100644 --- a/selected/atMostK.md +++ b/selected/atMostK.md @@ -32,7 +32,7 @@ 一种思路是总的连续子数组个数等于:**以索引为 0 结尾的子数组个数 + 以索引为 1 结尾的子数组个数 + ... + 以索引为 n - 1 结尾的子数组个数**,这无疑是完备的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj6m27kgbsj306u06gt8u.jpg) +![](https://p.ipic.vip/y4m3yr.jpg) 同时**利用母题 0 的前缀和思路, 边遍历边求和。** @@ -131,7 +131,7 @@ function countSubArray(k, nums) { 实际上是 betweenK 可以直接利用 atMostK,即 atMostK(k1, nums) - atMostK(k2 - 1, nums),其中 k1 > k2。前提是值是离散的, 比如上面我出的题都是整数。 因此我可以直接 减 1,因为 **1 是两个整数最小的间隔**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj8m692laxj30pz0grte9.jpg) +![](https://p.ipic.vip/kr5vog.jpg) 如上,`小于等于 10 的区域`减去 `小于 5 的区域`就是 `大于等于 5 且小于等于 10 的区域`。 @@ -569,11 +569,11 @@ class Solution: **注意到里层的 while 循环是连续的数组全部加上一个数字,不难想到可以利用母题 0 的前缀和思路优化。** -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj8k7w0bqyj30qh07540b.jpg) +![](https://p.ipic.vip/h3bpuz.jpg) 一种思路就是在 i 的位置 + k, 然后利用前缀和的技巧给 i 到 n 的元素都加上 k。但是题目需要加的是一个区间, j + 1 及其之后的元素会被多加一个 k。一个简单的技巧就是给 j + 1 的元素减去 k,这样正负就可以抵消。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj8k997nmbj30q9074dhm.jpg) +![](https://p.ipic.vip/hirwze.jpg) > 1094. 拼车 是这道题的换皮题, 思路一模一样。 diff --git a/selected/byte-dance-algo-ex-2017.md b/selected/byte-dance-algo-ex-2017.md index 6bf556d9d..7ebfd7d96 100644 --- a/selected/byte-dance-algo-ex-2017.md +++ b/selected/byte-dance-algo-ex-2017.md @@ -4,7 +4,7 @@ 这套题一共 11 道题, 三道编程题, 八道问答题。本次给大家带来的就是这三道编程题。更多精彩内容,请期待我的搞定算法面试专栏。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gip1ab1tb9j311b0u0jzi.jpg) +![](https://p.ipic.vip/5cu79p.jpg) 其中有一道题《异或》我没有通过所有的测试用例, 小伙伴可以找找茬,第一个找到并在公众号力扣加加留言的小伙伴奖励现金红包 10 元。 @@ -77,7 +77,7 @@ c-b<=10 实际上,这道题就是一个决策树, 我画个决策树出来你就明白了。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gip1se8id8j31p60u0n6z.jpg) +![](https://p.ipic.vip/o9lenf.jpg) > 图中红色边框表示自身可以组成套题的一部分, 我也用文字进行了说明。#2 代表第二题, #3 代表第三题。 @@ -85,7 +85,7 @@ c-b<=10 需要特别注意的是,由于需要凑整, 因此你需要使得题目的总数是 3 的倍数向上取整。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gip1x5c97lj30cs0eoq3c.jpg) +![](https://p.ipic.vip/4ifglo.jpg) ### 代码 @@ -189,7 +189,7 @@ print(cnt + 3 - cur) 1234 ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gip1zqrphpj30ae0cswex.jpg) +![](https://p.ipic.vip/p7s7t1.jpg) 先比较第一位,1 比较 0 大, 因此 1234 最大。再比较第二位, 4 比 1 大, 因此 456 大于 123,后面位不需要比较了。这其实就是剪枝的思想。 @@ -263,7 +263,7 @@ a 和 b 有共同的前缀 111,c 和 a 异或过了,当再次和 b 异或的 树的每一个节点存储的是:**n 个数中,从根节点到当前节点形成的前缀有多少个是一样的**,即多少个数的前缀是一样的。这样可以剪枝,提前退出的时候,就直接取出来用了。比如异或的结果是 1, m 当前二进制位是 0 ,那么这个前缀有 10 个,我都不需要比较了, 计数器直接 + 10 。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gip21tqf5gj31vl0u0n61.jpg) +![](https://p.ipic.vip/qgou7j.jpg) > 我用 17 直接复杂度过高,目前仅仅通过了 70 % - 80 % 测试用例, 希望大家可以帮我找找毛病,我猜测是语言的锅。 @@ -380,7 +380,7 @@ print(sorted(nums)[m - 1]) 接下来,我带你继续分析。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gipczpnhjij32440u0h2w.jpg) +![](https://p.ipic.vip/q0qb8q.jpg) 如图, 红色表示根节点。节点表示一个十进制数, **树的路径存储真正的数字**,比如图上的 100,109 等。 这不就是上面讲的前缀树么? @@ -467,11 +467,11 @@ class Solution: 它的孩子节点个数是 `20 - 10 = 10` 。 也就是它的**右边的兄弟节点的第一个子节点** 减去 它的**第一个子节点**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gipel153igj31u40r8qd0.jpg) +![](https://p.ipic.vip/7sfmam.jpg) 由于是完全十叉树,而不是满十叉树 。因此你需要考虑边界情况,比如题目的 n 是 15。 那么 1 的子节点个数就不是 20 - 10 = 10 了, 而是 15 - 10 + 1 = 16。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gipemlbs0cj31ty0mm79i.jpg) +![](https://p.ipic.vip/6qkn83.jpg) 其他也是类似的过程, 我们只要: @@ -527,4 +527,4 @@ print(findKthNumber(n, m)) 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/5pin2k.jpg) diff --git a/selected/byte-dance-algo-ex.md b/selected/byte-dance-algo-ex.md index d159f0db0..4a34091ce 100644 --- a/selected/byte-dance-algo-ex.md +++ b/selected/byte-dance-algo-ex.md @@ -10,7 +10,7 @@ 另一个问答是红包题目,这里不多说了。我们重点看一下剩下两个算法编程题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gigxwqs84rj312d0u0the.jpg) +![](https://p.ipic.vip/23f5lt.jpg) > 两个问答题由于不能在线判题,我没有做,只做了剩下两个编程题。 @@ -170,19 +170,19 @@ for i in range(t): > lucifer 小提示: 左侧的数字表示此时窗口大小,黄色格子表示修补的墙,黑色方框表示的是窗口。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gih11ey3hhj30ks05o0sx.jpg) +![](https://p.ipic.vip/p5x8po.jpg) 这里我形象地将 0 看成是洞,1 看成是墙, 我们的目标就是补洞,使得连续的墙最长。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gih12xgf04j30ik054dfx.jpg) +![](https://p.ipic.vip/u8ipyt.jpg) 每次碰到一个洞,我们都去不加选择地修补。由于 m 等于 1, 也就是说我们最多补一个洞。因此需要在修补超过一个洞的时候,我们需要调整窗口范围,使得窗口内最多修补一个墙。由于窗口表示的就是连续的墙(已有的或者修补的),因此最终我们返回窗口的最大值即可。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gih1588r5kj30xe0dm770.jpg) +![](https://p.ipic.vip/b87j8h.jpg) > 由于下面的图窗口内有两个洞,这和”最多补一个洞“冲突, 我们需要收缩窗口使得满足“最多补一个洞”的先决条件。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gih1ac1v5ij30o60ba76r.jpg) +![](https://p.ipic.vip/tkbcld.jpg) 因此最大的窗口就是 max(2, 3, 4, ...) = 4。 @@ -266,4 +266,4 @@ print(max(ans, j - i + 1)) 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/6ft83c.jpg) diff --git a/selected/construct-binary-tree.md b/selected/construct-binary-tree.md index 90b89e31b..2b884cff5 100644 --- a/selected/construct-binary-tree.md +++ b/selected/construct-binary-tree.md @@ -34,30 +34,30 @@ ### 思路 我们以题目给出的测试用例来讲解: -![](https://pic.leetcode-cn.com/584db66158d2b497b9fdd69b5dc10c3a76db6e2c0f6cff68789cfb79807b0756.jpg) +![](https://p.ipic.vip/1ir43q.jpg) 前序遍历是`根左右`,因此 preorder 第一个元素一定整个树的根。由于题目说明了没有重复元素,因此我们可以通过 val 去 inorder 找到根在 inorder 中的索引 i。 而由于中序遍历是`左根右`,我们容易找到 i 左边的都是左子树,i 右边都是右子树。 我使用红色表示根,蓝色表示左子树,绿色表示右子树。 -![](https://pic.leetcode-cn.com/faea3d9a78c1fa623457b28c8d20e09a47bb0911d78ff53f42fab0e463a7755d.jpg) +![](https://p.ipic.vip/47ywwa.jpg) 根据此时的信息,我们能构造的树是这样的: -![](https://pic.leetcode-cn.com/261696c859c562ca31dface08d3020bcd20362ab2205d614473cca02b1635eb0.jpg) +![](https://p.ipic.vip/hbznvj.jpg) 我们 preorder 继续向后移动一位,这个时候我们得到了第二个根节点”9“,实际上就是左子树的根节点。 -![](https://pic.leetcode-cn.com/eb8311e01ed86007b23460d6c933b53ad14bec2d63a0dc01f625754368f22376.jpg) +![](https://p.ipic.vip/k7hkj4.jpg) 我们 preorder 继续向后移动一位,这个时候我们得到了第二个根节点”20“,实际上就是右子树的根节点。其中右子树由于个数大于 1,我们无法确定,我们继续执行上述逻辑。 -![](https://pic.leetcode-cn.com/d90dc9bae9d819da997eb67d445524c8ef39ce2a4a8defb16b5a3b6b2a0fc783.jpg) +![](https://p.ipic.vip/8zc2e6.jpg) 根据此时的信息,我们能构造的树是这样的: -![](https://pic.leetcode-cn.com/f8553f668bed9f897f393a24d78e4469c4b5503c4ba8c59e90dca1b19acf4de5.jpg) +![](https://p.ipic.vip/qvjh0a.jpg) 我们不断执行上述逻辑即可。简单起见,递归的时候每次我都开辟了新的数组,这个其实是没有必要的,我们可以通过四个变量来记录 inorder 和 preorder 的起始位置即可。 @@ -117,26 +117,26 @@ class Solution: ### 思路 我们以题目给出的测试用例来讲解: -![](https://pic.leetcode-cn.com/fb9d700a67d70b5e68461fa1f0438d9c5c676557a776eda4cd1b196c41ce65a1.jpg) +![](https://p.ipic.vip/r78dsl.jpg) 后序遍历是`左右根`,因此 postorder 最后一个元素一定整个树的根。由于题目说明了没有重复元素,因此我们可以通过 val 去 inorder 找到根在 inorder 中的索引 i。 而由于中序遍历是`左根右`,我们容易找到 i 左边的都是左子树,i 右边都是右子树。 我使用红色表示根,蓝色表示左子树,绿色表示右子树。 -![](https://pic.leetcode-cn.com/10176eec270c90d8e0bd4640a628e9320b7d5c30f3c62ffdb1fd2800d87c6f7b.jpg) +![](https://p.ipic.vip/35n3lv.jpg) 根据此时的信息,我们能构造的树是这样的: -![](https://pic.leetcode-cn.com/261696c859c562ca31dface08d3020bcd20362ab2205d614473cca02b1635eb0.jpg) +![](https://p.ipic.vip/hbznvj.jpg) 其中右子树由于个数大于 1,我们无法确定,我们继续执行上述逻辑。我们 postorder 继续向前移动一位,这个时候我们得到了第二个根节点”20“,实际上就是右子树的根节点。 -![](https://pic.leetcode-cn.com/e6cac2b6a956c09d977c4cfd7883268644b42bdd0531a509d24b4aafebc147c4.jpg) +![](https://p.ipic.vip/kyjr7z.jpg) 根据此时的信息,我们能构造的树是这样的: -![](https://pic.leetcode-cn.com/f8553f668bed9f897f393a24d78e4469c4b5503c4ba8c59e90dca1b19acf4de5.jpg) +![](https://p.ipic.vip/qvjh0a.jpg) 我们不断执行上述逻辑即可。简单起见,递归的时候每次我都开辟了新的数组,这个其实是没有必要的,我们可以通过四个变量来记录 inorder 和 postorder 的起始位置即可。 @@ -195,7 +195,7 @@ pre[] 和 post[] 都是 1, 2, ..., pre.length 的排列 ### 思路 我们以题目给出的测试用例来讲解: -![](https://pic.leetcode-cn.com/584db66158d2b497b9fdd69b5dc10c3a76db6e2c0f6cff68789cfb79807b0756.jpg) +![](https://p.ipic.vip/1ir43q.jpg) 前序遍历是`根左右`,因此 preorder 第一个元素一定整个树的根,preorder 第二个元素(如果存在的话)一定是左子树。由于题目说明了没有重复元素,因此我们可以通过 val 去 postorder 找到 pre[1]在 postorder 中的索引 i。 而由于后序遍历是`左右根`,因此我们容易得出。 postorder 中的 0 到 i(包含)是左子树,preorder 的 1 到 i+1(包含)也是左子树。 @@ -256,4 +256,4 @@ node.right = self.constructFromPrePost(pre[i + 2:], post[i + 1:-1]) 大家也可以关注我的公众号《力扣加加》获取更多更新鲜的 LeetCode 题解 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/vzbaxz.jpg) diff --git a/selected/mother-01.md b/selected/mother-01.md index 05dcea61e..0400a7256 100644 --- a/selected/mother-01.md +++ b/selected/mother-01.md @@ -2,7 +2,7 @@ 记得我初中的时候,学校发的一个小册子的名字就是母题啥的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghbhlyhaadj308c08c3yv.jpg) +![](https://p.ipic.vip/blev8y.jpg) 大概意思是市面上的题(尤其是中考题)都是这些母题生的,都是它们的儿子。 @@ -386,7 +386,7 @@ mergeKLists 执行了 logk 次,每次都执行一次 mergeTwoLists,mergeTwoL 最后送大家一张全家福: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghbq8s05y0j31620u0gs0.jpg) +![](https://p.ipic.vip/lhef50.jpg) ## 子题 @@ -406,4 +406,4 @@ mergeKLists 执行了 logk 次,每次都执行一次 mergeTwoLists,mergeTwoL 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/cn09i2.jpg) diff --git a/selected/schedule-topic.md b/selected/schedule-topic.md index 067a1301c..eff796409 100644 --- a/selected/schedule-topic.md +++ b/selected/schedule-topic.md @@ -55,7 +55,7 @@ MyCalendar.book(20, 30); // returns true 对于两个 calendar,我们的判断逻辑都是一样的。假设连个 calendar 分别是`[s1, e1]`和`[s2, e2]`。那么如果`s1 >= e2 or s2 <= e1`, 则两个课程没有交叉,可以预定,否则不可以。如图,1,2,3 可以预定,剩下的不可以。 -![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbj1o8hvivj20w20ra76f.jpg) +![image.png](https://p.ipic.vip/f1rf2b.jpg) 代码是这样的: @@ -159,11 +159,11 @@ class MyCalendar: 和上面思路类似,只不过我们每次都对 calendars 进行排序,那么我们可以通过二分查找日程安排的情况来检查新日常安排是否可以预订。如果每次插入之前都进行一次排序,那么时间复杂度会很高。如图,我们的[s1,e1], [s2,e2], [s3,e3] 是按照时间顺序排好的日程安排。我们现在要插入[s,e],我们使用二分查找,找到要插入的位置,然后和插入位置的课程进行一次比对即可,这部分的时间复杂度是 $O(logN)$。 -![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbj28k6v4gj21100c2754.jpg) +![image.png](https://p.ipic.vip/u4fegk.jpg) 我们考虑使用平衡二叉树来维护这种动态的变化,在最差的情况时间复杂度会退化到上述的$O(N^2)$,平均情况是$O(NlogN)$,其中 N 是已预订的日常安排数。 -![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbj2dirnf0j20xs0fe75j.jpg) +![image.png](https://p.ipic.vip/jis4ob.jpg) ### 代码 @@ -468,11 +468,11 @@ class MyCalendarThree(object): 比如预定[1,3]和[5,7],我们产生一个预定即可: -![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbj50c37suj212q0bcq3t.jpg) +![image.png](https://p.ipic.vip/ctg91m.jpg) 再比如预定[1,5]和[3,7],我们需要两个预定: -![image.png](http://ww1.sinaimg.cn/large/e9f490c8ly1gbj45oq6fhj213e0ca0tm.jpg) +![image.png](https://p.ipic.vip/ouazzy.jpg) 我们可以使用红黑树来简化时间复杂度,如果你使用的是 Java,可以直接使用现成的数据结构 TreeMap。我这里偷懒,每次都排序,时间复杂度会很高,但是可以 AC。 diff --git a/selected/serialize.md b/selected/serialize.md index 84d08d609..530673029 100644 --- a/selected/serialize.md +++ b/selected/serialize.md @@ -12,7 +12,7 @@ 这样的数据结构来描述一颗树: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh2dqqnyzwj30ba0baglw.jpg) +![](https://p.ipic.vip/y0u9fo.jpg) ([1,2,3,null,null,4,5] 对应的二叉树) @@ -92,7 +92,7 @@ class Solution: > 选择这种记法,而不是 DFS 的记法的原因是看起来比较直观 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh2h5bhjryj30b40am74k.jpg) +![](https://p.ipic.vip/qse3bj.jpg) 序列化的代码非常简单, 我们只需要在普通的遍历基础上,增加对空节点的输出即可(普通的遍历是不处理空节点的)。 @@ -142,7 +142,7 @@ public class Codec { 我们先看一个短视频: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh2z5y87n0g30bo05vx6u.gif) +![](https://p.ipic.vip/qbfc18.gif) (动画来自力扣) @@ -232,11 +232,11 @@ class Codec: 如图有这样一棵树: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh2x3gj9n0j30j00gewfx.jpg) +![](https://p.ipic.vip/7h5ws2.jpg) 那么其层次遍历为 [1,2,3,#,#, 4, 5]。我们根据此层次遍历的结果来看下如何还原二叉树,如下是我画的一个示意图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh2x55lh7qj31780t0gq8.jpg) +![](https://p.ipic.vip/w01rtf.jpg) 容易看出: @@ -294,7 +294,7 @@ Python 代码: 我们从马后炮的角度来说,实际上对于序列化来说,BFS 和 DFS 都比较常规。对于反序列化,大家可以像我这样举个例子,画一个图。可以先在纸上,电脑上,如果你熟悉了之后,也可以画在脑子里。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gh30bapydej30rq0tcad5.jpg) +![](https://p.ipic.vip/wjtyzs.jpg) (Like This) diff --git a/selected/zuma-game.md b/selected/zuma-game.md index b95bdd00f..5ecac6730 100644 --- a/selected/zuma-game.md +++ b/selected/zuma-game.md @@ -68,13 +68,13 @@ https://leetcode-cn.com/problems/zuma-game/ 因此我们只需要两个指针记录连续相同颜色球的位置,如果可以消除,消除即可。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjfehgw7lnj31880fydkr.jpg) +![](https://p.ipic.vip/ny6vfo.jpg) 如图,我们记录了连续红球的位置, 如果手上有红球, 则可以尝试将其清除,这一次决策就是回溯树(决策树)的一个分支。之后我们会撤回到这个决策分支, 尝试其他可行的决策分支。 以 board = RRBBRR , hand 为 RRBB 为例,其决策树为: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjfg7kykx3j30u00wc46o.jpg) +![](https://p.ipic.vip/8g512f.jpg) 其中虚线表示无需手动干预,系统自动消除。叶子节点末尾的黄色表示全部消除需要的手球个数。路径上的文字后面的数字表示此次消除需要的手球个数 @@ -95,7 +95,7 @@ while i < len(board): i = j ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gjfegz0iwvj316e0my43t.jpg) +![](https://p.ipic.vip/iwk7wa.jpg) 具体算法: @@ -152,4 +152,4 @@ class Solution: 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 36K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/52jfo7.jpg) diff --git a/thanksGiving.md b/thanksGiving.md index b1e4abfa1..31431d187 100644 --- a/thanksGiving.md +++ b/thanksGiving.md @@ -2,23 +2,23 @@ 就在今天,我的《leetcode题解》项目首次突破1wstar, 在这里我特地写下这篇文章来记录这个时刻,同时非常感谢大家的支持和陪伴。 -![star-history](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlujoaw5nj30nm0gk75u.jpg) +![star-history](https://p.ipic.vip/ngminn.jpg) (star增长曲线图) 前几天,去了一趟山城重庆,在那里遇到了最美的人和最漂亮的风景。 -![chongqing-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlujuyqdqj31pq0u0b2f.jpg) +![chongqing-1](https://p.ipic.vip/4uha9n.jpg) -![chongqing-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluk4ds3dj31pq0u0e87.jpg) +![chongqing-2](https://p.ipic.vip/85963z.jpg) -![chongqing-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluk6vtqtj30u01pq4qv.jpg) +![chongqing-3](https://p.ipic.vip/jx54zu.jpg) 我是一个念旧的人,现在是节后的第一天,让我开启回忆模式: - 2017-05-30 项目成立,那是的它只是用来占位而已,目的就是让自己知道之后要做这件事。 -![first commit](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlukegozyj30bb06yaat.jpg) +![first commit](https://p.ipic.vip/3x958s.jpg) (第一次提交) @@ -28,7 +28,7 @@ 在朋友圈推广: -![朋友圈宣传](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlukey742j30u00zutb3.jpg) +![朋友圈宣传](https://p.ipic.vip/na5bhm.jpg) (在朋友圈宣传) @@ -37,11 +37,11 @@ - 之后我组建了微信和qq群,来让大家活跃起来,促进交流,戒指目前(2019-06-10)微信群总人数已经超过700, 里面有非常多的学生,留学生以及全球各地各大公司的员工。 -![群聊-qq](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlukjake0j30kx04taay.jpg) +![群聊-qq](https://p.ipic.vip/8tj7iu.jpg) (qq群) -![群聊-wechat](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlukllp4vj30l206674y.jpg) +![群聊-wechat](https://p.ipic.vip/4paakc.jpg) (微信群) @@ -49,35 +49,35 @@ 之后先后通过@每日时报, @阮一峰,@d2,@hello-github等的宣传,又迎来的一次高峰, 在那一段时间大概突破了1k。 -![ruanyifeng](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlukqpadhj30u01ixtb3.jpg) +![ruanyifeng](https://p.ipic.vip/olsy7z.jpg) (阮一峰的周报) -![hello-github](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlukrkvgoj30aj05vdg4.jpg) +![hello-github](https://p.ipic.vip/6r7cgm.jpg) (hello-github也收录了我和我的仓库) 二次元的司徒正美老师虽然没有帮忙宣传,但是它的star也在某种程度上起到了宣传作用。 -![司徒正美](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluks197mj30ak05d74q.jpg) +![司徒正美](https://p.ipic.vip/5vyj6j.jpg) (司徒正美) 并且之后这个项目在github trending活跃了一个月左右,甚至有一次冲上了日榜的总榜第一,并被“开发者头条”收入《GitHub Trending - All - Daily》。 -![日榜第一](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlukuiw2wj30u01jp75w.jpg) +![日榜第一](https://p.ipic.vip/fnow1s.jpg) (日榜第一) -![开发者头条](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlul1xszzj30u00y4jt0.jpg) +![开发者头条](https://p.ipic.vip/dwvzgj.jpg) (开发者头条的微博号) 截止到2019-06-10,项目star首次破万,幸运的是我刚好捕捉到了第9999个小可爱. -![9999](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlulasfn9j30u20u0myb.jpg) +![9999](https://p.ipic.vip/6pfwhg.jpg) (9999,一个很有意思的数字) @@ -85,33 +85,33 @@ 现在,项目除了JS,也在逐步加入C++,python,多编程语言正在筹备中。 -![多语言支持](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlulfo1blj30oh0hgdhy.jpg) +![多语言支持](https://p.ipic.vip/0l5ide.jpg) (我们正在努力加入更多编程语言) 另外,在大家的帮助下,我们也逐步走上了国际化,不仅仅有人来主动做翻译,还组建了电报群。 -![英文主页](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlulksyq8j30oy0if0un.jpg) +![英文主页](https://p.ipic.vip/0i0258.jpg) (英文主页) -![英语进展](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlulrp8rhj30r50fd0uk.jpg) +![英语进展](https://p.ipic.vip/sd2sxr.jpg) (英文翻译进展) 也不知道什么时候,《量子论》竟然悄悄地在知乎帮我宣传。 -![量子论](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlulxeyldj30u01k0mze.jpg) +![量子论](https://p.ipic.vip/e6v3x7.jpg) (知乎 - 量子论) 与此同时,我在知乎的最高赞竟然给了这条评论。 -![知乎点赞](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluly5zchj31390kgjs1.jpg) +![知乎点赞](https://p.ipic.vip/admds4.jpg) - 2019-06-04 首次在三个群里同步开通《每日一题》,大家也非常踊跃地帮忙整理题目,甚至出题给思路,非常感谢大家。 -![daily-problems](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlum4w1r1j30zz0f3wgp.jpg) +![daily-problems](https://p.ipic.vip/4ner1u.jpg) 非常感谢大家一直以来的陪伴和支持,我们一起努力,加油💪。 diff --git a/thanksGiving2.md b/thanksGiving2.md index d74bc0d09..e782231fc 100644 --- a/thanksGiving2.md +++ b/thanksGiving2.md @@ -1,17 +1,17 @@ 假期这几天我买了《逆转裁判 123》合集,面对着这香喷喷的冷饭吃了半天。从 GBA 玩到 NDS,从 NDS 玩到 3DS, 现在 NS 虽然没有出新作有点遗憾。不过有了高清重制,也当是个回忆和收藏了 🎉🎉 目前打通了第一第二关,剩下的过一段时间再玩好啦 😁 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluewwy6cj30u01pp0vq.jpg) +![](https://p.ipic.vip/m1eixt.jpg) 回到正题,就在今天,我的《leetcode 题解》项目成功突破 2w star, 并且现在 Github 搜索关键字"LeetCode"我的项目已经排名第一啦,这是继 1W star 之后的第二个巨大突破,非常感谢大家一路以来的支持和陪伴。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlueyaj9xj310m0fm76u.jpg) +![](https://p.ipic.vip/c2pxwa.jpg) 最近在写一本关于 LeetCode 题解的书,有很多人表示想买,这无形之中给了我很大的压力,名字还没定,暂时给它取一个代号《攻克 LeetCode》。 ## 新书《攻克 LeetCode》 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluez7qjqj305i04bt8j.jpg) +![](https://p.ipic.vip/4eraft.jpg) 这里是[《攻克 LeetCode》的草稿目录](https://lucifer.ren/blog/2019/10/03/draft/),目前有 20 章的内容,本书要讲的内容就是 LeetCode 上反复出现的算法,经过我进一步提炼,抽取数百道题目在这里进行讲解,帮助大家理清整体思绪,从而高效率地刷题,做到事半功倍。我这里总结了 7 个常见的数据结构和 7 个常见的算法以及 5 个常见的算法思想。 @@ -27,11 +27,11 @@ ## 2W star 截图 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluf0b2ogj30rm0ld42o.jpg) +![](https://p.ipic.vip/pfkp2n.jpg) ## Star 曲线 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluf5fqdqj30p00f1jry.jpg) +![](https://p.ipic.vip/k8ymwt.jpg) (star 增长曲线图) @@ -41,29 +41,29 @@ 上回提到知乎上的“量子位”在帮我做宣传,引入了不小的流量。 我就想为什么不自己去拉流量呢?我自己以作者的角度去回答一些问题岂不是更好,更受欢迎么?于是我就开始在知乎上回答问题,很开心其中一个还获得了专业认可。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluf67odgj30jw0gl419.jpg) +![](https://p.ipic.vip/306jlu.jpg) 事实上并没有我想的那么好,我回答了两个 LeetCode 话题的内容,虽然也有几百的点赞和感谢,但是这离我的目标还差很远。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluf78bfsj309q0b1mxu.jpg) +![](https://p.ipic.vip/qql53i.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufgjowdj30jk0h4tbr.jpg) +![](https://p.ipic.vip/l9j3ml.jpg) 但是转念一想,我知乎刚起步,也没什么粉丝,并且写答案的时间也就一个月左右,这样想就好多了。 我相信将来会有更多的人看到我的答案,然后加入进来。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufh06drj308907wjrh.jpg) +![](https://p.ipic.vip/zlvpiu.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufi2992j30to0pwwf3.jpg) +![](https://p.ipic.vip/4tnu1d.jpg) ## 建立自己的博客 现在我发表的文章都是在各大平台。这有一个很大的问题就是各个平台很难满足你的需求,比如按照标签,按照日期进行归档。 甚至很多平台的阅读体验很差,比如没有导航功能,广告太多等。因此我觉得自己搭建一个博客还是很有必要的,这个渠道也为我吸引了少部分的流量,目前添加的主要内容大概有: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufj0k9cj308m07aq37.jpg) +![](https://p.ipic.vip/6fbi4i.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufjy45rj30800hct92.jpg) +![](https://p.ipic.vip/7dgeym.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufkukm5j307z08zjrh.jpg) +![](https://p.ipic.vip/fonj3b.jpg) 总体上来说效果还是不错的,之后的文章会在博客首发,各个平台也会陆续更新,感兴趣的可以来个 RSS 订阅,订阅方式已经在[《每日一荐 - 九月刊》](https://lucifer.ren/blog/2019/09/30/daily-featured-2019-09/)里面介绍了。 @@ -71,23 +71,23 @@ GithubDaily 转载了量子位的文章也为我的仓库涨了至少几百的 star,非常感谢。GithubDaily 是一个拥有 3W 多读者的公众号,大家有兴趣的可以关注一波。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufp2p2rj30kl0eqwf6.jpg) +![](https://p.ipic.vip/0bi9gh.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufsdrjhj30j90arjrt.jpg) +![](https://p.ipic.vip/69zz2i.jpg) ## 其他自媒体的推荐 一些其他自媒体也会帮忙推广我的项目 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluftnigvj30u00y1dhz.jpg) +![](https://p.ipic.vip/h4t9j2.jpg) ## 口耳相传 我后来才知道竟然有海外华侨和一些华人社区都能看到我了。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufu7k1pj30ky0mm3z4.jpg) +![](https://p.ipic.vip/3pv8ff.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufv86v4j30ss1bmmzb.jpg) +![](https://p.ipic.vip/et0qr0.jpg) (一亩三分地是一个集中讨论美国加拿大留学的论坛) 另外通过朋友之间口耳相传的介绍也变得越来越多。 diff --git a/thanksGiving3.md b/thanksGiving3.md index c9130370f..2e54f8387 100644 --- a/thanksGiving3.md +++ b/thanksGiving3.md @@ -2,13 +2,13 @@ ## 30k 截图 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlum680iij30se0kk75a.jpg) +![](https://p.ipic.vip/3n3xjw.jpg) ## Star 曲线 Start 曲线上来看,有一点放缓。但是整体仍然是明显的上升趋势。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlum932gsj30rz0guwf6.jpg) +![](https://p.ipic.vip/qggv0o.jpg) (star 增长曲线图) @@ -18,15 +18,15 @@ Start 曲线上来看,有一点放缓。但是整体仍然是明显的上升 三月份是满勤奖,四月份有一次忘记了,缺卡一天。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlumahs4sj30wl0q9gqa.jpg) +![](https://p.ipic.vip/cpxgpf.jpg) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlumbcyfbj307h05mt8p.jpg) +![](https://p.ipic.vip/v26lnx.jpg) ## 新书即将上线 新书详情戳这里:[《或许是一本可以彻底改变你刷 LeetCode 效率的题解书》](https://lucifer.ren/blog/2020/04/07/leetcode-book.intro/),目前正在申请书号。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlumccgo6j30zg0l0whj.jpg) +![](https://p.ipic.vip/3h9kjm.jpg) 点名感谢各位作者,审阅,以及行政小姐姐。 @@ -34,7 +34,7 @@ Start 曲线上来看,有一点放缓。但是整体仍然是明显的上升 最近开始做视频题解了,目前更新了五个视频。和文字题解不同,视频题解可以承载的内容会更多。 https://space.bilibili.com/519510412 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlumhxw1pj30qd0jr417.jpg) +![](https://p.ipic.vip/6ldusk.jpg) 我计划更新一些文字题解很难表述的内容,当然还会提供 PPT,如果你喜欢文字,直接看 PPT 即可。 @@ -46,7 +46,7 @@ Start 曲线上来看,有一点放缓。但是整体仍然是明显的上升 我们的官网`力扣加加`上线啦 💐💐💐💐💐,有专题讲解,每日一题,下载区和视频题解,后续会增加更多内容,还不赶紧收藏起来?地址:http://leetcode-solution.cn/ -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlumjr16tj30z60d0753.jpg) +![](https://p.ipic.vip/b8hfh4.jpg) 点名感谢@三天 @CYL @Josephjinn @@ -54,12 +54,12 @@ Start 曲线上来看,有一点放缓。但是整体仍然是明显的上升 很多朋友也在关注我的项目,非常开心。点名感谢 @被单-加加 @童欧巴。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlumkrxucj30tu113gn9.jpg) +![](https://p.ipic.vip/ug8o5n.jpg) ## 交流群 交流群人数也有了很大的提升。 粉丝人数也扩充到了 7000+。交流群数目也增加到了 10 个。其中 QQ 群人数最多,有将近 1800 人。为了限制人数,我开启了收费模式,希望大家不要打我 😂。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlums4vbqj30tk156tar.jpg) +![](https://p.ipic.vip/9rzdnc.jpg) 非常感谢大家一直以来的陪伴和支持,Fighting 💪。 diff --git a/thinkings/GCD.en.md b/thinkings/GCD.en.md index e89da2d3a..7c8835cc1 100644 --- a/thinkings/GCD.en.md +++ b/thinkings/GCD.en.md @@ -65,19 +65,19 @@ If we have a piece of land of 1680 meters \*640 meters, we want to talk about la In fact, this is an application scenario for the greatest common divisor. Our goal is to solve the greatest common divisor of 1680 and 640. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluj0ysrjj30f104zmxs.jpg) +![](https://p.ipic.vip/6ylclm.jpg) Dividing 1680 meters\*640 meters of land is equivalent to dividing 400 meters\*640 meters of land. Why? If the side length of a square divided by 400 meters\*640 meters is x, then there is 640% x==0, then it will definitely satisfy the remaining two pieces of 640 meters\*640 meters. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluj6lpjej30g805aaap.jpg) +![](https://p.ipic.vip/k1j1uf.jpg) We continue to divide the above: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlujd4rhbj307x08v74i.jpg) +![](https://p.ipic.vip/djdnpp.jpg) Until the side length is 80, there is no need to proceed. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlujgvkvbj30aa04umx2.jpg) +![](https://p.ipic.vip/hveyzl.jpg) ## Instance analysis diff --git a/thinkings/GCD.md b/thinkings/GCD.md index b88721cec..c9bcef997 100644 --- a/thinkings/GCD.md +++ b/thinkings/GCD.md @@ -65,19 +65,19 @@ def GCD(a: int, b: int) -> int: 实际上这正是一个最大公约数的应用场景,我们的目标就是求解 1680 和 640 的最大公约数。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluj0ysrjj30f104zmxs.jpg) +![](https://p.ipic.vip/qblo0s.jpg) 将 1680 米 \* 640 米 的土地分割,相当于对将 400 米 \* 640 米 的土地进行分割。 为什么呢? 假如 400 米 \* 640 米分割的正方形边长为 x,那么有 640 % x == 0,那么肯定也满足剩下的两块 640 米 \* 640 米的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluj6lpjej30g805aaap.jpg) +![](https://p.ipic.vip/vglto7.jpg) 我们不断进行上面的分割: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlujd4rhbj307x08v74i.jpg) +![](https://p.ipic.vip/noxwrq.jpg) 直到边长为 80,没有必要进行下去了。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlujgvkvbj30aa04umx2.jpg) +![](https://p.ipic.vip/nfbmso.jpg) ## 实例解析 diff --git a/thinkings/backtrack.en.md b/thinkings/backtrack.en.md index 951cc69aa..efcd25df7 100644 --- a/thinkings/backtrack.en.md +++ b/thinkings/backtrack.en.md @@ -10,21 +10,21 @@ The backtracking method can be abstract as a tree structure, and it is a tree of Take a subset of the array [1,2,3] as an example: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkau6ustfdj30v80igtag.jpg) +![](https://p.ipic.vip/g9vawf.jpg) > The for loop is used to enumerate the division points. In fact, the interval dp division interval is a similar approach. As shown in the figure above, we will perform the operation of adding to the result set at each node. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkau9jceowj30uj0jrdhv.jpg) +![](https://p.ipic.vip/1flyhe.jpg) For the gray nodes above, adding the result set is [1]. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkauahh57bj30tj0j0wgg.jpg) +![](https://p.ipic.vip/mj1skc.jpg) The result set of this addition is [1,2]. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkaub4scgij30uu0io40h.jpg) +![](https://p.ipic.vip/y9t2mb.jpg) The result set of this addition is [2,3], and so on. There are six subsets in total, namely [1], [1,2], [1,2,3], [2], [2,3] And [3]. @@ -155,7 +155,7 @@ return backtrack(0, []) The pruning process is graphically represented like this: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glgcy6vcb5j30qb0bjabb.jpg) +![](https://p.ipic.vip/bjh1zs.jpg) **Pruning algorithm is a major test point for backtracking, everyone must be able to master it. ** diff --git a/thinkings/backtrack.md b/thinkings/backtrack.md index 8b5ed46fb..cccdb7f11 100644 --- a/thinkings/backtrack.md +++ b/thinkings/backtrack.md @@ -10,21 +10,21 @@ 以求数组 [1,2,3] 的子集为例: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkau6ustfdj30v80igtag.jpg) +![](https://p.ipic.vip/94t4uj.jpg) > for 循环用来枚举分割点,其实区间 dp 分割区间就是类似的做法 以上图来说, 我们会在每一个节点进行加入到结果集这一次操作。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkau9jceowj30uj0jrdhv.jpg) +![](https://p.ipic.vip/cfk0ru.jpg) 对于上面的灰色节点, 加入结果集就是 [1]。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkauahh57bj30tj0j0wgg.jpg) +![](https://p.ipic.vip/uuy9r7.jpg) 这个加入结果集就是 [1,2]。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkaub4scgij30uu0io40h.jpg) +![](https://p.ipic.vip/ze3qul.jpg) 这个加入结果集就是 [2,3],以此类推。一共有六个子集,分别是 [1], [1,2], [1,2,3], [2], [2,3] 和 [3]。 @@ -155,7 +155,7 @@ class Solution: 剪枝过程用图表示就是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1glgcy6vcb5j30qb0bjabb.jpg) +![](https://p.ipic.vip/bc5dgl.jpg) **剪枝算法回溯的一大考点,大家一定要掌握。** diff --git a/thinkings/balanced-tree.en.md b/thinkings/balanced-tree.en.md index e50d3a09d..c856a20f5 100644 --- a/thinkings/balanced-tree.en.md +++ b/thinkings/balanced-tree.en.md @@ -176,13 +176,13 @@ One possible answer is:[0, -3, 9, -10, null, 5], it can represent the followin The same idea as 108. The difference is the different data structures, so we need to pay attention to the operational differences between linked lists and arrays. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhp582uj31ii0pgjsw.jpg) +![](https://p.ipic.vip/24tsus.jpg) (The case of arrays) Let's take a look at the linked list again: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhpjgtqj31q30u0mzv.jpg) (The case of the linked list) +![](https://p.ipic.vip/7eia6x.jpg) (The case of the linked list) To find the midpoint, you only need to use the classic speed pointer. At the same time, in order to prevent the ring from appearing, we need to cut off the next pointer to mid, so we need to record a node before the midpoint. This only needs to be recorded with a variable pre. @@ -297,7 +297,7 @@ Let n be the length of the linked list. Some students are not very good at analyzing the time complexity and space complexity of recursion. We will introduce it to you again here. -![](https://tva1.sinaimg.cn/large/008i3skNly1gqmduc0j3dj314d0jk7ju.jpg) +![](https://p.ipic.vip/w5qjq6.jpg) First we try to draw the following recursive tree. Due to the recursive depth of the tree is $logn$ thus the space complexity is $logn$ \* recursive function inside the space complexity, due to the recursive function within the space complexity is $O(1)$, so the total space complexity is $O(logn)$。 @@ -335,7 +335,7 @@ example: ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhpzr87j306j07r0sm.jpg) +![](https://p.ipic.vip/93npuo.jpg) ``` diff --git a/thinkings/balanced-tree.md b/thinkings/balanced-tree.md index 6417c6bfc..be6db6734 100644 --- a/thinkings/balanced-tree.md +++ b/thinkings/balanced-tree.md @@ -178,13 +178,13 @@ class Solution: 和 108 思路一样。 不同的是数据结构的不同,因此我们需要关注的是链表和数组的操作差异。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhp582uj31ii0pgjsw.jpg) +![](https://p.ipic.vip/e7yblm.jpg) (数组的情况) 我们再来看下链表: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhpjgtqj31q30u0mzv.jpg) +![](https://p.ipic.vip/gkndvh.jpg) (链表的情况) 找到中点,只需要使用经典的快慢指针即可。同时为了防止环的出现, 我们需要斩断指向 mid 的 next 指针,因此需要记录一下中点前的一个节点,这只需要用一个变量 pre 记录即可。 @@ -301,7 +301,7 @@ public: 有的同学不太会分析递归的时间复杂度和空间复杂度,我们在这里给大家再次介绍一下。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gqmduc0j3dj314d0jk7ju.jpg) +![](https://p.ipic.vip/s8ejbw.jpg) 首先我们尝试画出如下的递归树。由于递归树的深度为 $logn$ 因此空间复杂度就是 $logn$ \* 递归函数内部的空间复杂度,由于递归函数内空间复杂度为 $O(1)$,因此总的空间复杂度为 $O(logn)$。 @@ -340,7 +340,7 @@ $$ ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhpzr87j306j07r0sm.jpg) +![](https://p.ipic.vip/6s67fh.jpg) ``` diff --git a/thinkings/basic-data-structure.en.md b/thinkings/basic-data-structure.en.md index dd70ae4c3..6da214272 100644 --- a/thinkings/basic-data-structure.en.md +++ b/thinkings/basic-data-structure.en.md @@ -22,7 +22,7 @@ Next, we will use a few interesting examples to deepen everyone's understanding The essence of Hooks is an array, pseudo-code: -![basic-data-structure-hooks.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugmr673j30m80bsq3j.jpg) +![basic-data-structure-hooks.png](https://p.ipic.vip/8o17i8.jpg) So why do hooks use arrays? We can explain from another perspective, what would happen if we didn't use arrays? @@ -84,7 +84,7 @@ There are two basic queue operations: Schematic diagram of FIFO (first in, first out) for elements in the queue: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h2kgnjfj30b907dt8x.jpg) +![](https://p.ipic.vip/tm0tnz.jpg) (Picture from https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/queue/README.zh-CN.md ) @@ -98,7 +98,7 @@ The protocol stipulates that for the same tcp connection, all http 1.0 requests It's as if we are waiting for the traffic light. Even if the green light is on next to you, your lane is a red light, you still can't go, you still have to wait. -![basic-data-structure-queue-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugoaepnj30gf0e2dgm.jpg) +![basic-data-structure-queue-1](https://p.ipic.vip/8sk4c8.jpg) `HTTP/1.0' and `HTTP/1.1`: @@ -110,7 +110,7 @@ However, `http 1.1 stipulates that the transmission of server-side responses mus If it is represented by a diagram, the process is probably: -![basic-data-structure-queue-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugpil19j31210d83zr.jpg) +![basic-data-structure-queue-2](https://p.ipic.vip/3locxt.jpg) `HTTP/2' and `HTTP/1.1`: @@ -141,7 +141,7 @@ In addition, there should be a peek operation to access the current top (end) el Schematic diagram of the push and pop operations of the stack: -![basic-data-structure-stack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugqxx3sj30lh0f074v.jpg) +![basic-data-structure-stack](https://p.ipic.vip/f61f0j.jpg) (Picture from https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/stack/README.zh-CN.md ) @@ -168,7 +168,7 @@ foo(); When it is actually executed, it looks like this internally: -![basic-data-structure-call-stack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugru58jj30v70hi0u8.jpg) +![basic-data-structure-call-stack](https://p.ipic.vip/7u0yjf.jpg) > The picture I drew does not show other parts of the execution context (this, scope, etc.). This part is the key to closure, and I am not talking about closure here, but to explain the stack. @@ -182,7 +182,7 @@ Legal stack shuffling operation is also a classic topic. In fact, there is a one Linked lists are one of the most basic data structures, and proficiency in the structure and common operations of linked lists is the foundation of the foundation. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h36dljuj30bc0153yj.jpg) +![](https://p.ipic.vip/okxhbu.jpg) (Picture from: https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/linked-list/traversal ) @@ -192,7 +192,7 @@ Many people say that fiber is implemented based on linked lists, but why should The purpose of fiber's appearance is actually to solve the problem that react cannot stop when it is executed, and it needs to be executed in one go. -![fiber-intro](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugunkhdj30rc0c0wez.jpg) +![fiber-intro](https://p.ipic.vip/a6w031.jpg) > The picture is shared by Lin Clark at ReactConf 2017 @@ -271,7 +271,7 @@ The important nature of the tree: The actual tree used may be more complicated. For example, a quadtree or octree may be used for collision detection in games. And the k-dimensional tree structure`k-d tree` and so on. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugv8xw6j306y06mdft.jpg) +![](https://p.ipic.vip/2kuyc2.jpg) (Picture from https://zh.wikipedia.org/wiki/K-d%E6%A0%91 ) ### Binary tree @@ -283,7 +283,7 @@ The principle behind it is the "eldest son + brother" method. In Teacher Deng's A typical binary tree: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h4obmnkj30rs0muq4k.jpg) +![](https://p.ipic.vip/w7p5ok.jpg) (Picture from https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/tree/README.zh-CN.md ) @@ -315,11 +315,11 @@ Characteristics of binary stacks: -In a min heap, if P is a parent node of C, then the key (or value) of P should be less than or equal to the corresponding value of C. Because of this, the top element of the heap must be the smallest. We will use this feature to find the minimum value or the kth smallest value. -![min-heap](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugz10gfj30ca07yjro.jpg) +![min-heap](https://p.ipic.vip/shen88.jpg) -In a max heap, the key (or value) of P is greater than or equal to the corresponding value of C. -![max-heap](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h43x3o8j30dx0ab74q.jpg) +![max-heap](https://p.ipic.vip/0voxz1.jpg) It should be noted that there are not only heaps of priority queues, but also more complex ones, but generally speaking, we will make the two equivalent. diff --git a/thinkings/basic-data-structure.md b/thinkings/basic-data-structure.md index 8cd98a5c5..011810318 100644 --- a/thinkings/basic-data-structure.md +++ b/thinkings/basic-data-structure.md @@ -22,7 +22,7 @@ Hooks 的本质就是一个数组, 伪代码: -![basic-data-structure-hooks.png](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugmr673j30m80bsq3j.jpg) +![basic-data-structure-hooks.png](https://p.ipic.vip/u9pfsv.jpg) 那么为什么 hooks 要用数组? 我们可以换个角度来解释,如果不用数组会怎么样? @@ -84,7 +84,7 @@ function Form() { 队列中元素先进先出 FIFO (first in, first out) 的示意: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h2kgnjfj30b907dt8x.jpg) +![](https://p.ipic.vip/vd0xqq.jpg) (图片来自 https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/queue/README.zh-CN.md) @@ -98,7 +98,7 @@ function Form() { 这就好像我们在等红绿灯,即使旁边绿灯亮了,你的这个车道是红灯,你还是不能走,还是要等着。 -![basic-data-structure-queue-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugoaepnj30gf0e2dgm.jpg) +![basic-data-structure-queue-1](https://p.ipic.vip/nflzy7.jpg) `HTTP/1.0` 和 `HTTP/1.1`: @@ -110,7 +110,7 @@ function Form() { 如果用图来表示的话,过程大概是: -![basic-data-structure-queue-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugpil19j31210d83zr.jpg) +![basic-data-structure-queue-2](https://p.ipic.vip/6epvep.jpg) `HTTP/2` 和 `HTTP/1.1`: @@ -141,7 +141,7 @@ function Form() { 栈的 push 和 pop 操作的示意: -![basic-data-structure-stack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugqxx3sj30lh0f074v.jpg) +![basic-data-structure-stack](https://p.ipic.vip/kzge8i.jpg) (图片来自 https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/stack/README.zh-CN.md) @@ -168,7 +168,7 @@ foo(); 真正执行的时候,内部大概是这样的: -![basic-data-structure-call-stack](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugru58jj30v70hi0u8.jpg) +![basic-data-structure-call-stack](https://p.ipic.vip/j4s1dt.jpg) > 我画的图没有画出执行上下文中其他部分(this 和 scope 等), 这部分是闭包的关键,而我这里不是讲闭包的,是为了讲解栈的。 @@ -182,7 +182,7 @@ foo(); 链表是一种最基本数据结构,熟练掌握链表的结构和常见操作是基础中的基础。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h36dljuj30bc0153yj.jpg) +![](https://p.ipic.vip/w0t5od.jpg) (图片来自: https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/linked-list/traversal) @@ -192,7 +192,7 @@ foo(); fiber 出现的目的其实是为了解决 react 在执行的时候是无法停下来的,需要一口气执行完的问题的。 -![fiber-intro](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugunkhdj30rc0c0wez.jpg) +![fiber-intro](https://p.ipic.vip/aop2rm.jpg) > 图片来自 Lin Clark 在 ReactConf 2017 分享 @@ -271,7 +271,7 @@ return, children, sibling 也都是一个 fiber,因此 fiber 看起来就是 实际使用的树有可能会更复杂,比如使用在游戏中的碰撞检测可能会用到四叉树或者八叉树。以及 k 维的树结构 `k-d 树`等。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugv8xw6j306y06mdft.jpg) +![](https://p.ipic.vip/obdpvz.jpg) (图片来自 https://zh.wikipedia.org/wiki/K-d%E6%A0%91) ### 二叉树 @@ -283,7 +283,7 @@ return, children, sibling 也都是一个 fiber,因此 fiber 看起来就是 一个典型的二叉树: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h4obmnkj30rs0muq4k.jpg) +![](https://p.ipic.vip/uclaew.jpg) (图片来自 https://github.com/trekhleb/javascript-algorithms/blob/master/src/data-structures/tree/README.zh-CN.md) @@ -315,11 +315,11 @@ return, children, sibling 也都是一个 fiber,因此 fiber 看起来就是 - 在一个 最小堆 (min heap) 中,如果 P 是 C 的一个父级节点,那么 P 的 key(或 value) 应小于或等于 C 的对应值。 正因为此,堆顶元素一定是最小的,我们会利用这个特点求最小值或者第 k 小的值。 -![min-heap](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugz10gfj30ca07yjro.jpg) +![min-heap](https://p.ipic.vip/vm13lg.jpg) - 在一个 最大堆 (max heap) 中,P 的 key(或 value) 大于或等于 C 的对应值。 -![max-heap](https://tva1.sinaimg.cn/large/0081Kckwly1gk7h43x3o8j30dx0ab74q.jpg) +![max-heap](https://p.ipic.vip/d771jf.jpg) 需要注意的是优先队列不仅有堆一种,还有更复杂的,但是通常来说,我们会把两者做等价。 @@ -342,7 +342,7 @@ return, children, sibling 也都是一个 fiber,因此 fiber 看起来就是 二叉查找树,**之所以叫查找树就是因为其非常适合查找**。举个例子,如下一颗二叉查找树,我们想找节点值小于且最接近 58 的节点,搜索的流程如图所示: -![bst](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluh33ttoj30rs0mudhi.jpg) +![bst](https://p.ipic.vip/7upfbi.jpg) (图片来自 https://www.geeksforgeeks.org/floor-in-binary-search-tree-bst/) @@ -385,7 +385,7 @@ return, children, sibling 也都是一个 fiber,因此 fiber 看起来就是 又称 Trie 树,是一种树形结构。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluh7b5gmj30al06q74c.jpg) +![](https://p.ipic.vip/xwqu33.jpg) (图来自 https://baike.baidu.com/item/%E5%AD%97%E5%85%B8%E6%A0%91/9825209?fr=aladdin) 它有 3 个基本性质: diff --git a/thinkings/binary-search-1.en.md b/thinkings/binary-search-1.en.md index a536e1101..451aa6c91 100644 --- a/thinkings/binary-search-1.en.md +++ b/thinkings/binary-search-1.en.md @@ -2,7 +2,7 @@ ## Foreword -![](https://tva1.sinaimg.cn/large/008eGmZEly1godspy7ue3j31c00pytb0.jpg) +![](https://p.ipic.vip/6zfu8a.jpg) Hello everyone, this is lucifer. What I bring to you today is the topic of "Two Points". Let's start with the outline of this article. This is a brain map drawn by me with mindmap. After that, I will continue to improve it and gradually improve other topics. diff --git a/thinkings/binary-search-1.md b/thinkings/binary-search-1.md index 8f6ace89d..38e3615bd 100644 --- a/thinkings/binary-search-1.md +++ b/thinkings/binary-search-1.md @@ -2,7 +2,7 @@ ## 前言 -![](https://tva1.sinaimg.cn/large/008eGmZEly1godspy7ue3j31c00pytb0.jpg) +![](https://p.ipic.vip/zlxbvk.jpg) 大家好,我是 lucifer。今天给大家带来的是《二分》专题。先上下本文的提纲,这个是我用 mindmap 画的一个脑图,之后我会继续完善,将其他专题逐步完善起来。 @@ -21,7 +21,7 @@ 本文内容已经同步到我的刷题插件的 RoadMap 中,结合刷题插件食用味道更佳哦~ 插件的获取方式可以在我的公众号力扣加加中回复插件查看。 -![刷题插件](https://tva1.sinaimg.cn/large/008eGmZEly1godsvaj344j30rw0qo433.jpg) +![刷题插件](https://p.ipic.vip/uw95ox.jpg) > 如果觉得文章有用,请点赞留言转发一下,让我有动力继续做下去。 diff --git a/thinkings/binary-search-2.en.md b/thinkings/binary-search-2.en.md index 518656abd..b58996b95 100644 --- a/thinkings/binary-search-2.en.md +++ b/thinkings/binary-search-2.en.md @@ -6,7 +6,7 @@ Hello everyone, this is lucifer. What I bring to you today is the topic of "Two > You can also use vscode blink-mind to open the source file to view. There are some notes in it that you can click to view. The source file can be obtained by replying to the brain map on my official account "Force Buckle Plus", and the brain map will continue to be updated with more content in the future. vscode plug-in address:https://marketplace.visualstudio.com/items?itemName=awehook.vscode-blink-mind -![](https://tva1.sinaimg.cn/large/008eGmZEly1godspy7ue3j31c00pytb0.jpg) +![](https://p.ipic.vip/7b946x.jpg) This series contains the following topics: @@ -70,7 +70,7 @@ Terms used in binary search: -l and r-left and right pointers -mid--the midpoint of the left and right pointers, which is used to determine the index we should look to the left or the right (in fact, it is to shrink the solution space) -![Term illustration](https://tva1.sinaimg.cn/large/008eGmZEly1gosrap0p6nj30fe0940t9.jpg) +![Term illustration](https://p.ipic.vip/6zhzd0.jpg) It is worth noting that, except that the target is fixed, everything else changes dynamically. Where l and r refer to the upper and lower boundaries of the solution space, mid is the intermediate value of the upper and lower boundaries, and index is the traversal pointer, which is used to control the traversal process. @@ -413,7 +413,7 @@ The key to the two-way solution is: In summary, we can use the leftmost boundary, that is, the right boundary is constantly shrinking. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4rmzwcj30q00lv40j.jpg) +![](https://p.ipic.vip/77n5hj.jpg) > The upper limit of the number of bananas in the banana pile is 10^9. Keke is too edible, right? diff --git a/thinkings/binary-search-2.md b/thinkings/binary-search-2.md index 6ff61438f..8b167fa36 100644 --- a/thinkings/binary-search-2.md +++ b/thinkings/binary-search-2.md @@ -10,7 +10,7 @@ > 。vscode 插件地址 > :https://marketplace.visualstudio.com/items?itemName=awehook.vscode-blink-mind -![](https://tva1.sinaimg.cn/large/008eGmZEly1godspy7ue3j31c00pytb0.jpg) +![](https://p.ipic.vip/f9hf3p.jpg) 本系列包含以下专题: @@ -88,7 +88,7 @@ target。如果存在, 则返回其在 nums 中的索引。如果不存在, - mid —— 左右指针的中点,用来确定我们应该向左查找还是向右查找的索引(其实就是收 缩解空间) -![术语图示](https://tva1.sinaimg.cn/large/008eGmZEly1gosrap0p6nj30fe0940t9.jpg) +![术语图示](https://p.ipic.vip/5mz2pf.jpg) 值得注意的是,除了 target 是固定不变的,其他都是动态变化的。其中 l 和 r 指的是解 空间的上下界,mid 是上下界的中间值, index 是遍历指针,用于控制遍历过程。 @@ -115,13 +115,13 @@ target 为 4·。 - 7 > 4 ,由于 7 右边的数字都大于 7 ,因此不可能是答案。我们将范围缩写到了 7 的 左侧。 -![调整解空间](https://tva1.sinaimg.cn/large/008eGmZEly1gosrelnzhuj30c905bweq.jpg) +![调整解空间](https://p.ipic.vip/lopd47.jpg) - 解空间变成了 [1,3,4,6],此时中间元素为 3。 - 3 < 4,由于 3 左边的数字都小于 3 ,因此不可能是答案。我们将范围缩写到了 3 的右 侧。 -![再次调整解空间](https://tva1.sinaimg.cn/large/008eGmZEly1gosrg6phvzj305b033glk.jpg) +![再次调整解空间](https://p.ipic.vip/8n5f38.jpg) - 解空间变成了 [4,6],此时中间元素为 4,正好是我们要找的,返回其索引 2 即可。 @@ -482,7 +482,7 @@ Piles 中最大的数(也就是解空间的最大值)。 综上,我们可以使用最左二分,即不断收缩右边界。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu4rmzwcj30q00lv40j.jpg) +![](https://p.ipic.vip/f95aa2.jpg) > 香蕉堆的香蕉个数上限是 10^9, 珂珂这也太能吃了吧? @@ -880,7 +880,7 @@ for a in A: 上面代码的 d 就是有序序列。 -![”插入排序“图示](https://tva1.sinaimg.cn/large/008eGmZEly1gog0u4kxoxj30ks0ygwiz.jpg) +![”插入排序“图示](https://p.ipic.vip/z4z3i4.jpg) 理论知识到此为止,接下来通过一个例子来说明。 @@ -1018,7 +1018,7 @@ https://leetcode-cn.com/problems/reverse-pairs/ 的**,这样我们就可以用二分了。和上面题目一样,使用平衡二叉树代替数组可使得插入的 时间复杂度降低到 $O(logn)$。 -![平衡二叉树](https://tva1.sinaimg.cn/large/008eGmZEly1goss2vq59dj30c407rgm0.jpg) +![平衡二叉树](https://p.ipic.vip/kh1ub9.jpg) ##### 关键点 diff --git a/thinkings/binary-tree-traversal.en.md b/thinkings/binary-tree-traversal.en.md index 70f3e9409..fb9a2707e 100644 --- a/thinkings/binary-tree-traversal.en.md +++ b/thinkings/binary-tree-traversal.en.md @@ -18,7 +18,7 @@ Stack can be used to simplify the process of DFS traversal. Besides, since tree Graph for DFS: -![binary-tree-traversal-dfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhzhynsg30dw0dw3yl.gif) +![binary-tree-traversal-dfs](https://p.ipic.vip/sbj4as.gif) (from https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search) @@ -46,7 +46,7 @@ If you look at the bigger picture, you'll find that the process of traversal is The traversal will look something like this. -![binary-tree-traversal-preorder](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui0d6ewj30n30azaar.jpg) +![binary-tree-traversal-preorder](https://p.ipic.vip/ma5fog.jpg) This way of problem solving is a bit similar to `backtrack`, on which I have written a post. You can benefit a lot from it because it can be used to `solve all three DFS traversal problems` mentioned aboved. If you don't know this yet, make a memo on it. @@ -90,7 +90,7 @@ As for `2) both its left and right subtrees have been traversed`, we only need a The key point of level order traversal is how do we know whether the traversal of each level is done. The answer is that we use a variable as a flag representing the end of the traversal of the current level. -![binary-tree-traversal-bfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui1tpoug30dw0dw3yl.gif) +![binary-tree-traversal-bfs](https://p.ipic.vip/epbeoj.gif) (from https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/breadth-first-search) diff --git a/thinkings/binary-tree-traversal.md b/thinkings/binary-tree-traversal.md index 27b613bf7..d7c1bf6f6 100644 --- a/thinkings/binary-tree-traversal.md +++ b/thinkings/binary-tree-traversal.md @@ -14,7 +14,7 @@ DFS 都可以使用栈来简化操作,并且其实树本身是一种递归的 DFS 图解: -![binary-tree-traversal-dfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui7vcmwg30dw0dw3yl.gif) +![binary-tree-traversal-dfs](https://p.ipic.vip/phae05.gif) (图片来自 https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search) @@ -44,7 +44,7 @@ BFS 的关键点在于如何记录每一层次是否遍历完成, 我们可以 整个过程大概是这样: -![binary-tree-traversal-preorder](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui8rph4j30n30azaar.jpg) +![binary-tree-traversal-preorder](https://p.ipic.vip/ei0wj1.jpg) 这种思路有一个好处就是可以`统一三种遍历的思路`. 这个很重要,如果不了解的朋友,希望能够记住这一点。 @@ -90,7 +90,7 @@ BFS 的关键点在于如何记录每一层次是否遍历完成, 我们可以 层次遍历的关键点在于如何记录每一层次是否遍历完成, 我们可以用一个标识位来表式当前层的结束。 -![binary-tree-traversal-bfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluic79lag30dw0dw3yl.gif) +![binary-tree-traversal-bfs](https://p.ipic.vip/9z2nxw.gif) (图片来自 https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/breadth-first-search) diff --git a/thinkings/bloom-filter.en.md b/thinkings/bloom-filter.en.md index 9bffbffff..a7745ebb8 100644 --- a/thinkings/bloom-filter.en.md +++ b/thinkings/bloom-filter.en.md @@ -38,7 +38,7 @@ The Bloom filter is actually `bit + multiple hash functions`. The k-time hash (i In other words, the Bloom filter answered: ** There may be ** and **There must be no ** questions. -![bloom-filter-url](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhc0933j31dw0j2wgz.jpg) +![bloom-filter-url](https://p.ipic.vip/m7bvee.jpg) As can be seen from the figure above, the Bloom filter is essentially composed of ** a long binary vector** and ** multiple hash functions**. diff --git a/thinkings/bloom-filter.md b/thinkings/bloom-filter.md index fffce60db..972cbbc82 100644 --- a/thinkings/bloom-filter.md +++ b/thinkings/bloom-filter.md @@ -39,7 +39,7 @@ 也就是说布隆过滤器回答了:**可能存在** 和 **一定不存在** 的问题。 -![bloom-filter-url](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhc0933j31dw0j2wgz.jpg) +![bloom-filter-url](https://p.ipic.vip/1aeqlp.jpg) 从上图可以看出, 布隆过滤器本质上是由**一个很长的二进制向量**和**多个哈希函数**组成。 diff --git a/thinkings/dynamic-programming.en.md b/thinkings/dynamic-programming.en.md index 2a2b8f015..4d4852087 100644 --- a/thinkings/dynamic-programming.en.md +++ b/thinkings/dynamic-programming.en.md @@ -163,7 +163,7 @@ climbStairs(10) Here I use a hash table named ** memo to store the return value of the recursive function, where key is the parameter and value is the return value of the recursive function. ** -![Hash indicates intent](https://tva1.sinaimg.cn/large/008eGmZEly1gpmof67uptj307w0fe3zk.jpg) +![Hash indicates intent](https://p.ipic.vip/h4suhr.jpg) > The form of key is (x, y), which represents an ancestor. Usually there are multiple parameters for dynamic programming, so we can use the ancestor method to memorize them. Or it can take the form of a multidimensional array. For the figure above, a two-dimensional array can be used to represent it. diff --git a/thinkings/dynamic-programming.md b/thinkings/dynamic-programming.md index 5bb62fc5b..0b82eb53d 100644 --- a/thinkings/dynamic-programming.md +++ b/thinkings/dynamic-programming.md @@ -11,7 +11,7 @@ 每个阶段抽象为状态(用圆圈来表示),状态之间可能会发生转化(用箭头表示)。可以画出类似如下的图: -![状态转移图解](https://tva1.sinaimg.cn/large/008eGmZEly1gpoaanln73j31ak0p0dpd.jpg) +![状态转移图解](https://p.ipic.vip/ohuutq.jpg) 那我们应该做出如何的**决策序列**才能使得结果最优?换句话说就是每一个状态应该如何选择到下一个具体状态,并最终到达目标状态。这就是动态规划研究的问题。 @@ -115,7 +115,7 @@ def f(x): 解决动态规划问题可以看成是填充函数这个黑盒,使得定义域中的数并正确地映射到值域。 -![数学函数vs动态规划](https://tva1.sinaimg.cn/large/008eGmZEly1gplrxy60mpj30pt0daacn.jpg) +![数学函数vs动态规划](https://p.ipic.vip/x645hl.jpg) 递归并不是算法,它是和迭代对应的一种编程方法。只不过,我们通常借助递归去分解问题而已。比如我们定义一个递归函数 f(n),用 f(n) 来描述问题。就和使用普通动态规划 f[n] 描述问题是一样的,这里的 f 是 dp 数组。 @@ -141,7 +141,7 @@ function climbStairs(n) { 我们用一个递归树来直观感受以下(每一个圆圈表示一个子问题): -![重叠子问题](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhw6pf2j30mz0b2dgk.jpg) +![重叠子问题](https://p.ipic.vip/xoh2he.jpg) 红色表示重复的计算。即 Fib(N-2) 和 Fib(N-3) 都被计算了两次,实际上计算一次就够了。比如第一次计算出了 Fib(N-2) 的值,那么下次再次需要计算 Fib(N-2)的时候,可以直接将上次计算的结果返回。之所以可以这么做的原因正是前文提到的**我们的递归函数是数学中的函数,也就是说参数一定,那么返回值也一定不会变**,因此下次如果碰到相同的参数,我们就可以**将上次计算过的值直接返回,而不必重新计算**。这样节省的时间就等价于重叠子问题的个数。 @@ -165,7 +165,7 @@ climbStairs(10) 这里我使用了一个名为 **memo 的哈希表来存储递归函数的返回值,其中 key 为参数,value 为递归函数的返回值。** -![哈希表示意图](https://tva1.sinaimg.cn/large/008eGmZEly1gpmof67uptj307w0fe3zk.jpg) +![哈希表示意图](https://p.ipic.vip/zzqj2d.jpg) > key 的形式为 (x, y),表示的是一个元祖。通常动态规划的参数有多个,我们就可以使用元祖的方式来记忆化。或者也可采取多维数组的形式。对于上图来说,就可使用二维数组来表示。 @@ -254,7 +254,7 @@ climbStairs(10) 关于状态定义,真的非常重要,以至于我将其列为动态规划的核心。因此我觉得有必要举几个例子来进行说明。我直接从力扣的[动态规划专题](https://leetcode-cn.com/tag/dynamic-programming/problemset/ "动态规划专题")中抽取前两道给大家讲讲。 -![力扣动态规划专题](https://tva1.sinaimg.cn/large/008eGmZEly1gpmtitey5hj315k0lsjxk.jpg) +![力扣动态规划专题](https://p.ipic.vip/ak58fr.jpg) 第一道题:《5. 最长回文子串》难度中等 @@ -381,7 +381,7 @@ p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和  这道题我定义状态为 f(i, j) 表示机器人到达点 (i,j) 的总的路径数。那么状态总数就是 i 和 j 的取值的笛卡尔积,也就是 m \* n 。 -![二维爬楼梯](https://tva1.sinaimg.cn/large/008eGmZEly1gpn6m7knnij30u00v1di1.jpg) +![二维爬楼梯](https://p.ipic.vip/p8qlli.jpg) 总的来说,动态规划的空间和时间复杂度**打底就是状态的个数**,而状态的个数通常是参数的笛卡尔积,这是由动态规划的无后向性决定的。 @@ -435,7 +435,7 @@ dp[n] = dp[n - 1] + dp[n - 2] 就是【状态转移方程】 如下图所示: -![状态转移图解](https://tva1.sinaimg.cn/large/008eGmZEly1gpoaanln73j31ak0p0dpd.jpg) +![状态转移图解](https://p.ipic.vip/ohuutq.jpg) 理论差不多先这样,接下来来几个实战消化一下。 @@ -493,7 +493,7 @@ $$ 是不是觉得状态转移方程写起来麻烦?这里我给大家介绍一个小技巧,那就是使用 latex,latex 语法可以方便地写出这样的公式。另外西法还贴心地写了**一键生成动态规划转移方程公式**的功能,帮助大家以最快速度生成公诉处。 插件地址:https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template -![插件用法](https://tva1.sinaimg.cn/large/008eGmZEly1gpoaw5c5l0j314a0dq0ui.jpg) +![插件用法](https://p.ipic.vip/73qkz7.jpg) 状态转移方程实在是没有什么灵丹妙药,不同的题目有不同的解法。状态转移方程同时也是解决动态规划问题中最最困难和关键的点,大家一定要多多练习,提高题感。接下来,我们来看下不那么困难,但是新手疑问比较多的问题 - **如何枚举状态**。 @@ -510,7 +510,7 @@ for i in range(1, n + 1): pass ``` -![一维状态](https://tva1.sinaimg.cn/large/008eGmZEly1gpn6bxt7erj31n00u0jtx.jpg) +![一维状态](https://p.ipic.vip/2mf74c.jpg) - 如果是两维状态,那么我们使用两层循环可以搞定。 @@ -520,7 +520,7 @@ for i in range(1, m + 1): pass ``` -![二维状态](https://tva1.sinaimg.cn/large/008eGmZEly1gpn6ceroi3j31970u0dit.jpg) +![二维状态](https://p.ipic.vip/87lgok.jpg) - 。。。 @@ -621,7 +621,7 @@ function dp(n) { 动态规划的查表过程如果画成图,就是这样的: -![动态规划查表](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhxylbhj30n40cbaaq.jpg) +![动态规划查表](https://p.ipic.vip/62j2sx.jpg) > 虚线代表的是查表过程 @@ -827,7 +827,7 @@ class Solution: 以 coins = [1,2,3], amount = 6 来说,我们可以画出如下的递归树。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1goil47ztakj31jk0nm791.jpg) +![](https://p.ipic.vip/t1ow73.jpg) (图片来自https://leetcode.com/problems/coin-change/solution/) diff --git a/thinkings/graph.en.md b/thinkings/graph.en.md index 349399fa4..8a8d22cd9 100644 --- a/thinkings/graph.en.md +++ b/thinkings/graph.en.md @@ -34,7 +34,7 @@ How many edges point to node A, then the degree of entry of node A is what. Simi Still take the figure above as an example. The entry and exit degrees of all nodes in this figure are 1. -![](https://tva1.sinaimg.cn/large/008i3skNly1guani9qrnbj61800u0jsj02.jpg) +![](https://p.ipic.vip/zgyf24.jpg) ### Path & Ring [Path: Path] @@ -43,7 +43,7 @@ Still take the figure above as an example. The entry and exit degrees of all nod I can transform the figure above into a loop-free diagram with a little modification. At this time, there is no loop. -![](https://tva1.sinaimg.cn/large/008i3skNly1guanjtoizlj61d90u0dh702.jpg) +![](https://p.ipic.vip/r7h4zu.jpg) ### Connectedness Diagram & Strong Connectedness Diagram @@ -73,7 +73,7 @@ Use an n\*n matrix to describe the graph graph, which is a two-dimensional matri Generally speaking, for all graphs, I use graph[i][j]=1 to indicate that there is an edge between vertex i and vertex j, and the direction of the edge is from i to J. Use graph[i][j]= 0 to indicate that there is no edge between vertex i and vertex J. For this graph, we can store other numbers, which represent weights. -![](https://tva1.sinaimg.cn/large/008i3skNly1guantlobk3j60eo0d20t702.jpg) +![](https://p.ipic.vip/0f39q4.jpg) It can be seen that the picture above is diagonally symmetrical, so we only need to look at half of it, which causes half of the space to be wasted. @@ -141,14 +141,14 @@ For each point, a linked list is stored, which is used to point to all points di For example, in an undirected graph: -![graph-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluh8tbb5j30k00akq48.jpg) +![graph-1](https://p.ipic.vip/41khvw.jpg) (Picture from https://zhuanlan.zhihu.com/p/25498681 ) It can be seen that in an undirected graph, the adjacency matrix is symmetrical about the diagonal, and the adjacency list always has two symmetrical edges. And in a directed graph: -![graph-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhb46urj30k00aq0ux.jpg) +![graph-2](https://p.ipic.vip/rfxn5j.jpg) (Picture from https://zhuanlan.zhihu.com/p/25498681 ) @@ -170,7 +170,7 @@ Here, take a directed graph as an example, and a directed graph is similar. I wi The depth-first method of traversing the graph is to start from a certain vertex v in the graph and continue to visit the neighbors, and the neighbors of the neighbors until the access is complete. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gjy6kp2117j30b507mq31.jpg) +![](https://p.ipic.vip/odnpw3.jpg) As shown in the figure above, IF we use DFS and start from node A, **A possible** access order is: **A->C-> B-> D-> F->G->E**, Of course, it may also be **A->D->C->B->F->G->E**, etc., Depending on your code, but THEY are all depth-first. @@ -178,7 +178,7 @@ As shown in the figure above, IF we use DFS and start from node A, **A possible* Breadth-first search can be vividly described as "shallow and endless". It also requires a queue to maintain the order of the traversed apex so that the adjacent apex of these apex can be accessed in the order of dequeue. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gjy7ds6u2lj30ea0a4dhf.jpg) +![](https://p.ipic.vip/r0h2f4.jpg) As shown in the figure above, IF we use BFS and start from node A, ** A possible** access order is: ** A->B-> C-> F-> E->G-> D**, Of course, it may also be **A->B->F->E->C->G->D**, etc., Depending on your code, but they are all breadth-first. diff --git a/thinkings/graph.md b/thinkings/graph.md index f56a3df38..8768c04b5 100644 --- a/thinkings/graph.md +++ b/thinkings/graph.md @@ -4,7 +4,7 @@  如下就是一种逻辑上的图结构: -![逻辑上的图结构](https://tva1.sinaimg.cn/large/008i3skNly1guanati4zlj61800u075p02.jpg) +![逻辑上的图结构](https://p.ipic.vip/7jm3eo.jpg) 图是一种最复杂的数据结构,前面讲的数据结构都可以看成是图的特例。那为什么不都用图就好了,还要分那么多种数据结构呢? @@ -34,7 +34,7 @@ 仍然以上面的图为例,这幅图的所有节点的入度和出度都为 1。 -![](https://tva1.sinaimg.cn/large/008i3skNly1guani9qrnbj61800u0jsj02.jpg) +![](https://p.ipic.vip/r4js08.jpg) ### 路径 & 环〔路径:Path〕 @@ -43,7 +43,7 @@ 我可以将上面的图稍加改造就变成了无环图,此时没有任何一个环路。 -![](https://tva1.sinaimg.cn/large/008i3skNly1guanjtoizlj61d90u0dh702.jpg) +![](https://p.ipic.vip/6suzbw.jpg) ### 连通图 & 强连通图 @@ -73,7 +73,7 @@ 一般而言,对于无权图我都用 graph[i][j] = 1 来表示 顶点 i 和顶点 j 之间有一条边,并且边的指向是从 i 到 j。用 graph[i][j] = 0 来表示 顶点 i 和顶点 j 之间不存在一条边。 对于有权图来说,我们可以存储其他数字,表示的是权重。 -![](https://tva1.sinaimg.cn/large/008i3skNly1guantlobk3j60eo0d20t702.jpg) +![](https://p.ipic.vip/g6qhtl.jpg) 可以看出上图是对角线对称的,这样我们只需看一半就好了,这就造成了一半的空间浪费。 @@ -141,14 +141,14 @@ for fr, to, w in times: 例如在无向无权图中: -![graph-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluh8tbb5j30k00akq48.jpg) +![graph-1](https://p.ipic.vip/i1t6uf.jpg) (图片来自 https://zhuanlan.zhihu.com/p/25498681) 可以看出在无向图中,邻接矩阵关于对角线对称,而邻接链表总有两条对称的边。 而在有向无权图中: -![graph-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhb46urj30k00aq0ux.jpg) +![graph-2](https://p.ipic.vip/g1v1ts.jpg) (图片来自 https://zhuanlan.zhihu.com/p/25498681) @@ -170,7 +170,7 @@ for fr, to, w in times: 深度优先遍历图的方法是,从图中某顶点 v 出发, 不断访问邻居, 邻居的邻居直到访问完毕。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gjy6kp2117j30b507mq31.jpg) +![](https://p.ipic.vip/fqq7k0.jpg) 如上图, 如果我们使用 DFS,并且从 A 节点开始的话, **一个可能的**的访问顺序是: **A -> C -> B -> D -> F -> G -> E**,当然也可能是 **A -> D -> C -> B -> F -> G -> E** 等,具体取决于你的代码,但他们都是深度优先的。 @@ -178,7 +178,7 @@ for fr, to, w in times: 广度优先搜索,可以被形象地描述为 "浅尝辄止",它也需要一个队列以保持遍历过的顶点顺序,以便按出队的顺序再去访问这些顶点的邻接顶点。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gjy7ds6u2lj30ea0a4dhf.jpg) +![](https://p.ipic.vip/eq4g1r.jpg) 如上图, 如果我们使用 BFS,并且从 A 节点开始的话, **一个可能的**的访问顺序是: **A -> B -> C -> F -> E -> G -> D**,当然也可能是 **A -> B -> F -> E -> C -> G -> D** 等,具体取决于你的代码,但他们都是广度优先的。 @@ -429,7 +429,7 @@ Floyd-Warshall 可以**解决任意两个点距离**,即多源最短路径, 算法也不难理解,简单来说就是: **i 到 j 的最短路径 = i 到 k 的最短路径 + k 到 j 的最短路径**的最小值。如下图: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gk3qh59semj30ec05ptab.jpg) +![](https://p.ipic.vip/592ov2.jpg) u 到 v 的最短距离是 u 到 x 的最短距离 + x 到 v 的最短距离。上图 x 是 u 到 v 的必经之路,如果不是的话,我们需要多个中间节点的值,并取最小的。 @@ -645,7 +645,7 @@ var Floyd-Warshall = function(graph, n){ 举个例子。对于如下的一个图,存在一个 B -> C -> D -> B,这样 B 到 C 和 D 的距离理论上可以无限小。我们需要检测到这一种情况,并退出。 -![](https://tva1.sinaimg.cn/large/008i3skNly1grc449csg0j30h705a3yt.jpg) +![](https://p.ipic.vip/4909ju.jpg) 此算法时间复杂度:$O(V*E)$, 空间复杂度:$O(V)$。 @@ -757,13 +757,13 @@ topologicalSort(graph) 首先生成树是原图的一个子图,它本质是一棵树,这也是为什么叫做生成树,而不是生成图的原因。其次生成树应该包括图中所有的顶点。 如下图由于没有包含所有顶点,换句话说所有顶点没有在同一个联通域,因此不是一个生成树。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gw90jdhugxj30jg0c6mxj.jpg) +![](https://p.ipic.vip/9qlhgv.jpg) > 黄色顶点没有包括在内 你可以将生成树看成是根节点不确定的多叉树,由于是一棵树,那么一定不包含环。如下图就不是生成树。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gw90i7uk9aj30pw0cmq3l.jpg) +![](https://p.ipic.vip/js111h.jpg) 因此不难得出,最小生成树的边的个数是 n - 1,其中 n 为顶点个数。 @@ -773,7 +773,7 @@ topologicalSort(graph) 最小生成树在实际生活中有很强的价值。比如我要修建一个地铁,并覆盖 n 个站,这 n 个站要互相都可以到达(同一个联通域),如果建造才能使得花费最小?由于每个站之间的路线不同,因此造价也不一样,因此这就是一个最小生成树的实际使用场景,类似的例子还有很多。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmst4yvz7sj308c06qjrl.jpg) +![](https://p.ipic.vip/bedy0j.jpg) (图来自维基百科) @@ -917,7 +917,7 @@ A 星寻路解决的问题是在一个二维的表格中找出任意两点的最 这里有一个重要的概念是**估价算法**,一般我们使用 **曼哈顿距离**来进行估价,即 `H(n) = D * (abs ( n.x – goal.x ) + abs ( n.y – goal.y ) )`。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gjy9j7k3jdg305u05umy9.gif) +![](https://p.ipic.vip/wlg8gk.gif) (图来自维基百科 https://zh.wikipedia.org/wiki/A*%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95 ) diff --git a/thinkings/greedy.en.md b/thinkings/greedy.en.md index ec5b21a55..24e467a06 100644 --- a/thinkings/greedy.en.md +++ b/thinkings/greedy.en.md @@ -46,11 +46,11 @@ Here we use the greedy strategy to solve it. That is, every time you choose a po As shown in the figure below, the starting position is 2, and the range that can be jumped is the orange node. Since 3 can jump farther, enough to cover the situation of 2, it should jump to the position of 3. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluifqw9sj309i03xq2t.jpg) +![](https://p.ipic.vip/pgh1f7.jpg) When we jump to the position of 3. As shown in the figure below, the range that can be jumped is 1, 1, and 4 in orange. Since 4 can jump farther, it jumps to the position of 4. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluimff8dj30c1039wed.jpg) +![](https://p.ipic.vip/ccdr3u.jpg) If you write code, we can use end to represent the current boundary that can be jumped, corresponding to orange 1 in the first picture and orange 4 in the second picture. And when traversing the array, when the boundary is reached, the boundary is updated again. @@ -194,7 +194,7 @@ Please return the minimum number of taps that can irrigate the entire garden. If Example 1: ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluiubf2gj30bm05xmx4.jpg) +![](https://p.ipic.vip/w0ltjw.jpg) ``` Input: n = 5, ranges = [3,4,1,1,0,0] @@ -285,12 +285,12 @@ For extreme-value problems, we can consider using dynamic programming and greedy The more than 1,000 pages of e-books I organized have been developed and downloaded. You can go to the background of my public account "Force Buckle Plus" to reply to the e-books to get them. -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928846461-image.png) +![](https://p.ipic.vip/ywp3od.png) -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928862442-image.png) +![](https://p.ipic.vip/vngp5k.png) If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/yp4ttk.jpg) diff --git a/thinkings/greedy.md b/thinkings/greedy.md index dfe2b614f..667cb2115 100644 --- a/thinkings/greedy.md +++ b/thinkings/greedy.md @@ -46,11 +46,11 @@ LeetCode 上对于贪婪策略有 73 道题目。我们将其分成几个类型 如下图,开始的位置是 2,可跳的范围是橙色节点的。由于 3 可以跳的更远,足以覆盖 2 的情况,因此应该跳到 3 的位置。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluifqw9sj309i03xq2t.jpg) +![](https://p.ipic.vip/qsqtgu.jpg) 当我们跳到 3 的位置后。 如下图,能跳的范围是橙色的 1,1,4。由于 4 可以跳的更远,因此跳到 4 的位置。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluimff8dj30c1039wed.jpg) +![](https://p.ipic.vip/l6ey7y.jpg) 写代码的话,我们可以使用 end 表示当前能跳的边界,对应第一个图的橙色 1,第二个图的橙色 4。并且遍历数组的时候,到了边界,就重新更新边界。 @@ -194,7 +194,7 @@ class Solution: 示例 1: ``` -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluiubf2gj30bm05xmx4.jpg) +![](https://p.ipic.vip/ydxkrm.jpg) ``` 输入:n = 5, ranges = [3,4,1,1,0,0] @@ -285,12 +285,12 @@ class Solution: 我整理的 1000 多页的电子书已经开发下载了,大家可以去我的公众号《力扣加加》后台回复电子书获取。 -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928846461-image.png) +![](https://p.ipic.vip/v7h0rf.png) -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928862442-image.png) +![](https://p.ipic.vip/kx37gp.png) 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/wu7dm6.jpg) diff --git a/thinkings/heap-2.en.md b/thinkings/heap-2.en.md index 19eb4f7ce..272cff73f 100644 --- a/thinkings/heap-2.en.md +++ b/thinkings/heap-2.en.md @@ -4,11 +4,11 @@ Last time I did a small survey for everyone on my public account, "Vote for the programming language you want to solve~". The following are the results of the survey: -![Voting results](https://tva1.sinaimg.cn/large/008eGmZEly1gmtc6l6sfij30p20gqjsu.jpg) +![Voting results](https://p.ipic.vip/0ambnc.jpg) Regarding others, most of them are in the Go language. -![What did the other people who voted for write?](https://tva1.sinaimg.cn/large/008eGmZEly1gmtc7yeqxyj317y0me0wd.jpg) +![What did the other people who voted for write?](https://p.ipic.vip/deg4hc.jpg) Since the proportion of Java and Python has exceeded 60%, this time I will try to write in both Java and Python. Thank you @ CaptainZ for providing the Java code. At the same time, in order to prevent the article from being stinky and long, I put all the code (Java and Python) of this article in Java on the official website of Likujiajia\*\*, website address:https://leetcode-solution.cn/solution-code @@ -16,7 +16,7 @@ Since the proportion of Java and Python has exceeded 60%, this time I will try t ## Body -![](https://tva1.sinaimg.cn/large/0081Kckwly1glegve2v47j319g0u041x.jpg) +![](https://p.ipic.vip/j81nlc.jpg) Hello everyone, this is lucifer. What I bring to you today is the topic of "Heap". Let's start with the outline of this article. This is a brain map drawn by me with mindmap. After that, I will continue to improve it and gradually improve other topics. @@ -356,7 +356,7 @@ mat[i] is a non-decreasing array In fact, this question is to give you m one-dimensional arrays of the same length. Let us select a number from these m arrays, that is, select a total of m numbers, and find that the sum of these m numbers is The kth smallest among all selection possibilities. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gmanik03omj31p40u0q8q.jpg) +![](https://p.ipic.vip/pypsm9.jpg) A simple idea is to use multiple pointers to solve. For this question, it is to use m pointers to point to m one-dimensional arrays. The position of the pointers indicates that the first few in the one-dimensional array are currently selected. diff --git a/thinkings/heap-2.md b/thinkings/heap-2.md index 104256d7f..867d0e473 100644 --- a/thinkings/heap-2.md +++ b/thinkings/heap-2.md @@ -4,11 +4,11 @@ 上次在我的公众号给大家做了一个小调查《投出你想要的题解编程语言吧~》。以下是调查的结果: -![投票结果](https://tva1.sinaimg.cn/large/008eGmZEly1gmtc6l6sfij30p20gqjsu.jpg) +![投票结果](https://p.ipic.vip/vu8rjd.jpg) 而关于其他,则大多数是 Go 语言。 -![投其他的人都写了什么?](https://tva1.sinaimg.cn/large/008eGmZEly1gmtc7yeqxyj317y0me0wd.jpg) +![投其他的人都写了什么?](https://p.ipic.vip/zwzwd1.jpg) 由于 Java 和 Python 所占比例已经超过了 60%,这次我尝试一下 Java 和 Python 双语言来写,感谢 @CaptainZ 提供的 Java 代码。同时为了**不让文章又臭又长,我将 Java 本文所有代码(Java 和 Python)都放到了力扣加加官网上**,网站地址:https://leetcode-solution.cn/solution-code @@ -16,7 +16,7 @@ ## 正文 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glegve2v47j319g0u041x.jpg) +![](https://p.ipic.vip/n746a5.jpg) 大家好,我是 lucifer。今天给大家带来的是《堆》专题。先上下本文的提纲,这个是我用 mindmap 画的一个脑图,之后我会继续完善,将其他专题逐步完善起来。 @@ -60,7 +60,7 @@ heapq.heappop() # 弹出 (4,5,6) 用图来表示堆结构就是下面这样: -![使用元组的小顶堆](https://tva1.sinaimg.cn/large/0081Kckwly1gmbn0faqqaj30jy0lkq4n.jpg) +![使用元组的小顶堆](https://p.ipic.vip/jua2n1.jpg) 简单解释一下上面代码的执行结果。 @@ -100,7 +100,7 @@ for a in A: 用图来表示就是下面这样: -![小顶堆模拟大顶堆](https://tva1.sinaimg.cn/large/0081Kckwly1gmbn35fzhyj30k20mk75z.jpg) +![小顶堆模拟大顶堆](https://p.ipic.vip/i2a4l1.jpg) 铺垫就到这里,接下来进入正题。 @@ -114,7 +114,7 @@ for a in A: 然而,我们也可不先全部入堆,而是建立**大顶堆**(注意不是上面的小顶堆),并维持堆的大小为 k 个。如果新的数入堆之后堆的大小大于 k,则需要将堆顶的数和新的数进行比较,**并将较大的移除**。这样可以保证**堆中的数是全体数字中最小的 k 个**,而这最小的 k 个中最大的(即堆顶)不就是第 k 小的么?这也就是选择建立大顶堆,而不是小顶堆的原因。 -![固定大顶堆求第 5 小的数](https://tva1.sinaimg.cn/large/0081Kckwly1gmbgz93840j30zk0u0jv7.jpg) +![固定大顶堆求第 5 小的数](https://p.ipic.vip/4llpwb.jpg) 简单一句话总结就是**固定一个大小为 k 的大顶堆可以快速求第 k 小的数,反之固定一个大小为 k 的小顶堆可以快速求第 k 大的数**。比如力扣 2020-02-24 的周赛第三题[5663. 找出第 K 大的异或坐标值](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/ "5663. 找出第 K 大的异或坐标值")就可以用固定小顶堆技巧来实现(这道题让你求第 k 大的数)。 @@ -358,7 +358,7 @@ mat[i] 是一个非递减数组 其实这道题就是给你 m 个长度均相同的一维数组,让我们从这 m 个数组中分别选出一个数,即一共选取 m 个数,求这 m 个数的和是**所有选取可能性**中和第 k 小的。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gmanik03omj31p40u0q8q.jpg) +![](https://p.ipic.vip/38ox6w.jpg) 一个朴素的想法是使用多指针来解。对于这道题来说就是使用 m 个指针,分别指向 m 个一维数组,指针的位置表示当前选取的是该一维数组中第几个。 @@ -750,7 +750,7 @@ n == nums.length 用图来表示就是下面这样的: -![一维数组转二维数组](https://tva1.sinaimg.cn/large/0081Kckwly1gmbn4sty3aj30p0184mzv.jpg) +![一维数组转二维数组](https://p.ipic.vip/9pcj1q.jpg) 这不就相当于: 从 [[1,2], [1,2], [3,6], [1,2,4]] 这样的一个二维数组中的每一行分别选取一个数,并使得其差最小么?这难道不是和上面的题目一模一样么? @@ -798,7 +798,7 @@ class Solution: ### 技巧三 - 事后小诸葛 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmf8regfq7j30fd0c7myc.jpg) +![](https://p.ipic.vip/aqqg1v.jpg) 这个技巧指的是:当从左到右遍历的时候,我们是不知道右边是什么的,需要等到你到了右边之后才知道。 @@ -883,7 +883,7 @@ for i, fuel in stations: 那这个算法是如何体现**事后小诸葛**的呢?你可以把自己代入到题目中进行模拟。 把自己想象成正在开车,你的目标就是题目中的要求:**最少加油次数**。当你开到一个站的时候,你是不知道你的油量够不够支撑到下个站的,并且就算撑不到下个站,其实也许在上个站加油会更好。所以**现实中**你无论如何都**无法知道在当前站,我是应该加油还是不加油的**,因为信息太少了。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmf8sheozpj308s07i3z3.jpg) +![](https://p.ipic.vip/tygyyh.jpg) 那我会怎么做呢?如果是我在开车的话,我只能每次都加油,这样都无法到达目的地,那肯定就无法到达目的地了。但如果这样可以到达目的地,我就可以说**如果我们在那个站加油,这个站选择不加就可以最少加油次数到达目的地了**。你怎么不早说呢? 这不就是事后诸葛亮么? @@ -994,7 +994,7 @@ ans.length == rains.length “今天天气很好,我开了天眼,明天湖泊 2 会被洪水淹没,我们今天就先抽干它,否则就洪水泛滥了。”。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmf8tc1ct1j30m70ec41e.jpg) +![](https://p.ipic.vip/ztgz23.jpg) 和上面的题目一样,我们也可以不先遍历 rain 数组,再模拟每天的变化,而是直接模拟,即使当前是晴天我们也不抽干任何湖泊。接着在模拟的过程**记录晴天的情况**,等到洪水发生的时候,我们再考虑前面**哪一个晴天**应该抽干哪个湖泊。因此这个事后诸葛亮体现在**我们是等到洪水泛滥了才去想应该在之前的某天采取什么手段**。 @@ -1076,7 +1076,7 @@ class Solution: 如果以最佳方式使用给定的梯子和砖块,返回你可以到达的最远建筑物的下标(下标 从 0 开始 )。 ``` -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmf8ug1b7mg30fm0fldtn.gif) +![](https://p.ipic.vip/r12e0t.gif) ``` @@ -1327,7 +1327,7 @@ src = 0, dst = 2, k = 1 城市航班图如下 ``` -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmjkdt3eeaj30do0a0aag.jpg) +![](https://p.ipic.vip/li3v94.jpg) ``` @@ -1343,7 +1343,7 @@ src = 0, dst = 2, k = 0 城市航班图如下 ``` -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmjke11y8yj30do0a0aag.jpg) +![](https://p.ipic.vip/6nsi3i.jpg) ``` @@ -1470,4 +1470,4 @@ def heap_sort(h): 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。目前已经 39K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![二维码](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![二维码](https://p.ipic.vip/nj3yjo.jpg) diff --git a/thinkings/heap.en.md b/thinkings/heap.en.md index 8c60f0680..058bc6fd5 100644 --- a/thinkings/heap.en.md +++ b/thinkings/heap.en.md @@ -1,6 +1,6 @@ # 堆专题 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glegve2v47j319g0u041x.jpg) +![](https://p.ipic.vip/kznqh4.jpg) 大家好,我是 lucifer。今天给大家带来的是《堆》专题。先上下本文的提纲,这个是我用 mindmap 画的一个脑图,之后我会继续完善,将其他专题逐步完善起来。 @@ -18,7 +18,7 @@ [堆标签](https://leetcode-cn.com/tag/tree/ "堆标签")在 leetcode 一共有 **42 道题**。 为了准备这个专题,我将 leetcode 几乎所有的堆题目都刷了一遍。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gldit71vq1j314a0kajtk.jpg) +![](https://p.ipic.vip/iubecr.jpg) 可以看出,除了 3 个上锁的,其他我都刷了一遍。通过集中刷这些题,我发现了一些有趣的信息,今天就分享给大家。 @@ -64,7 +64,7 @@ 如果不同的客户使用不同的窗口。那么我们可以设计三个队列,分别存放正在排队的三种人。这种设计满足了题目要求,也足够简单。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glld41811yj30x20h4jsp.jpg) +![](https://p.ipic.vip/oylgd6.jpg) 如果我们**只有一个窗口**,所有的病人需要使用同一个队列,并且同样的客户类型按照上面讲的**先到先服务原则**,但是不同客户类型之间可能会插队。 @@ -78,7 +78,7 @@ 因此我们就可以继续使用刚才的三个队列的方式,只不过队列存储的不是真实时间,而是虚拟时间。每次开始叫号的时候,我们使用虚拟时间比较,虚拟时间较小的先服务即可。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glldcdznpsj313w0k60w2.jpg) +![](https://p.ipic.vip/ccyo6b.jpg) > 不难看出,队列内部的时间都是有序。 @@ -95,10 +95,10 @@ 如下图是插入一个 1:30 开始排队的普通客户的情况。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glldpnev60j311r0u0wis.jpg) +![](https://p.ipic.vip/epfmkj.jpg) (查找插入位置) -![](https://tva1.sinaimg.cn/large/0081Kckwly1glldqz1z6bj31220l8adl.jpg) +![](https://p.ipic.vip/od3eg0.jpg) (将其插入) 如果队列使用数组实现, 上面插队过程的时间复杂度为 $O(N)$,其中 $N$ 为被插队的队伍长度。如果队伍很长,那么调整的次数明显增加。 @@ -107,11 +107,11 @@ 上面说了链表的实现核心在于查找也需要 $O(N)$,我们可以优化这个过程吗?实际上这就是优先级队列的链表实现,由于是有序的,我们可以用跳表加速查找,时间复杂度可以优化到 $O(logN)$。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glle4lyjv9j30ui0bm0tz.jpg) +![](https://p.ipic.vip/ao1pua.jpg) 其实算法界有很多类似的问题。比如建立数据库索引的算法,如果给某一个有序的列添加索引,不能每次插入一条数据都去调整所有的数据吧(上面的数组实现)?因此我们可以用平衡树来实现,这样每次插入可以最多调整 $(O(logN))$。优先队列的另外一种实现 - 二叉堆就是这个思想,时间复杂度也可以优化到 $O(logN)$ -![](https://tva1.sinaimg.cn/large/0081Kckwly1glle5g74zaj30i60gwwfb.jpg) +![](https://p.ipic.vip/vycxgm.jpg) 本文只讲解常见的二叉堆实现,对于跳表和红黑树不再这里讲。 关于优先队列的二叉堆实现,我们会在后面给大家详细介绍。这里大家只有明白优先队列解决的问题是什么就可以了。 @@ -500,13 +500,13 @@ class Solution { 我们知道,不借助额外空间的情况下,在链表中查找一个值,需要按照顺序一个个查找,时间复杂度为 $O(N)$,其中 N 为链表长度。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6ynn9eknj31ts05wgn5.jpg) +![](https://p.ipic.vip/g6tg2n.jpg) (单链表) 当链表长度很大的时候, 这种时间是很难接受的。 一种常见的的优化方式是**建立哈希表,将所有节点都放到哈希表中,以空间换时间的方式减少时间复杂度**,这种做法时间复杂度为 $O(1)$,但是空间复杂度为 $O(N)$。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6ysd1g34j317o0lun0d.jpg) +![](https://p.ipic.vip/da1rok.jpg) (单链表 + 哈希表) @@ -518,7 +518,7 @@ class Solution { > 注意这个算法要求链表是有序的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6yzbgxdcj32340kun2t.jpg) +![](https://p.ipic.vip/is3071.jpg) (建立一级索引) @@ -529,7 +529,7 @@ class Solution { 这个例子看不出性能提升。但是如果元素继续增大, 继续增加索引的层数,建立二级,三级。。。索引,使得链表能够实现二分查找,从而获得更好的效率。但是相应地,我们需要付出额外空间的代价。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6z4oovv2j31u90u0n50.jpg) +![](https://p.ipic.vip/dky40x.jpg) (增加索引层数) @@ -572,7 +572,7 @@ h.heappop() # 3 本质上来说,二叉堆就是一颗特殊的完全二叉树。它的特殊性只体现在一点,那就是**父节点的权值不大于儿子的权值(小顶堆)**。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15lpppkej30ka0kajsm.jpg) +![](https://p.ipic.vip/v24509.jpg) (一个小顶堆) 上面这句话需要大家记住,一切的一切都源于上面这句话。 @@ -587,24 +587,24 @@ h.heappop() # 3 如果仅仅是删除,那么一个堆就会变成两个堆了,问题变复杂了。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15mal0rzj30j40dct9g.jpg) +![](https://p.ipic.vip/5hp8ip.jpg) (上图出堆之后会生成两个新的堆) 一个常见的操作是,把根结点和最后一个结点交换。但是新的根结点可能不满足 **父节点的权值不大于儿子的权值(小顶堆)**。 如下图,我们将根节点的 2 和尾部的数字进行交换后,这个时候是不满足堆性质的。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15r11v3yj30k60hg75g.jpg) +![](https://p.ipic.vip/140g5w.jpg) 这个时候,其实只需要将新的根节点下沉到正确位置即可。这里的**正确位置**,指的还是那句话**父节点的权值不大于儿子的权值(小顶堆)**。如果不满足这一点,我们就继续下沉,直到满足。 我们知道根节点往下下沉的过程,其实有两个方向可供选择,是下沉到左子节点?还是下沉到右子节点?以小顶堆来说,答案应该是下沉到较小的子节点处,否则会错失正确答案。以上面的堆为例,如果下沉到右子节点 4,那么就无法得到正确的堆顶 3。因此我们需要下沉到左子节点。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15sz0fj6j30i80kaac3.jpg) +![](https://p.ipic.vip/ilhzim.jpg) 下沉到如图位置,还是不满足 **父节点的权值不大于儿子的权值(小顶堆)**,于是我们继续执行同样的操作。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15uqs8c9j30ke0g4q4x.jpg) +![](https://p.ipic.vip/0emyhf.jpg) 有的同学可能有疑问。弹出根节点前堆满足堆的性质,但是弹出之后经过你上面讲的下沉操作,一定还满足么? @@ -623,7 +623,7 @@ h.heappop() # 3 > 之所以这么做的其中一个原因是时间复杂度更低,因为我们是用数组进行模拟的,而在数组尾部添加元素的时间复杂度为 $O(1)$。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm18fd0uytj30mo0j6tab.jpg) +![](https://p.ipic.vip/jxt7ki.jpg) 这次我们发现,不满足堆的节点目前是刚刚被插入节点的尾部节点,因此不能进行下沉操作了。这一次我们需要执行**上浮操作**。 @@ -631,10 +631,10 @@ h.heappop() # 3 和上面基本类似,如果不满足堆的性质,我们将其和父节点交换(上浮),继续这个过程,直到满足堆的性质。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm18h61qyvj30ss0g840w.jpg) +![](https://p.ipic.vip/0suv9m.jpg) (第一次上浮,仍然不满足堆特性,继续上浮) -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm18iyp68qj30ne0hi400.jpg) +![](https://p.ipic.vip/op82y6.jpg) (满足了堆特性,上浮过程完毕) 经过这样的操作,其还是一个满足堆性质的堆。证明过程和上面类似,不再赘述。 @@ -652,12 +652,12 @@ h.heappop() # 3 如图所示是一个完全二叉树和树的数组表示法。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm1833aeutj30dt0f3q3v.jpg) +![](https://p.ipic.vip/c2et7m.jpg) (注意数组索引的对应关系) 形象点来看,我们可以可以画出如下的对应关系图: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm185zwz93j30fu0nj0ud.jpg) +![](https://p.ipic.vip/nl4hlg.jpg) 这样一来,是不是和上面的树差不多一致了?有没有容易理解一点呢? @@ -831,4 +831,4 @@ public class Heap { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。目前已经 37K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![二维码](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![二维码](https://p.ipic.vip/0ss8wk.jpg) diff --git a/thinkings/heap.md b/thinkings/heap.md index 8c60f0680..6df293020 100644 --- a/thinkings/heap.md +++ b/thinkings/heap.md @@ -1,6 +1,6 @@ # 堆专题 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glegve2v47j319g0u041x.jpg) +![](https://p.ipic.vip/f2erxy.jpg) 大家好,我是 lucifer。今天给大家带来的是《堆》专题。先上下本文的提纲,这个是我用 mindmap 画的一个脑图,之后我会继续完善,将其他专题逐步完善起来。 @@ -18,7 +18,7 @@ [堆标签](https://leetcode-cn.com/tag/tree/ "堆标签")在 leetcode 一共有 **42 道题**。 为了准备这个专题,我将 leetcode 几乎所有的堆题目都刷了一遍。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gldit71vq1j314a0kajtk.jpg) +![](https://p.ipic.vip/culzde.jpg) 可以看出,除了 3 个上锁的,其他我都刷了一遍。通过集中刷这些题,我发现了一些有趣的信息,今天就分享给大家。 @@ -64,7 +64,7 @@ 如果不同的客户使用不同的窗口。那么我们可以设计三个队列,分别存放正在排队的三种人。这种设计满足了题目要求,也足够简单。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glld41811yj30x20h4jsp.jpg) +![](https://p.ipic.vip/oratcr.jpg) 如果我们**只有一个窗口**,所有的病人需要使用同一个队列,并且同样的客户类型按照上面讲的**先到先服务原则**,但是不同客户类型之间可能会插队。 @@ -78,7 +78,7 @@ 因此我们就可以继续使用刚才的三个队列的方式,只不过队列存储的不是真实时间,而是虚拟时间。每次开始叫号的时候,我们使用虚拟时间比较,虚拟时间较小的先服务即可。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glldcdznpsj313w0k60w2.jpg) +![](https://p.ipic.vip/cn3q3l.jpg) > 不难看出,队列内部的时间都是有序。 @@ -95,10 +95,10 @@ 如下图是插入一个 1:30 开始排队的普通客户的情况。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glldpnev60j311r0u0wis.jpg) +![](https://p.ipic.vip/mv5jgi.jpg) (查找插入位置) -![](https://tva1.sinaimg.cn/large/0081Kckwly1glldqz1z6bj31220l8adl.jpg) +![](https://p.ipic.vip/v79j9v.jpg) (将其插入) 如果队列使用数组实现, 上面插队过程的时间复杂度为 $O(N)$,其中 $N$ 为被插队的队伍长度。如果队伍很长,那么调整的次数明显增加。 @@ -107,11 +107,11 @@ 上面说了链表的实现核心在于查找也需要 $O(N)$,我们可以优化这个过程吗?实际上这就是优先级队列的链表实现,由于是有序的,我们可以用跳表加速查找,时间复杂度可以优化到 $O(logN)$。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1glle4lyjv9j30ui0bm0tz.jpg) +![](https://p.ipic.vip/3gbp35.jpg) 其实算法界有很多类似的问题。比如建立数据库索引的算法,如果给某一个有序的列添加索引,不能每次插入一条数据都去调整所有的数据吧(上面的数组实现)?因此我们可以用平衡树来实现,这样每次插入可以最多调整 $(O(logN))$。优先队列的另外一种实现 - 二叉堆就是这个思想,时间复杂度也可以优化到 $O(logN)$ -![](https://tva1.sinaimg.cn/large/0081Kckwly1glle5g74zaj30i60gwwfb.jpg) +![](https://p.ipic.vip/n18igs.jpg) 本文只讲解常见的二叉堆实现,对于跳表和红黑树不再这里讲。 关于优先队列的二叉堆实现,我们会在后面给大家详细介绍。这里大家只有明白优先队列解决的问题是什么就可以了。 @@ -500,13 +500,13 @@ class Solution { 我们知道,不借助额外空间的情况下,在链表中查找一个值,需要按照顺序一个个查找,时间复杂度为 $O(N)$,其中 N 为链表长度。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6ynn9eknj31ts05wgn5.jpg) +![](https://p.ipic.vip/p1gvu8.jpg) (单链表) 当链表长度很大的时候, 这种时间是很难接受的。 一种常见的的优化方式是**建立哈希表,将所有节点都放到哈希表中,以空间换时间的方式减少时间复杂度**,这种做法时间复杂度为 $O(1)$,但是空间复杂度为 $O(N)$。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6ysd1g34j317o0lun0d.jpg) +![](https://p.ipic.vip/6jqk71.jpg) (单链表 + 哈希表) @@ -518,7 +518,7 @@ class Solution { > 注意这个算法要求链表是有序的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6yzbgxdcj32340kun2t.jpg) +![](https://p.ipic.vip/6h9dm0.jpg) (建立一级索引) @@ -529,7 +529,7 @@ class Solution { 这个例子看不出性能提升。但是如果元素继续增大, 继续增加索引的层数,建立二级,三级。。。索引,使得链表能够实现二分查找,从而获得更好的效率。但是相应地,我们需要付出额外空间的代价。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg6z4oovv2j31u90u0n50.jpg) +![](https://p.ipic.vip/4x8k76.jpg) (增加索引层数) @@ -572,7 +572,7 @@ h.heappop() # 3 本质上来说,二叉堆就是一颗特殊的完全二叉树。它的特殊性只体现在一点,那就是**父节点的权值不大于儿子的权值(小顶堆)**。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15lpppkej30ka0kajsm.jpg) +![](https://p.ipic.vip/6t6jtn.jpg) (一个小顶堆) 上面这句话需要大家记住,一切的一切都源于上面这句话。 @@ -587,24 +587,24 @@ h.heappop() # 3 如果仅仅是删除,那么一个堆就会变成两个堆了,问题变复杂了。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15mal0rzj30j40dct9g.jpg) +![](https://p.ipic.vip/gx25ru.jpg) (上图出堆之后会生成两个新的堆) 一个常见的操作是,把根结点和最后一个结点交换。但是新的根结点可能不满足 **父节点的权值不大于儿子的权值(小顶堆)**。 如下图,我们将根节点的 2 和尾部的数字进行交换后,这个时候是不满足堆性质的。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15r11v3yj30k60hg75g.jpg) +![](https://p.ipic.vip/j1l594.jpg) 这个时候,其实只需要将新的根节点下沉到正确位置即可。这里的**正确位置**,指的还是那句话**父节点的权值不大于儿子的权值(小顶堆)**。如果不满足这一点,我们就继续下沉,直到满足。 我们知道根节点往下下沉的过程,其实有两个方向可供选择,是下沉到左子节点?还是下沉到右子节点?以小顶堆来说,答案应该是下沉到较小的子节点处,否则会错失正确答案。以上面的堆为例,如果下沉到右子节点 4,那么就无法得到正确的堆顶 3。因此我们需要下沉到左子节点。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15sz0fj6j30i80kaac3.jpg) +![](https://p.ipic.vip/82emug.jpg) 下沉到如图位置,还是不满足 **父节点的权值不大于儿子的权值(小顶堆)**,于是我们继续执行同样的操作。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm15uqs8c9j30ke0g4q4x.jpg) +![](https://p.ipic.vip/fedp74.jpg) 有的同学可能有疑问。弹出根节点前堆满足堆的性质,但是弹出之后经过你上面讲的下沉操作,一定还满足么? @@ -623,7 +623,7 @@ h.heappop() # 3 > 之所以这么做的其中一个原因是时间复杂度更低,因为我们是用数组进行模拟的,而在数组尾部添加元素的时间复杂度为 $O(1)$。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm18fd0uytj30mo0j6tab.jpg) +![](https://p.ipic.vip/ricpp2.jpg) 这次我们发现,不满足堆的节点目前是刚刚被插入节点的尾部节点,因此不能进行下沉操作了。这一次我们需要执行**上浮操作**。 @@ -631,10 +631,10 @@ h.heappop() # 3 和上面基本类似,如果不满足堆的性质,我们将其和父节点交换(上浮),继续这个过程,直到满足堆的性质。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm18h61qyvj30ss0g840w.jpg) +![](https://p.ipic.vip/5vwwp2.jpg) (第一次上浮,仍然不满足堆特性,继续上浮) -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm18iyp68qj30ne0hi400.jpg) +![](https://p.ipic.vip/xig47g.jpg) (满足了堆特性,上浮过程完毕) 经过这样的操作,其还是一个满足堆性质的堆。证明过程和上面类似,不再赘述。 @@ -652,12 +652,12 @@ h.heappop() # 3 如图所示是一个完全二叉树和树的数组表示法。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm1833aeutj30dt0f3q3v.jpg) +![](https://p.ipic.vip/8cjv19.jpg) (注意数组索引的对应关系) 形象点来看,我们可以可以画出如下的对应关系图: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm185zwz93j30fu0nj0ud.jpg) +![](https://p.ipic.vip/30h4kq.jpg) 这样一来,是不是和上面的树差不多一致了?有没有容易理解一点呢? @@ -831,4 +831,4 @@ public class Heap { 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。目前已经 37K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![二维码](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![二维码](https://p.ipic.vip/kdi9ji.jpg) diff --git a/thinkings/island.en.md b/thinkings/island.en.md index 09094649f..28266d45e 100644 --- a/thinkings/island.en.md +++ b/thinkings/island.en.md @@ -157,7 +157,7 @@ Output: 2 Explanation: The 2x2 grid is as follows: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tfleu8lj302a02aa9y.jpg) +![](https://p.ipic.vip/7iwzmr.jpg) ``` @@ -172,7 +172,7 @@ Output: 1 Explanation: The 2x2 grid is as follows: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tg0a44lj302b02a3ye.jpg) +![](https://p.ipic.vip/p7frnm.jpg) ``` @@ -189,7 +189,7 @@ The 2x2 grid is as follows: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tg5hn8vj302b02at8m.jpg) +![](https://p.ipic.vip/d2n90a.jpg) ``` @@ -205,7 +205,7 @@ Explanation: (Recall that because the \ character is escaped, "/\\" means /\, an The 2x2 grid is as follows: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tgi6g9ij3029029jra.jpg) +![](https://p.ipic.vip/vxa1bh.jpg) ``` @@ -220,7 +220,7 @@ Output: 3 Explanation: The 2x2 grid is as follows: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tgn4yysj302a02at8m.jpg) +![](https://p.ipic.vip/06aw2l.jpg) ``` prompt: @@ -267,4 +267,4 @@ The above is the entire content of this article. If you have any comments on thi You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/l0dmxf.jpg) diff --git a/thinkings/island.md b/thinkings/island.md index b8b355a87..4f1a66123 100644 --- a/thinkings/island.md +++ b/thinkings/island.md @@ -158,7 +158,7 @@ class Solution: 解释:2x2 网格如下: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tfleu8lj302a02aa9y.jpg) +![](https://p.ipic.vip/ie8a2v.jpg) ``` @@ -173,7 +173,7 @@ class Solution: 解释:2x2 网格如下: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tg0a44lj302b02a3ye.jpg) +![](https://p.ipic.vip/cm4wgd.jpg) ``` @@ -190,7 +190,7 @@ class Solution: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tg5hn8vj302b02at8m.jpg) +![](https://p.ipic.vip/wb8ru7.jpg) ``` @@ -206,7 +206,7 @@ class Solution: 2x2 网格如下: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tgi6g9ij3029029jra.jpg) +![](https://p.ipic.vip/dpuon4.jpg) ``` @@ -221,7 +221,7 @@ class Solution: 解释:2x2 网格如下: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gm5tgn4yysj302a02at8m.jpg) +![](https://p.ipic.vip/i7hmlc.jpg) ``` @@ -269,4 +269,4 @@ class Solution: 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) +![](https://p.ipic.vip/hnxxzn.jpg) diff --git a/thinkings/linked-list.en.md b/thinkings/linked-list.en.md index 7ca8df806..63c8fe512 100644 --- a/thinkings/linked-list.en.md +++ b/thinkings/linked-list.en.md @@ -1,6 +1,6 @@ # I have almost finished brushing all the linked topics of Lixu, and I found these things. 。 。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gki5nbjcgqj31be0u0q5w.jpg) +![](https://p.ipic.vip/y32bsg.jpg) Let's start with the outline of this article. This is a brain map drawn by me with mindmap. After that, I will continue to improve it and gradually improve other topics. @@ -10,7 +10,7 @@ Hello everyone, this is lucifer. The topic that I bring to you today is "Linked [Linked List Tag] (https://leetcode-cn.com/tag/linked-list /"Linked list tag") There are a total of ** 54 questions** in leetcode. In order to prepare for this topic, I spent a few days brushing almost all the linked list topics of leetcode. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gki5vhm12jj310y0raadh.jpg) +![](https://p.ipic.vip/fdv0l4.jpg) It can be seen that except for the six locked ones, I have brushed all the others. In fact, these six locked ones are not difficult, and they are even similar to the other 48 questions. @@ -22,13 +22,13 @@ By focusing on these questions, I found some interesting information, and I will Various data structures, whether they are linear data structures such as queues and stacks, or non-linear data structures such as trees and graphs, are fundamentally arrays and linked lists. Whether you are using an array or a linked list, you are using computer memory. Physical memory is composed of memory units of the same size, as shown in the figure.: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfqt71jt4cj30kg0b4wfl.jpg) +![](https://p.ipic.vip/4toqem.jpg) (Figure 1. Physical memory) Although arrays and linked lists use physical memory, they are very different in their physical use, as shown in the figure.: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfqtbpbwmrj31gu0qmtej.jpg) +![](https://p.ipic.vip/8e68pn.jpg) (Figure 2. Physical storage diagram of arrays and linked lists) @@ -49,7 +49,7 @@ A linked list is a kind of non-continuous, non-sequential storage structure on a From the physical structure diagram above, it can be seen that an array is a contiguous space, and each item of the array is closely connected, so it is troublesome to perform insert and delete operations. The logarithm of Group Head of insertion and deletion time complexity is$O(N)$, while the average complexity is$O(N)$, only the tail of the Insert and delete is$O(1)$。 Simply put” "arrays are particularly friendly to queries, but unfriendly to deletions and additions“" In order to solve this problem, there is a data structure like a linked list. Linked lists are suitable for scenarios where data needs to be in a certain order, but frequent additions, deletions and deletions are required. For details, please refer to the "Basic Operations of Linked Lists" subsection later. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfigmeqc3xj316o094jt6.jpg) +![](https://p.ipic.vip/kqyqnr.jpg) (Figure 3. A typical logical representation of a linked list) @@ -220,7 +220,7 @@ Some friends do linked list questions, first replace the linked list with an arr This question is really not difficult. It is not difficult to say that there is evidence. Taking the LeetCode platform as an example, there are only two difficult topics. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhptfjewrj310c0fajt1.jpg) +![](https://p.ipic.vip/5h1s19.jpg) Among them, Question 23 basically has no linked list operation. A conventional "merge and sort" can be done, and merging two ordered linked lists is a simple question. If you know how to merge and sort arrays and merge two ordered linked lists, you should easily win this question. @@ -280,11 +280,11 @@ First of all, all we have to do is draw pictures. I have talked about this in th As shown in the figure below, is the part of the linked list that we need to reverse: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhy98pp5fj31d40am3zx.jpg) +![](https://p.ipic.vip/zjpjco.jpg) And we expect it to look like this after reversal: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhs3rsde4j31cc09o75p.jpg) +![](https://p.ipic.vip/8trs7c.jpg) It is not difficult to see that ** Can finally return to tail**. @@ -292,7 +292,7 @@ Due to the recursiveness of the linked list, in fact, we only need to reverse th > Linked lists are a kind of recursive data structure, so using the idea of recursion to consider it often does more with half the effort. Thinking about linked lists recursively will be expanded in the "Three Notes" section later. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhs8pccboj30ku09u0td.jpg) +![](https://p.ipic.vip/ev3ox7.jpg) For the two nodes, we only need to modify the pointer once, which seems not difficult. @@ -300,7 +300,7 @@ For the two nodes, we only need to modify the pointer once, which seems not diff cur. next = pre ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhsaoodvrj30yu0h8761.jpg) +![](https://p.ipic.vip/g8cwne.jpg) It is this operation that not only abruptly has a ring, but also makes you cycle endlessly. They also let them part ways that shouldn't be cut off. @@ -313,11 +313,11 @@ cur. next = pre cur = next ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhsft0cyuj30wa0s80ux.jpg) +![](https://p.ipic.vip/ejtmfc.jpg) What about the ring? In fact, the ring does not need to be solved. Because if we traverse from front to back, then in fact, the previous linked list has been reversed, so my picture above is wrong. The correct picture should be: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhsi5tiorj311y0gcwg1.jpg) +![](https://p.ipic.vip/uuyodd.jpg) So far, we can write the following code: @@ -462,7 +462,7 @@ The figure below is the picture we should draw when traversing the preface. Just 1. The previous one has been processed 2. The rest hasn't been processed yet -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhvdo54mpj31ly0ikjvp.jpg) +![](https://p.ipic.vip/o6vkeo.jpg) Accordingly, it is not difficult for us to write the following recursive code. The code comments are very detailed. Just look at the comments. @@ -480,7 +480,7 @@ dfs(head, None) What if it is a back-order traversal? The old rule, adhering to one of our principles, **Draw a picture first**. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhvf05u10j31n20ikdk6.jpg) +![](https://p.ipic.vip/w9qk6z.jpg) It is not difficult to see that we can pass head. Next gets the next element, and then points the next of the next element to itself to complete the reversal. @@ -490,4 +490,4 @@ It is expressed in code: head. next. next = head ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhvhje71pj31ji0k2gq0.jpg) +![](https://p.ipic.vip/6ttbmh.jpg) diff --git a/thinkings/linked-list.md b/thinkings/linked-list.md index ffbd3a4ca..8498eab17 100644 --- a/thinkings/linked-list.md +++ b/thinkings/linked-list.md @@ -1,6 +1,6 @@ # 几乎刷完了力扣所有的链表题,我发现了这些东西。。。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gki5nbjcgqj31be0u0q5w.jpg) +![](https://p.ipic.vip/msbze4.jpg) 先上下本文的提纲,这个是我用 mindmap 画的一个脑图,之后我后继续完善,将其他专题逐步完善起来。 @@ -10,7 +10,7 @@ [链表标签](https://leetcode-cn.com/tag/linked-list/ "链表标签")在 leetcode 一共有 **54 道题**。 为了准备这个专题,我花了几天时间将 leetcode 几乎所有的链表题目都刷了一遍。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gki5vhm12jj310y0raadh.jpg) +![](https://p.ipic.vip/zbtxl9.jpg) 可以看出,除了六个上锁的,其他我都刷了一遍。而实际上,这六个上锁的也没有什么难度,甚至和其他 48 道题差不多。 @@ -22,13 +22,13 @@ 各种数据结构,不管是队列,栈等线性数据结构还是树,图的等非线性数据结构,从根本上底层都是数组和链表。不管你用的是数组还是链表,用的都是计算机内存,物理内存是一个个大小相同的内存单元构成的,如图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfqt71jt4cj30kg0b4wfl.jpg) +![](https://p.ipic.vip/xhq1to.jpg) (图 1. 物理内存) 而数组和链表虽然用的都是物理内存,都是两者在对物理的使用上是非常不一样的,如图: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfqtbpbwmrj31gu0qmtej.jpg) +![](https://p.ipic.vip/1ka0f8.jpg) (图 2. 数组和链表的物理存储图) @@ -49,7 +49,7 @@ data 是数据域,存放数据,next 是一个指向下一个节点的指针 从上面的物理结构图可以看出数组是一块连续的空间,数组的每一项都是紧密相连的,因此如果要执行插入和删除操作就很麻烦。对数组头部的插入和删除时间复杂度都是$O(N)$,而平均复杂度也是$O(N)$,只有对尾部的插入和删除才是$O(1)$。简单来说”数组对查询特别友好,对删除和添加不友好“。为了解决这个问题,就有了链表这种数据结构。链表适合在数据需要有一定顺序,但是又需要进行频繁增删除的场景,具体内容参考后面的《链表的基本操作》小节。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfigmeqc3xj316o094jt6.jpg) +![](https://p.ipic.vip/u30pfe.jpg) (图 3. 一个典型的链表逻辑表示图) @@ -220,7 +220,7 @@ arr[arr.length - 1] = 'lucifer' 链表题真的不难。说链表不难是有证据的。就拿 LeetCode 平台来说,处于困难难度的题目只有两个。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhptfjewrj310c0fajt1.jpg) +![](https://p.ipic.vip/3swdk5.jpg) 其中 第 23 题基本没有什么链表操作,一个常规的“归并排序”即可搞定,而合并两个有序链表是一个简单题。如果你懂得数组的归并排序和合并两个有序链表,应该轻松拿下这道题。 @@ -280,11 +280,11 @@ reverse(self, head: ListNode, tail: ListNode)。 如下图,是我们需要反转的部分链表: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhy98pp5fj31d40am3zx.jpg) +![](https://p.ipic.vip/nc83zm.jpg) 而我们期望反转之后的长这个样子: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhs3rsde4j31cc09o75p.jpg) +![](https://p.ipic.vip/lxotqm.jpg) 不难看出, **最终返回 tail 即可**。 @@ -292,7 +292,7 @@ reverse(self, head: ListNode, tail: ListNode)。 > 链表是一种递归的数据结构,因此采用递归的思想去考虑往往事半功倍,关于递归思考链表将在后面《三个注意》部分展开。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhs8pccboj30ku09u0td.jpg) +![](https://p.ipic.vip/4hip0a.jpg) 对于两个节点来说,我们只需要下修改一次指针即可,这好像不难。 @@ -300,7 +300,7 @@ reverse(self, head: ListNode, tail: ListNode)。 cur.next = pre ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhsaoodvrj30yu0h8761.jpg) +![](https://p.ipic.vip/4q5r2c.jpg) 就是这一个操作,不仅硬生生有了环,让你死循环。还让不应该一刀两断的它们分道扬镳。 @@ -313,11 +313,11 @@ cur.next = pre cur = next ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhsft0cyuj30wa0s80ux.jpg) +![](https://p.ipic.vip/on3e98.jpg) 那么环呢? 实际上, 环不用解决。因为如果我们是从前往后遍历,那么实际上,前面的链表已经被反转了,因此上面我的图是错的。正确的图应该是: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhsi5tiorj311y0gcwg1.jpg) +![](https://p.ipic.vip/yezxnp.jpg) 至此为止,我们可以写出如下代码: @@ -462,7 +462,7 @@ def dfs(head): 1. 前面的已经处理好了 2. 后面的还没处理好 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhvdo54mpj31ly0ikjvp.jpg) +![](https://p.ipic.vip/87uwuu.jpg) 据此,我们不难写出以下递归代码,代码注释很详细,大家看注释就好了。 @@ -480,7 +480,7 @@ dfs(head, None) 如果是后序遍历呢?老规矩,秉承我们的一个原则,**先画图**。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhvf05u10j31n20ikdk6.jpg) +![](https://p.ipic.vip/i9i8d5.jpg) 不难看出,我们可以通过 head.next 拿到下一个元素,然后将下一个元素的 next 指向自身来完成反转。 @@ -490,13 +490,13 @@ dfs(head, None) head.next.next = head ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhvhje71pj31ji0k2gq0.jpg) +![](https://p.ipic.vip/ybnksi.jpg) 画出图之后,是不是很容易看出图中有一个环? 现在知道画图的好处了吧?就是这么直观,当你很熟练了,就不需要画了,但是在此之前,请不要偷懒。 因此我们需要将 head.next 改为不会造成环的一个值,比如置空。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhvj9pa2oj31j40k0n1h.jpg) +![](https://p.ipic.vip/yto283.jpg) ```py def dfs(head): @@ -513,7 +513,7 @@ def dfs(head): 值得注意的是,**前序遍历很容易改造成迭代,因此推荐大家使用前序遍历**。我拿上面的迭代和这里的前序遍历给大家对比一下。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhur25rl6j317i0iiq7b.jpg) +![](https://p.ipic.vip/6xhd8g.jpg) 那么为什么**前序遍历很容易改造成迭代**呢?实际上,这句话我说的不准确,准确地说应该是**前序遍历容易改成不需要栈的递归,而后续遍历需要借助栈来完成**。这也不难理解,由于后续遍历的主逻辑在函数调用栈的弹出过程,而前序遍历则不需要。 @@ -569,13 +569,13 @@ A3: ListNode(3) **ans.next 指向什么取决于最后切断 ans.next 指向的地方在哪**。比如 Q1,ans.next 指向的是 head,我们假设其指向的内存编号为 `9527`。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhw2lifs8j30rs0d275t.jpg) +![](https://p.ipic.vip/mplvs9.jpg) 之后执行 `head = head.next` (ans 和 head 被切断联系了),此时的内存图: > 我们假设头节点的 next 指针指向的节点的内存地址为 10200 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhw3pzl7xj30wa0nmwh7.jpg) +![](https://p.ipic.vip/l5uhz9.jpg) 不难看出,ans 没变。 @@ -588,7 +588,7 @@ head.next = ListNode(4) ans 和 head 又同时指向 ListNode(3) 了。如图: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhwb3y2yfj30tc0g4aca.jpg) +![](https://p.ipic.vip/m2veru.jpg) `head.next = ListNode(4)` 也是同理。因此最终的指向 ans.next 是 ListNode(4)。 @@ -607,7 +607,7 @@ head = ListNode(2) head.next = ListNode(4) ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhwgh3gkfj311q0qadj0.jpg) +![](https://p.ipic.vip/kb4hkr.jpg) 指向了 `head = ListNode(2)` 之后, head 和 ans 的关系就被切断了,**当前以及之后所有的 head 操作都不会影响到 ans**,因此 ans 还指向被切断前的节点,因此 ans.next 输出的是 ListNode(3)。 @@ -642,15 +642,15 @@ head.next = ListNode(4) 还是以反转链表为例,只不过这次是`反转链表的中间一部分`,那我们该怎么做? -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhx2qi4r3j31y80jsq5s.jpg) +![](https://p.ipic.vip/pidaw4.jpg) 反转前面我们已经讲过了,于是我假设链表已经反转好了,那么如何将反转好的链表拼后去呢? -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhx496ge5j31w60gywh1.jpg) +![](https://p.ipic.vip/guk4mw.jpg) 我们想要的效果是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhx5vpcg2j31lg0u0afd.jpg) +![](https://p.ipic.vip/pw5mw6.jpg) 那怎么达到图上的效果呢?我的做法是从左到右给断点编号。如图有两个断点,共涉及到四个节点。于是我给它们依次编号为 a,b,c,d。 @@ -665,7 +665,7 @@ a.next = c b.next = d ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkhyqypiltj31b40oy77h.jpg) +![](https://p.ipic.vip/8e2key.jpg) 这不就好了么?我记得的就有 25 题,61 题 和 92 题都是这么做的,清晰不混乱。 @@ -803,6 +803,6 @@ while cur: 我整理的 1000 多页的电子书已经开发下载了,大家可以去我的公众号《力扣加加》后台回复电子书获取。 -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928846461-image.png) +![](https://p.ipic.vip/qjoumi.png) -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928862442-image.png) +![](https://p.ipic.vip/b02qzv.png) diff --git a/thinkings/monotone-stack.en.md b/thinkings/monotone-stack.en.md index 02a65fa0e..c068c8403 100644 --- a/thinkings/monotone-stack.en.md +++ b/thinkings/monotone-stack.en.md @@ -4,7 +4,7 @@ As the name suggests, a monotonic stack is a kind of stack. Therefore, to learn ## What is a stack? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfbikq9ipmj30cd0a73yp.jpg) +![](https://p.ipic.vip/nkmnv8.jpg) The stack is a restricted data structure, which is reflected in the fact that only new content is allowed to be inserted or deleted from one direction. This direction is called the top of the stack, and obtaining content from other locations is not allowed. @@ -28,7 +28,7 @@ Since the stack only operates at the end, if we use arrays for simulation, the t 1. In-stack-O (1) 2. Out of the stack-O (1) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfbil9jqqej30sd0fhdgz.jpg) +![](https://p.ipic.vip/35ede1.jpg) ### Application diff --git a/thinkings/monotone-stack.md b/thinkings/monotone-stack.md index 0818e3884..ef1e3690c 100644 --- a/thinkings/monotone-stack.md +++ b/thinkings/monotone-stack.md @@ -4,7 +4,7 @@ ## 栈是什么? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfbikq9ipmj30cd0a73yp.jpg) +![](https://p.ipic.vip/3ad96r.jpg) 栈是一种受限的数据结构, 体现在只允许新的内容从一个方向插入或删除,这个方向我们叫栈顶,而从其他位置获取内容是不被允许的 @@ -28,7 +28,7 @@ 1. 进栈 - O(1) 2. 出栈 - O(1) -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfbil9jqqej30sd0fhdgz.jpg) +![](https://p.ipic.vip/x9l30w.jpg) ### 应用 diff --git a/thinkings/prefix.en.md b/thinkings/prefix.en.md index e356964f7..feab711c1 100644 --- a/thinkings/prefix.en.md +++ b/thinkings/prefix.en.md @@ -34,7 +34,7 @@ If you were asked to find the total number of consecutive subarrays of an array, One idea is that the total number of consecutive subarrays is equal to: ** The number of subarrays ending with index 0 + the number of subarrays ending with index 1 +. . . + The number of subarrays ending with index n-1**, which is undoubtedly complete. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj6m27kgbsj306u06gt8u.jpg) +![](https://p.ipic.vip/gp8wlg.jpg) At the same time, ** Use the prefix and idea of the subject 0 to sum while traversing. ** @@ -133,7 +133,7 @@ What if I ask you to find that the maximum value of the subarray is exactly the In fact, betweenK can directly use atMostK, that is, atMostK(k1, nums)-atMostK(k2-1, nums), where k1> k2. The premise is that the values are discrete, for example, the questions I asked above are all integers. Therefore, I can directly subtract 1, because **1 is the smallest interval between two integers**. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj8m692laxj30pz0grte9.jpg) +![](https://p.ipic.vip/v5t94x.jpg) As above, `an area less than or equal to 10` minus`an area less than 5` means`an area greater than or equal to 5 and less than or equal to 10'. diff --git a/thinkings/prefix.md b/thinkings/prefix.md index ade57c8d6..ba97f2063 100644 --- a/thinkings/prefix.md +++ b/thinkings/prefix.md @@ -34,7 +34,7 @@ 一种思路是总的连续子数组个数等于:**以索引为 0 结尾的子数组个数 + 以索引为 1 结尾的子数组个数 + ... + 以索引为 n - 1 结尾的子数组个数**,这无疑是完备的。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj6m27kgbsj306u06gt8u.jpg) +![](https://p.ipic.vip/p00ihn.jpg) 同时**利用母题 0 的前缀和思路, 边遍历边求和。** @@ -133,7 +133,7 @@ function countSubArray(k, nums) { 实际上是 betweenK 可以直接利用 atMostK,即 atMostK(k1, nums) - atMostK(k2 - 1, nums),其中 k1 > k2。前提是值是离散的, 比如上面我出的题都是整数。 因此我可以直接 减 1,因为 **1 是两个整数最小的间隔**。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj8m692laxj30pz0grte9.jpg) +![](https://p.ipic.vip/9angl4.jpg) 如上,`小于等于 10 的区域`减去 `小于 5 的区域`就是 `大于等于 5 且小于等于 10 的区域`。 @@ -571,11 +571,11 @@ class Solution: **注意到里层的 while 循环是连续的数组全部加上一个数字,不难想到可以利用母题 0 的前缀和思路优化。** -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj8k7w0bqyj30qh07540b.jpg) +![](https://p.ipic.vip/0xpuf6.jpg) 一种思路就是在 i 的位置 + k, 然后利用前缀和的技巧给 i 到 n 的元素都加上 k。但是题目需要加的是一个区间, j + 1 及其之后的元素会被多加一个 k。一个简单的技巧就是给 j + 1 的元素减去 k,这样正负就可以抵消。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gj8k997nmbj30q9074dhm.jpg) +![](https://p.ipic.vip/u5ogtv.jpg) ### 代码(Python) diff --git a/thinkings/run-length-encode-and-huffman-encode.en.md b/thinkings/run-length-encode-and-huffman-encode.en.md index c09306038..a514f4541 100644 --- a/thinkings/run-length-encode-and-huffman-encode.en.md +++ b/thinkings/run-length-encode-and-huffman-encode.en.md @@ -37,7 +37,7 @@ For example, the result of our frequency statistics for a string is as follows The result is like this: -![huffman-example](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhusda8j30re0hmabe.jpg) +![huffman-example](https://p.ipic.vip/1wqdu2.jpg) | character | frequency | encoding | | :-------: | :-------: | :------: | diff --git a/thinkings/run-length-encode-and-huffman-encode.md b/thinkings/run-length-encode-and-huffman-encode.md index 90b98b705..ba9d47d0b 100644 --- a/thinkings/run-length-encode-and-huffman-encode.md +++ b/thinkings/run-length-encode-and-huffman-encode.md @@ -12,7 +12,7 @@ Huffman 编码的过程包含两个主要部分: 上面提到了他的基本原理就是`用短的编码表示出现频率高的字符,用长的编码来表示出现频率低的字符`,因此首先要做的就是统计字符的出现频率,然后根据统计的频率来构建 Huffman 树(又叫最优二叉树)。 -![Huffman-tree](../assets/thinkings/huffman-tree.webp) +![Huffman-tree](https://p.ipic.vip/v13yj7.jpg) 如图,**huffman 树以一颗二叉树**。 其中节点的左子节点路径用 0 表示,右子节点用 1 表示,节点的值表示的是其权重,权重越大深度越小。深度表示的其实就是编码的长度。通常我们使用字符出现的频率作为权重。真正执行编码的时候,类似字典树,节点不用来编码,节点的路径用来编码. @@ -37,7 +37,7 @@ Huffman 编码的过程包含两个主要部分: 结果是这样的: -![huffman-example](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhusda8j30re0hmabe.jpg) +![huffman-example](https://p.ipic.vip/bn2vws.jpg) | character | frequency | encoding | | :-------: | :-------: | :------: | diff --git a/thinkings/search.en.md b/thinkings/search.en.md index fbf30e4db..cf6260864 100644 --- a/thinkings/search.en.md +++ b/thinkings/search.en.md @@ -57,7 +57,7 @@ Based on this, you can draw the following decision tree. (The figure below describes part of the process of making decisions on an array of length 3. The numbers in the tree nodes represent the index. That is, it is determined that there are three choices for the first number, and it is determined that the second number will become the remaining two choices based on the last choice) -![](https://tva1.sinaimg.cn/large/008i3skNly1gqwu244duoj30n40iaabd.jpg) +![](https://p.ipic.vip/xk7cqr.jpg) Animated demonstration of the decision-making process: diff --git a/thinkings/search.md b/thinkings/search.md index 74f42d697..e3418fee2 100644 --- a/thinkings/search.md +++ b/thinkings/search.md @@ -62,11 +62,11 @@ (下图描述的是对一个长度为 3 的数组进行决策的部分过程,树节点中的数字表示索引。即确定第一个数有三个选择,确定第二个数会根据上次的选择变为剩下的两个选择) -![](https://tva1.sinaimg.cn/large/008i3skNly1gqwu244duoj30n40iaabd.jpg) +![](https://p.ipic.vip/us3d79.jpg) 决策过程动图演示: -![搜索-决策树.svg](https://pic.stackoverflow.wiki/uploadImages/115/238/39/106/2021/05/27/18/33/b97ee92b-a516-48e1-83d9-b29c1eaf2eff.svg) +![搜索-决策树.svg](https://p.ipic.vip/1ftp32.jpg) **一些搜索算法就是基于这个朴素的思想,本质就是模拟这个决策树**。这里面其实也有很多有趣的细节,后面我们会对其进行更加详细的讲解。而现在大家只需要对**解空间是什么以及如何对解空间进行遍历有一点概念就行了。** 后面我会继续对这个概念进行加深。 @@ -500,13 +500,13 @@ class Solution: 以上就是双向搜索的大体思路。用图来表示就是这样的: -![](https://tva1.sinaimg.cn/large/008i3skNly1gr1qya05soj30kp0n8q4u.jpg) +![](https://p.ipic.vip/epi1dl.jpg) 如上图,我们从起点和终点(A 和 Z)分别开始搜索,如果起点的扩展状态和终点的扩展状态重叠(本质上就是队列中的元素重叠了),那么我们就知道了一个从节点到终点的最短路径。 动图演示: -![双向搜索.svg](https://pic.stackoverflow.wiki/uploadImages/115/238/39/106/2021/05/31/17/41/ab3959a8-ebc2-4772-9f04-390f5cac675b.svg) +![双向搜索.svg](https://p.ipic.vip/1j44k8.jpg) 看到这里有必要暂停一下插几句话。 @@ -518,13 +518,13 @@ class Solution: - 为什么双向搜索更快了?通过上面的图我们发现通常刚开始的时候边比较少,队列中的数据也比较少。而随着搜索的进行,**搜索树越来越大, 队列中的节点随之增多**。和上面双向搜索类似,这种增长速度很多情况下是指数级别的,而双向搜索**可以将指数的常系数移动到多项式系数**。如果不使用双向搜索那么搜索树大概是这样的: -![](https://tva1.sinaimg.cn/large/008i3skNly1gr1r2x6ijij30hz0nvmyz.jpg) +![](https://p.ipic.vip/hm471g.jpg) 可以看出搜索树大了很多,以至于很多点我都画不下,只好用 ”。。。“ 来表示。 - 什么情况下更快?相比于单向搜索,双向搜索通常更快。当然也有例外,举个极端的例子,假如从起点到终点只有一条路径,那么无论使用单向搜索还是双向搜索结果都是一样。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gr1qrck4fqj30g808edgf.jpg) +![](https://p.ipic.vip/k5nm5q.jpg) 如图使用单向搜索还是双向搜索都是一样的。 @@ -547,7 +547,7 @@ class Solution: - 构建邻接矩阵 - 每次都尝试从 q1 和 q2 中的较小的进行扩展。这样可以达到剪枝的效果。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gr1q5h0haxj30ig08ygmg.jpg) +![](https://p.ipic.vip/zu7r4y.jpg) - 如果 q1 和 q2 交汇了,则将两者的路径拼接起来即可。 @@ -618,13 +618,13 @@ Python3 Code: 如下图: -![](https://tva1.sinaimg.cn/large/008i3skNly1gr1pyirzhvj30jo096t9o.jpg) +![](https://p.ipic.vip/sbozez.jpg) 上面的队列是普通的队列。 而下面的双端队列,可以看出我们在队头插队了一个 B。 动图演示: -![双端队列.svg](https://pic.stackoverflow.wiki/uploadImages/115/238/39/106/2021/05/31/17/07/5d905ba0-c4c4-4bb4-91e9-d2ccb79d435b.svg) +![双端队列.svg](https://p.ipic.vip/nj812l.jpg) > 思考:如果图对应的权值不是 0 和 1,而是任意正整数呢? @@ -642,23 +642,23 @@ BFS 和 DFS 分别处理什么样的问题?两者究竟有什么样的区别 如下图,我们遍历到 A,有三个选择。此时我们可以任意选择一条,比如选择了 B,程序会继续往下进行选择分支 2,3 。。。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gqrjwjmdegj30yy0u0n29.jpg) +![](https://p.ipic.vip/pv1i95.jpg) 如下动图演示了一个典型的 DFS 流程。后面的章节,我们会给大家带来更复杂的图上 DFS。 -![binary-tree-traversal-dfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui7vcmwg30dw0dw3yl.gif) +![binary-tree-traversal-dfs](https://p.ipic.vip/p7rnza.gif) - BFS 在分叉点会选择搜索的路径各尝试一次。使用队列来存储待处理的元素时,队列中**最多**只会有两层的元素,且满足单调性,即相同层的元素在一起。**基于这个特点有很多有趣的优化。** 如下图,广度优先遍历会将搜索的选择全部选择一遍会才会进入到下一层。和上面一样,我给大家标注了程序执行的一种可能的顺序。 -![](https://tva1.sinaimg.cn/large/008i3skNly1gqrjziifatj31er0u0dqj.jpg) +![](https://p.ipic.vip/u8m52f.jpg) 可以发现,和我上面说的一样。右侧的队列始终最多有两层的节点,并且相同层的总在一起,换句话说队列的元素在层上**满足单调性**。 如下动图演示了一个典型的 BFS 流程。后面的章节,我们会给大家带来更复杂的图上 BFS。 -![binary-tree-traversal-bfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluic79lag30dw0dw3yl.gif) +![binary-tree-traversal-bfs](https://p.ipic.vip/oynlqu.gif) ## 总结 diff --git a/thinkings/slide-window.en.en.md b/thinkings/slide-window.en.en.md index 4f4729d3c..d5ab85b02 100644 --- a/thinkings/slide-window.en.en.md +++ b/thinkings/slide-window.en.en.md @@ -31,7 +31,7 @@ For fixed window size problem, we only need to keep track of the left pointer l - 4.1 If they satisfy, based on whether we need an optimal solution or not, we either return the solution or keep updating until we find the optimal one. - 4.2 Otherwise, we continue to find an appropriate window -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhfr2c3j308z0d5aaa.jpg) +![](https://p.ipic.vip/41ke5d.jpg) ### Variable Window Size @@ -47,7 +47,7 @@ For variable window, we initialize the left and right pointers the same way. The If we view it another way, it's simply moving the pointer r to find an appropriate window and we only move the pointer l once we find an appropriate window to minimize the window and find an optimal solution. -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluhlt7wwj30d90d50t5.jpg) +![](https://p.ipic.vip/z8ram4.jpg) ## Code Template diff --git a/thinkings/slide-window.en.md b/thinkings/slide-window.en.md index 120751d72..eb834af54 100644 --- a/thinkings/slide-window.en.md +++ b/thinkings/slide-window.en.md @@ -31,7 +31,7 @@ - 4.1 如果满足,再判断是否需要更新最优解,如果需要则更新最优解 - 4.2 如果不满足,则继续。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugkc80jj308z0d5aaa.jpg) +![](https://p.ipic.vip/vpgo8a.jpg) ### 可变窗口大小 @@ -45,7 +45,7 @@ 形象地来看的话,就是 r 指针不停向右移动,l 指针仅仅在窗口满足条件之后才会移动,起到窗口收缩的效果。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugl94y8j30d90d50t5.jpg) +![](https://p.ipic.vip/n7w2h5.jpg) ## 模板代码 diff --git a/thinkings/slide-window.md b/thinkings/slide-window.md index 120751d72..e368f2773 100644 --- a/thinkings/slide-window.md +++ b/thinkings/slide-window.md @@ -31,7 +31,7 @@ - 4.1 如果满足,再判断是否需要更新最优解,如果需要则更新最优解 - 4.2 如果不满足,则继续。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugkc80jj308z0d5aaa.jpg) +![](https://p.ipic.vip/aw5lz6.jpg) ### 可变窗口大小 @@ -45,7 +45,7 @@ 形象地来看的话,就是 r 指针不停向右移动,l 指针仅仅在窗口满足条件之后才会移动,起到窗口收缩的效果。 -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlugl94y8j30d90d50t5.jpg) +![](https://p.ipic.vip/q5hcro.jpg) ## 模板代码 diff --git a/thinkings/tree.en.md b/thinkings/tree.en.md index c00692982..c1499f02a 100644 --- a/thinkings/tree.en.md +++ b/thinkings/tree.en.md @@ -1,6 +1,6 @@ # I have almost finished brushing all the tree questions of Lixu, and I found these things. 。 。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkybjfbpubj30uo0u0gqz.jpg) +![](https://p.ipic.vip/cwv5zz.jpg) Let's start with the outline of this article. This is a brain map drawn by me with mindmap. After that, I will continue to improve it and gradually improve other topics. @@ -14,11 +14,11 @@ This series contains the following topics: First light up the protagonist of this article-tree (my makeup technique is okay ^\_^): -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkyz162e1ij30lu0ssdhm.jpg) +![](https://p.ipic.vip/5lkkd6.jpg) [Tree Tag] (https://leetcode-cn.com/tag/tree /"Tree tag") There are a total of 175 questions in leetcode. In order to prepare for this topic, I spent a few days brushing almost all the tree topics of leetcode. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkpkbu92m2j30u00vg0xu.jpg) +![](https://p.ipic.vip/bdo0jv.jpg) Except for 35 locked ones, 1 question that cannot be done (1628 questions, I don't know why I can't do it), and 4 questions that are labeled with trees but are pictures. I have brushed all the others. By focusing on these questions, I found some interesting information, and I will share it with you today. @@ -36,11 +36,11 @@ Finally, it should be emphasized that this article is only a common routine to h When it comes to trees, everyone is more familiar with the trees in reality, and the trees in reality are like this: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkydk0w4uoj31750u0amg.jpg) +![](https://p.ipic.vip/4vw7kq.jpg) The tree in the computer is actually the reflection of the tree in reality. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkydoh5we8j31bl0u0kjn.jpg) +![](https://p.ipic.vip/w7a1lt.jpg) The data structure of a computer is an abstraction of the relationship between objects in the real world. For example, the family tree of the family, the organizational relationship of the personnel in the company structure, the folder structure in the computer, the dom structure of the html rendering, etc., These hierarchical structures are called trees in the computer field. @@ -62,13 +62,13 @@ Obviously, its input parameters and return values are not trees, but they do not Continue to go back to the above code, according to the above code, you can draw the following recursive tree. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqv37r0x4j30f90iot9s.jpg) +![](https://p.ipic.vip/ikc4cu.jpg) Where the edges of the tree represent the return value, and the tree nodes represent the values that need to be calculated, namely fn(n). Taking the calculation of 5's fibbonacci as an example, the process is probably like this (animated demonstration): -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqvazbxs8g30gi0my4qp.gif) +![](https://p.ipic.vip/y5iown.gif) **This is actually the subsequent traversal of a tree. **, do you think the tree (logical tree) is very important? We will talk about the post-sequence traversal later, now everyone knows that this is the case. @@ -86,7 +86,7 @@ A generalized tree is really useful, but its scope is too large. The topic of tr A tree is a non-linear data structure. The basic unit of tree structure is the node. The link between nodes is called a branch. Nodes and branches form a tree, and the beginning of the structure is called the root, or root node. Nodes other than the root node are called child nodes. Nodes that are not linked to other child nodes are called leaf nodes (leaf). The figure below is a typical tree structure: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjv3xmkknj30jb0cymxw.jpg) +![](https://p.ipic.vip/abgn4d.jpg) Each node can be represented by the following data structure: @@ -195,7 +195,7 @@ It can be seen that in the implementation, White represents the first entry proc If you want to implement preorder and postorder traversal, you only need to adjust the stacking order of the left and right child nodes, and there is no need to make any changes to the other parts. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkq01o7423j31gg0u0dwg.jpg) (You only need to adjust the position of these three sentences to traverse the front, middle and back sequence) +![](https://p.ipic.vip/o9d4m4.jpg) (You only need to adjust the position of these three sentences to traverse the front, middle and back sequence) > Note: The preface and preface of this schematic diagram are reversed @@ -225,7 +225,7 @@ While BFS is suitable for seeking the shortest distance, this is not the same as Hierarchical traversal is to traverse the tree layer by layer and access it in the hierarchical order of the tree. -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkye7nyrjaj30yw0ec762.jpg) (Hierarchical traversal diagram) +![](https://p.ipic.vip/7n2sg5.jpg) (Hierarchical traversal diagram) \*\*The core of BFS is that it can be terminated early when the shortest time is required. This is its core value. Hierarchical traversal is a byproduct of BFS that does not require early termination. This early termination is different from the early termination of DFS pruning, but the early termination of finding the nearest target. For example, if I want to find the nearest target node, BFS can return directly after finding the target node. And DFS has to exhaustively list all possibilities to find the nearest one, which is the core value of BFS. In fact, we can also use DFS to achieve the effect of hierarchical traversal. With the help of recursion, the code will be even simpler. @@ -243,7 +243,7 @@ As of now (2020-02-21), there are 129 questions in the LeetCode for depth-first DFS illustration: -![binary-tree-traversal-dfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui7vcmwg30dw0dw3yl.gif) +![binary-tree-traversal-dfs](https://p.ipic.vip/7zo12v.gif) (Picture from https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search ) diff --git a/thinkings/tree.md b/thinkings/tree.md index ae8b3a738..8445f85e0 100644 --- a/thinkings/tree.md +++ b/thinkings/tree.md @@ -1,6 +1,6 @@ # 几乎刷完了力扣所有的树题,我发现了这些东西。。。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkybjfbpubj30uo0u0gqz.jpg) +![](https://p.ipic.vip/6lmcjx.jpg) 先上下本文的提纲,这个是我用 mindmap 画的一个脑图,之后我会继续完善,将其他专题逐步完善起来。 @@ -15,11 +15,11 @@ 首先亮一下本文的主角 - 树(我的化妆技术还行吧^\_^): -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkyz162e1ij30lu0ssdhm.jpg) +![](https://p.ipic.vip/pe39ec.jpg) [树标签](https://leetcode-cn.com/tag/tree/ "树标签")在 leetcode 一共有 **175 道题**。 为了准备这个专题,我花了几天时间将 leetcode 几乎所有的树题目都刷了一遍。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkpkbu92m2j30u00vg0xu.jpg) +![](https://p.ipic.vip/rsenck.jpg) 除了 35 个上锁的,1 个不能做的题(1628 题不知道为啥做不了), 4 个标着树的标签但却是图的题目,其他我都刷了一遍。通过集中刷这些题,我发现了一些有趣的信息,今天就分享给大家。 @@ -37,11 +37,11 @@ 提到树大家更熟悉的是现实中的树,而现实中的树是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkydk0w4uoj31750u0amg.jpg) +![](https://p.ipic.vip/b170o8.jpg) 而计算机中的树其实是现实中的树的倒影。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkydoh5we8j31bl0u0kjn.jpg) +![](https://p.ipic.vip/dkkqya.jpg) 计算机的数据结构是对现实世界物体间关系的一种抽象。比如家族的族谱,公司架构中的人员组织关系,电脑中的文件夹结构,html 渲染的 dom 结构等等,这些有层次关系的结构在计算机领域都叫做树。 @@ -63,13 +63,13 @@ function fn(n) { 继续回到上面的代码,根据上面的代码可以画出如下的递归树。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqv37r0x4j30f90iot9s.jpg) +![](https://p.ipic.vip/bcwh8q.jpg) 其中树的边表示的是返回值,树节点表示的是需要计算的值,即 fn(n)。 以计算 5 的 fibbonacci 为例,过程大概是这样的(动图演示): -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqvazbxs8g30gi0my4qp.gif) +![](https://p.ipic.vip/tq20mp.gif) **这其实就是一个树的后序遍历**,你说树(逻辑上的树)是不是很重要?关于后序遍历咱们后面再讲,现在大家知道是这么回事就行。 @@ -87,7 +87,7 @@ function fn(n) { 树是一种非线性数据结构。树结构的基本单位是节点。节点之间的链接,称为分支(branch)。节点与分支形成树状,结构的开端,称为根(root),或根结点。根节点之外的节点,称为子节点(child)。没有链接到其他子节点的节点,称为叶节点(leaf)。如下图是一个典型的树结构: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfjv3xmkknj30jb0cymxw.jpg) +![](https://p.ipic.vip/zxziz6.jpg) 每个节点可以用以下数据结构来表示: @@ -209,7 +209,7 @@ class Solution: 如要**实现前序、后序遍历,也只需要调整左右子节点的入栈顺序即可,其他部分是无需做任何变化**。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkq01o7423j31gg0u0dwg.jpg) +![](https://p.ipic.vip/jgzo24.jpg) (前中后序遍历只需要调整这三句话的位置即可) > 注:这张示意图的前序和后序画反了 @@ -240,7 +240,7 @@ class Solution: 层次遍历就是一层层遍历树,按照树的层次顺序进行访问。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkye7nyrjaj30yw0ec762.jpg) +![](https://p.ipic.vip/d93wqd.jpg) (层次遍历图示) **BFS 的核心在于求最短问题时候可以提前终止,这才是它的核心价值,层次遍历是一种不需要提前终止的 BFS 的副产物**。这个提前终止不同于 DFS 的剪枝的提前终止,而是找到最近目标的提前终止。比如我要找距离最近的目标节点,BFS 找到目标节点就可以直接返回。而 DFS 要穷举所有可能才能找到最近的,这才是 BFS 的核心价值。实际上,我们也可以使用 DFS 实现层次遍历的效果,借助于递归,代码甚至会更简单。 @@ -259,7 +259,7 @@ class Solution: DFS 图解: -![binary-tree-traversal-dfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlui7vcmwg30dw0dw3yl.gif) +![binary-tree-traversal-dfs](https://p.ipic.vip/9l3es0.gif) (图片来自 https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search) @@ -395,7 +395,7 @@ function dfs(root) { 4 5 ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkyyxm3hamj31990u0dtc.jpg) +![](https://p.ipic.vip/tmo5xd.jpg) 图画的还算比较清楚, 就不多解释了。大家遇到题目多画几次这样的递归图,慢慢就对递归有感觉了。 @@ -409,7 +409,7 @@ BFS 比较适合找**最短距离/路径**和**某一个距离的目标**。比 BFS 图解: -![binary-tree-traversal-bfs](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluic79lag30dw0dw3yl.gif) +![binary-tree-traversal-bfs](https://p.ipic.vip/ngpvx8.gif) (图片来自 https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/breadth-first-search) @@ -511,7 +511,7 @@ class Solution: 树的遍历是后面所有内容的基础,而树的遍历的两种方式 DFS 和 BFS 到这里就简单告一段落,现在大家只要知道 DFS 和 BFS 分别有两种常见的方式就够了,后面我会给大家详细补充。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkpw8vgshuj30ce0kqwgt.jpg) +![](https://p.ipic.vip/ns8q58.jpg) ## 三种题型 @@ -613,7 +613,7 @@ class Solution: 比如: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkpxakvvlwj30650anjrj.jpg) +![](https://p.ipic.vip/g9kzbm.jpg) ``` 此时需要返回 3 @@ -676,7 +676,7 @@ class Solution: 最经典的就是 [剑指 Offer 37. 序列化二叉树](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/)。我们知道力扣的所有的树表示都是使用数字来表示的,而这个数组就是一棵树的层次遍历结果,部分叶子节点的子节点(空节点)也会被打印。比如:[1,2,3,null,null,4,5],就表示的是如下的一颗二叉树: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkpyzzz9jrj30a20a8dge.jpg) +![](https://p.ipic.vip/h0vpxq.jpg) 我们是如何根据这样的一个层次遍历结果构造出原始二叉树的呢?这其实就属于构造二叉树的内容,这个类型目前力扣就这一道题。这道题如果你彻底理解 BFS,那么就难不倒你。 @@ -731,7 +731,7 @@ class Solution { 简单回顾一下这一小节的知识。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkq1lml9dej30fw0fw40e.jpg) +![](https://p.ipic.vip/qam2jk.jpg) 接下来是做树的题目不得不知的四个重要概念。 @@ -756,7 +756,7 @@ class Solution { 举个例子,如下一颗二叉查找树,我们想找节点值小于且最接近 58 的节点,搜索的流程如图所示: -![bst](https://tva1.sinaimg.cn/large/007S8ZIlly1ghluh33ttoj30rs0mudhi.jpg) +![bst](https://p.ipic.vip/gk03po.jpg) (图片来自 https://www.geeksforgeeks.org/floor-in-binary-search-tree-bst/) 可以看出每次向下走,都会排除了一个分支,如果一颗二叉搜索树同时也是一颗二叉平衡树的话,那么其搜索过程时间复杂度就是 $O(logN)$。实际上,**平衡二叉搜索树的查找和有序数组的二分查找本质都是一样的,只是数据的存储方式不同罢了**。那为什么有了有序数组二分,还需要二叉搜索树呢?原因在于树的结构对于动态数据比较友好,比如数据是频繁变动的,比如经常添加和删除,那么就可以使用二叉搜索树。理论上添加和删除的时间复杂度都是 $O(h)$,其中 h 为树的高度,如果是一颗平衡二叉搜索树,那么时间复杂度就是 $O(logN)$。而数组的添加和删除的时间复杂度为 $O(N)$,其中 N 为数组长度。 @@ -786,15 +786,15 @@ class Solution { 如下就是一颗完全二叉树: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqbvfgqj7j307g042wei.jpg) +![](https://p.ipic.vip/6gxl9n.jpg) 直接考察完全二叉树的题目虽然不多,貌似只有一道 [222. 完全二叉树的节点个数](https://leetcode-cn.com/problems/count-complete-tree-nodes/)(二分可解),但是理解完全二叉树对你做题其实帮助很大。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkq92qmj8yj313g0p0mz4.jpg) +![](https://p.ipic.vip/giot6z.jpg) 如上图,是一颗普通的二叉树。如果我将其中的空节点补充完全,那么它就是一颗完全二叉树了。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkq93usnp2j316m0p40vh.jpg) +![](https://p.ipic.vip/w7hk68.jpg) 这有什么用呢?这很有用!我总结了两个用处: @@ -903,7 +903,7 @@ class Codec: 如果我用一个箭头表示节点的父子关系,箭头指向节点的两个子节点,那么大概是这样的: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqb8mcsv7j31z60sggrm.jpg) +![](https://p.ipic.vip/nvzvze.jpg) 我们刚才提到了: @@ -942,13 +942,13 @@ class Codec: 但是上面的代码是不对的,因为我们序列化的时候其实不是完全二叉树,这也是上面我埋下的伏笔。因此遇到类似这样的 case 就会挂: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqcfujvv4j315s0u078j.jpg) +![](https://p.ipic.vip/xdhqsd.jpg) 这也是我前面说”上面代码的序列化并不是一颗完全二叉树“的原因。 其实这个很好解决, 核心还是上面我画的那种图: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqb8mcsv7j31z60sggrm.jpg) +![](https://p.ipic.vip/nvzvze.jpg) 其实我们可以: @@ -995,11 +995,11 @@ def deserialize(self, data): 首先是官网给的两个例子: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqsytwibqj30kh07pgm8.jpg) +![](https://p.ipic.vip/dto1q5.jpg) 接着是我自己画的一个例子: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkqsz5bhtqj30hu0cd3zk.jpg) +![](https://p.ipic.vip/7ihqmk.jpg) 如图红色的部分是最大路径上的节点。 @@ -1074,11 +1074,11 @@ def dfs(node): 第二个原因是:这样写相当于把 root 当成是 current 指针来用了。最开始 current 指针指向 root,然后不断修改指向树的其它节点。这样就概念就简化了,只有一个当前指针的概念。如果使用 node,就是当前指针 + root 指针两个概念了。 -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkurtwpr6lj30bl0aowey.jpg) +![](https://p.ipic.vip/qesbgr.jpg) (一开始 current 就是 root) -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkurvb2pwbj30ap0b8aaj.jpg) +![](https://p.ipic.vip/skhbmx.jpg) (后面 current 不断改变。具体如何改变,取决于你的搜索算法,是 dfs 还是 bfs 等) @@ -1169,7 +1169,7 @@ def dfs_main(root): 右图为返回的答案。 ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkuovucp7nj30z809v74w.jpg) +![](https://p.ipic.vip/skicf9.jpg) ``` @@ -1179,7 +1179,7 @@ def dfs_main(root): 输出: [1,null,1,null,1] ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkuovzkq1bj316t09v3ze.jpg) +![](https://p.ipic.vip/otw4cl.jpg) ``` 示例3: @@ -1187,7 +1187,7 @@ def dfs_main(root): 输出: [1,1,0,1,1,null,1] ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkuowgc9oaj319w0ccjsm.jpg) +![](https://p.ipic.vip/mgbg5z.jpg) ``` 说明: @@ -1262,7 +1262,7 @@ var pruneTree = function (root) { 示例 1: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkup76wi89j30s706b0t7.jpg) +![](https://p.ipic.vip/ct6qbq.jpg) ``` @@ -1275,7 +1275,7 @@ var pruneTree = function (root) { 示例 2: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkup80cyszj30gc06bmxd.jpg) +![](https://p.ipic.vip/6c2ahn.jpg) ``` @@ -1285,7 +1285,7 @@ var pruneTree = function (root) { 示例 3: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkup89sd5vj30k406o3yr.jpg) +![](https://p.ipic.vip/9p1dgx.jpg) ``` @@ -1385,7 +1385,7 @@ def dfs(root): 一张图总结一下: -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkye36obl1j310k0pe0wg.jpg) +![](https://p.ipic.vip/vr7kd9.jpg) 经过这样的处理,后面的代码基本都不需要判空了。 @@ -1584,7 +1584,7 @@ dfs 返回数组比较少见。即使题目要求返回数组,我们也通常 示例 1: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkuq5u4zclj308x08xq33.jpg) +![](https://p.ipic.vip/pjheed.jpg) ```   @@ -1597,7 +1597,7 @@ dfs 返回数组比较少见。即使题目要求返回数组,我们也通常 示例 2: ``` -![](https://tva1.sinaimg.cn/large/0081Kckwly1gkuq63va2gj30c908xjrr.jpg) +![](https://p.ipic.vip/ds1khy.jpg) ``` @@ -1694,6 +1694,6 @@ class Solution: 我整理的 1000 多页的电子书已经开发下载了,大家可以去我的公众号《力扣加加》后台回复电子书获取。 -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928846461-image.png) +![](https://p.ipic.vip/y4jc3t.png) -![](https://cdn.jsdelivr.net/gh/azl397985856/cdn/2020-10-17/1602928862442-image.png) +![](https://p.ipic.vip/sco829.png) diff --git a/thinkings/trie.en.md b/thinkings/trie.en.md index 64593b643..988b4ac77 100644 --- a/thinkings/trie.en.md +++ b/thinkings/trie.en.md @@ -13,7 +13,7 @@ The main interface of a trie should include the following: Among all of the above, `startWith` is one of the most essential methods, which leads to the naming for 'Prefix Tree'. You can start with [208.implement-trie-prefix-tree](https://leetcode.com/problems/implement-trie-prefix-tree) to get yourself familiar with this data structure, and then try to solve other problems. Here's the graph illustration of a trie: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlug6ei8jj30lg0h0wfg.jpg) +![](https://p.ipic.vip/0vkcix.jpg) As the graph shows, each node of the trie would store a character and a boolean `isWord`, which suggests whether the node is the end of a word. There might be some slight differences in the actual implementation, but they are essentially the same. diff --git a/thinkings/trie.md b/thinkings/trie.md index f496c96bb..5c1b06846 100644 --- a/thinkings/trie.md +++ b/thinkings/trie.md @@ -18,7 +18,7 @@ 我们可以将 words 存储到一个树上,这棵树叫做前缀树。 一个前缀树大概是这个样子: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlug87vyfj30mz0gq406.jpg) +![](https://p.ipic.vip/l22fyo.jpg) 如图每一个节点存储一个字符,然后外加一个控制信息表示是否是单词结尾,实际使用过程可能会有细微差别,不过变化不大。 @@ -67,11 +67,11 @@ f(count = 1, preCount=1) 构建 Trie 的核心就是插入。而插入指的就是将单词(words)全部依次插入到前缀树中。假定给出几个单词 words [she,he,her,good,god]构造出一个 Trie 如下图: -![](https://tva1.sinaimg.cn/large/008i3skNly1gsf3s7q1blj30gb0abjs0.jpg) +![](https://p.ipic.vip/znbzcd.jpg) 也就是说从根结点出发到某一粉色节点所经过的字符组成的单词,在单词列表中出现过,当然我们也可以给树的每个节点加个 count 属性,代表根结点到该节点所构成的字符串前缀出现的次数 -![](https://tva1.sinaimg.cn/large/008i3skNly1gsf3shfx9nj30g709nq3k.jpg) +![](https://p.ipic.vip/qelwml.jpg) 可以看出树的构造非常简单:**插入新单词的时候就从根结点出发一个字符一个字符插入,有对应的字符节点就更新对应的属性,没有就创建一个!** diff --git a/thinkings/union-find.en.md b/thinkings/union-find.en.md index fec56f4a8..0443ac492 100644 --- a/thinkings/union-find.en.md +++ b/thinkings/union-find.en.md @@ -4,7 +4,7 @@ I believe everyone has played the following maze game. Your goal is to move from a certain corner of the map to the exit of the map. The rules are simple, as long as you can't pass through the wall. -![](https://tva1.sinaimg.cn/large/008eGmZEly1goxczig610j30as0ar48s.jpg) +![](https://p.ipic.vip/r4ihyb.jpg) In fact, this problem cannot be solved by using parallel collections. However, if I change the rule to, “Is there a path from the entrance to the exit”, then this is a simple unicom question, so that it can be done with the help of the parallel check set to be discussed in this section. @@ -26,7 +26,7 @@ For example, there are two commanders. There are a number of commanders under th How do we judge whether two division commanders belong to the same commander (connectivity)? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufxh5lhj30gs0bzwet.jpg) +![](https://p.ipic.vip/p4t2ub.jpg) Very simple, we followed the division commander, looked up, and found the commander. If the two division commanders find the same commander, then the two people will be in charge of the same commander. (Assuming that these two are lower in rank than the commander) @@ -42,11 +42,11 @@ This operation of constantly looking up is generally called find. Using ta, we c As shown in the picture, there are two commanders: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufys950j30wp0el0th.jpg) +![](https://p.ipic.vip/5hao10.jpg) We merge it into a unicom domain, and the easiest way is to directly point one of the domains to the other.: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlug0ni3jj30ym0cojsb.jpg) +![](https://p.ipic.vip/usvfn4.jpg) The above is a visual explanation of the three core APIs "find", "connected" and "union". Let's take a look at the code implementation. @@ -60,7 +60,7 @@ The union-find Algorithm defines two operations for this data structure: First, we initialize that each point is a connected domain, similar to the figure below: -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4f8vpp3j30p9024jra.jpg) +![](https://p.ipic.vip/vbnydv.jpg) In order to define these methods more accurately, it is necessary to define how to represent a collection. A common strategy is to select a fixed element for each collection, called a representative, to represent the entire collection. Next, Find(x) returns the representative of the collection to which x belongs, and Union uses the representative of the two collections as a parameter to merge. At the beginning, everyone's representative was himself. @@ -113,11 +113,11 @@ What's the use of this? We know that every time we find, we will continue to sea > Note that it is approaching O(1), to be precise, it is an inverse function of Ackerman's function. -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4i1vrclg30ni05wtj9.gif) +![](https://p.ipic.vip/gvnmod.gif) In the extreme case, every path will be compressed. In this case, the time complexity of continuing to find is $O(1)$. -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4zjf5evj30u00aigml.jpg) +![](https://p.ipic.vip/0y7hub.jpg) ### connected @@ -134,13 +134,13 @@ Hang one of the nodes to the ancestor of the other node, so that the ancestors o For the following figure: -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4avz4iej30lv04rmx9.jpg) +![](https://p.ipic.vip/8u6mqx.jpg) If we merge 0 and 7 once. That is, `union(0, 7)`, the following process will occur. -Find the root node of 0 3 -Found the root node of 7 6 -Point 6 to 3. (In order to make the merged tree as balanced as possible, generally choose to mount a small tree on top of a large tree. The following code template will reflect this. The rank of 3 is larger than that of 6, which is more conducive to the balance of the tree and avoids extreme situations) -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4btv06yg30ni05wwze.gif) +![](https://p.ipic.vip/p8ng7e.gif) The small trees and big trees mentioned above are the so-called ** merged by rank**. diff --git a/thinkings/union-find.md b/thinkings/union-find.md index 418c8bbd4..30c652d47 100644 --- a/thinkings/union-find.md +++ b/thinkings/union-find.md @@ -4,7 +4,7 @@ 相信大家都玩过下面的迷宫游戏。你的目标是从地图的某一个角落移动到地图的出口。规则很简单,仅仅你不能穿过墙。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1goxczig610j30as0ar48s.jpg) +![](https://p.ipic.vip/dg1jyf.jpg) 实际上,这道题并不能够使用并查集来解决。 不过如果我将规则变成,“是否存在一条从入口到出口的路径”,那么这就是一个简单的联通问题,这样就可以借助本节要讲的并查集来完成。 @@ -26,7 +26,7 @@ 我们如何判断某两个师长是否归同一个司令管呢(连通性)? -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufxh5lhj30gs0bzwet.jpg) +![](https://p.ipic.vip/nvj6x2.jpg) 很简单,我们顺着师长,往上找,找到司令。 如果两个师长找到的是同一个司令,那么两个人就归同一个司令管。(假设这两人级别比司令低) @@ -42,11 +42,11 @@ 如图有两个司令: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlufys950j30wp0el0th.jpg) +![](https://p.ipic.vip/7b6a0l.jpg) 我们将其合并为一个联通域,最简单的方式就是直接将其中一个司令指向另外一个即可: -![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlug0ni3jj30ym0cojsb.jpg) +![](https://p.ipic.vip/m1mgqv.jpg) 以上就是三个核心 API `find`,`connnected` 和 `union`, 的形象化解释,下面我们来看下代码实现。 @@ -60,7 +60,7 @@ 首先我们初始化每一个点都是一个连通域,类似下图: -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4f8vpp3j30p9024jra.jpg) +![](https://p.ipic.vip/knr558.jpg) 为了更加精确的定义这些方法,需要定义如何表示集合。一种常用的策略是为每个集合选定一个固定的元素,称为代表,以表示整个集合。接着,Find(x) 返回 x 所属集合的代表,而 Union 使用两个集合的代表作为参数进行合并。初始时,每个人的代表都是自己本身。 @@ -113,11 +113,11 @@ def find(self, x): > 注意是趋近 O(1),准确来说是阿克曼函数的某个反函数。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4i1vrclg30ni05wtj9.gif) +![](https://p.ipic.vip/xknazz.gif) 极限情况下,每一个路径都会被压缩,这种情况下**继续**查找的时间复杂度就是 $O(1)$。 -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4zjf5evj30u00aigml.jpg) +![](https://p.ipic.vip/bl6gt4.jpg) ### connected @@ -134,7 +134,7 @@ def connected(self, p, q): 对于如下的一个图: -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4avz4iej30lv04rmx9.jpg) +![](https://p.ipic.vip/grnq9g.jpg) 如果我们将 0 和 7 进行一次合并。即 `union(0, 7)` ,则会发生如下过程。 @@ -142,7 +142,7 @@ def connected(self, p, q): - 找到 7 的根节点 6 - 将 6 指向 3。(为了使得合并之后的树尽可能平衡,一般选择将小树挂载到大树上面,下面的代码模板会体现这一点。3 的秩比 6 的秩大,这样更利于树的平衡,避免出现极端的情况) -![](https://tva1.sinaimg.cn/large/008eGmZEly1gmm4btv06yg30ni05wwze.gif) +![](https://p.ipic.vip/64k05c.gif) 上面讲的小树挂大树就是所谓的**按秩合并**。