README ¶ LeetCode刷题整理 算法 Algorithm 总结自己学习过的数据结构和算法,以及按照题目类型分类。 根据灵神的说法: 一个算法模板应当涵盖以下几点: 对该算法的基本介绍(核心思想、复杂度等) 参考链接或书籍章节(讲的比较好的资料) 模板代码(可以包含一些注释、使用说明) 模板补充内容(常见题型中的额外代码、建模技巧等) 相关题目链接(模板题、经典题、思维转换题等) 那这个仓库的目标是:用自己的代码风格,实现常见的数据结构和算法模板。 实现过程: 通过「默写」代码,了解模板中的细节,增加套用模板的熟练度,避免bug。 通过「阅读」他人优秀的代码,提高自身代码的健壮性和可读性。 通过「死记」解题代码,串联其他类似的题目 数据结构 集合 set.go 堆 最小堆/最大堆/优先队列 heap.go 线段树 线段树 segment_tree.go 延迟标记线段树 lazy_segment_tree.go 树 红黑树 rbtree.go 排序 sort.go 二分查找 归并排序 字符串 KMP 字符串匹配算法 string.go 动态规划 记忆化搜索 memory_search.go 实现了类似Python的cache装饰器 背包 DP knapsack.go 0-1 背包 完全背包 分组背包 线性 DP LCS 最长公共子序列 lcs.go LIS 最长递增子序列 lis.go 图 最短路径 path_shortest.go Floyd (任意图上多源最短路径算法) Dijkstra (非负权图上单源最短路径算法) 代码生成 为了方便在IDE中解题,编写了脚本用于生成LeetCode的代码模板。 生成每日一题的代码模板 根据 「题号」 或 「题名」problem slug 生成代码模板 LCR等前缀题目,仅支持通过题名生成模板 支持本地运行单元测试和自定义测试用例。 查看题解的原始 markdown 交互式查看题解 自定义代码模板 根据「配置文件」 自定义生成的文件名 根据「模板文件」 自定义文件内容 使用 text/template 解析模板文件,用于渲染的数据为:Data 结构体 Expand ▾ Collapse ▴ Directories ¶ Show internal Expand all Path Synopsis cmd leetcode command container segtree tree/rbtree copypasta dp graph internal api server tmpl problem 1000_minimum-cost-to-merge-stones command 1004_max-consecutive-ones-iii command 100_same-tree command 1011_capacity-to-ship-packages-within-d-days command 1019_next-greater-node-in-linked-list command 101_symmetric-tree command 1026_maximum-difference-between-node-and-ancestor command 102_binary-tree-level-order-traversal command 1038_binary-search-tree-to-greater-sum-tree command 1039_minimum-score-triangulation-of-polygon command 103_binary-tree-zigzag-level-order-traversal command 1049_last-stone-weight-ii command 104_maximum-depth-of-binary-tree command 105_construct-binary-tree-from-preorder-and-inorder-traversal command 106_construct-binary-tree-from-inorder-and-postorder-traversal command 107_binary-tree-level-order-traversal-ii command 1080_insufficient-nodes-in-root-to-leaf-paths command 1094_car-pooling command 10_regular-expression-matching command 1109_corporate-flight-bookings command 110_balanced-binary-tree command 111_minimum-depth-of-binary-tree command 1123_lowest-common-ancestor-of-deepest-leaves command 1124_longest-well-performing-interval command 112_path-sum command 1143_longest-common-subsequence command 1155_number-of-dice-rolls-with-target-sum command 1156_swap-for-longest-repeated-character-substring command 117_populating-next-right-pointers-in-each-node-ii command 1185_day-of-the-week command 118_pascals-triangle command 11_container-with-most-water command 1208_get-equal-substrings-within-budget command 1222_queens-that-can-attack-the-king command 122_best-time-to-buy-and-sell-stock-ii command 1234_replace-the-substring-for-balanced-string command 123_best-time-to-buy-and-sell-stock-iii command 124_binary-tree-maximum-path-sum command 125_valid-palindrome command 1267_count-servers-that-communicate command 1276_number-of-burgers-with-no-waste-of-ingredients command 1277_count-square-submatrices-with-all-ones command 128_longest-consecutive-sequence command 12_integer-to-roman command 1312_minimum-insertion-steps-to-make-a-string-palindrome command 131_palindrome-partitioning command 132_palindrome-partitioning-ii command 1334_find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance command 1349_maximum-students-taking-exam command 136_single-number command 1377_frog-position-after-t-seconds command 137_single-number-ii command 139_word-break command 13_roman-to-integer command 1402_reducing-dishes command 1410_html-entity-parser command 141_linked-list-cycle command 1423_maximum-points-you-can-obtain-from-cards command 142_linked-list-cycle-ii command 1438_longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit command 143_reorder-list command 1457_pseudo-palindromic-paths-in-a-binary-tree command 1458_max-dot-product-of-two-subsequences command 1465_maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts command 1466_reorder-routes-to-make-all-paths-lead-to-the-city-zero command 146_lru-cache command 1475_final-prices-with-a-special-discount-in-a-shop command 1488_avoid-flood-in-the-city command 148_sort-list command 1493_longest-subarray-of-1s-after-deleting-one-element command 1499_max-value-of-equation command 14_longest-common-prefix command 1503_last-moment-before-all-ants-fall-out-of-a-plank command 153_find-minimum-in-rotated-sorted-array command 1547_minimum-cost-to-cut-a-stick command 154_find-minimum-in-rotated-sorted-array-ii command 155_min-stack command 15_3sum command 1626_best-team-with-no-conflicts command 162_find-peak-element command 1631_path-with-minimum-effort command 1657_determine-if-two-strings-are-close command 1658_minimum-operations-to-reduce-x-to-zero command 1670_design-front-middle-back-queue command 1671_minimum-number-of-removals-to-make-mountain-array command 167_two-sum-ii-input-array-is-sorted command 1686_stone-game-vi command 1690_stone-game-vii command 1696_jump-game-vi command 169_majority-element command 16_3sum-closest command 1726_tuple-with-same-product command 1760_minimum-limit-of-balls-in-a-bag command 1771_maximize-palindrome-length-from-subsequences command 1781_sum-of-beauty-of-all-substrings command 179_largest-number command 17_letter-combinations-of-a-phone-number command 1832_check-if-the-sentence-is-pangram command 187_repeated-dna-sequences command 188_best-time-to-buy-and-sell-stock-iv command 18_4sum command 1911_maximum-alternating-subsequence-sum command 1921_eliminate-maximum-number-of-monsters command 1944_number-of-visible-people-in-a-queue command 1954_minimum-garden-perimeter-to-collect-enough-apples command 1962_remove-stones-to-minimize-the-total command 1964_find-the-longest-valid-obstacle-course-at-each-position command 1969_minimum-non-zero-product-of-the-array-elements command 1976_number-of-ways-to-arrive-at-destination command 198_house-robber command 1993_operations-on-tree command 199_binary-tree-right-side-view command 19_remove-nth-node-from-end-of-list command 1_two-sum command 2003_smallest-missing-genetic-value-in-each-subtree command 2008_maximum-earnings-from-taxi command 2034_stock-price-fluctuation command 203_remove-linked-list-elements command 2048_next-greater-numerically-balanced-number command 204_count-primes command 206_reverse-linked-list command 207_course-schedule command 208_implement-trie-prefix-tree command 209_minimum-size-subarray-sum command 20_valid-parentheses command 2103_rings-and-rods command 210_course-schedule-ii command 2132_stamping-the-grid command 2136_earliest-possible-day-of-full-bloom command 213_house-robber-ii command 215_kth-largest-element-in-an-array command 216_combination-sum-iii command 2171_removing-minimum-number-of-magic-beans command 217_contains-duplicate command 2182_construct-string-with-repeat-limit command 219_contains-duplicate-ii command 21_merge-two-sorted-lists command 220_contains-duplicate-iii command 2216_minimum-deletions-to-make-array-beautiful command 221_maximal-square command 222_count-complete-tree-nodes command 2240_number-of-ways-to-buy-pens-and-pencils command 2246_longest-path-with-different-adjacent-characters command 2251_number-of-flowers-in-full-bloom command 225_implement-stack-using-queues command 226_invert-binary-tree command 2276_count-integers-in-intervals command 22_generate-parentheses command 2300_successful-pairs-of-spells-and-potions command 2304_minimum-path-cost-in-a-grid command 2312_selling-pieces-of-wood command 2316_count-unreachable-pairs-of-nodes-in-an-undirected-graph command 232_implement-queue-using-stacks command 2336_smallest-number-in-infinite-set command 233_number-of-digit-one command 2342_max-sum-of-a-pair-with-equal-sum-of-digits command 235_lowest-common-ancestor-of-a-binary-search-tree command 2368_reachable-nodes-with-restrictions command 2369_check-if-there-is-a-valid-partition-for-the-array command 236_lowest-common-ancestor-of-a-binary-tree command 2376_count-special-integers command 237_delete-node-in-a-linked-list command 2381_shifting-letters-ii command 238_product-of-array-except-self command 2397_maximum-rows-covered-by-columns command 2398_maximum-number-of-robots-within-budget command 239_sliding-window-maximum command 23_merge-k-sorted-lists command 2406_divide-intervals-into-minimum-number-of-groups command 2415_reverse-odd-levels-of-binary-tree command 2426_number-of-pairs-satisfying-inequality command 2432_the-employee-that-worked-on-the-longest-task command 2454_next-greater-element-iv command 2466_count-ways-to-build-good-strings command 2476_closest-nodes-queries-in-a-binary-search-tree command 2477_minimum-fuel-cost-to-report-to-the-capital command 2487_remove-nodes-from-linked-list command 24_swap-nodes-in-pairs command 2511_maximum-enemy-forts-that-can-be-captured command 2512_reward-top-k-students command 2516_take-k-of-each-character-from-left-and-right command 2520_count-the-digits-that-divide-a-number command 2525_categorize-box-according-to-criteria command 2528_maximize-the-minimum-powered-city command 2530_maximal-score-after-applying-k-operations command 2537_count-the-number-of-good-subarrays command 2538_difference-between-maximum-and-minimum-price-sum command 2547_minimum-cost-to-split-an-array command 2552_count-increasing-quadruplets command 2555_maximize-win-from-two-segments command 2558_take-gifts-from-the-richest-pile command 2560_house-robber-iv command 2562_find-the-array-concatenation-value command 2563_count-the-number-of-fair-pairs command 2565_subsequence-with-the-minimum-score command 2575_find-the-divisibility-array-of-a-string command 2576_find-the-maximum-number-of-marked-indices command 2578_split-with-minimum-sum command 2581_count-number-of-possible-root-nodes command 2583_kth-largest-sum-in-a-binary-tree command 2585_number-of-ways-to-earn-points command 2586_count-the-number-of-vowel-strings-in-range command 258_add-digits command 2591_distribute-money-to-maximum-children command 2594_minimum-time-to-repair-cars command 2596_check-knight-tour-configuration command 2597_the-number-of-beautiful-subsets command 259_3sum-smaller command 25_reverse-nodes-in-k-group command 2600_k-items-with-the-maximum-sum command 2602_minimum-operations-to-make-all-array-elements-equal command 2605_form-smallest-number-from-two-digit-arrays command 2606_find-the-substring-with-maximum-cost command 2609_find-the-longest-balanced-substring-of-a-binary-string command 260_single-number-iii command 2616_minimize-the-maximum-difference-of-pairs command 2617_minimum-number-of-visited-cells-in-a-grid command 2641_cousins-in-binary-tree-ii command 2645_minimum-additions-to-make-valid-string command 2646_minimize-the-total-price-of-the-trips command 2652_sum-multiples command 2653_sliding-subarray-beauty command 2656_maximum-sum-with-exactly-k-elements command 2660_determine-the-winner-of-a-bowling-game command 2661_first-completely-painted-row-or-column command 2670_find-the-distinct-difference-array command 2673_make-costs-of-paths-equal-in-a-binary-tree command 2681_power-of-heroes command 2684_maximum-number-of-moves-in-a-grid command 2696_minimum-string-length-after-removing-substrings command 2697_lexicographically-smallest-palindrome command 2698_find-the-punishment-number-of-an-integer command 26_remove-duplicates-from-sorted-array command 2706_buy-two-chocolates command 2707_extra-characters-in-a-string command 2718_sum-of-matrix-after-queries command 2719_count-of-integers command 2730_find-the-longest-semi-repetitive-substring command 2731_movement-of-robots command 2735_collecting-chocolates command 2736_maximum-sum-queries command 2742_painting-the-walls command 2744_find-maximum-number-of-string-pairs command 274_h-index command 275_h-index-ii command 2760_longest-even-odd-subarray-with-threshold command 2761_prime-pairs-with-target-sum command 2762_continuous-subarrays command 2765_longest-alternating-subarray command 2767_partition-string-into-minimum-beautiful-substrings command 2770_maximum-number-of-jumps-to-reach-the-last-index command 2771_longest-non-decreasing-subarray-from-two-arrays command 2772_apply-operations-to-make-all-array-elements-equal-to-zero command 2779_maximum-beauty-of-an-array-after-applying-operation command 2786_visit-array-positions-to-maximize-score command 2787_ways-to-express-an-integer-as-sum-of-powers command 2788_split-strings-by-separator command 2799_count-complete-subarrays-in-an-array command 279_perfect-squares command 27_remove-element command 2807_insert-greatest-common-divisors-in-linked-list command 2808_minimum-seconds-to-equalize-a-circular-array command 2824_count-pairs-whose-sum-is-less-than-target command 2825_make-string-a-subsequence-using-cyclic-increments command 2826_sorting-three-groups command 2827_number-of-beautiful-integers-in-the-range command 2828_check-if-a-string-is-an-acronym-of-words command 2830_maximize-the-profit-as-the-salesman command 2831_find-the-longest-equal-subarray command 2834_find-the-minimum-possible-sum-of-a-beautiful-array command 283_move-zeroes command 2841_maximum-sum-of-almost-unique-subarray command 2845_count-of-interesting-subarrays command 2861_maximum-number-of-alloys command 2864_maximum-odd-binary-number command 2865_beautiful-towers-i command 2866_beautiful-towers-ii command 2867_count-valid-paths-in-a-tree command 2874_maximum-value-of-an-ordered-triplet-ii command 2875_minimum-size-subarray-in-infinite-array command 2896_apply-operations-to-make-two-strings-equal command 2901_longest-unequal-adjacent-groups-subsequence-ii command 2905_find-indices-with-index-and-value-difference-ii command 2906_construct-product-matrix command 2909_minimum-sum-of-mountain-triplets-ii command 290_word-pattern command 2915_length-of-the-longest-subsequence-that-sums-to-target command 2919_minimum-increment-operations-to-make-array-beautiful command 2925_maximum-score-after-applying-operations-on-a-tree command 2929_distribute-candies-among-children-ii command 292_nim-game command 2943_maximize-area-of-square-hole-in-grid command 2944_minimum-number-of-coins-for-fruits command 2948_make-lexicographically-smallest-array-by-swapping-elements command 2949_count-beautiful-substrings-ii command 2953_count-complete-substrings command 2958_length-of-longest-subarray-with-at-most-k-frequency command 2962_count-subarrays-where-max-element-appears-at-least-k-times command 2976_minimum-cost-to-convert-string-i command 2999_count-the-number-of-powerful-integers command 2_add-two-numbers command 3007_maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k command 300_longest-increasing-subsequence command 3015_count-the-number-of-houses-at-a-certain-distance-i command 301_remove-invalid-parentheses command 3020_find-the-maximum-number-of-elements-in-subset command 303_range-sum-query-immutable command 306_additive-number command 307_range-sum-query-mutable command 309_best-time-to-buy-and-sell-stock-with-cooldown command 315_count-of-smaller-numbers-after-self command 318_maximum-product-of-word-lengths command 322_coin-change command 327_count-of-range-sum command 332_reconstruct-itinerary command 337_house-robber-iii command 33_search-in-rotated-sorted-array command 342_power-of-four command 343_integer-break command 344_reverse-string command 34_find-first-and-last-position-of-element-in-sorted-array command 354_russian-doll-envelopes command 35_search-insert-position command 365_water-and-jug-problem command 367_valid-perfect-square command 36_valid-sudoku command 375_guess-number-higher-or-lower-ii command 376_wiggle-subsequence command 377_combination-sum-iv command 37_sudoku-solver command 382_linked-list-random-node command 383_ransom-note command 387_first-unique-character-in-a-string command 38_count-and-say command 394_decode-string command 39_combination-sum command 3_longest-substring-without-repeating-characters command 402_remove-k-digits command 409_longest-palindrome command 40_combination-sum-ii command 410_split-array-largest-sum command 415_add-strings command 416_partition-equal-subset-sum command 41_first-missing-positive command 421_maximum-xor-of-two-numbers-in-an-array command 424_longest-repeating-character-replacement command 429_n-ary-tree-level-order-traversal command 42_trapping-rain-water command 435_non-overlapping-intervals command 438_find-all-anagrams-in-a-string command 445_add-two-numbers-ii command 447_number-of-boomerangs command 449_serialize-and-deserialize-bst command 454_4sum-ii command 456_132-pattern command 46_permutations command 474_ones-and-zeroes command 47_permutations-ii command 48_rotate-image command 491_non-decreasing-subsequences command 493_reverse-pairs command 494_target-sum command 496_next-greater-element-i command 4_median-of-two-sorted-arrays command 500_keyboard-row command 503_next-greater-element-ii command 509_fibonacci-number command 50_powx-n command 513_find-bottom-left-tree-value command 514_freedom-trail command 516_longest-palindromic-subsequence command 518_coin-change-ii command 51_n-queens command 523_continuous-subarray-sum command 525_contiguous-array command 52_n-queens-ii command 539_minimum-time-difference command 53_maximum-subarray command 543_diameter-of-binary-tree command 547_number-of-provinces command 560_subarray-sum-equals-k command 567_permutation-in-string command 56_merge-intervals command 583_delete-operation-for-two-strings command 589_n-ary-tree-preorder-traversal command 58_length-of-last-word command 5_longest-palindromic-substring command 611_valid-triangle-number command 62_unique-paths command 630_course-schedule-iii command 63_unique-paths-ii command 649_dota2-senate command 64_minimum-path-sum command 66_plus-one command 670_maximum-swap command 673_number-of-longest-increasing-subsequence command 67_add-binary command 687_longest-univalue-path command 689_maximum-sum-of-3-non-overlapping-subarrays command 69_sqrtx command 6_zigzag-conversion command 704_binary-search command 70_climbing-stairs command 712_minimum-ascii-delete-sum-for-two-strings command 713_subarray-product-less-than-k command 714_best-time-to-buy-and-sell-stock-with-transaction-fee command 715_range-module command 71_simplify-path command 72_edit-distance command 739_daily-temperatures command 73_set-matrix-zeroes command 745_prefix-and-suffix-search command 746_min-cost-climbing-stairs command 75_sort-colors command 765_couples-holding-hands command 76_minimum-window-substring command 77_combinations command 784_letter-case-permutation command 78_subsets command 79_word-search command 7_reverse-integer command 828_count-unique-characters-of-all-substrings-of-a-given-string command 82_remove-duplicates-from-sorted-list-ii command 834_sum-of-distances-in-tree command 837_new-21-game command 83_remove-duplicates-from-sorted-list command 842_split-array-into-fibonacci-sequence command 84_largest-rectangle-in-histogram command 861_score-after-flipping-matrix command 862_shortest-subarray-with-sum-at-least-k command 876_middle-of-the-linked-list command 889_construct-binary-tree-from-preorder-and-postorder-traversal command 88_merge-sorted-array command 891_sum-of-subsequence-widths command 8_string-to-integer-atoi command 901_online-stock-span command 902_numbers-at-most-n-given-digit-set command 904_fruit-into-baskets command 907_sum-of-subarray-minimums command 90_subsets-ii command 92_reverse-linked-list-ii command 938_range-sum-of-bst command 93_restore-ip-addresses command 94_binary-tree-inorder-traversal command 968_binary-tree-cameras command 96_unique-binary-search-trees command 974_subarray-sums-divisible-by-k command 977_squares-of-a-sorted-array command 97_interleaving-string command 987_vertical-order-traversal-of-a-binary-tree command 98_validate-binary-search-tree command 993_cousins-in-binary-tree command 9_palindrome-number command Algorithms_give-gem command Algorithms_magic-tower command Algorithms_min-count command Algorithms_reverse-pairs command LCCI_find-longest-subarray command codeforces/1689/c command luogu/p1352 command luogu/p2458 command tests Click to show internal directories. Click to hide internal directories.