From 6dcb4ea9b73e511033bd6fcf17aaf88a7a3e3477 Mon Sep 17 00:00:00 2001 From: aQua Date: Sun, 20 Aug 2017 22:31:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0helper=E5=8A=A9=E6=89=8B?= =?UTF-8?q?=E5=92=8CREADME.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Algorithms/0044.wildcard-matching/README.md | 20 ++++++ Algorithms/0054.spiral-matrix/README.md | 17 +++++ Algorithms/0055.jump-game/README.md | 18 +++++ Algorithms/0056.merge-intervals/README.md | 8 +++ Algorithms/0057.insert-interval/README.md | 18 +++++ Algorithms/0058.length-of-last-word/README.md | 12 ++++ Algorithms/0059.spiral-matrix-ii/README.md | 14 ++++ .../0060.permutation-sequence/README.md | 18 +++++ Algorithms/0061.rotate-list/README.md | 6 ++ Algorithms/0062.unique-paths/README.md | 13 ++++ Algorithms/0063.unique-paths-ii/README.md | 18 +++++ Algorithms/0064.minimum-path-sum/README.md | 4 ++ Algorithms/0065.valid-number/README.md | 19 +++++ Algorithms/0067.add-binary/README.md | 11 +++ Algorithms/0068.text-justification/README.md | 47 +++++++++++++ Algorithms/0069.sqrtx/README.md | 4 ++ Algorithms/0070.climbing-stairs/README.md | 8 +++ Algorithms/0071.simplify-path/README.md | 20 ++++++ Algorithms/0072.edit-distance/README.md | 14 ++++ Algorithms/0073.set-matrix-zeroes/README.md | 16 +++++ Algorithms/0074.search-a-2d-matrix/README.md | 23 +++++++ Algorithms/0075.sort-colors/README.md | 23 +++++++ .../0076.minimum-window-substring/README.md | 21 ++++++ Algorithms/0077.combinations/README.md | 18 +++++ Algorithms/0078.subsets/README.md | 22 ++++++ Algorithms/0079.word-search/README.md | 22 ++++++ .../README.md | 12 ++++ .../README.md | 15 ++++ .../README.md | 9 +++ .../README.md | 9 +++ .../README.md | 20 ++++++ Algorithms/0085.maximal-rectangle/README.md | 13 ++++ Algorithms/0086.partition-list/README.md | 11 +++ Algorithms/0087.scramble-string/README.md | 49 +++++++++++++ Algorithms/0089.gray-code/README.md | 18 +++++ Algorithms/0090.subsets-ii/README.md | 20 ++++++ Algorithms/0091.decode-ways/README.md | 24 +++++++ .../0092.reverse-linked-list-ii/README.md | 17 +++++ .../0093.restore-ip-addresses/README.md | 10 +++ .../README.md | 18 +++++ .../README.md | 14 ++++ .../0096.unique-binary-search-trees/README.md | 14 ++++ Algorithms/0097.interleaving-string/README.md | 15 ++++ .../README.md | 29 ++++++++ .../0099.recover-binary-search-tree/README.md | 10 +++ Algorithms/0100.same-tree/README.md | 7 ++ Algorithms/0101.symmetric-tree/README.md | 26 +++++++ .../README.md | 22 ++++++ .../README.md | 22 ++++++ .../README.md | 4 ++ .../README.md | 6 ++ .../README.md | 6 ++ .../README.md | 22 ++++++ .../README.md | 2 + .../README.md | 2 + .../0110.balanced-binary-tree/README.md | 7 ++ .../README.md | 4 ++ Algorithms/0112.path-sum/README.md | 18 +++++ Algorithms/0113.path-sum-ii/README.md | 24 +++++++ .../README.md | 35 ++++++++++ .../0115.distinct-subsequences/README.md | 16 +++++ .../README.md | 42 +++++++++++ .../README.md | 26 +++++++ Algorithms/0119.pascals-triangle-ii/README.md | 12 ++++ Algorithms/0120.triangle/README.md | 21 ++++++ .../README.md | 20 ++++++ .../README.md | 4 ++ .../README.md | 7 ++ .../README.md | 18 +++++ Algorithms/0125.valid-palindrome/README.md | 17 +++++ Algorithms/0126.word-ladder-ii/README.md | 42 +++++++++++ Algorithms/0127.word-ladder/README.md | 38 ++++++++++ .../README.md | 12 ++++ .../0129.sum-root-to-leaf-numbers/README.md | 19 +++++ Algorithms/0130.surrounded-regions/README.md | 24 +++++++ .../0131.palindrome-partitioning/README.md | 17 +++++ .../0132.palindrome-partitioning-ii/README.md | 11 +++ Algorithms/0133.clone-graph/README.md | 42 +++++++++++ Algorithms/0134.gas-station/README.md | 17 +++++ Algorithms/0135.candy/README.md | 14 ++++ Algorithms/0136.single-number/README.md | 7 ++ Algorithms/0137.single-number-ii/README.md | 9 +++ .../README.md | 8 +++ Algorithms/0139.word-break/README.md | 18 +++++ Algorithms/0140.word-break-ii/README.md | 23 +++++++ Algorithms/0141.linked-list-cycle/README.md | 9 +++ .../0142.linked-list-cycle-ii/README.md | 12 ++++ Algorithms/0143.reorder-list/README.md | 12 ++++ .../README.md | 18 +++++ .../README.md | 18 +++++ Algorithms/0146.lru-cache/README.md | 27 ++++++++ Algorithms/0147.insertion-sort-list/README.md | 2 + Algorithms/0148.sort-list/README.md | 2 + .../0149.max-points-on-a-line/README.md | 2 + .../README.md | 15 ++++ .../0151.reverse-words-in-a-string/README.md | 31 +++++++++ .../0152.maximum-product-subarray/README.md | 9 +++ .../README.md | 8 +++ .../README.md | 15 ++++ Algorithms/0155.min-stack/README.md | 30 ++++++++ .../README.md | 22 ++++++ Algorithms/0162.find-peak-element/README.md | 18 +++++ Algorithms/0164.maximum-gap/README.md | 10 +++ .../0165.compare-version-numbers/README.md | 12 ++++ .../README.md | 14 ++++ .../README.md | 11 +++ .../0168.excel-sheet-column-title/README.md | 13 ++++ Algorithms/0169.majority-element/README.md | 6 ++ .../0171.excel-sheet-column-number/README.md | 14 ++++ .../0172.factorial-trailing-zeroes/README.md | 6 ++ .../README.md | 8 +++ Algorithms/0174.dungeon-game/README.md | 51 ++++++++++++++ Algorithms/0179.largest-number/README.md | 8 +++ .../0187.repeated-dna-sequences/README.md | 13 ++++ .../README.md | 9 +++ Algorithms/0189.rotate-array/README.md | 16 +++++ Algorithms/0190.reverse-bits/README.md | 13 ++++ Algorithms/0191.number-of-1-bits/README.md | 6 ++ Algorithms/0198.house-robber/README.md | 6 ++ .../README.md | 18 +++++ Algorithms/0200.number-of-islands/README.md | 11 +++ .../README.md | 8 +++ Algorithms/0202.happy-number/README.md | 15 ++++ .../README.md | 9 +++ Algorithms/0204.count-primes/README.md | 5 ++ Algorithms/0205.isomorphic-strings/README.md | 16 +++++ Algorithms/0207.course-schedule/README.md | 33 +++++++++ .../0208.implement-trie-prefix-tree/README.md | 9 +++ .../0209.minimum-size-subarray-sum/README.md | 17 +++++ Algorithms/0210.course-schedule-ii/README.md | 35 ++++++++++ .../README.md | 31 +++++++++ Algorithms/0212.word-search-ii/README.md | 33 +++++++++ Algorithms/0213.house-robber-ii/README.md | 8 +++ Algorithms/0214.shortest-palindrome/README.md | 10 +++ .../README.md | 11 +++ Algorithms/0216.combination-sum-iii/README.md | 20 ++++++ Algorithms/0217.contains-duplicate/README.md | 4 ++ Algorithms/0218.the-skyline-problem/README.md | 34 +++++++++ .../0219.contains-duplicate-ii/README.md | 4 ++ .../0220.contains-duplicate-iii/README.md | 4 ++ Algorithms/0221.maximal-square/README.md | 15 ++++ .../0222.count-complete-tree-nodes/README.md | 5 ++ Algorithms/0223.rectangle-area/README.md | 11 +++ Algorithms/0224.basic-calculator/README.md | 17 +++++ .../README.md | 26 +++++++ Algorithms/0226.invert-binary-tree/README.md | 18 +++++ Algorithms/0227.basic-calculator-ii/README.md | 19 +++++ Algorithms/0228.summary-ranges/README.md | 9 +++ Algorithms/0229.majority-element-ii/README.md | 2 + .../README.md | 10 +++ Algorithms/0231.power-of-two/README.md | 6 ++ .../README.md | 24 +++++++ Algorithms/0233.number-of-digit-one/README.md | 8 +++ .../0234.palindrome-linked-list/README.md | 5 ++ .../README.md | 20 ++++++ .../README.md | 20 ++++++ .../README.md | 8 +++ .../README.md | 10 +++ .../0239.sliding-window-maximum/README.md | 23 +++++++ .../0240.search-a-2d-matrix-ii/README.md | 26 +++++++ .../README.md | 19 +++++ Algorithms/0242.valid-anagram/README.md | 13 ++++ Algorithms/0257.binary-tree-paths/README.md | 21 ++++++ Algorithms/0258.add-digits/README.md | 17 +++++ Algorithms/0260.single-number-iii/README.md | 19 +++++ Algorithms/0263.ugly-number/README.md | 14 ++++ Algorithms/0264.ugly-number-ii/README.md | 14 ++++ Algorithms/0268.missing-number/README.md | 15 ++++ .../0273.integer-to-english-words/README.md | 9 +++ Algorithms/0274.h-index/README.md | 18 +++++ Algorithms/0275.h-index-ii/README.md | 4 ++ Algorithms/0278.first-bad-version/README.md | 14 ++++ Algorithms/0279.perfect-squares/README.md | 10 +++ .../0282.expression-add-operators/README.md | 14 ++++ Algorithms/0283.move-zeroes/README.md | 18 +++++ Algorithms/0284.peeking-iterator/README.md | 16 +++++ .../0287.find-the-duplicate-number/README.md | 16 +++++ Algorithms/0289.game-of-life/README.md | 30 ++++++++ Algorithms/0290.word-pattern/README.md | 19 +++++ Algorithms/0292.nim-game/README.md | 14 ++++ .../README.md | 24 +++++++ .../README.md | 21 ++++++ Algorithms/0299.bulls-and-cows/README.md | 23 +++++++ .../README.md | 16 +++++ .../0301.remove-invalid-parentheses/README.md | 16 +++++ .../0303.range-sum-query-immutable/README.md | 18 +++++ .../README.md | 30 ++++++++ Algorithms/0306.additive-number/README.md | 25 +++++++ .../0307.range-sum-query-mutable/README.md | 20 ++++++ .../README.md | 18 +++++ .../0310.minimum-height-trees/README.md | 65 +++++++++++++++++ Algorithms/0312.burst-balloons/README.md | 34 +++++++++ Algorithms/0313.super-ugly-number/README.md | 21 ++++++ .../README.md | 20 ++++++ .../0316.remove-duplicate-letters/README.md | 18 +++++ .../README.md | 33 +++++++++ Algorithms/0319.bulb-switcher/README.md | 16 +++++ .../0321.create-maximum-number/README.md | 36 ++++++++++ Algorithms/0322.coin-change/README.md | 23 +++++++ Algorithms/0324.wiggle-sort-ii/README.md | 23 +++++++ Algorithms/0326.power-of-three/README.md | 10 +++ Algorithms/0327.count-of-range-sum/README.md | 20 ++++++ .../0328.odd-even-linked-list/README.md | 17 +++++ .../README.md | 37 ++++++++++ Algorithms/0330.patching-array/README.md | 23 +++++++ .../README.md | 32 +++++++++ .../0332.reconstruct-itinerary/README.md | 25 +++++++ .../README.md | 25 +++++++ Algorithms/0335.self-crossing/README.md | 48 +++++++++++++ Algorithms/0336.palindrome-pairs/README.md | 19 +++++ Algorithms/0337.house-robber-iii/README.md | 30 ++++++++ Algorithms/0338.counting-bits/README.md | 17 +++++ .../README.md | 18 +++++ Algorithms/0342.power-of-four/README.md | 12 ++++ Algorithms/0343.integer-break/README.md | 14 ++++ Algorithms/0344.reverse-string/README.md | 7 ++ .../0345.reverse-vowels-of-a-string/README.md | 17 +++++ .../0347.top-k-frequent-elements/README.md | 13 ++++ .../0349.intersection-of-two-arrays/README.md | 15 ++++ .../README.md | 23 +++++++ .../README.md | 16 +++++ .../0354.russian-doll-envelopes/README.md | 10 +++ Algorithms/0355.design-twitter/README.md | 39 +++++++++++ .../README.md | 9 +++ .../README.md | 22 ++++++ .../0365.water-and-jug-problem/README.md | 28 ++++++++ .../0367.valid-perfect-square/README.md | 20 ++++++ .../0368.largest-divisible-subset/README.md | 23 +++++++ Algorithms/0371.sum-of-two-integers/README.md | 8 +++ Algorithms/0372.super-pow/README.md | 22 ++++++ .../README.md | 41 +++++++++++ .../README.md | 19 +++++ .../README.md | 26 +++++++ Algorithms/0376.wiggle-subsequence/README.md | 26 +++++++ Algorithms/0377.combination-sum-iv/README.md | 28 ++++++++ .../README.md | 21 ++++++ .../0380.insert-delete-getrandom-o1/README.md | 37 ++++++++++ .../README.md | 34 +++++++++ .../0382.linked-list-random-node/README.md | 18 +++++ Algorithms/0383.ransom-note/README.md | 18 +++++ Algorithms/0384.shuffle-an-array/README.md | 19 +++++ Algorithms/0385.mini-parser/README.md | 33 +++++++++ .../0386.lexicographical-numbers/README.md | 12 ++++ .../README.md | 16 +++++ .../0388.longest-absolute-file-path/README.md | 40 +++++++++++ Algorithms/0389.find-the-difference/README.md | 19 +++++ Algorithms/0390.elimination-game/README.md | 22 ++++++ Algorithms/0391.perfect-rectangle/README.md | 69 +++++++++++++++++++ Algorithms/0392.is-subsequence/README.md | 31 +++++++++ Algorithms/0393.utf-8-validation/README.md | 43 ++++++++++++ Algorithms/0394.decode-string/README.md | 19 +++++ .../README.md | 26 +++++++ Algorithms/0396.rotate-function/README.md | 30 ++++++++ Algorithms/0397.integer-replacement/README.md | 43 ++++++++++++ Algorithms/0398.random-pick-index/README.md | 21 ++++++ Algorithms/0399.evaluate-division/README.md | 20 ++++++ Algorithms/0400.nth-digit/README.md | 27 ++++++++ Algorithms/0401.binary-watch/README.md | 19 +++++ Algorithms/0402.remove-k-digits/README.md | 32 +++++++++ Algorithms/0403.frog-jump/README.md | 38 ++++++++++ Algorithms/0404.sum-of-left-leaves/README.md | 13 ++++ .../README.md | 31 +++++++++ .../README.md | 16 +++++ .../0407.trapping-rain-water-ii/README.md | 29 ++++++++ Algorithms/0409.longest-palindrome/README.md | 20 ++++++ .../0410.split-array-largest-sum/README.md | 26 +++++++ Algorithms/0412.fizz-buzz/README.md | 28 ++++++++ Algorithms/0413.arithmetic-slices/README.md | 24 +++++++ .../0414.third-maximum-number/README.md | 30 ++++++++ Algorithms/0415.add-strings/README.md | 11 +++ .../0416.partition-equal-subset-sum/README.md | 28 ++++++++ .../README.md | 29 ++++++++ .../0419.battleships-in-a-board/README.md | 24 +++++++ .../0420.strong-password-checker/README.md | 12 ++++ .../README.md | 15 ++++ .../README.md | 24 +++++++ .../README.md | 33 +++++++++ .../0432.all-oone-data-structure/README.md | 15 ++++ .../README.md | 10 +++ .../0435.non-overlapping-intervals/README.md | 38 ++++++++++ Algorithms/0436.find-right-interval/README.md | 45 ++++++++++++ Algorithms/0437.path-sum-iii/README.md | 28 ++++++++ .../README.md | 33 +++++++++ .../README.md | 17 +++++ Algorithms/0441.arranging-coins/README.md | 31 +++++++++ .../README.md | 14 ++++ Algorithms/0445.add-two-numbers-ii/README.md | 15 ++++ .../README.md | 36 ++++++++++ .../0447.number-of-boomerangs/README.md | 16 +++++ .../README.md | 12 ++++ .../0450.delete-node-in-a-bst/README.md | 41 +++++++++++ .../README.md | 41 +++++++++++ .../README.md | 16 +++++ .../README.md | 16 +++++ Algorithms/0454.4sum-ii/README.md | 21 ++++++ Algorithms/0455.assign-cookies/README.md | 31 +++++++++ Algorithms/0456.132-pattern/README.md | 33 +++++++++ .../0459.repeated-substring-pattern/README.md | 27 ++++++++ Algorithms/0460.lfu-cache/README.md | 27 ++++++++ Algorithms/0461.hamming-distance/README.md | 22 ++++++ .../README.md | 18 +++++ Algorithms/0463.island-perimeter/README.md | 14 ++++ Algorithms/0464.can-i-win/README.md | 28 ++++++++ .../0466.count-the-repetitions/README.md | 14 ++++ .../README.md | 28 ++++++++ Algorithms/0468.validate-ip-address/README.md | 58 ++++++++++++++++ Algorithms/0472.concatenated-words/README.md | 21 ++++++ .../0473.matchsticks-to-square/README.md | 27 ++++++++ Algorithms/0474.ones-and-zeroes/README.md | 31 +++++++++ Algorithms/0475.heaters/README.md | 29 ++++++++ Algorithms/0476.number-complement/README.md | 23 +++++++ .../0477.total-hamming-distance/README.md | 23 +++++++ .../0479.largest-palindrome-product/README.md | 16 +++++ .../0480.sliding-window-median/README.md | 25 +++++++ Algorithms/0481.magical-string/README.md | 45 ++++++++++++ .../0482.license-key-formatting/README.md | 34 +++++++++ Algorithms/0483.smallest-good-base/README.md | 31 +++++++++ Algorithms/0486.predict-the-winner/README.md | 26 +++++++ Algorithms/0488.zuma-game/README.md | 34 +++++++++ .../0491.increasing-subsequences/README.md | 18 +++++ .../0492.construct-the-rectangle/README.md | 24 +++++++ Algorithms/0493.reverse-pairs/README.md | 21 ++++++ Algorithms/0494.target-sum/README.md | 30 ++++++++ Algorithms/0495.teemo-attacking/README.md | 29 ++++++++ .../0496.next-greater-element-i/README.md | 35 ++++++++++ Algorithms/0498.diagonal-traverse/README.md | 23 +++++++ Algorithms/0500.keyboard-row/README.md | 21 ++++++ .../README.md | 33 +++++++++ Algorithms/0502.ipo/README.md | 33 +++++++++ .../0503.next-greater-element-ii/README.md | 15 ++++ Algorithms/0504.base-7/README.md | 18 +++++ Algorithms/0506.relative-ranks/README.md | 18 +++++ Algorithms/0507.perfect-number/README.md | 16 +++++ Draft/0433.minimum-genetic-mutation/README.md | 45 ++++++++++++ Draft/0457.circular-array-loop/README.md | 24 +++++++ Draft/0458.poor-pigs/README.md | 15 ++++ Draft/0518.coin-change-2/README.md | 36 ++++++++++ Draft/0626.exchange-seats/README.md | 30 ++++++++ 338 files changed, 6802 insertions(+) diff --git a/Algorithms/0044.wildcard-matching/README.md b/Algorithms/0044.wildcard-matching/README.md index 75b8f876d..889340925 100755 --- a/Algorithms/0044.wildcard-matching/README.md +++ b/Algorithms/0044.wildcard-matching/README.md @@ -2,6 +2,26 @@ ## 题目 + Implement wildcard pattern matching with support for '?' and '*'. + +'?' Matches any single character. +'*' Matches any sequence of characters (including the empty sequence). + +The matching should cover the entire input string (not partial). + +The function prototype should be: +bool isMatch(const char *s, const char *p) + +Some examples: +isMatch("aa","a") → false +isMatch("aa","aa") → true +isMatch("aaa","aa") → false +isMatch("aa", "*") → true +isMatch("aa", "a*") → true +isMatch("ab", "?*") → true +isMatch("aab", "c*a*b") → false + + ## 解题思路 diff --git a/Algorithms/0054.spiral-matrix/README.md b/Algorithms/0054.spiral-matrix/README.md index 6ef60c42f..a315de8a3 100755 --- a/Algorithms/0054.spiral-matrix/README.md +++ b/Algorithms/0054.spiral-matrix/README.md @@ -2,6 +2,23 @@ ## 题目 + Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. + + + +For example, +Given the following matrix: + +[ + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ] +] + + +You should return [1,2,3,6,9,8,7,4,5]. + + ## 解题思路 diff --git a/Algorithms/0055.jump-game/README.md b/Algorithms/0055.jump-game/README.md index 7d32caa54..36dd28715 100755 --- a/Algorithms/0055.jump-game/README.md +++ b/Algorithms/0055.jump-game/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given an array of non-negative integers, you are initially positioned at the first index of the array. + + +Each element in the array represents your maximum jump length at that position. + + +Determine if you are able to reach the last index. + + + +For example: +A = [2,3,1,1,4], return true. + + +A = [3,2,1,0,4], return false. + + ## 解题思路 diff --git a/Algorithms/0056.merge-intervals/README.md b/Algorithms/0056.merge-intervals/README.md index eca52faff..e451d6d74 100755 --- a/Algorithms/0056.merge-intervals/README.md +++ b/Algorithms/0056.merge-intervals/README.md @@ -2,6 +2,14 @@ ## 题目 + Given a collection of intervals, merge all overlapping intervals. + + +For example, +Given [1,3],[2,6],[8,10],[15,18], +return [1,6],[8,10],[15,18]. + + ## 解题思路 diff --git a/Algorithms/0057.insert-interval/README.md b/Algorithms/0057.insert-interval/README.md index 4f914edf3..2975faeb0 100755 --- a/Algorithms/0057.insert-interval/README.md +++ b/Algorithms/0057.insert-interval/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). + +You may assume that the intervals were initially sorted according to their start times. + + +Example 1: +Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. + + + +Example 2: +Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16]. + + + +This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10]. + + ## 解题思路 diff --git a/Algorithms/0058.length-of-last-word/README.md b/Algorithms/0058.length-of-last-word/README.md index 76711d611..9d6d9261f 100755 --- a/Algorithms/0058.length-of-last-word/README.md +++ b/Algorithms/0058.length-of-last-word/README.md @@ -2,6 +2,18 @@ ## 题目 + Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. + +If the last word does not exist, return 0. + +Note: A word is defined as a character sequence consists of non-space characters only. + + +For example, +Given s = "Hello World", +return 5. + + ## 解题思路 diff --git a/Algorithms/0059.spiral-matrix-ii/README.md b/Algorithms/0059.spiral-matrix-ii/README.md index cc6a8db0b..e4807d1b3 100755 --- a/Algorithms/0059.spiral-matrix-ii/README.md +++ b/Algorithms/0059.spiral-matrix-ii/README.md @@ -2,6 +2,20 @@ ## 题目 + Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. + + +For example, +Given n = 3, + +You should return the following matrix: +[ + [ 1, 2, 3 ], + [ 8, 9, 4 ], + [ 7, 6, 5 ] +] + + ## 解题思路 diff --git a/Algorithms/0060.permutation-sequence/README.md b/Algorithms/0060.permutation-sequence/README.md index 30fa3578c..a7214a153 100755 --- a/Algorithms/0060.permutation-sequence/README.md +++ b/Algorithms/0060.permutation-sequence/README.md @@ -2,6 +2,24 @@ ## 题目 + The set [1,2,3,…,n] contains a total of n! unique permutations. + +By listing and labeling all of the permutations in order, +We get the following sequence (ie, for n = 3): + +"123" +"132" +"213" +"231" +"312" +"321" + + + +Given n and k, return the kth permutation sequence. + +Note: Given n will be between 1 and 9 inclusive. + ## 解题思路 diff --git a/Algorithms/0061.rotate-list/README.md b/Algorithms/0061.rotate-list/README.md index fca5e001f..32261dd9d 100755 --- a/Algorithms/0061.rotate-list/README.md +++ b/Algorithms/0061.rotate-list/README.md @@ -2,6 +2,12 @@ ## 题目 + Given a list, rotate the list to the right by k places, where k is non-negative. + +For example: +Given 1->2->3->4->5->NULL and k = 2, +return 4->5->1->2->3->NULL. + ## 解题思路 diff --git a/Algorithms/0062.unique-paths/README.md b/Algorithms/0062.unique-paths/README.md index 1c4818243..29dfe1d65 100755 --- a/Algorithms/0062.unique-paths/README.md +++ b/Algorithms/0062.unique-paths/README.md @@ -2,6 +2,19 @@ ## 题目 + A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). + +The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). + +How many possible unique paths are there? + + + +Above is a 3 x 7 grid. How many possible unique paths are there? + + +Note: m and n will be at most 100. + ## 解题思路 diff --git a/Algorithms/0063.unique-paths-ii/README.md b/Algorithms/0063.unique-paths-ii/README.md index acd22ab84..2983b95d5 100755 --- a/Algorithms/0063.unique-paths-ii/README.md +++ b/Algorithms/0063.unique-paths-ii/README.md @@ -2,6 +2,24 @@ ## 题目 + Follow up for "Unique Paths": + +Now consider if some obstacles are added to the grids. How many unique paths would there be? + +An obstacle and empty space is marked as 1 and 0 respectively in the grid. + +For example, +There is one obstacle in the middle of a 3x3 grid as illustrated below. +[ + [0,0,0], + [0,1,0], + [0,0,0] +] + +The total number of unique paths is 2. + +Note: m and n will be at most 100. + ## 解题思路 diff --git a/Algorithms/0064.minimum-path-sum/README.md b/Algorithms/0064.minimum-path-sum/README.md index 78504874f..bf61eeb4a 100755 --- a/Algorithms/0064.minimum-path-sum/README.md +++ b/Algorithms/0064.minimum-path-sum/README.md @@ -2,6 +2,10 @@ ## 题目 + Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. + +Note: You can only move either down or right at any point in time. + ## 解题思路 diff --git a/Algorithms/0065.valid-number/README.md b/Algorithms/0065.valid-number/README.md index 6c7326af2..706150fc4 100755 --- a/Algorithms/0065.valid-number/README.md +++ b/Algorithms/0065.valid-number/README.md @@ -2,6 +2,25 @@ ## 题目 + Validate if a given string is numeric. + + +Some examples: +"0" => true +" 0.1 " => true +"abc" => false +"1 a" => false +"2e10" => true + + +Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. + + + +Update (2015-02-10): +The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition. + + ## 解题思路 diff --git a/Algorithms/0067.add-binary/README.md b/Algorithms/0067.add-binary/README.md index 6da37b115..fb92ce0b1 100755 --- a/Algorithms/0067.add-binary/README.md +++ b/Algorithms/0067.add-binary/README.md @@ -2,6 +2,17 @@ ## 题目 + +Given two binary strings, return their sum (also a binary string). + + + +For example, +a = "11" +b = "1" +Return "100". + + ## 解题思路 diff --git a/Algorithms/0068.text-justification/README.md b/Algorithms/0068.text-justification/README.md index ed443d83b..9a9ecaa57 100755 --- a/Algorithms/0068.text-justification/README.md +++ b/Algorithms/0068.text-justification/README.md @@ -2,6 +2,53 @@ ## 题目 + +Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. + + + +You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly L characters. + + + +Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. + + + +For the last line of text, it should be left justified and no extra space is inserted between words. + + + +For example, +words: ["This", "is", "an", "example", "of", "text", "justification."] +L: 16. + + + +Return the formatted lines as: +[ + "This is an", + "example of text", + "justification. " +] + + + + +Note: Each word is guaranteed not to exceed L in length. + + + +click to show corner cases. + +Corner Cases: + + +A line other than the last line might contain only one word. What should you do in this case? +In this case, that line should be left-justified. + + + ## 解题思路 diff --git a/Algorithms/0069.sqrtx/README.md b/Algorithms/0069.sqrtx/README.md index b4b1ec576..67be43490 100755 --- a/Algorithms/0069.sqrtx/README.md +++ b/Algorithms/0069.sqrtx/README.md @@ -2,6 +2,10 @@ ## 题目 + Implement int sqrt(int x). + +Compute and return the square root of x. + ## 解题思路 diff --git a/Algorithms/0070.climbing-stairs/README.md b/Algorithms/0070.climbing-stairs/README.md index c9d3a3317..0ad3d0987 100755 --- a/Algorithms/0070.climbing-stairs/README.md +++ b/Algorithms/0070.climbing-stairs/README.md @@ -2,6 +2,14 @@ ## 题目 + You are climbing a stair case. It takes n steps to reach to the top. + +Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? + + +Note: Given n will be a positive integer. + + ## 解题思路 diff --git a/Algorithms/0071.simplify-path/README.md b/Algorithms/0071.simplify-path/README.md index 0cfa189db..e5ce805a0 100755 --- a/Algorithms/0071.simplify-path/README.md +++ b/Algorithms/0071.simplify-path/README.md @@ -2,6 +2,26 @@ ## 题目 + Given an absolute path for a file (Unix-style), simplify it. + +For example, +path = "/home/", => "/home" +path = "/a/./b/../../c/", => "/c" + + +click to show corner cases. + +Corner Cases: + + + +Did you consider the case where path = "/../"? +In this case, you should return "/". +Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/". +In this case, you should ignore redundant slashes and return "/home/foo". + + + ## 解题思路 diff --git a/Algorithms/0072.edit-distance/README.md b/Algorithms/0072.edit-distance/README.md index 613bf7e50..5e2d75955 100755 --- a/Algorithms/0072.edit-distance/README.md +++ b/Algorithms/0072.edit-distance/README.md @@ -2,6 +2,20 @@ ## 题目 + +Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) + + + +You have the following 3 operations permitted on a word: + + + +a) Insert a character +b) Delete a character +c) Replace a character + + ## 解题思路 diff --git a/Algorithms/0073.set-matrix-zeroes/README.md b/Algorithms/0073.set-matrix-zeroes/README.md index 664e39449..f1b6bbf4d 100755 --- a/Algorithms/0073.set-matrix-zeroes/README.md +++ b/Algorithms/0073.set-matrix-zeroes/README.md @@ -2,6 +2,22 @@ ## 题目 + +Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. + + +click to show follow up. + +Follow up: + + +Did you use extra space? +A straight forward solution using O(mn) space is probably a bad idea. +A simple improvement uses O(m + n) space, but still not the best solution. +Could you devise a constant space solution? + + + ## 解题思路 diff --git a/Algorithms/0074.search-a-2d-matrix/README.md b/Algorithms/0074.search-a-2d-matrix/README.md index 7ac9b1b5c..b521c61ed 100755 --- a/Algorithms/0074.search-a-2d-matrix/README.md +++ b/Algorithms/0074.search-a-2d-matrix/README.md @@ -2,6 +2,29 @@ ## 题目 + Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: + + + +Integers in each row are sorted from left to right. +The first integer of each row is greater than the last integer of the previous row. + + + + +For example, + +Consider the following matrix: + +[ + [1, 3, 5, 7], + [10, 11, 16, 20], + [23, 30, 34, 50] +] + + +Given target = 3, return true. + ## 解题思路 diff --git a/Algorithms/0075.sort-colors/README.md b/Algorithms/0075.sort-colors/README.md index 98574bf5c..594ea73e9 100755 --- a/Algorithms/0075.sort-colors/README.md +++ b/Algorithms/0075.sort-colors/README.md @@ -2,6 +2,29 @@ ## 题目 + +Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. + + + +Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. + + + +Note: +You are not suppose to use the library's sort function for this problem. + + +click to show follow up. + + +Follow up: +A rather straight forward solution is a two-pass algorithm using counting sort. +First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's. +Could you come up with an one-pass algorithm using only constant space? + + + ## 解题思路 diff --git a/Algorithms/0076.minimum-window-substring/README.md b/Algorithms/0076.minimum-window-substring/README.md index 148b6c0b7..6d72db5d3 100755 --- a/Algorithms/0076.minimum-window-substring/README.md +++ b/Algorithms/0076.minimum-window-substring/README.md @@ -2,6 +2,27 @@ ## 题目 + +Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). + + + +For example, +S = "ADOBECODEBANC" +T = "ABC" + + +Minimum window is "BANC". + + + +Note: +If there is no such window in S that covers all characters in T, return the empty string "". + + +If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S. + + ## 解题思路 diff --git a/Algorithms/0077.combinations/README.md b/Algorithms/0077.combinations/README.md index 0182e32c4..ae3e6bea6 100755 --- a/Algorithms/0077.combinations/README.md +++ b/Algorithms/0077.combinations/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. + + +For example, +If n = 4 and k = 2, a solution is: + + +[ + [2,4], + [3,4], + [2,3], + [1,2], + [1,3], + [1,4], +] + + ## 解题思路 diff --git a/Algorithms/0078.subsets/README.md b/Algorithms/0078.subsets/README.md index d9e31fe43..126b6e0ad 100755 --- a/Algorithms/0078.subsets/README.md +++ b/Algorithms/0078.subsets/README.md @@ -2,6 +2,28 @@ ## 题目 + +Given a set of distinct integers, nums, return all possible subsets. + +Note: The solution set must not contain duplicate subsets. + + +For example, +If nums = [1,2,3], a solution is: + + +[ + [3], + [1], + [2], + [1,2,3], + [1,3], + [2,3], + [1,2], + [] +] + + ## 解题思路 diff --git a/Algorithms/0079.word-search/README.md b/Algorithms/0079.word-search/README.md index f00b107bd..23e2b34fa 100755 --- a/Algorithms/0079.word-search/README.md +++ b/Algorithms/0079.word-search/README.md @@ -2,6 +2,28 @@ ## 题目 + +Given a 2D board and a word, find if the word exists in the grid. + + +The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. + + + +For example, +Given board = +[ + ['A','B','C','E'], + ['S','F','C','S'], + ['A','D','E','E'] +] + + +word = "ABCCED", -> returns true, +word = "SEE", -> returns true, +word = "ABCB", -> returns false. + + ## 解题思路 diff --git a/Algorithms/0080.remove-duplicates-from-sorted-array-ii/README.md b/Algorithms/0080.remove-duplicates-from-sorted-array-ii/README.md index fce50efc4..e6c7669af 100755 --- a/Algorithms/0080.remove-duplicates-from-sorted-array-ii/README.md +++ b/Algorithms/0080.remove-duplicates-from-sorted-array-ii/README.md @@ -2,6 +2,18 @@ ## 题目 + +Follow up for "Remove Duplicates": +What if duplicates are allowed at most twice? + + +For example, +Given sorted array nums = [1,1,1,2,2,3], + + +Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length. + + ## 解题思路 diff --git a/Algorithms/0081.search-in-rotated-sorted-array-ii/README.md b/Algorithms/0081.search-in-rotated-sorted-array-ii/README.md index e65b2fbc3..b8a9bd00a 100755 --- a/Algorithms/0081.search-in-rotated-sorted-array-ii/README.md +++ b/Algorithms/0081.search-in-rotated-sorted-array-ii/README.md @@ -2,6 +2,21 @@ ## 题目 + +Follow up for "Search in Rotated Sorted Array": +What if duplicates are allowed? + +Would this affect the run-time complexity? How and why? + + +Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. + +(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). + +Write a function to determine if a given target is in the array. + +The array may contain duplicates. + ## 解题思路 diff --git a/Algorithms/0082.remove-duplicates-from-sorted-list-ii/README.md b/Algorithms/0082.remove-duplicates-from-sorted-list-ii/README.md index 4407fd0a7..fd651effe 100755 --- a/Algorithms/0082.remove-duplicates-from-sorted-list-ii/README.md +++ b/Algorithms/0082.remove-duplicates-from-sorted-list-ii/README.md @@ -2,6 +2,15 @@ ## 题目 + +Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. + + +For example, +Given 1->2->3->3->4->4->5, return 1->2->5. +Given 1->1->1->2->3, return 2->3. + + ## 解题思路 diff --git a/Algorithms/0083.remove-duplicates-from-sorted-list/README.md b/Algorithms/0083.remove-duplicates-from-sorted-list/README.md index 7e9f4a909..c8d9dda5a 100755 --- a/Algorithms/0083.remove-duplicates-from-sorted-list/README.md +++ b/Algorithms/0083.remove-duplicates-from-sorted-list/README.md @@ -2,6 +2,15 @@ ## 题目 + +Given a sorted linked list, delete all duplicates such that each element appear only once. + + +For example, +Given 1->1->2, return 1->2. +Given 1->1->2->3->3, return 1->2->3. + + ## 解题思路 diff --git a/Algorithms/0084.largest-rectangle-in-histogram/README.md b/Algorithms/0084.largest-rectangle-in-histogram/README.md index 7083b7150..f00acdc89 100755 --- a/Algorithms/0084.largest-rectangle-in-histogram/README.md +++ b/Algorithms/0084.largest-rectangle-in-histogram/README.md @@ -2,6 +2,26 @@ ## 题目 + +Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. + + + + +Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. + + + + +The largest rectangle is shown in the shaded area, which has area = 10 unit. + + + +For example, +Given heights = [2,1,5,6,2,3], +return 10. + + ## 解题思路 diff --git a/Algorithms/0085.maximal-rectangle/README.md b/Algorithms/0085.maximal-rectangle/README.md index 5360718ac..772067e45 100755 --- a/Algorithms/0085.maximal-rectangle/README.md +++ b/Algorithms/0085.maximal-rectangle/README.md @@ -2,6 +2,19 @@ ## 题目 + +Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. + + +For example, given the following matrix: +1 0 1 0 0 +1 0 1 1 1 +1 1 1 1 1 +1 0 0 1 0 + +Return 6. + + ## 解题思路 diff --git a/Algorithms/0086.partition-list/README.md b/Algorithms/0086.partition-list/README.md index d7f86679a..374722670 100755 --- a/Algorithms/0086.partition-list/README.md +++ b/Algorithms/0086.partition-list/README.md @@ -2,6 +2,17 @@ ## 题目 + Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. + + +You should preserve the original relative order of the nodes in each of the two partitions. + + +For example, +Given 1->4->3->2->5->2 and x = 3, +return 1->2->2->4->3->5. + + ## 解题思路 diff --git a/Algorithms/0087.scramble-string/README.md b/Algorithms/0087.scramble-string/README.md index 6aad018e3..167f4ad15 100755 --- a/Algorithms/0087.scramble-string/README.md +++ b/Algorithms/0087.scramble-string/README.md @@ -2,6 +2,55 @@ ## 题目 + +Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. + + +Below is one possible representation of s1 = "great": + + great + / \ + gr eat + / \ / \ +g r e at + / \ + a t + + +To scramble the string, we may choose any non-leaf node and swap its two children. + + +For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat". + + rgeat + / \ + rg eat + / \ / \ +r g e at + / \ + a t + + +We say that "rgeat" is a scrambled string of "great". + + +Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae". + + rgtae + / \ + rg tae + / \ / \ +r g ta e + / \ + t a + + +We say that "rgtae" is a scrambled string of "great". + + +Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1. + + ## 解题思路 diff --git a/Algorithms/0089.gray-code/README.md b/Algorithms/0089.gray-code/README.md index 0d5d64658..4ed2551fc 100755 --- a/Algorithms/0089.gray-code/README.md +++ b/Algorithms/0089.gray-code/README.md @@ -2,6 +2,24 @@ ## 题目 + The gray code is a binary numeral system where two successive values differ in only one bit. + +Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. + +For example, given n = 2, return [0,1,3,2]. Its gray code sequence is: +00 - 0 +01 - 1 +11 - 3 +10 - 2 + + +Note: +For a given n, a gray code sequence is not uniquely defined. + +For example, [0,2,3,1] is also a valid gray code sequence according to the above definition. + +For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that. + ## 解题思路 diff --git a/Algorithms/0090.subsets-ii/README.md b/Algorithms/0090.subsets-ii/README.md index ae6d6fc8a..3cc692b2a 100755 --- a/Algorithms/0090.subsets-ii/README.md +++ b/Algorithms/0090.subsets-ii/README.md @@ -2,6 +2,26 @@ ## 题目 + +Given a collection of integers that might contain duplicates, nums, return all possible subsets. + +Note: The solution set must not contain duplicate subsets. + + +For example, +If nums = [1,2,2], a solution is: + + +[ + [2], + [1], + [1,2,2], + [2,2], + [1,2], + [] +] + + ## 解题思路 diff --git a/Algorithms/0091.decode-ways/README.md b/Algorithms/0091.decode-ways/README.md index 2ed1932e6..4742730db 100755 --- a/Algorithms/0091.decode-ways/README.md +++ b/Algorithms/0091.decode-ways/README.md @@ -2,6 +2,30 @@ ## 题目 + +A message containing letters from A-Z is being encoded to numbers using the following mapping: + + +'A' -> 1 +'B' -> 2 +... +'Z' -> 26 + + + +Given an encoded message containing digits, determine the total number of ways to decode it. + + + +For example, +Given encoded message "12", +it could be decoded as "AB" (1 2) or "L" (12). + + + +The number of ways decoding "12" is 2. + + ## 解题思路 diff --git a/Algorithms/0092.reverse-linked-list-ii/README.md b/Algorithms/0092.reverse-linked-list-ii/README.md index 5da4205d9..334da7441 100755 --- a/Algorithms/0092.reverse-linked-list-ii/README.md +++ b/Algorithms/0092.reverse-linked-list-ii/README.md @@ -2,6 +2,23 @@ ## 题目 + +Reverse a linked list from position m to n. Do it in-place and in one-pass. + + + +For example: +Given 1->2->3->4->5->NULL, m = 2 and n = 4, + + +return 1->4->3->2->5->NULL. + + +Note: +Given m, n satisfy the following condition: +1 ≤ m ≤ n ≤ length of list. + + ## 解题思路 diff --git a/Algorithms/0093.restore-ip-addresses/README.md b/Algorithms/0093.restore-ip-addresses/README.md index 723e524f7..96550193e 100755 --- a/Algorithms/0093.restore-ip-addresses/README.md +++ b/Algorithms/0093.restore-ip-addresses/README.md @@ -2,6 +2,16 @@ ## 题目 + Given a string containing only digits, restore it by returning all possible valid IP address combinations. + + +For example: +Given "25525511135", + + +return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) + + ## 解题思路 diff --git a/Algorithms/0094.binary-tree-inorder-traversal/README.md b/Algorithms/0094.binary-tree-inorder-traversal/README.md index 627d2ef5d..a555ec37c 100755 --- a/Algorithms/0094.binary-tree-inorder-traversal/README.md +++ b/Algorithms/0094.binary-tree-inorder-traversal/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a binary tree, return the inorder traversal of its nodes' values. + + +For example: +Given binary tree [1,null,2,3], + 1 + \ + 2 + / + 3 + + + +return [1,3,2]. + + +Note: Recursive solution is trivial, could you do it iteratively? + ## 解题思路 diff --git a/Algorithms/0095.unique-binary-search-trees-ii/README.md b/Algorithms/0095.unique-binary-search-trees-ii/README.md index 9149ae92c..ecbe948a0 100755 --- a/Algorithms/0095.unique-binary-search-trees-ii/README.md +++ b/Algorithms/0095.unique-binary-search-trees-ii/README.md @@ -2,6 +2,20 @@ ## 题目 + Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. + + +For example, +Given n = 3, your program should return all 5 unique BST's shown below. + + 1 3 3 2 1 + \ / / / \ \ + 3 2 1 1 3 2 + / / \ \ + 2 1 2 3 + + + ## 解题思路 diff --git a/Algorithms/0096.unique-binary-search-trees/README.md b/Algorithms/0096.unique-binary-search-trees/README.md index f3c68b7f7..b53af8b80 100755 --- a/Algorithms/0096.unique-binary-search-trees/README.md +++ b/Algorithms/0096.unique-binary-search-trees/README.md @@ -2,6 +2,20 @@ ## 题目 + Given n, how many structurally unique BST's (binary search trees) that store values 1...n? + + +For example, +Given n = 3, there are a total of 5 unique BST's. + + 1 3 3 2 1 + \ / / / \ \ + 3 2 1 1 3 2 + / / \ \ + 2 1 2 3 + + + ## 解题思路 diff --git a/Algorithms/0097.interleaving-string/README.md b/Algorithms/0097.interleaving-string/README.md index bbc27c568..898b30a9b 100755 --- a/Algorithms/0097.interleaving-string/README.md +++ b/Algorithms/0097.interleaving-string/README.md @@ -2,6 +2,21 @@ ## 题目 + +Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. + + + +For example, +Given: +s1 = "aabcc", +s2 = "dbbca", + + +When s3 = "aadbbcbcac", return true. +When s3 = "aadbbbaccc", return false. + + ## 解题思路 diff --git a/Algorithms/0098.validate-binary-search-tree/README.md b/Algorithms/0098.validate-binary-search-tree/README.md index 8c0d3b492..93bc8eaba 100755 --- a/Algorithms/0098.validate-binary-search-tree/README.md +++ b/Algorithms/0098.validate-binary-search-tree/README.md @@ -2,6 +2,35 @@ ## 题目 + +Given a binary tree, determine if it is a valid binary search tree (BST). + + + +Assume a BST is defined as follows: + +The left subtree of a node contains only nodes with keys less than the node's key. +The right subtree of a node contains only nodes with keys greater than the node's key. +Both the left and right subtrees must also be binary search trees. + + + +Example 1: + 2 + / \ + 1 3 + +Binary tree [2,1,3], return true. + + +Example 2: + 1 + / \ + 2 3 + +Binary tree [1,2,3], return false. + + ## 解题思路 diff --git a/Algorithms/0099.recover-binary-search-tree/README.md b/Algorithms/0099.recover-binary-search-tree/README.md index 96e8ab124..93c55bd20 100755 --- a/Algorithms/0099.recover-binary-search-tree/README.md +++ b/Algorithms/0099.recover-binary-search-tree/README.md @@ -2,6 +2,16 @@ ## 题目 + +Two elements of a binary search tree (BST) are swapped by mistake. + +Recover the tree without changing its structure. + + +Note: +A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? + + ## 解题思路 diff --git a/Algorithms/0100.same-tree/README.md b/Algorithms/0100.same-tree/README.md index 04f6db162..dd0071f30 100755 --- a/Algorithms/0100.same-tree/README.md +++ b/Algorithms/0100.same-tree/README.md @@ -2,6 +2,13 @@ ## 题目 + +Given two binary trees, write a function to check if they are equal or not. + + +Two binary trees are considered equal if they are structurally identical and the nodes have the same value. + + ## 解题思路 diff --git a/Algorithms/0101.symmetric-tree/README.md b/Algorithms/0101.symmetric-tree/README.md index fd85ec43a..eb84fb25b 100755 --- a/Algorithms/0101.symmetric-tree/README.md +++ b/Algorithms/0101.symmetric-tree/README.md @@ -2,6 +2,32 @@ ## 题目 + Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). + + +For example, this binary tree [1,2,2,3,4,4,3] is symmetric: + 1 + / \ + 2 2 + / \ / \ +3 4 4 3 + + + +But the following [1,2,2,null,3,null,3] is not: + 1 + / \ + 2 2 + \ \ + 3 3 + + + + +Note: +Bonus points if you could solve it both recursively and iteratively. + + ## 解题思路 diff --git a/Algorithms/0102.binary-tree-level-order-traversal/README.md b/Algorithms/0102.binary-tree-level-order-traversal/README.md index ae4e504f4..32a162605 100755 --- a/Algorithms/0102.binary-tree-level-order-traversal/README.md +++ b/Algorithms/0102.binary-tree-level-order-traversal/README.md @@ -2,6 +2,28 @@ ## 题目 + Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). + + +For example: +Given binary tree [3,9,20,null,null,15,7], + 3 + / \ + 9 20 + / \ + 15 7 + + + +return its level order traversal as: +[ + [3], + [9,20], + [15,7] +] + + + ## 解题思路 diff --git a/Algorithms/0103.binary-tree-zigzag-level-order-traversal/README.md b/Algorithms/0103.binary-tree-zigzag-level-order-traversal/README.md index efb34fb58..0b170165e 100755 --- a/Algorithms/0103.binary-tree-zigzag-level-order-traversal/README.md +++ b/Algorithms/0103.binary-tree-zigzag-level-order-traversal/README.md @@ -2,6 +2,28 @@ ## 题目 + Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). + + +For example: +Given binary tree [3,9,20,null,null,15,7], + 3 + / \ + 9 20 + / \ + 15 7 + + + +return its zigzag level order traversal as: +[ + [3], + [20,9], + [15,7] +] + + + ## 解题思路 diff --git a/Algorithms/0104.maximum-depth-of-binary-tree/README.md b/Algorithms/0104.maximum-depth-of-binary-tree/README.md index 3de521e96..b512299c1 100755 --- a/Algorithms/0104.maximum-depth-of-binary-tree/README.md +++ b/Algorithms/0104.maximum-depth-of-binary-tree/README.md @@ -2,6 +2,10 @@ ## 题目 + Given a binary tree, find its maximum depth. + +The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. + ## 解题思路 diff --git a/Algorithms/0105.construct-binary-tree-from-preorder-and-inorder-traversal/README.md b/Algorithms/0105.construct-binary-tree-from-preorder-and-inorder-traversal/README.md index 7f3e7de5d..63609f1f7 100755 --- a/Algorithms/0105.construct-binary-tree-from-preorder-and-inorder-traversal/README.md +++ b/Algorithms/0105.construct-binary-tree-from-preorder-and-inorder-traversal/README.md @@ -2,6 +2,12 @@ ## 题目 + Given preorder and inorder traversal of a tree, construct the binary tree. + +Note: +You may assume that duplicates do not exist in the tree. + + ## 解题思路 diff --git a/Algorithms/0106.construct-binary-tree-from-inorder-and-postorder-traversal/README.md b/Algorithms/0106.construct-binary-tree-from-inorder-and-postorder-traversal/README.md index 9e01d9eba..28fffd51b 100755 --- a/Algorithms/0106.construct-binary-tree-from-inorder-and-postorder-traversal/README.md +++ b/Algorithms/0106.construct-binary-tree-from-inorder-and-postorder-traversal/README.md @@ -2,6 +2,12 @@ ## 题目 + Given inorder and postorder traversal of a tree, construct the binary tree. + +Note: +You may assume that duplicates do not exist in the tree. + + ## 解题思路 diff --git a/Algorithms/0107.binary-tree-level-order-traversal-ii/README.md b/Algorithms/0107.binary-tree-level-order-traversal-ii/README.md index 572ba0c55..d81f3478a 100755 --- a/Algorithms/0107.binary-tree-level-order-traversal-ii/README.md +++ b/Algorithms/0107.binary-tree-level-order-traversal-ii/README.md @@ -2,6 +2,28 @@ ## 题目 + Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). + + +For example: +Given binary tree [3,9,20,null,null,15,7], + 3 + / \ + 9 20 + / \ + 15 7 + + + +return its bottom-up level order traversal as: +[ + [15,7], + [9,20], + [3] +] + + + ## 解题思路 diff --git a/Algorithms/0108.convert-sorted-array-to-binary-search-tree/README.md b/Algorithms/0108.convert-sorted-array-to-binary-search-tree/README.md index 54e5c1323..9856b625d 100755 --- a/Algorithms/0108.convert-sorted-array-to-binary-search-tree/README.md +++ b/Algorithms/0108.convert-sorted-array-to-binary-search-tree/README.md @@ -2,6 +2,8 @@ ## 题目 + Given an array where elements are sorted in ascending order, convert it to a height balanced BST. + ## 解题思路 diff --git a/Algorithms/0109.convert-sorted-list-to-binary-search-tree/README.md b/Algorithms/0109.convert-sorted-list-to-binary-search-tree/README.md index 45c2975d9..11ea301bf 100755 --- a/Algorithms/0109.convert-sorted-list-to-binary-search-tree/README.md +++ b/Algorithms/0109.convert-sorted-list-to-binary-search-tree/README.md @@ -2,6 +2,8 @@ ## 题目 + Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. + ## 解题思路 diff --git a/Algorithms/0110.balanced-binary-tree/README.md b/Algorithms/0110.balanced-binary-tree/README.md index 18de4cbed..8055b1006 100755 --- a/Algorithms/0110.balanced-binary-tree/README.md +++ b/Algorithms/0110.balanced-binary-tree/README.md @@ -2,6 +2,13 @@ ## 题目 + Given a binary tree, determine if it is height-balanced. + + + +For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. + + ## 解题思路 diff --git a/Algorithms/0111.minimum-depth-of-binary-tree/README.md b/Algorithms/0111.minimum-depth-of-binary-tree/README.md index 3da581908..fe0c9fb1f 100755 --- a/Algorithms/0111.minimum-depth-of-binary-tree/README.md +++ b/Algorithms/0111.minimum-depth-of-binary-tree/README.md @@ -2,6 +2,10 @@ ## 题目 + Given a binary tree, find its minimum depth. + +The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. + ## 解题思路 diff --git a/Algorithms/0112.path-sum/README.md b/Algorithms/0112.path-sum/README.md index b225333b8..389a1f68e 100755 --- a/Algorithms/0112.path-sum/README.md +++ b/Algorithms/0112.path-sum/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. + + +For example: +Given the below binary tree and sum = 22, + 5 + / \ + 4 8 + / / \ + 11 13 4 + / \ \ + 7 2 1 + + + +return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22. + ## 解题思路 diff --git a/Algorithms/0113.path-sum-ii/README.md b/Algorithms/0113.path-sum-ii/README.md index db80c79e8..d5c68d4ad 100755 --- a/Algorithms/0113.path-sum-ii/README.md +++ b/Algorithms/0113.path-sum-ii/README.md @@ -2,6 +2,30 @@ ## 题目 + +Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. + + +For example: +Given the below binary tree and sum = 22, + 5 + / \ + 4 8 + / / \ + 11 13 4 + / \ / \ + 7 2 5 1 + + + +return +[ + [5,4,11,2], + [5,8,4,5] +] + + + ## 解题思路 diff --git a/Algorithms/0114.flatten-binary-tree-to-linked-list/README.md b/Algorithms/0114.flatten-binary-tree-to-linked-list/README.md index 1156c9c67..68f8085f9 100755 --- a/Algorithms/0114.flatten-binary-tree-to-linked-list/README.md +++ b/Algorithms/0114.flatten-binary-tree-to-linked-list/README.md @@ -2,6 +2,41 @@ ## 题目 + +Given a binary tree, flatten it to a linked list in-place. + + + +For example, +Given + 1 + / \ + 2 5 + / \ \ + 3 4 6 + + + +The flattened tree should look like: + 1 + \ + 2 + \ + 3 + \ + 4 + \ + 5 + \ + 6 + + +click to show hints. + +Hints: +If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. + + ## 解题思路 diff --git a/Algorithms/0115.distinct-subsequences/README.md b/Algorithms/0115.distinct-subsequences/README.md index 75214412b..9330d4c35 100755 --- a/Algorithms/0115.distinct-subsequences/README.md +++ b/Algorithms/0115.distinct-subsequences/README.md @@ -2,6 +2,22 @@ ## 题目 + +Given a string S and a string T, count the number of distinct subsequences of S which equals T. + + + +A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not). + + + +Here is an example: +S = "rabbbit", T = "rabbit" + + +Return 3. + + ## 解题思路 diff --git a/Algorithms/0116.populating-next-right-pointers-in-each-node/README.md b/Algorithms/0116.populating-next-right-pointers-in-each-node/README.md index 122a4a71b..605ebc444 100755 --- a/Algorithms/0116.populating-next-right-pointers-in-each-node/README.md +++ b/Algorithms/0116.populating-next-right-pointers-in-each-node/README.md @@ -2,6 +2,48 @@ ## 题目 + +Given a binary tree + struct TreeLinkNode { + TreeLinkNode *left; + TreeLinkNode *right; + TreeLinkNode *next; + } + + + +Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. + +Initially, all next pointers are set to NULL. + + +Note: + +You may only use constant extra space. +You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). + + + + +For example, +Given the following perfect binary tree, + 1 + / \ + 2 3 + / \ / \ + 4 5 6 7 + + + +After calling your function, the tree should look like: + 1 -> NULL + / \ + 2 -> 3 -> NULL + / \ / \ + 4->5->6->7 -> NULL + + + ## 解题思路 diff --git a/Algorithms/0117.populating-next-right-pointers-in-each-node-ii/README.md b/Algorithms/0117.populating-next-right-pointers-in-each-node-ii/README.md index 45cea73d3..5de294612 100755 --- a/Algorithms/0117.populating-next-right-pointers-in-each-node-ii/README.md +++ b/Algorithms/0117.populating-next-right-pointers-in-each-node-ii/README.md @@ -2,6 +2,32 @@ ## 题目 + Follow up for problem "Populating Next Right Pointers in Each Node". +What if the given tree could be any binary tree? Would your previous solution still work? + +Note: +You may only use constant extra space. + + +For example, +Given the following binary tree, + 1 + / \ + 2 3 + / \ \ + 4 5 7 + + + +After calling your function, the tree should look like: + 1 -> NULL + / \ + 2 -> 3 -> NULL + / \ \ + 4-> 5 -> 7 -> NULL + + + ## 解题思路 diff --git a/Algorithms/0119.pascals-triangle-ii/README.md b/Algorithms/0119.pascals-triangle-ii/README.md index 94418051f..ba5bc60d1 100755 --- a/Algorithms/0119.pascals-triangle-ii/README.md +++ b/Algorithms/0119.pascals-triangle-ii/README.md @@ -2,6 +2,18 @@ ## 题目 + Given an index k, return the kth row of the Pascal's triangle. + + +For example, given k = 3, +Return [1,3,3,1]. + + + +Note: +Could you optimize your algorithm to use only O(k) extra space? + + ## 解题思路 diff --git a/Algorithms/0120.triangle/README.md b/Algorithms/0120.triangle/README.md index 7486c0d8b..88bd0b904 100755 --- a/Algorithms/0120.triangle/README.md +++ b/Algorithms/0120.triangle/README.md @@ -2,6 +2,27 @@ ## 题目 + Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. + + +For example, given the following triangle +[ + [2], + [3,4], + [6,5,7], + [4,1,8,3] +] + + + +The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). + + + +Note: +Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. + + ## 解题思路 diff --git a/Algorithms/0121.best-time-to-buy-and-sell-stock/README.md b/Algorithms/0121.best-time-to-buy-and-sell-stock/README.md index ccdade85a..f0c855247 100755 --- a/Algorithms/0121.best-time-to-buy-and-sell-stock/README.md +++ b/Algorithms/0121.best-time-to-buy-and-sell-stock/README.md @@ -2,6 +2,26 @@ ## 题目 + Say you have an array for which the ith element is the price of a given stock on day i. + +If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. + +Example 1: +Input: [7, 1, 5, 3, 6, 4] +Output: 5 + +max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price) + + + +Example 2: +Input: [7, 6, 4, 3, 1] +Output: 0 + +In this case, no transaction is done, i.e. max profit = 0. + + + ## 解题思路 diff --git a/Algorithms/0122.best-time-to-buy-and-sell-stock-ii/README.md b/Algorithms/0122.best-time-to-buy-and-sell-stock-ii/README.md index 7d71b6c21..ab192f050 100755 --- a/Algorithms/0122.best-time-to-buy-and-sell-stock-ii/README.md +++ b/Algorithms/0122.best-time-to-buy-and-sell-stock-ii/README.md @@ -2,6 +2,10 @@ ## 题目 + Say you have an array for which the ith element is the price of a given stock on day i. + +Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). + ## 解题思路 diff --git a/Algorithms/0123.best-time-to-buy-and-sell-stock-iii/README.md b/Algorithms/0123.best-time-to-buy-and-sell-stock-iii/README.md index 7577af7ed..700bc0eba 100755 --- a/Algorithms/0123.best-time-to-buy-and-sell-stock-iii/README.md +++ b/Algorithms/0123.best-time-to-buy-and-sell-stock-iii/README.md @@ -2,6 +2,13 @@ ## 题目 + Say you have an array for which the ith element is the price of a given stock on day i. + +Design an algorithm to find the maximum profit. You may complete at most two transactions. + +Note: +You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). + ## 解题思路 diff --git a/Algorithms/0124.binary-tree-maximum-path-sum/README.md b/Algorithms/0124.binary-tree-maximum-path-sum/README.md index 24d908c7d..f779e5cc9 100755 --- a/Algorithms/0124.binary-tree-maximum-path-sum/README.md +++ b/Algorithms/0124.binary-tree-maximum-path-sum/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given a binary tree, find the maximum path sum. + + +For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. + + +For example: +Given the below binary tree, + 1 + / \ + 2 3 + + + +Return 6. + + ## 解题思路 diff --git a/Algorithms/0125.valid-palindrome/README.md b/Algorithms/0125.valid-palindrome/README.md index 1fef57000..b655121ed 100755 --- a/Algorithms/0125.valid-palindrome/README.md +++ b/Algorithms/0125.valid-palindrome/README.md @@ -2,6 +2,23 @@ ## 题目 + +Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. + + + +For example, +"A man, a plan, a canal: Panama" is a palindrome. +"race a car" is not a palindrome. + + + +Note: +Have you consider that the string might be empty? This is a good question to ask during an interview. + +For the purpose of this problem, we define empty string as valid palindrome. + + ## 解题思路 diff --git a/Algorithms/0126.word-ladder-ii/README.md b/Algorithms/0126.word-ladder-ii/README.md index 24fdfe517..6406719a9 100755 --- a/Algorithms/0126.word-ladder-ii/README.md +++ b/Algorithms/0126.word-ladder-ii/README.md @@ -2,6 +2,48 @@ ## 题目 + +Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: + + +Only one letter can be changed at a time +Each transformed word must exist in the word list. Note that beginWord is not a transformed word. + + + +For example, + + +Given: +beginWord = "hit" +endWord = "cog" +wordList = ["hot","dot","dog","lot","log","cog"] + + +Return + [ + ["hit","hot","dot","dog","cog"], + ["hit","hot","lot","log","cog"] + ] + + + + +Note: + +Return an empty list if there is no such transformation sequence. +All words have the same length. +All words contain only lowercase alphabetic characters. +You may assume no duplicates in the word list. +You may assume beginWord and endWord are non-empty and are not the same. + + + + +UPDATE (2017/1/20): +The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes. + + ## 解题思路 diff --git a/Algorithms/0127.word-ladder/README.md b/Algorithms/0127.word-ladder/README.md index 91e234cfc..8b22dd902 100755 --- a/Algorithms/0127.word-ladder/README.md +++ b/Algorithms/0127.word-ladder/README.md @@ -2,6 +2,44 @@ ## 题目 + +Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: + + +Only one letter can be changed at a time. +Each transformed word must exist in the word list. Note that beginWord is not a transformed word. + + + +For example, + + +Given: +beginWord = "hit" +endWord = "cog" +wordList = ["hot","dot","dog","lot","log","cog"] + + +As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", +return its length 5. + + + +Note: + +Return 0 if there is no such transformation sequence. +All words have the same length. +All words contain only lowercase alphabetic characters. +You may assume no duplicates in the word list. +You may assume beginWord and endWord are non-empty and are not the same. + + + + +UPDATE (2017/1/20): +The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes. + + ## 解题思路 diff --git a/Algorithms/0128.longest-consecutive-sequence/README.md b/Algorithms/0128.longest-consecutive-sequence/README.md index dec1b057d..a20f04459 100755 --- a/Algorithms/0128.longest-consecutive-sequence/README.md +++ b/Algorithms/0128.longest-consecutive-sequence/README.md @@ -2,6 +2,18 @@ ## 题目 + +Given an unsorted array of integers, find the length of the longest consecutive elements sequence. + + +For example, +Given [100, 4, 200, 1, 3, 2], +The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. + + +Your algorithm should run in O(n) complexity. + + ## 解题思路 diff --git a/Algorithms/0129.sum-root-to-leaf-numbers/README.md b/Algorithms/0129.sum-root-to-leaf-numbers/README.md index ea61bd097..3542bcdcf 100755 --- a/Algorithms/0129.sum-root-to-leaf-numbers/README.md +++ b/Algorithms/0129.sum-root-to-leaf-numbers/README.md @@ -2,6 +2,25 @@ ## 题目 + Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. +An example is the root-to-leaf path 1->2->3 which represents the number 123. + +Find the total sum of all root-to-leaf numbers. + +For example, + 1 + / \ + 2 3 + + + +The root-to-leaf path 1->2 represents the number 12. +The root-to-leaf path 1->3 represents the number 13. + + +Return the sum = 12 + 13 = 25. + + ## 解题思路 diff --git a/Algorithms/0130.surrounded-regions/README.md b/Algorithms/0130.surrounded-regions/README.md index 8c74ffd30..046d588d8 100755 --- a/Algorithms/0130.surrounded-regions/README.md +++ b/Algorithms/0130.surrounded-regions/README.md @@ -2,6 +2,30 @@ ## 题目 + +Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. + +A region is captured by flipping all 'O's into 'X's in that surrounded region. + + + +For example, +X X X X +X O O X +X X O X +X O X X + + + + +After running your function, the board should be: +X X X X +X X X X +X X X X +X O X X + + + ## 解题思路 diff --git a/Algorithms/0131.palindrome-partitioning/README.md b/Algorithms/0131.palindrome-partitioning/README.md index a59c8194e..7684f2fac 100755 --- a/Algorithms/0131.palindrome-partitioning/README.md +++ b/Algorithms/0131.palindrome-partitioning/README.md @@ -2,6 +2,23 @@ ## 题目 + +Given a string s, partition s such that every substring of the partition is a palindrome. + + +Return all possible palindrome partitioning of s. + + +For example, given s = "aab", + +Return +[ + ["aa","b"], + ["a","a","b"] +] + + + ## 解题思路 diff --git a/Algorithms/0132.palindrome-partitioning-ii/README.md b/Algorithms/0132.palindrome-partitioning-ii/README.md index 2e89c19df..6070cd86b 100755 --- a/Algorithms/0132.palindrome-partitioning-ii/README.md +++ b/Algorithms/0132.palindrome-partitioning-ii/README.md @@ -2,6 +2,17 @@ ## 题目 + +Given a string s, partition s such that every substring of the partition is a palindrome. + + +Return the minimum cuts needed for a palindrome partitioning of s. + + +For example, given s = "aab", +Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut. + + ## 解题思路 diff --git a/Algorithms/0133.clone-graph/README.md b/Algorithms/0133.clone-graph/README.md index b5e21b731..b9da26e56 100755 --- a/Algorithms/0133.clone-graph/README.md +++ b/Algorithms/0133.clone-graph/README.md @@ -2,6 +2,48 @@ ## 题目 + +Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. + + + + +OJ's undirected graph serialization: + + +Nodes are labeled uniquely. + + +We use # as a separator for each node, and , as a separator for node label and each neighbor of the node. + + + + +As an example, consider the serialized graph {0,1,2#1,2#2,2}. + + + +The graph has a total of three nodes, and therefore contains three parts as separated by #. + +First node is labeled as 0. Connect node 0 to both nodes 1 and 2. +Second node is labeled as 1. Connect node 1 to node 2. +Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle. + + + + +Visually, the graph looks like the following: + 1 + / \ + / \ + 0 --- 2 + / \ + \_/ + + + + + ## 解题思路 diff --git a/Algorithms/0134.gas-station/README.md b/Algorithms/0134.gas-station/README.md index 151c498b7..916eb0a42 100755 --- a/Algorithms/0134.gas-station/README.md +++ b/Algorithms/0134.gas-station/README.md @@ -2,6 +2,23 @@ ## 题目 + +There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. + + + +You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. + + + +Return the starting gas station's index if you can travel around the circuit once, otherwise return -1. + + + +Note: +The solution is guaranteed to be unique. + + ## 解题思路 diff --git a/Algorithms/0135.candy/README.md b/Algorithms/0135.candy/README.md index 10eb761e8..14381d924 100755 --- a/Algorithms/0135.candy/README.md +++ b/Algorithms/0135.candy/README.md @@ -2,6 +2,20 @@ ## 题目 + +There are N children standing in a line. Each child is assigned a rating value. + + +You are giving candies to these children subjected to the following requirements: + + +Each child must have at least one candy. +Children with a higher rating get more candies than their neighbors. + + +What is the minimum candies you must give? + + ## 解题思路 diff --git a/Algorithms/0136.single-number/README.md b/Algorithms/0136.single-number/README.md index 1d3f1c816..14ec7a009 100755 --- a/Algorithms/0136.single-number/README.md +++ b/Algorithms/0136.single-number/README.md @@ -2,6 +2,13 @@ ## 题目 + Given an array of integers, every element appears twice except for one. Find that single one. + + +Note: +Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? + + ## 解题思路 diff --git a/Algorithms/0137.single-number-ii/README.md b/Algorithms/0137.single-number-ii/README.md index b20588e13..88b76a342 100755 --- a/Algorithms/0137.single-number-ii/README.md +++ b/Algorithms/0137.single-number-ii/README.md @@ -2,6 +2,15 @@ ## 题目 + +Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. + + + +Note: +Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? + + ## 解题思路 diff --git a/Algorithms/0138.copy-list-with-random-pointer/README.md b/Algorithms/0138.copy-list-with-random-pointer/README.md index dc4d1c063..e726d8648 100755 --- a/Algorithms/0138.copy-list-with-random-pointer/README.md +++ b/Algorithms/0138.copy-list-with-random-pointer/README.md @@ -2,6 +2,14 @@ ## 题目 + +A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. + + + +Return a deep copy of the list. + + ## 解题思路 diff --git a/Algorithms/0139.word-break/README.md b/Algorithms/0139.word-break/README.md index 7ccf54488..4b75a6311 100755 --- a/Algorithms/0139.word-break/README.md +++ b/Algorithms/0139.word-break/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words. + + +For example, given +s = "leetcode", +dict = ["leet", "code"]. + + + +Return true because "leetcode" can be segmented as "leet code". + + + +UPDATE (2017/1/4): +The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes. + + ## 解题思路 diff --git a/Algorithms/0140.word-break-ii/README.md b/Algorithms/0140.word-break-ii/README.md index df5d9799f..2622053b6 100755 --- a/Algorithms/0140.word-break-ii/README.md +++ b/Algorithms/0140.word-break-ii/README.md @@ -2,6 +2,29 @@ ## 题目 + +Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words. + + + +Return all such possible sentences. + + + +For example, given +s = "catsanddog", +dict = ["cat", "cats", "and", "sand", "dog"]. + + + +A solution is ["cats and dog", "cat sand dog"]. + + + +UPDATE (2017/1/4): +The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes. + + ## 解题思路 diff --git a/Algorithms/0141.linked-list-cycle/README.md b/Algorithms/0141.linked-list-cycle/README.md index 52b981814..d3d5b93d3 100755 --- a/Algorithms/0141.linked-list-cycle/README.md +++ b/Algorithms/0141.linked-list-cycle/README.md @@ -2,6 +2,15 @@ ## 题目 + +Given a linked list, determine if it has a cycle in it. + + + +Follow up: +Can you solve it without using extra space? + + ## 解题思路 diff --git a/Algorithms/0142.linked-list-cycle-ii/README.md b/Algorithms/0142.linked-list-cycle-ii/README.md index 33c05dea4..bc15f4cf8 100755 --- a/Algorithms/0142.linked-list-cycle-ii/README.md +++ b/Algorithms/0142.linked-list-cycle-ii/README.md @@ -2,6 +2,18 @@ ## 题目 + +Given a linked list, return the node where the cycle begins. If there is no cycle, return null. + + + +Note: Do not modify the linked list. + + +Follow up: +Can you solve it without using extra space? + + ## 解题思路 diff --git a/Algorithms/0143.reorder-list/README.md b/Algorithms/0143.reorder-list/README.md index c1aecf1f6..6940b38c7 100755 --- a/Algorithms/0143.reorder-list/README.md +++ b/Algorithms/0143.reorder-list/README.md @@ -2,6 +2,18 @@ ## 题目 + +Given a singly linked list L: L0?L1?…?Ln-1?Ln, +reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?… + + +You must do this in-place without altering the nodes' values. + + +For example, +Given {1,2,3,4}, reorder it to {1,4,2,3}. + + ## 解题思路 diff --git a/Algorithms/0144.binary-tree-preorder-traversal/README.md b/Algorithms/0144.binary-tree-preorder-traversal/README.md index 42878210f..6ce1ca7a8 100755 --- a/Algorithms/0144.binary-tree-preorder-traversal/README.md +++ b/Algorithms/0144.binary-tree-preorder-traversal/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a binary tree, return the preorder traversal of its nodes' values. + + +For example: +Given binary tree {1,#,2,3}, + 1 + \ + 2 + / + 3 + + + +return [1,2,3]. + + +Note: Recursive solution is trivial, could you do it iteratively? + ## 解题思路 diff --git a/Algorithms/0145.binary-tree-postorder-traversal/README.md b/Algorithms/0145.binary-tree-postorder-traversal/README.md index b4c812eb5..e2943da70 100755 --- a/Algorithms/0145.binary-tree-postorder-traversal/README.md +++ b/Algorithms/0145.binary-tree-postorder-traversal/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a binary tree, return the postorder traversal of its nodes' values. + + +For example: +Given binary tree {1,#,2,3}, + 1 + \ + 2 + / + 3 + + + +return [3,2,1]. + + +Note: Recursive solution is trivial, could you do it iteratively? + ## 解题思路 diff --git a/Algorithms/0146.lru-cache/README.md b/Algorithms/0146.lru-cache/README.md index f5eb745cf..ef607dfa0 100755 --- a/Algorithms/0146.lru-cache/README.md +++ b/Algorithms/0146.lru-cache/README.md @@ -2,6 +2,33 @@ ## 题目 + +Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. + + + +get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. +put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. + + +Follow up: +Could you do both operations in O(1) time complexity? + +Example: +LRUCache cache = new LRUCache( 2 /* capacity */ ); + +cache.put(1, 1); +cache.put(2, 2); +cache.get(1); // returns 1 +cache.put(3, 3); // evicts key 2 +cache.get(2); // returns -1 (not found) +cache.put(4, 4); // evicts key 1 +cache.get(1); // returns -1 (not found) +cache.get(3); // returns 3 +cache.get(4); // returns 4 + + + ## 解题思路 diff --git a/Algorithms/0147.insertion-sort-list/README.md b/Algorithms/0147.insertion-sort-list/README.md index f152bc392..5784b450b 100755 --- a/Algorithms/0147.insertion-sort-list/README.md +++ b/Algorithms/0147.insertion-sort-list/README.md @@ -2,6 +2,8 @@ ## 题目 + Sort a linked list using insertion sort. + ## 解题思路 diff --git a/Algorithms/0148.sort-list/README.md b/Algorithms/0148.sort-list/README.md index 2f5d297ab..9a12836af 100755 --- a/Algorithms/0148.sort-list/README.md +++ b/Algorithms/0148.sort-list/README.md @@ -2,6 +2,8 @@ ## 题目 + Sort a linked list in O(n log n) time using constant space complexity. + ## 解题思路 diff --git a/Algorithms/0149.max-points-on-a-line/README.md b/Algorithms/0149.max-points-on-a-line/README.md index 89d506287..4c5633c38 100755 --- a/Algorithms/0149.max-points-on-a-line/README.md +++ b/Algorithms/0149.max-points-on-a-line/README.md @@ -2,6 +2,8 @@ ## 题目 + Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. + ## 解题思路 diff --git a/Algorithms/0150.evaluate-reverse-polish-notation/README.md b/Algorithms/0150.evaluate-reverse-polish-notation/README.md index f1423f3ed..9fd82694a 100755 --- a/Algorithms/0150.evaluate-reverse-polish-notation/README.md +++ b/Algorithms/0150.evaluate-reverse-polish-notation/README.md @@ -2,6 +2,21 @@ ## 题目 + +Evaluate the value of an arithmetic expression in Reverse Polish Notation. + + + +Valid operators are +, -, *, /. Each operand may be an integer or another expression. + + + +Some examples: + ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 + ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 + + + ## 解题思路 diff --git a/Algorithms/0151.reverse-words-in-a-string/README.md b/Algorithms/0151.reverse-words-in-a-string/README.md index 2a3de1efc..299c5432d 100755 --- a/Algorithms/0151.reverse-words-in-a-string/README.md +++ b/Algorithms/0151.reverse-words-in-a-string/README.md @@ -2,6 +2,37 @@ ## 题目 + +Given an input string, reverse the string word by word. + + + +For example, +Given s = "the sky is blue", +return "blue is sky the". + + + +Update (2015-02-12): +For C programmers: Try to solve it in-place in O(1) space. + + +click to show clarification. + +Clarification: + + + +What constitutes a word? +A sequence of non-space characters constitutes a word. +Could the input string contain leading or trailing spaces? +Yes. However, your reversed string should not contain leading or trailing spaces. +How about multiple spaces between two words? +Reduce them to a single space in the reversed string. + + + + ## 解题思路 diff --git a/Algorithms/0152.maximum-product-subarray/README.md b/Algorithms/0152.maximum-product-subarray/README.md index 529693217..d8acfe203 100755 --- a/Algorithms/0152.maximum-product-subarray/README.md +++ b/Algorithms/0152.maximum-product-subarray/README.md @@ -2,6 +2,15 @@ ## 题目 + +Find the contiguous subarray within an array (containing at least one number) which has the largest product. + + + +For example, given the array [2,3,-2,4], +the contiguous subarray [2,3] has the largest product = 6. + + ## 解题思路 diff --git a/Algorithms/0153.find-minimum-in-rotated-sorted-array/README.md b/Algorithms/0153.find-minimum-in-rotated-sorted-array/README.md index a21a6be6f..cb701a082 100755 --- a/Algorithms/0153.find-minimum-in-rotated-sorted-array/README.md +++ b/Algorithms/0153.find-minimum-in-rotated-sorted-array/README.md @@ -2,6 +2,14 @@ ## 题目 + Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. + +(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). + +Find the minimum element. + +You may assume no duplicate exists in the array. + ## 解题思路 diff --git a/Algorithms/0154.find-minimum-in-rotated-sorted-array-ii/README.md b/Algorithms/0154.find-minimum-in-rotated-sorted-array-ii/README.md index 00e5e7d5b..afe392c06 100755 --- a/Algorithms/0154.find-minimum-in-rotated-sorted-array-ii/README.md +++ b/Algorithms/0154.find-minimum-in-rotated-sorted-array-ii/README.md @@ -2,6 +2,21 @@ ## 题目 + +Follow up for "Find Minimum in Rotated Sorted Array": +What if duplicates are allowed? + +Would this affect the run-time complexity? How and why? + + +Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. + +(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). + +Find the minimum element. + +The array may contain duplicates. + ## 解题思路 diff --git a/Algorithms/0155.min-stack/README.md b/Algorithms/0155.min-stack/README.md index 27f55acba..c5243a68e 100755 --- a/Algorithms/0155.min-stack/README.md +++ b/Algorithms/0155.min-stack/README.md @@ -2,6 +2,36 @@ ## 题目 + +Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. + + +push(x) -- Push element x onto stack. + + +pop() -- Removes the element on top of the stack. + + +top() -- Get the top element. + + +getMin() -- Retrieve the minimum element in the stack. + + + + +Example: +MinStack minStack = new MinStack(); +minStack.push(-2); +minStack.push(0); +minStack.push(-3); +minStack.getMin(); --> Returns -3. +minStack.pop(); +minStack.top(); --> Returns 0. +minStack.getMin(); --> Returns -2. + + + ## 解题思路 diff --git a/Algorithms/0160.intersection-of-two-linked-lists/README.md b/Algorithms/0160.intersection-of-two-linked-lists/README.md index c26153f0a..c75dba7fb 100755 --- a/Algorithms/0160.intersection-of-two-linked-lists/README.md +++ b/Algorithms/0160.intersection-of-two-linked-lists/README.md @@ -2,6 +2,28 @@ ## 题目 + Write a program to find the node at which the intersection of two singly linked lists begins. + +For example, the following two linked lists: +A: a1 → a2 + ↘ + c1 → c2 → c3 + ↗ +B: b1 → b2 → b3 + +begin to intersect at node c1. + +Notes: + +If the two linked lists have no intersection at all, return null. +The linked lists must retain their original structure after the function returns. +You may assume there are no cycles anywhere in the entire linked structure. +Your code should preferably run in O(n) time and use only O(1) memory. + + + +Credits:Special thanks to @stellari for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0162.find-peak-element/README.md b/Algorithms/0162.find-peak-element/README.md index c2ce69fcc..bfe1c9dbd 100755 --- a/Algorithms/0162.find-peak-element/README.md +++ b/Algorithms/0162.find-peak-element/README.md @@ -2,6 +2,24 @@ ## 题目 + A peak element is an element that is greater than its neighbors. + +Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. + +The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. + +You may imagine that num[-1] = num[n] = -∞. + +For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2. + +click to show spoilers. + +Note: +Your solution should be in logarithmic complexity. + + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0164.maximum-gap/README.md b/Algorithms/0164.maximum-gap/README.md index c7b676a78..05c2889c9 100755 --- a/Algorithms/0164.maximum-gap/README.md +++ b/Algorithms/0164.maximum-gap/README.md @@ -2,6 +2,16 @@ ## 题目 + Given an unsorted array, find the maximum difference between the successive elements in its sorted form. + +Try to solve it in linear time/space. + +Return 0 if the array contains less than 2 elements. + +You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. + +Credits:Special thanks to @porker2008 for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0165.compare-version-numbers/README.md b/Algorithms/0165.compare-version-numbers/README.md index 97b86dd88..2c78a041f 100755 --- a/Algorithms/0165.compare-version-numbers/README.md +++ b/Algorithms/0165.compare-version-numbers/README.md @@ -2,6 +2,18 @@ ## 题目 + Compare two version numbers version1 and version2. +If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. + +You may assume that the version strings are non-empty and contain only digits and the . character. +The . character does not represent a decimal point and is used to separate number sequences. +For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision. + +Here is an example of version numbers ordering: +0.1 < 1.1 < 1.2 < 13.37 + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0166.fraction-to-recurring-decimal/README.md b/Algorithms/0166.fraction-to-recurring-decimal/README.md index 325a0b9aa..0f6909440 100755 --- a/Algorithms/0166.fraction-to-recurring-decimal/README.md +++ b/Algorithms/0166.fraction-to-recurring-decimal/README.md @@ -2,6 +2,20 @@ ## 题目 + Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. + +If the fractional part is repeating, enclose the repeating part in parentheses. + +For example, + +Given numerator = 1, denominator = 2, return "0.5". +Given numerator = 2, denominator = 1, return "2". +Given numerator = 2, denominator = 3, return "0.(6)". + + + +Credits:Special thanks to @Shangrila for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0167.two-sum-ii-input-array-is-sorted/README.md b/Algorithms/0167.two-sum-ii-input-array-is-sorted/README.md index fc600d248..a88e02257 100755 --- a/Algorithms/0167.two-sum-ii-input-array-is-sorted/README.md +++ b/Algorithms/0167.two-sum-ii-input-array-is-sorted/README.md @@ -2,6 +2,17 @@ ## 题目 + Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. + +The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. + +You may assume that each input would have exactly one solution and you may not use the same element twice. + + +Input: numbers={2, 7, 11, 15}, target=9 +Output: index1=1, index2=2 + + ## 解题思路 diff --git a/Algorithms/0168.excel-sheet-column-title/README.md b/Algorithms/0168.excel-sheet-column-title/README.md index bd10b5a96..167a8562f 100755 --- a/Algorithms/0168.excel-sheet-column-title/README.md +++ b/Algorithms/0168.excel-sheet-column-title/README.md @@ -2,6 +2,19 @@ ## 题目 + Given a positive integer, return its corresponding column title as appear in an Excel sheet. + +For example: + 1 -> A + 2 -> B + 3 -> C + ... + 26 -> Z + 27 -> AA + 28 -> AB + +Credits:Special thanks to @ifanchu for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0169.majority-element/README.md b/Algorithms/0169.majority-element/README.md index ba4c86e57..16fb458d8 100755 --- a/Algorithms/0169.majority-element/README.md +++ b/Algorithms/0169.majority-element/README.md @@ -2,6 +2,12 @@ ## 题目 + Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. + +You may assume that the array is non-empty and the majority element always exist in the array. + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0171.excel-sheet-column-number/README.md b/Algorithms/0171.excel-sheet-column-number/README.md index 25f24e582..7daaadd59 100755 --- a/Algorithms/0171.excel-sheet-column-number/README.md +++ b/Algorithms/0171.excel-sheet-column-number/README.md @@ -2,6 +2,20 @@ ## 题目 + Related to question Excel Sheet Column Title +Given a column title as appear in an Excel sheet, return its corresponding column number. + +For example: + A -> 1 + B -> 2 + C -> 3 + ... + Z -> 26 + AA -> 27 + AB -> 28 + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0172.factorial-trailing-zeroes/README.md b/Algorithms/0172.factorial-trailing-zeroes/README.md index 9db2966ac..c2cc8ea5c 100755 --- a/Algorithms/0172.factorial-trailing-zeroes/README.md +++ b/Algorithms/0172.factorial-trailing-zeroes/README.md @@ -2,6 +2,12 @@ ## 题目 + Given an integer n, return the number of trailing zeroes in n!. + +Note: Your solution should be in logarithmic time complexity. + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0173.binary-search-tree-iterator/README.md b/Algorithms/0173.binary-search-tree-iterator/README.md index 2ea169888..2a4f57243 100755 --- a/Algorithms/0173.binary-search-tree-iterator/README.md +++ b/Algorithms/0173.binary-search-tree-iterator/README.md @@ -2,6 +2,14 @@ ## 题目 + Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. + +Calling next() will return the next smallest number in the BST. + +Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0174.dungeon-game/README.md b/Algorithms/0174.dungeon-game/README.md index b32027ede..4a98a6e2c 100755 --- a/Algorithms/0174.dungeon-game/README.md +++ b/Algorithms/0174.dungeon-game/README.md @@ -2,6 +2,57 @@ ## 题目 + +table.dungeon, .dungeon th, .dungeon td { + border:3px solid black; +} + + .dungeon th, .dungeon td { + text-align: center; + height: 70px; + width: 70px; +} + + +The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess. +The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately. +Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; +other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers). +In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step. + + +Write a function to determine the knight's minimum initial health so that he is able to rescue the princess. +For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN. + + + +-2 (K) +-3 +3 + + +-5 +-10 +1 + + +10 +30 +-5 (P) + + + + + +Notes: + +The knight's health has no upper bound. +Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned. + + + +Credits:Special thanks to @stellari for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0179.largest-number/README.md b/Algorithms/0179.largest-number/README.md index ff565618d..a15451786 100755 --- a/Algorithms/0179.largest-number/README.md +++ b/Algorithms/0179.largest-number/README.md @@ -2,6 +2,14 @@ ## 题目 + Given a list of non negative integers, arrange them such that they form the largest number. + +For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. + +Note: The result may be very large, so you need to return a string instead of an integer. + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0187.repeated-dna-sequences/README.md b/Algorithms/0187.repeated-dna-sequences/README.md index 0410f21de..d801abfed 100755 --- a/Algorithms/0187.repeated-dna-sequences/README.md +++ b/Algorithms/0187.repeated-dna-sequences/README.md @@ -2,6 +2,19 @@ ## 题目 + +All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. + +Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. + + +For example, +Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", + +Return: +["AAAAACCCCC", "CCCCCAAAAA"]. + + ## 解题思路 diff --git a/Algorithms/0188.best-time-to-buy-and-sell-stock-iv/README.md b/Algorithms/0188.best-time-to-buy-and-sell-stock-iv/README.md index 145170b82..5768a84fd 100755 --- a/Algorithms/0188.best-time-to-buy-and-sell-stock-iv/README.md +++ b/Algorithms/0188.best-time-to-buy-and-sell-stock-iv/README.md @@ -2,6 +2,15 @@ ## 题目 + Say you have an array for which the ith element is the price of a given stock on day i. + +Design an algorithm to find the maximum profit. You may complete at most k transactions. + +Note: +You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). + +Credits:Special thanks to @Freezen for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0189.rotate-array/README.md b/Algorithms/0189.rotate-array/README.md index 0820c0c68..a1056fdc6 100755 --- a/Algorithms/0189.rotate-array/README.md +++ b/Algorithms/0189.rotate-array/README.md @@ -2,6 +2,22 @@ ## 题目 + Rotate an array of n elements to the right by k steps. +For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. + +Note: +Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. + + +[show hint] +Hint: +Could you do it in-place with O(1) extra space? + + +Related problem: Reverse Words in a String II + +Credits:Special thanks to @Freezen for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0190.reverse-bits/README.md b/Algorithms/0190.reverse-bits/README.md index 8ba76e4ed..c5892c8d4 100755 --- a/Algorithms/0190.reverse-bits/README.md +++ b/Algorithms/0190.reverse-bits/README.md @@ -2,6 +2,19 @@ ## 题目 + Reverse bits of a given 32 bits unsigned integer. + +For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). + + +Follow up: +If this function is called many times, how would you optimize it? + + +Related problem: Reverse Integer + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0191.number-of-1-bits/README.md b/Algorithms/0191.number-of-1-bits/README.md index be86a68a7..4c252a2da 100755 --- a/Algorithms/0191.number-of-1-bits/README.md +++ b/Algorithms/0191.number-of-1-bits/README.md @@ -2,6 +2,12 @@ ## 题目 + Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). + +For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3. + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0198.house-robber/README.md b/Algorithms/0198.house-robber/README.md index bbdf136d9..93504290d 100755 --- a/Algorithms/0198.house-robber/README.md +++ b/Algorithms/0198.house-robber/README.md @@ -2,6 +2,12 @@ ## 题目 + You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. + +Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police. + +Credits:Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases. + ## 解题思路 diff --git a/Algorithms/0199.binary-tree-right-side-view/README.md b/Algorithms/0199.binary-tree-right-side-view/README.md index 5c7c3e972..bec53d46d 100755 --- a/Algorithms/0199.binary-tree-right-side-view/README.md +++ b/Algorithms/0199.binary-tree-right-side-view/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. + + +For example: +Given the following binary tree, + 1 <--- + / \ +2 3 <--- + \ \ + 5 4 <--- + + + +You should return [1, 3, 4]. + + +Credits:Special thanks to @amrsaqr for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0200.number-of-islands/README.md b/Algorithms/0200.number-of-islands/README.md index 2c9cac5d7..5cb8e5004 100755 --- a/Algorithms/0200.number-of-islands/README.md +++ b/Algorithms/0200.number-of-islands/README.md @@ -2,6 +2,17 @@ ## 题目 + Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. + +Example 1: +11110110101100000000 +Answer: 1 +Example 2: +11000110000010000011 +Answer: 3 + +Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0201.bitwise-and-of-numbers-range/README.md b/Algorithms/0201.bitwise-and-of-numbers-range/README.md index 679af8e31..a7cdccf87 100755 --- a/Algorithms/0201.bitwise-and-of-numbers-range/README.md +++ b/Algorithms/0201.bitwise-and-of-numbers-range/README.md @@ -2,6 +2,14 @@ ## 题目 + Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. + + +For example, given the range [5, 7], you should return 4. + + +Credits:Special thanks to @amrsaqr for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0202.happy-number/README.md b/Algorithms/0202.happy-number/README.md index cfd230e95..fe6aa847a 100755 --- a/Algorithms/0202.happy-number/README.md +++ b/Algorithms/0202.happy-number/README.md @@ -2,6 +2,21 @@ ## 题目 + Write an algorithm to determine if a number is "happy". + +A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers. + +Example: 19 is a happy number + + +12 + 92 = 82 +82 + 22 = 68 +62 + 82 = 100 +12 + 02 + 02 = 1 + + +Credits:Special thanks to @mithmatt and @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0203.remove-linked-list-elements/README.md b/Algorithms/0203.remove-linked-list-elements/README.md index 6aba46e71..d65d01f11 100755 --- a/Algorithms/0203.remove-linked-list-elements/README.md +++ b/Algorithms/0203.remove-linked-list-elements/README.md @@ -2,6 +2,15 @@ ## 题目 + Remove all elements from a linked list of integers that have value val. + +Example +Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 +Return: 1 --> 2 --> 3 --> 4 --> 5 + + +Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0204.count-primes/README.md b/Algorithms/0204.count-primes/README.md index 85747faab..04c41683a 100755 --- a/Algorithms/0204.count-primes/README.md +++ b/Algorithms/0204.count-primes/README.md @@ -2,6 +2,11 @@ ## 题目 + Description: +Count the number of prime numbers less than a non-negative number, n. + +Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0205.isomorphic-strings/README.md b/Algorithms/0205.isomorphic-strings/README.md index 1000c92d0..11bf3e778 100755 --- a/Algorithms/0205.isomorphic-strings/README.md +++ b/Algorithms/0205.isomorphic-strings/README.md @@ -2,6 +2,22 @@ ## 题目 + Given two strings s and t, determine if they are isomorphic. + +Two strings are isomorphic if the characters in s can be replaced to get t. + +All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself. + +For example, +Given "egg", "add", return true. + +Given "foo", "bar", return false. + +Given "paper", "title", return true. + +Note: +You may assume both s and t have the same length. + ## 解题思路 diff --git a/Algorithms/0207.course-schedule/README.md b/Algorithms/0207.course-schedule/README.md index 477a43366..34376099a 100755 --- a/Algorithms/0207.course-schedule/README.md +++ b/Algorithms/0207.course-schedule/README.md @@ -2,6 +2,39 @@ ## 题目 + +There are a total of n courses you have to take, labeled from 0 to n - 1. + +Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] + + +Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses? + + +For example: +2, [[1,0]] +There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible. + +2, [[1,0],[0,1]] +There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. + +Note: + +The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented. +You may assume that there are no duplicate edges in the input prerequisites. + + + +click to show more hints. + +Hints: + +This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. +Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. +Topological sort could also be done via BFS. + + + ## 解题思路 diff --git a/Algorithms/0208.implement-trie-prefix-tree/README.md b/Algorithms/0208.implement-trie-prefix-tree/README.md index 6fb52456e..8a8257112 100755 --- a/Algorithms/0208.implement-trie-prefix-tree/README.md +++ b/Algorithms/0208.implement-trie-prefix-tree/README.md @@ -2,6 +2,15 @@ ## 题目 + +Implement a trie with insert, search, and startsWith methods. + + + +Note: +You may assume that all inputs are consist of lowercase letters a-z. + + ## 解题思路 diff --git a/Algorithms/0209.minimum-size-subarray-sum/README.md b/Algorithms/0209.minimum-size-subarray-sum/README.md index 85ed04db3..07ad94432 100755 --- a/Algorithms/0209.minimum-size-subarray-sum/README.md +++ b/Algorithms/0209.minimum-size-subarray-sum/README.md @@ -2,6 +2,23 @@ ## 题目 + +Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. + + +For example, given the array [2,3,1,2,4,3] and s = 7, +the subarray [4,3] has the minimal length under the problem constraint. + + +click to show more practice. + +More practice: + +If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n). + + +Credits:Special thanks to @Freezen for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0210.course-schedule-ii/README.md b/Algorithms/0210.course-schedule-ii/README.md index 1c24b49f1..2b333d4b2 100755 --- a/Algorithms/0210.course-schedule-ii/README.md +++ b/Algorithms/0210.course-schedule-ii/README.md @@ -2,6 +2,41 @@ ## 题目 + +There are a total of n courses you have to take, labeled from 0 to n - 1. + +Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] + + +Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses. + +There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array. + + +For example: +2, [[1,0]] +There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1] + +4, [[1,0],[2,0],[3,1],[3,2]] +There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3]. + +Note: + +The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented. +You may assume that there are no duplicate edges in the input prerequisites. + + + +click to show more hints. + +Hints: + +This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses. +Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort. +Topological sort could also be done via BFS. + + + ## 解题思路 diff --git a/Algorithms/0211.add-and-search-word-data-structure-design/README.md b/Algorithms/0211.add-and-search-word-data-structure-design/README.md index 8e88d8f1c..8dda3c760 100755 --- a/Algorithms/0211.add-and-search-word-data-structure-design/README.md +++ b/Algorithms/0211.add-and-search-word-data-structure-design/README.md @@ -2,6 +2,37 @@ ## 题目 + +Design a data structure that supports the following two operations: + +void addWord(word) +bool search(word) + + + +search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. + + +For example: +addWord("bad") +addWord("dad") +addWord("mad") +search("pad") -> false +search("bad") -> true +search(".ad") -> true +search("b..") -> true + + + +Note: +You may assume that all words are consist of lowercase letters a-z. + + +click to show hint. + +You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first. + + ## 解题思路 diff --git a/Algorithms/0212.word-search-ii/README.md b/Algorithms/0212.word-search-ii/README.md index e14060701..1233efdf2 100755 --- a/Algorithms/0212.word-search-ii/README.md +++ b/Algorithms/0212.word-search-ii/README.md @@ -2,6 +2,39 @@ ## 题目 + +Given a 2D board and a list of words from the dictionary, find all words in the board. + + +Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. + + + +For example, +Given words = ["oath","pea","eat","rain"] and board = +[ + ['o','a','a','n'], + ['e','t','a','e'], + ['i','h','k','r'], + ['i','f','l','v'] +] + + +Return ["eat","oath"]. + + + +Note: +You may assume that all inputs are consist of lowercase letters a-z. + + +click to show hint. + +You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier? + +If the current candidate does not exist in all words' prefix, you could stop backtracking immediately. What kind of data structure could answer such query efficiently? Does a hash table work? Why or why not? How about a Trie? If you would like to learn how to implement a basic trie, please work on this problem: Implement Trie (Prefix Tree) first. + + ## 解题思路 diff --git a/Algorithms/0213.house-robber-ii/README.md b/Algorithms/0213.house-robber-ii/README.md index de9e6577a..a6977f8be 100755 --- a/Algorithms/0213.house-robber-ii/README.md +++ b/Algorithms/0213.house-robber-ii/README.md @@ -2,6 +2,14 @@ ## 题目 + Note: This is an extension of House Robber. + +After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street. + +Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police. + +Credits:Special thanks to @Freezen for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0214.shortest-palindrome/README.md b/Algorithms/0214.shortest-palindrome/README.md index 41539139b..201ed70c9 100755 --- a/Algorithms/0214.shortest-palindrome/README.md +++ b/Algorithms/0214.shortest-palindrome/README.md @@ -2,6 +2,16 @@ ## 题目 + +Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. + + +For example: +Given "aacecaaa", return "aaacecaaa". +Given "abcd", return "dcbabcd". + +Credits:Special thanks to @ifanchu for adding this problem and creating all test cases. Thanks to @Freezen for additional test cases. + ## 解题思路 diff --git a/Algorithms/0215.kth-largest-element-in-an-array/README.md b/Algorithms/0215.kth-largest-element-in-an-array/README.md index 3e68f7a9d..1d0e287de 100755 --- a/Algorithms/0215.kth-largest-element-in-an-array/README.md +++ b/Algorithms/0215.kth-largest-element-in-an-array/README.md @@ -2,6 +2,17 @@ ## 题目 + Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. + +For example, +Given [3,2,1,5,6,4] and k = 2, return 5. + + +Note: +You may assume k is always valid, 1 ? k ? array's length. + +Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0216.combination-sum-iii/README.md b/Algorithms/0216.combination-sum-iii/README.md index 9b9f821fd..8a3476adf 100755 --- a/Algorithms/0216.combination-sum-iii/README.md +++ b/Algorithms/0216.combination-sum-iii/README.md @@ -2,6 +2,26 @@ ## 题目 + +Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. + + + + Example 1: +Input: k = 3, n = 7 +Output: +[[1,2,4]] + + + Example 2: +Input: k = 3, n = 9 +Output: +[[1,2,6], [1,3,5], [2,3,4]] + + + +Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0217.contains-duplicate/README.md b/Algorithms/0217.contains-duplicate/README.md index eb6f59a1b..a1038fdbf 100755 --- a/Algorithms/0217.contains-duplicate/README.md +++ b/Algorithms/0217.contains-duplicate/README.md @@ -2,6 +2,10 @@ ## 题目 + +Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. + + ## 解题思路 diff --git a/Algorithms/0218.the-skyline-problem/README.md b/Algorithms/0218.the-skyline-problem/README.md index 70c5525d9..bd3c505f8 100755 --- a/Algorithms/0218.the-skyline-problem/README.md +++ b/Algorithms/0218.the-skyline-problem/README.md @@ -2,6 +2,40 @@ ## 题目 + A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B). + + + + + + + + + + + + + + +The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ? Li, Ri ? INT_MAX, 0 < Hi ? INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0. + +For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] . + +The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour. + +For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]. + +Notes: + + The number of buildings in any input list is guaranteed to be in the range [0, 10000]. + The input list is already sorted in ascending order by the left x position Li. + The output list must be sorted by the x position. + There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...] + + + +Credits:Special thanks to @stellari for adding this problem, creating these two awesome images and all test cases. + ## 解题思路 diff --git a/Algorithms/0219.contains-duplicate-ii/README.md b/Algorithms/0219.contains-duplicate-ii/README.md index 2c90bd9e2..3d71c81ad 100755 --- a/Algorithms/0219.contains-duplicate-ii/README.md +++ b/Algorithms/0219.contains-duplicate-ii/README.md @@ -2,6 +2,10 @@ ## 题目 + +Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. + + ## 解题思路 diff --git a/Algorithms/0220.contains-duplicate-iii/README.md b/Algorithms/0220.contains-duplicate-iii/README.md index 8c6175766..bd5bd430e 100755 --- a/Algorithms/0220.contains-duplicate-iii/README.md +++ b/Algorithms/0220.contains-duplicate-iii/README.md @@ -2,6 +2,10 @@ ## 题目 + +Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. + + ## 解题思路 diff --git a/Algorithms/0221.maximal-square/README.md b/Algorithms/0221.maximal-square/README.md index f3393750c..7c171a3b1 100755 --- a/Algorithms/0221.maximal-square/README.md +++ b/Algorithms/0221.maximal-square/README.md @@ -2,6 +2,21 @@ ## 题目 + +Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. + + +For example, given the following matrix: +1 0 1 0 0 +1 0 1 1 1 +1 1 1 1 1 +1 0 0 1 0 + +Return 4. + + +Credits:Special thanks to @Freezen for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0222.count-complete-tree-nodes/README.md b/Algorithms/0222.count-complete-tree-nodes/README.md index 3f43b870b..eb4a63fde 100755 --- a/Algorithms/0222.count-complete-tree-nodes/README.md +++ b/Algorithms/0222.count-complete-tree-nodes/README.md @@ -2,6 +2,11 @@ ## 题目 + Given a complete binary tree, count the number of nodes. + +Definition of a complete binary tree from Wikipedia: +In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. + ## 解题思路 diff --git a/Algorithms/0223.rectangle-area/README.md b/Algorithms/0223.rectangle-area/README.md index 900d4b3f2..c7f5750f4 100755 --- a/Algorithms/0223.rectangle-area/README.md +++ b/Algorithms/0223.rectangle-area/README.md @@ -2,6 +2,17 @@ ## 题目 + Find the total area covered by two rectilinear rectangles in a 2D plane. +Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. + + + + +Assume that the total area is never beyond the maximum possible value of int. + + +Credits:Special thanks to @mithmatt for adding this problem, creating the above image and all test cases. + ## 解题思路 diff --git a/Algorithms/0224.basic-calculator/README.md b/Algorithms/0224.basic-calculator/README.md index 60b2365ac..5a606ba32 100755 --- a/Algorithms/0224.basic-calculator/README.md +++ b/Algorithms/0224.basic-calculator/README.md @@ -2,6 +2,23 @@ ## 题目 + Implement a basic calculator to evaluate a simple expression string. + +The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . + +You may assume that the given expression is always valid. + +Some examples: +"1 + 1" = 2 +" 2-1 + 2 " = 3 +"(1+(4+5+2)-3)+(6+8)" = 23 + + + + +Note: Do not use the eval built-in library function. + + ## 解题思路 diff --git a/Algorithms/0225.implement-stack-using-queues/README.md b/Algorithms/0225.implement-stack-using-queues/README.md index cbafceb78..967ffea26 100755 --- a/Algorithms/0225.implement-stack-using-queues/README.md +++ b/Algorithms/0225.implement-stack-using-queues/README.md @@ -2,6 +2,32 @@ ## 题目 + +Implement the following operations of a stack using queues. + + +push(x) -- Push element x onto stack. + + +pop() -- Removes the element on top of the stack. + + +top() -- Get the top element. + + +empty() -- Return whether the stack is empty. + + +Notes: + +You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid. +Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue. +You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). + + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and all test cases. + ## 解题思路 diff --git a/Algorithms/0226.invert-binary-tree/README.md b/Algorithms/0226.invert-binary-tree/README.md index 6cbecc4b7..e279830d4 100755 --- a/Algorithms/0226.invert-binary-tree/README.md +++ b/Algorithms/0226.invert-binary-tree/README.md @@ -2,6 +2,24 @@ ## 题目 + Invert a binary tree. + 4 + / \ + 2 7 + / \ / \ +1 3 6 9 + +to + 4 + / \ + 7 2 + / \ / \ +9 6 3 1 + +Trivia: +This problem was inspired by this original tweet by Max Howell: +Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off. + ## 解题思路 diff --git a/Algorithms/0227.basic-calculator-ii/README.md b/Algorithms/0227.basic-calculator-ii/README.md index d2b73f7a2..325138b92 100755 --- a/Algorithms/0227.basic-calculator-ii/README.md +++ b/Algorithms/0227.basic-calculator-ii/README.md @@ -2,6 +2,25 @@ ## 题目 + Implement a basic calculator to evaluate a simple expression string. + +The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. + +You may assume that the given expression is always valid. + +Some examples: +"3+2*2" = 7 +" 3/2 " = 1 +" 3+5 / 2 " = 5 + + + + +Note: Do not use the eval built-in library function. + + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0228.summary-ranges/README.md b/Algorithms/0228.summary-ranges/README.md index 8b322ee30..c24175740 100755 --- a/Algorithms/0228.summary-ranges/README.md +++ b/Algorithms/0228.summary-ranges/README.md @@ -2,6 +2,15 @@ ## 题目 + +Given a sorted integer array without duplicates, return the summary of its ranges. + + +For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0229.majority-element-ii/README.md b/Algorithms/0229.majority-element-ii/README.md index 5db33c52c..a1aaab842 100755 --- a/Algorithms/0229.majority-element-ii/README.md +++ b/Algorithms/0229.majority-element-ii/README.md @@ -2,6 +2,8 @@ ## 题目 + Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. + ## 解题思路 diff --git a/Algorithms/0230.kth-smallest-element-in-a-bst/README.md b/Algorithms/0230.kth-smallest-element-in-a-bst/README.md index 686f3ecb4..2d734de08 100755 --- a/Algorithms/0230.kth-smallest-element-in-a-bst/README.md +++ b/Algorithms/0230.kth-smallest-element-in-a-bst/README.md @@ -2,6 +2,16 @@ ## 题目 + Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. + +Note: +You may assume k is always valid, 1 ? k ? BST's total elements. + +Follow up: +What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine? + +Credits:Special thanks to @ts for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0231.power-of-two/README.md b/Algorithms/0231.power-of-two/README.md index 782a97ca9..8b0949508 100755 --- a/Algorithms/0231.power-of-two/README.md +++ b/Algorithms/0231.power-of-two/README.md @@ -2,6 +2,12 @@ ## 题目 + +Given an integer, write a function to determine if it is a power of two. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0232.implement-queue-using-stacks/README.md b/Algorithms/0232.implement-queue-using-stacks/README.md index dcd7ae98f..8efad1921 100755 --- a/Algorithms/0232.implement-queue-using-stacks/README.md +++ b/Algorithms/0232.implement-queue-using-stacks/README.md @@ -2,6 +2,30 @@ ## 题目 + +Implement the following operations of a queue using stacks. + + +push(x) -- Push element x to the back of queue. + + +pop() -- Removes the element from in front of queue. + + +peek() -- Get the front element. + + +empty() -- Return whether the queue is empty. + + +Notes: + +You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid. +Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack. +You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue). + + + ## 解题思路 diff --git a/Algorithms/0233.number-of-digit-one/README.md b/Algorithms/0233.number-of-digit-one/README.md index 08639e11e..80cbb45c6 100755 --- a/Algorithms/0233.number-of-digit-one/README.md +++ b/Algorithms/0233.number-of-digit-one/README.md @@ -2,6 +2,14 @@ ## 题目 + Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. + + +For example: +Given n = 13, +Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. + + ## 解题思路 diff --git a/Algorithms/0234.palindrome-linked-list/README.md b/Algorithms/0234.palindrome-linked-list/README.md index b6703b5e7..3ebf84691 100755 --- a/Algorithms/0234.palindrome-linked-list/README.md +++ b/Algorithms/0234.palindrome-linked-list/README.md @@ -2,6 +2,11 @@ ## 题目 + Given a singly linked list, determine if it is a palindrome. + +Follow up: +Could you do it in O(n) time and O(1) space? + ## 解题思路 diff --git a/Algorithms/0235.lowest-common-ancestor-of-a-binary-search-tree/README.md b/Algorithms/0235.lowest-common-ancestor-of-a-binary-search-tree/README.md index f161a5b0e..708574cba 100755 --- a/Algorithms/0235.lowest-common-ancestor-of-a-binary-search-tree/README.md +++ b/Algorithms/0235.lowest-common-ancestor-of-a-binary-search-tree/README.md @@ -2,6 +2,26 @@ ## 题目 + +Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. + + + +According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).” + + + _______6______ + / \ + ___2__ ___8__ + / \ / \ + 0 _4 7 9 + / \ + 3 5 + + + +For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition. + ## 解题思路 diff --git a/Algorithms/0236.lowest-common-ancestor-of-a-binary-tree/README.md b/Algorithms/0236.lowest-common-ancestor-of-a-binary-tree/README.md index 58abb69f9..3918d1a18 100755 --- a/Algorithms/0236.lowest-common-ancestor-of-a-binary-tree/README.md +++ b/Algorithms/0236.lowest-common-ancestor-of-a-binary-tree/README.md @@ -2,6 +2,26 @@ ## 题目 + +Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. + + + +According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).” + + + _______3______ + / \ + ___5__ ___1__ + / \ / \ + 6 _2 0 8 + / \ + 7 4 + + + +For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. + ## 解题思路 diff --git a/Algorithms/0237.delete-node-in-a-linked-list/README.md b/Algorithms/0237.delete-node-in-a-linked-list/README.md index 708542d7b..fbb23ed80 100755 --- a/Algorithms/0237.delete-node-in-a-linked-list/README.md +++ b/Algorithms/0237.delete-node-in-a-linked-list/README.md @@ -2,6 +2,14 @@ ## 题目 + +Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. + + + +Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function. + + ## 解题思路 diff --git a/Algorithms/0238.product-of-array-except-self/README.md b/Algorithms/0238.product-of-array-except-self/README.md index ca8b95bf3..1446d0796 100755 --- a/Algorithms/0238.product-of-array-except-self/README.md +++ b/Algorithms/0238.product-of-array-except-self/README.md @@ -2,6 +2,16 @@ ## 题目 + +Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. + +Solve it without division and in O(n). + +For example, given [1,2,3,4], return [24,12,8,6]. + +Follow up: +Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) + ## 解题思路 diff --git a/Algorithms/0239.sliding-window-maximum/README.md b/Algorithms/0239.sliding-window-maximum/README.md index 09e2b0a50..123e8eb02 100755 --- a/Algorithms/0239.sliding-window-maximum/README.md +++ b/Algorithms/0239.sliding-window-maximum/README.md @@ -2,6 +2,29 @@ ## 题目 + Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. + +For example, +Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. + +Window position Max +--------------- ----- +[1 3 -1] -3 5 3 6 7 3 + 1 [3 -1 -3] 5 3 6 7 3 + 1 3 [-1 -3 5] 3 6 7 5 + 1 3 -1 [-3 5 3] 6 7 5 + 1 3 -1 -3 [5 3 6] 7 6 + 1 3 -1 -3 5 [3 6 7] 7 + + +Therefore, return the max sliding window as [3,3,5,5,6,7]. + +Note: +You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array. + +Follow up: +Could you solve it in linear time? + ## 解题思路 diff --git a/Algorithms/0240.search-a-2d-matrix-ii/README.md b/Algorithms/0240.search-a-2d-matrix-ii/README.md index bdee88eee..775d8ebb3 100755 --- a/Algorithms/0240.search-a-2d-matrix-ii/README.md +++ b/Algorithms/0240.search-a-2d-matrix-ii/README.md @@ -2,6 +2,32 @@ ## 题目 + Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: + + + +Integers in each row are sorted in ascending from left to right. +Integers in each column are sorted in ascending from top to bottom. + + + + +For example, + +Consider the following matrix: + +[ + [1, 4, 7, 11, 15], + [2, 5, 8, 12, 19], + [3, 6, 9, 16, 22], + [10, 13, 14, 17, 24], + [18, 21, 23, 26, 30] +] + + +Given target = 5, return true. +Given target = 20, return false. + ## 解题思路 diff --git a/Algorithms/0241.different-ways-to-add-parentheses/README.md b/Algorithms/0241.different-ways-to-add-parentheses/README.md index 63d344b0e..48bb20209 100755 --- a/Algorithms/0241.different-ways-to-add-parentheses/README.md +++ b/Algorithms/0241.different-ways-to-add-parentheses/README.md @@ -2,6 +2,25 @@ ## 题目 + Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. + +Example 1 +Input: "2-1-1". +((2-1)-1) = 0 +(2-(1-1)) = 2 +Output: [0, 2] + +Example 2 +Input: "2*3-4*5" +(2*(3-(4*5))) = -34 +((2*3)-(4*5)) = -14 +((2*(3-4))*5) = -10 +(2*((3-4)*5)) = -10 +(((2*3)-4)*5) = 10 +Output: [-34, -14, -10, -10, 10] + +Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0242.valid-anagram/README.md b/Algorithms/0242.valid-anagram/README.md index 1dd1e2d35..9e4a7ab07 100755 --- a/Algorithms/0242.valid-anagram/README.md +++ b/Algorithms/0242.valid-anagram/README.md @@ -2,6 +2,19 @@ ## 题目 + Given two strings s and t, write a function to determine if t is an anagram of s. + +For example, +s = "anagram", t = "nagaram", return true. +s = "rat", t = "car", return false. + + +Note: +You may assume the string contains only lowercase alphabets. + +Follow up: +What if the inputs contain unicode characters? How would you adapt your solution to such case? + ## 解题思路 diff --git a/Algorithms/0257.binary-tree-paths/README.md b/Algorithms/0257.binary-tree-paths/README.md index 688e55fea..723d5c21f 100755 --- a/Algorithms/0257.binary-tree-paths/README.md +++ b/Algorithms/0257.binary-tree-paths/README.md @@ -2,6 +2,27 @@ ## 题目 + +Given a binary tree, return all root-to-leaf paths. + + +For example, given the following binary tree: + + + 1 + / \ +2 3 + \ + 5 + + + +All root-to-leaf paths are: +["1->2->5", "1->3"] + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0258.add-digits/README.md b/Algorithms/0258.add-digits/README.md index 97d47dfbb..0105f4c8e 100755 --- a/Algorithms/0258.add-digits/README.md +++ b/Algorithms/0258.add-digits/README.md @@ -2,6 +2,23 @@ ## 题目 + +Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. + + + +For example: + + +Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. + + +Follow up: +Could you do it without any loop/recursion in O(1) runtime? + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0260.single-number-iii/README.md b/Algorithms/0260.single-number-iii/README.md index 1c9329f64..45299ba2c 100755 --- a/Algorithms/0260.single-number-iii/README.md +++ b/Algorithms/0260.single-number-iii/README.md @@ -2,6 +2,25 @@ ## 题目 + +Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. + + +For example: + + +Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. + + +Note: + +The order of the result is not important. So in the above example, [5, 3] is also correct. +Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity? + + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0263.ugly-number/README.md b/Algorithms/0263.ugly-number/README.md index 4960ceb58..0dd91763a 100755 --- a/Algorithms/0263.ugly-number/README.md +++ b/Algorithms/0263.ugly-number/README.md @@ -2,6 +2,20 @@ ## 题目 + +Write a program to check whether a given number is an ugly number. + + + +Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. + + + +Note that 1 is typically treated as an ugly number. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0264.ugly-number-ii/README.md b/Algorithms/0264.ugly-number-ii/README.md index 542c298b0..067c51bde 100755 --- a/Algorithms/0264.ugly-number-ii/README.md +++ b/Algorithms/0264.ugly-number-ii/README.md @@ -2,6 +2,20 @@ ## 题目 + +Write a program to find the n-th ugly number. + + + +Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. + + + +Note that 1 is typically treated as an ugly number, and n does not exceed 1690. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0268.missing-number/README.md b/Algorithms/0268.missing-number/README.md index fd7ac214f..1db0b0dfa 100755 --- a/Algorithms/0268.missing-number/README.md +++ b/Algorithms/0268.missing-number/README.md @@ -2,6 +2,21 @@ ## 题目 + +Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. + + +For example, +Given nums = [0, 1, 3] return 2. + + + +Note: +Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity? + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0273.integer-to-english-words/README.md b/Algorithms/0273.integer-to-english-words/README.md index 31d881a09..015b2c08f 100755 --- a/Algorithms/0273.integer-to-english-words/README.md +++ b/Algorithms/0273.integer-to-english-words/README.md @@ -2,6 +2,15 @@ ## 题目 + +Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. + + +For example, +123 -> "One Hundred Twenty Three" +12345 -> "Twelve Thousand Three Hundred Forty Five" +1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" + ## 解题思路 diff --git a/Algorithms/0274.h-index/README.md b/Algorithms/0274.h-index/README.md index 47b074c8f..2f5091d2f 100755 --- a/Algorithms/0274.h-index/README.md +++ b/Algorithms/0274.h-index/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index. + + + +According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each." + + + +For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3. + + + +Note: If there are several possible values for h, the maximum one is taken as the h-index. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0275.h-index-ii/README.md b/Algorithms/0275.h-index-ii/README.md index 6d7f012f8..85c07cebd 100755 --- a/Algorithms/0275.h-index-ii/README.md +++ b/Algorithms/0275.h-index-ii/README.md @@ -2,6 +2,10 @@ ## 题目 + +Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? + + ## 解题思路 diff --git a/Algorithms/0278.first-bad-version/README.md b/Algorithms/0278.first-bad-version/README.md index d997ff473..8a43c08f3 100755 --- a/Algorithms/0278.first-bad-version/README.md +++ b/Algorithms/0278.first-bad-version/README.md @@ -2,6 +2,20 @@ ## 题目 + +You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. + + + +Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. + + + +You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0279.perfect-squares/README.md b/Algorithms/0279.perfect-squares/README.md index e3e35312c..89c9f7c44 100755 --- a/Algorithms/0279.perfect-squares/README.md +++ b/Algorithms/0279.perfect-squares/README.md @@ -2,6 +2,16 @@ ## 题目 + +Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. + + + +For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0282.expression-add-operators/README.md b/Algorithms/0282.expression-add-operators/README.md index b7e2a4e3c..97d811be4 100755 --- a/Algorithms/0282.expression-add-operators/README.md +++ b/Algorithms/0282.expression-add-operators/README.md @@ -2,6 +2,20 @@ ## 题目 + +Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value. + + +Examples: +"123", 6 -> ["1+2+3", "1*2*3"] +"232", 8 -> ["2*3+2", "2+3*2"] +"105", 5 -> ["1*0+5","10-5"] +"00", 0 -> ["0+0", "0-0", "0*0"] +"3456237490", 9191 -> [] + + +Credits:Special thanks to @davidtan1890 for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0283.move-zeroes/README.md b/Algorithms/0283.move-zeroes/README.md index 8c7b7f14c..65cac0a02 100755 --- a/Algorithms/0283.move-zeroes/README.md +++ b/Algorithms/0283.move-zeroes/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. + + + +For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. + + + +Note: + +You must do this in-place without making a copy of the array. +Minimize the total number of operations. + + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0284.peeking-iterator/README.md b/Algorithms/0284.peeking-iterator/README.md index 4e9e35652..014122dca 100755 --- a/Algorithms/0284.peeking-iterator/README.md +++ b/Algorithms/0284.peeking-iterator/README.md @@ -2,6 +2,22 @@ ## 题目 + Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next(). + + +Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3]. + +Call next() gets you 1, the first element in the list. + +Now you call peek() and it returns 2, the next element. Calling next() after that still return 2. + +You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false. + + +Follow up: How would you extend your design to be generic and work with all types, not just integer? + +Credits:Special thanks to @porker2008 for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0287.find-the-duplicate-number/README.md b/Algorithms/0287.find-the-duplicate-number/README.md index b721707b3..de681ad54 100755 --- a/Algorithms/0287.find-the-duplicate-number/README.md +++ b/Algorithms/0287.find-the-duplicate-number/README.md @@ -2,6 +2,22 @@ ## 题目 + +Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. + + + +Note: + +You must not modify the array (assume the array is read only). +You must use only constant, O(1) extra space. +Your runtime complexity should be less than O(n2). +There is only one duplicate number in the array, but it could be repeated more than once. + + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0289.game-of-life/README.md b/Algorithms/0289.game-of-life/README.md index 02978afc2..3300ec7c4 100755 --- a/Algorithms/0289.game-of-life/README.md +++ b/Algorithms/0289.game-of-life/README.md @@ -2,6 +2,36 @@ ## 题目 + +According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." + + + +Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): + + + + +Any live cell with fewer than two live neighbors dies, as if caused by under-population. +Any live cell with two or three live neighbors lives on to the next generation. +Any live cell with more than three live neighbors dies, as if by over-population.. +Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. + + + + +Write a function to compute the next state (after one update) of the board given its current state. + + +Follow up: + +Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells. +In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems? + + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0290.word-pattern/README.md b/Algorithms/0290.word-pattern/README.md index f4857aad4..da5c2c792 100755 --- a/Algorithms/0290.word-pattern/README.md +++ b/Algorithms/0290.word-pattern/README.md @@ -2,6 +2,25 @@ ## 题目 + Given a pattern and a string str, find if str follows the same pattern. + Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. + +Examples: + +pattern = "abba", str = "dog cat cat dog" should return true. +pattern = "abba", str = "dog cat cat fish" should return false. +pattern = "aaaa", str = "dog cat cat dog" should return false. +pattern = "abba", str = "dog dog dog dog" should return false. + + + + +Notes: +You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space. + + +Credits:Special thanks to @minglotus6 for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0292.nim-game/README.md b/Algorithms/0292.nim-game/README.md index 41c398549..b6ca1238a 100755 --- a/Algorithms/0292.nim-game/README.md +++ b/Algorithms/0292.nim-game/README.md @@ -2,6 +2,20 @@ ## 题目 + +You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones. + + + +Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap. + + + +For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0295.find-median-from-data-stream/README.md b/Algorithms/0295.find-median-from-data-stream/README.md index 9bf00cd61..350c6679d 100755 --- a/Algorithms/0295.find-median-from-data-stream/README.md +++ b/Algorithms/0295.find-median-from-data-stream/README.md @@ -2,6 +2,30 @@ ## 题目 + Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. +Examples: +[2,3,4] , the median is 3 +[2,3], the median is (2 + 3) / 2 = 2.5 + + +Design a data structure that supports the following two operations: + + +void addNum(int num) - Add a integer number from the data stream to the data structure. +double findMedian() - Return the median of all elements so far. + + + +For example: +addNum(1) +addNum(2) +findMedian() -> 1.5 +addNum(3) +findMedian() -> 2 + + +Credits:Special thanks to @Louis1992 for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0297.serialize-and-deserialize-binary-tree/README.md b/Algorithms/0297.serialize-and-deserialize-binary-tree/README.md index fc906622b..556938e9e 100755 --- a/Algorithms/0297.serialize-and-deserialize-binary-tree/README.md +++ b/Algorithms/0297.serialize-and-deserialize-binary-tree/README.md @@ -2,6 +2,27 @@ ## 题目 + Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. + +Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure. + + +For example, you may serialize the following tree + 1 + / \ + 2 3 + / \ + 4 5 + +as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself. + + + +Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless. + + +Credits:Special thanks to @Louis1992 for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0299.bulls-and-cows/README.md b/Algorithms/0299.bulls-and-cows/README.md index d38138ca4..9017c1781 100755 --- a/Algorithms/0299.bulls-and-cows/README.md +++ b/Algorithms/0299.bulls-and-cows/README.md @@ -2,6 +2,29 @@ ## 题目 + You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number. + + +For example: +Secret number: "1807" +Friend's guess: "7810" + +Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.) + + +Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B". + +Please note that both secret number and friend's guess may contain duplicate digits, for example: +Secret number: "1123" +Friend's guess: "0111" + +In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B". + + +You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal. + +Credits:Special thanks to @jeantimex for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0300.longest-increasing-subsequence/README.md b/Algorithms/0300.longest-increasing-subsequence/README.md index 8442540f9..99d9f99e1 100755 --- a/Algorithms/0300.longest-increasing-subsequence/README.md +++ b/Algorithms/0300.longest-increasing-subsequence/README.md @@ -2,6 +2,22 @@ ## 题目 + +Given an unsorted array of integers, find the length of longest increasing subsequence. + + +For example, +Given [10, 9, 2, 5, 3, 7, 101, 18], +The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length. + + +Your algorithm should run in O(n2) complexity. + + +Follow up: Could you improve it to O(n log n) time complexity? + +Credits:Special thanks to @pbrother for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0301.remove-invalid-parentheses/README.md b/Algorithms/0301.remove-invalid-parentheses/README.md index b576d9e85..7ba230f0a 100755 --- a/Algorithms/0301.remove-invalid-parentheses/README.md +++ b/Algorithms/0301.remove-invalid-parentheses/README.md @@ -2,6 +2,22 @@ ## 题目 + +Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. + +Note: The input string may contain letters other than the parentheses ( and ). + + + +Examples: +"()())()" -> ["()()()", "(())()"] +"(a)())()" -> ["(a)()()", "(a())()"] +")(" -> [""] + + + +Credits:Special thanks to @hpplayer for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0303.range-sum-query-immutable/README.md b/Algorithms/0303.range-sum-query-immutable/README.md index d35e778f9..2ac7462fa 100755 --- a/Algorithms/0303.range-sum-query-immutable/README.md +++ b/Algorithms/0303.range-sum-query-immutable/README.md @@ -2,6 +2,24 @@ ## 题目 + Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. + +Example: +Given nums = [-2, 0, 3, -5, 2, -1] + +sumRange(0, 2) -> 1 +sumRange(2, 5) -> -1 +sumRange(0, 5) -> -3 + + + +Note: + +You may assume that the array does not change. +There are many calls to sumRange function. + + + ## 解题思路 diff --git a/Algorithms/0304.range-sum-query-2d-immutable/README.md b/Algorithms/0304.range-sum-query-2d-immutable/README.md index b0b8387ab..f2a71bc5c 100755 --- a/Algorithms/0304.range-sum-query-2d-immutable/README.md +++ b/Algorithms/0304.range-sum-query-2d-immutable/README.md @@ -2,6 +2,36 @@ ## 题目 + Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). + + + +The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8. + + +Example: +Given matrix = [ + [3, 0, 1, 4, 2], + [5, 6, 3, 2, 1], + [1, 2, 0, 1, 5], + [4, 1, 0, 1, 7], + [1, 0, 3, 0, 5] +] + +sumRegion(2, 1, 4, 3) -> 8 +sumRegion(1, 1, 2, 2) -> 11 +sumRegion(1, 2, 2, 4) -> 12 + + + +Note: + +You may assume that the matrix does not change. +There are many calls to sumRegion function. +You may assume that row1 ≤ row2 and col1 ≤ col2. + + + ## 解题思路 diff --git a/Algorithms/0306.additive-number/README.md b/Algorithms/0306.additive-number/README.md index 1a44db682..5e7a669a0 100755 --- a/Algorithms/0306.additive-number/README.md +++ b/Algorithms/0306.additive-number/README.md @@ -2,6 +2,31 @@ ## 题目 + Additive number is a string whose digits can form additive sequence. + +A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. + + +For example: +"112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8. +1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8 +"199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199. +1 + 99 = 100, 99 + 100 = 199 + + + +Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid. + + +Given a string containing only digits '0'-'9', write a function to determine if it's an additive number. + + +Follow up: +How would you handle overflow for very large input integers? + + +Credits:Special thanks to @jeantimex for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0307.range-sum-query-mutable/README.md b/Algorithms/0307.range-sum-query-mutable/README.md index 5fe19aa84..1657ed914 100755 --- a/Algorithms/0307.range-sum-query-mutable/README.md +++ b/Algorithms/0307.range-sum-query-mutable/README.md @@ -2,6 +2,26 @@ ## 题目 + Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. + +The update(i, val) function modifies nums by updating the element at index i to val. + +Example: +Given nums = [1, 3, 5] + +sumRange(0, 2) -> 9 +update(1, 2) +sumRange(0, 2) -> 8 + + + +Note: + +The array is only modifiable by the update function. +You may assume the number of calls to update and sumRange function is distributed evenly. + + + ## 解题思路 diff --git a/Algorithms/0309.best-time-to-buy-and-sell-stock-with-cooldown/README.md b/Algorithms/0309.best-time-to-buy-and-sell-stock-with-cooldown/README.md index bbe1ef0ea..39063d63b 100755 --- a/Algorithms/0309.best-time-to-buy-and-sell-stock-with-cooldown/README.md +++ b/Algorithms/0309.best-time-to-buy-and-sell-stock-with-cooldown/README.md @@ -2,6 +2,24 @@ ## 题目 + Say you have an array for which the ith element is the price of a given stock on day i. + +Design an algorithm to find the maximum profit. You may complete as many transactions as you like +(ie, buy one and sell one share of the stock multiple times) with the following restrictions: + + + You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). + After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) + + +Example: +prices = [1, 2, 3, 0, 2] +maxProfit = 3 +transactions = [buy, sell, cooldown, buy, sell] + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0310.minimum-height-trees/README.md b/Algorithms/0310.minimum-height-trees/README.md index fddd1209e..54fe9ee2c 100755 --- a/Algorithms/0310.minimum-height-trees/README.md +++ b/Algorithms/0310.minimum-height-trees/README.md @@ -2,6 +2,71 @@ ## 题目 + + For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). + Given such a graph, write a function to find all the MHTs and return a list of their root labels. + + + + Format + The graph contains n nodes which are labeled from 0 to n - 1. + You will be given the number n and a list of undirected edges (each edge is a pair of labels). + + +You can assume that no duplicate edges will appear in edges. Since all edges are + undirected, [0, 1] is the same as [1, 0] and thus will not appear together in + edges. + + + Example 1: + + + Given n = 4, edges = [[1, 0], [1, 2], [1, 3]] + + + 0 + | + 1 + / \ + 2 3 + + + return [1] + + + + Example 2: + + + Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]] + + 0 1 2 + \ | / + 3 + | + 4 + | + 5 + + + return [3, 4] + + + + Note: + + + (1) According to the definition + of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by + exactly one path. In other words, any connected graph without simple cycles is a tree.” + + + (2) The height of a rooted tree is the number of edges on the longest downward path between the root and a + leaf. + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0312.burst-balloons/README.md b/Algorithms/0312.burst-balloons/README.md index dbeaa5127..82e6c30e8 100755 --- a/Algorithms/0312.burst-balloons/README.md +++ b/Algorithms/0312.burst-balloons/README.md @@ -2,6 +2,40 @@ ## 题目 + + Given n balloons, indexed from 0 to n-1. Each balloon is painted with a + number on it represented by array nums. + + You are asked to burst all the balloons. If the you burst + balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left + and right are adjacent indices of i. After the burst, the left and right + then becomes adjacent. + + + Find the maximum coins you can collect by bursting the balloons wisely. + + + Note: + (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. + (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100 + + + + + Example: + + + Given [3, 1, 5, 8] + + + Return 167 + + nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] + coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167 + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0313.super-ugly-number/README.md b/Algorithms/0313.super-ugly-number/README.md index 50e2b62aa..111b18dd6 100755 --- a/Algorithms/0313.super-ugly-number/README.md +++ b/Algorithms/0313.super-ugly-number/README.md @@ -2,6 +2,27 @@ ## 题目 + + Write a program to find the nth super ugly number. + + + + Super ugly numbers are positive numbers whose all prime factors are in the given prime list + primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] + is the sequence of the first 12 super ugly numbers given primes + = [2, 7, 13, 19] of size 4. + + + + Note: + (1) 1 is a super ugly number for any given primes. + (2) The given numbers in primes are in ascending order. + (3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000. + (4) The nth super ugly number is guaranteed to fit in a 32-bit signed integer. + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0315.count-of-smaller-numbers-after-self/README.md b/Algorithms/0315.count-of-smaller-numbers-after-self/README.md index 3f1507a5c..1816979f7 100755 --- a/Algorithms/0315.count-of-smaller-numbers-after-self/README.md +++ b/Algorithms/0315.count-of-smaller-numbers-after-self/README.md @@ -2,6 +2,26 @@ ## 题目 + +You are given an integer array nums and you have to return a new counts array. +The counts array has the property where counts[i] is +the number of smaller elements to the right of nums[i]. + + +Example: + +Given nums = [5, 2, 6, 1] + +To the right of 5 there are 2 smaller elements (2 and 1). +To the right of 2 there is only 1 smaller element (1). +To the right of 6 there is 1 smaller element (1). +To the right of 1 there is 0 smaller element. + + + +Return the array [2, 1, 1, 0]. + + ## 解题思路 diff --git a/Algorithms/0316.remove-duplicate-letters/README.md b/Algorithms/0316.remove-duplicate-letters/README.md index 10241dd91..e98572339 100755 --- a/Algorithms/0316.remove-duplicate-letters/README.md +++ b/Algorithms/0316.remove-duplicate-letters/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. + + + +Example: + + +Given "bcabc" +Return "abc" + + +Given "cbacdcbc" +Return "acdb" + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0318.maximum-product-of-word-lengths/README.md b/Algorithms/0318.maximum-product-of-word-lengths/README.md index a419c45e6..514d1cc5b 100755 --- a/Algorithms/0318.maximum-product-of-word-lengths/README.md +++ b/Algorithms/0318.maximum-product-of-word-lengths/README.md @@ -2,6 +2,39 @@ ## 题目 + + Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. + You may assume that each word will contain only lower case letters. + If no such two words exist, return 0. + + + + Example 1: + + + Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"] + Return 16 + The two words can be "abcw", "xtfn". + + + Example 2: + + + Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"] + Return 4 + The two words can be "ab", "cd". + + + Example 3: + + + Given ["a", "aa", "aaa", "aaaa"] + Return 0 + No such pair of words. + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0319.bulb-switcher/README.md b/Algorithms/0319.bulb-switcher/README.md index 4c7c5d265..7985f6dbc 100755 --- a/Algorithms/0319.bulb-switcher/README.md +++ b/Algorithms/0319.bulb-switcher/README.md @@ -2,6 +2,22 @@ ## 题目 + +There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. + +Find how many bulbs are on after n rounds. + + + +Example: +Given n = 3. +At first, the three bulbs are [off, off, off]. +After first round, the three bulbs are [on, on, on]. +After second round, the three bulbs are [on, off, on]. +After third round, the three bulbs are [on, off, off]. +So you should return 1, because there is only one bulb is on. + + ## 解题思路 diff --git a/Algorithms/0321.create-maximum-number/README.md b/Algorithms/0321.create-maximum-number/README.md index 3a8db62aa..74d6bca45 100755 --- a/Algorithms/0321.create-maximum-number/README.md +++ b/Algorithms/0321.create-maximum-number/README.md @@ -2,6 +2,42 @@ ## 题目 + + Given two arrays of length m and n with digits 0-9 representing two numbers. + Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits + from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity. + + + + Example 1: + + + nums1 = [3, 4, 6, 5] + nums2 = [9, 1, 2, 5, 8, 3] + k = 5 + return [9, 8, 6, 5, 3] + + + Example 2: + + + nums1 = [6, 7] + nums2 = [6, 0, 4] + k = 5 + return [6, 7, 6, 0, 4] + + + Example 3: + + + nums1 = [3, 9] + nums2 = [8, 9] + k = 3 + return [9, 8, 9] + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0322.coin-change/README.md b/Algorithms/0322.coin-change/README.md index c94228a38..d8ba92154 100755 --- a/Algorithms/0322.coin-change/README.md +++ b/Algorithms/0322.coin-change/README.md @@ -2,6 +2,29 @@ ## 题目 + +You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. + + + +Example 1: +coins = [1, 2, 5], amount = 11 +return 3 (11 = 5 + 5 + 1) + + + +Example 2: +coins = [2], amount = 3 +return -1. + + + +Note: +You may assume that you have an infinite number of each kind of coin. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0324.wiggle-sort-ii/README.md b/Algorithms/0324.wiggle-sort-ii/README.md index cc3bb51cb..a5a462ed6 100755 --- a/Algorithms/0324.wiggle-sort-ii/README.md +++ b/Algorithms/0324.wiggle-sort-ii/README.md @@ -2,6 +2,29 @@ ## 题目 + + Given an unsorted array nums, reorder it such that + nums[0] < nums[1] > nums[2] < nums[3].... + + + + Example: + (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. + (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2]. + + + + Note: + You may assume all input has valid answer. + + + + Follow Up: + Can you do it in O(n) time and/or in-place with O(1) extra space? + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0326.power-of-three/README.md b/Algorithms/0326.power-of-three/README.md index c904f4caa..0588204c8 100755 --- a/Algorithms/0326.power-of-three/README.md +++ b/Algorithms/0326.power-of-three/README.md @@ -2,6 +2,16 @@ ## 题目 + + Given an integer, write a function to determine if it is a power of three. + + + Follow up: + Could you do it without using any loop / recursion? + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0327.count-of-range-sum/README.md b/Algorithms/0327.count-of-range-sum/README.md index c0b1bd1a5..07077e471 100755 --- a/Algorithms/0327.count-of-range-sum/README.md +++ b/Algorithms/0327.count-of-range-sum/README.md @@ -2,6 +2,26 @@ ## 题目 + + Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive. + + Range sum S(i, j) is defined as the sum of the elements in nums between indices i and + j (i ? j), inclusive. + + + + Note: + A naive algorithm of O(n2) is trivial. You MUST do better than that. + + + Example: + Given nums = [-2, 5, -1], lower = -2, upper = 2, + Return 3. + The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums are: -2, -1, 2. + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0328.odd-even-linked-list/README.md b/Algorithms/0328.odd-even-linked-list/README.md index ac87bab09..137a7c194 100755 --- a/Algorithms/0328.odd-even-linked-list/README.md +++ b/Algorithms/0328.odd-even-linked-list/README.md @@ -2,6 +2,23 @@ ## 题目 + Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. + +You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity. + + +Example: +Given 1->2->3->4->5->NULL, +return 1->3->5->2->4->NULL. + + +Note: +The relative order inside both the even and odd groups should remain as it was in the input. +The first node is considered odd, the second node even and so on ... + + +Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0329.longest-increasing-path-in-a-matrix/README.md b/Algorithms/0329.longest-increasing-path-in-a-matrix/README.md index e57dbd043..c4dc8dc6c 100755 --- a/Algorithms/0329.longest-increasing-path-in-a-matrix/README.md +++ b/Algorithms/0329.longest-increasing-path-in-a-matrix/README.md @@ -2,6 +2,43 @@ ## 题目 + Given an integer matrix, find the length of the longest increasing path. + + +From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed). + + +Example 1: +nums = [ + [9,9,4], + [6,6,8], + [2,1,1] +] + + + + +Return 4 + +The longest increasing path is [1, 2, 6, 9]. + + +Example 2: +nums = [ + [3,4,5], + [3,2,6], + [2,2,1] +] + + + + +Return 4 + +The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed. + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0330.patching-array/README.md b/Algorithms/0330.patching-array/README.md index 1faf9eb3e..ed6f4181d 100755 --- a/Algorithms/0330.patching-array/README.md +++ b/Algorithms/0330.patching-array/README.md @@ -2,6 +2,29 @@ ## 题目 + Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. + + +Example 1: +nums = [1, 3], n = 6 +Return 1. + +Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4. +Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3]. +Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6]. +So we only need 1 patch. + +Example 2: +nums = [1, 5, 10], n = 20 +Return 2. +The two patches can be [2, 4]. + +Example 3: +nums = [1, 2, 2], n = 5 +Return 0. + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0331.verify-preorder-serialization-of-a-binary-tree/README.md b/Algorithms/0331.verify-preorder-serialization-of-a-binary-tree/README.md index 7a57403b0..5a55c987f 100755 --- a/Algorithms/0331.verify-preorder-serialization-of-a-binary-tree/README.md +++ b/Algorithms/0331.verify-preorder-serialization-of-a-binary-tree/README.md @@ -2,6 +2,38 @@ ## 题目 + One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. + + _9_ + / \ + 3 2 + / \ / \ + 4 1 # 6 +/ \ / \ / \ +# # # # # # + + +For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. + + +Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. + +Each comma separated value in the string must be either an integer or a character '#' representing null pointer. + +You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". + +Example 1: +"9,3,4,#,#,1,#,#,2,#,6,#,#" +Return true +Example 2: +"1,#" +Return false +Example 3: +"9,#,#,1" +Return false + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0332.reconstruct-itinerary/README.md b/Algorithms/0332.reconstruct-itinerary/README.md index 6017ff667..7dba6ba8c 100755 --- a/Algorithms/0332.reconstruct-itinerary/README.md +++ b/Algorithms/0332.reconstruct-itinerary/README.md @@ -2,6 +2,31 @@ ## 题目 + Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK. + + +Note: + +If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"]. +All airports are represented by three capital letters (IATA code). +You may assume all tickets form at least one valid itinerary. + + + + + Example 1: + tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]] + Return ["JFK", "MUC", "LHR", "SFO", "SJC"]. + + + Example 2: + tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] + Return ["JFK","ATL","JFK","SFO","ATL","SFO"]. + Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order. + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0334.increasing-triplet-subsequence/README.md b/Algorithms/0334.increasing-triplet-subsequence/README.md index 7cd0867c7..76c190956 100755 --- a/Algorithms/0334.increasing-triplet-subsequence/README.md +++ b/Algorithms/0334.increasing-triplet-subsequence/README.md @@ -2,6 +2,31 @@ ## 题目 + +Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. + + +Formally the function should: +Return true if there exists i, j, k +such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 +else return false. + + + +Your algorithm should run in O(n) time complexity and O(1) space complexity. + + +Examples: +Given [1, 2, 3, 4, 5], +return true. + + +Given [5, 4, 3, 2, 1], +return false. + + +Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0335.self-crossing/README.md b/Algorithms/0335.self-crossing/README.md index 78a245bc0..ffe4c362a 100755 --- a/Algorithms/0335.self-crossing/README.md +++ b/Algorithms/0335.self-crossing/README.md @@ -2,6 +2,54 @@ ## 题目 + + You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, + x[2] metres to the south, + x[3] metres to the east and so on. In other words, after each move your direction changes + counter-clockwise. + + + Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not. + + + +Example 1: +Given x = [2, 1, 1, 2], +????? +? ? +???????> + ? + +Return true (self crossing) + + + + +Example 2: +Given x = [1, 2, 3, 4], +???????? +? ? +? +? +?????????????> + +Return false (not self crossing) + + + + +Example 3: +Given x = [1, 1, 1, 1], +????? +? ? +?????> + +Return true (self crossing) + + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0336.palindrome-pairs/README.md b/Algorithms/0336.palindrome-pairs/README.md index 1640a3224..8f649322e 100755 --- a/Algorithms/0336.palindrome-pairs/README.md +++ b/Algorithms/0336.palindrome-pairs/README.md @@ -2,6 +2,25 @@ ## 题目 + + Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. + + + + Example 1: + Given words = ["bat", "tab", "cat"] + Return [[0, 1], [1, 0]] + The palindromes are ["battab", "tabbat"] + + + Example 2: + Given words = ["abcd", "dcba", "lls", "s", "sssll"] + Return [[0, 1], [1, 0], [3, 2], [2, 4]] + The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"] + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0337.house-robber-iii/README.md b/Algorithms/0337.house-robber-iii/README.md index 7d375fe23..746c0b1ca 100755 --- a/Algorithms/0337.house-robber-iii/README.md +++ b/Algorithms/0337.house-robber-iii/README.md @@ -2,6 +2,36 @@ ## 题目 + +The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night. + + + +Determine the maximum amount of money the thief can rob tonight without alerting the police. + + +Example 1: + 3 + / \ + 2 3 + \ \ + 3 1 + +Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. + + +Example 2: + 3 + / \ + 4 5 + / \ \ + 1 3 1 + +Maximum amount of money the thief can rob = 4 + 5 = 9. + + +Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0338.counting-bits/README.md b/Algorithms/0338.counting-bits/README.md index ed5a1367d..49559c3ef 100755 --- a/Algorithms/0338.counting-bits/README.md +++ b/Algorithms/0338.counting-bits/README.md @@ -2,6 +2,23 @@ ## 题目 + Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. + + +Example: +For num = 5 you should return [0,1,1,2,1,2]. + + +Follow up: + +It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass? +Space complexity should be O(n). +Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language. + + + +Credits:Special thanks to @ syedee for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0341.flatten-nested-list-iterator/README.md b/Algorithms/0341.flatten-nested-list-iterator/README.md index cba3923b4..b7fe740e2 100755 --- a/Algorithms/0341.flatten-nested-list-iterator/README.md +++ b/Algorithms/0341.flatten-nested-list-iterator/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a nested list of integers, implement an iterator to flatten it. + +Each element is either an integer, or a list -- whose elements may also be integers or other lists. + +Example 1: +Given the list [[1,1],2,[1,1]], + +By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. + + + +Example 2: +Given the list [1,[4,[6]]], + +By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]. + + + ## 解题思路 diff --git a/Algorithms/0342.power-of-four/README.md b/Algorithms/0342.power-of-four/README.md index b5f3ed90d..38a0b455a 100755 --- a/Algorithms/0342.power-of-four/README.md +++ b/Algorithms/0342.power-of-four/README.md @@ -2,6 +2,18 @@ ## 题目 + +Given an integer (signed 32 bits), write a function to check whether it is a power of 4. + +Example: +Given num = 16, return true. +Given num = 5, return false. + + +Follow up: Could you solve it without loops/recursion? + +Credits:Special thanks to @yukuairoy for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0343.integer-break/README.md b/Algorithms/0343.integer-break/README.md index 603f75bb3..aeee808cd 100755 --- a/Algorithms/0343.integer-break/README.md +++ b/Algorithms/0343.integer-break/README.md @@ -2,6 +2,20 @@ ## 题目 + +Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. + + + +For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). + + + +Note: You may assume that n is not less than 2 and not larger than 58. + + +Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0344.reverse-string/README.md b/Algorithms/0344.reverse-string/README.md index f1153c10c..7c8d653b7 100755 --- a/Algorithms/0344.reverse-string/README.md +++ b/Algorithms/0344.reverse-string/README.md @@ -2,6 +2,13 @@ ## 题目 + Write a function that takes a string as input and returns the string reversed. + + +Example: +Given s = "hello", return "olleh". + + ## 解题思路 diff --git a/Algorithms/0345.reverse-vowels-of-a-string/README.md b/Algorithms/0345.reverse-vowels-of-a-string/README.md index 8a44ae6e9..b403d0f08 100755 --- a/Algorithms/0345.reverse-vowels-of-a-string/README.md +++ b/Algorithms/0345.reverse-vowels-of-a-string/README.md @@ -2,6 +2,23 @@ ## 题目 + Write a function that takes a string as input and reverse only the vowels of a string. + + +Example 1: +Given s = "hello", return "holle". + + + +Example 2: +Given s = "leetcode", return "leotcede". + + + +Note: +The vowels does not include the letter "y". + + ## 解题思路 diff --git a/Algorithms/0347.top-k-frequent-elements/README.md b/Algorithms/0347.top-k-frequent-elements/README.md index f68cba58d..b5a9cdbd3 100755 --- a/Algorithms/0347.top-k-frequent-elements/README.md +++ b/Algorithms/0347.top-k-frequent-elements/README.md @@ -2,6 +2,19 @@ ## 题目 + +Given a non-empty array of integers, return the k most frequent elements. + +For example, +Given [1,1,1,2,2,3] and k = 2, return [1,2]. + + +Note: + +You may assume k is always valid, 1 ? k ? number of unique elements. +Your algorithm's time complexity must be better than O(n log n), where n is the array's size. + + ## 解题思路 diff --git a/Algorithms/0349.intersection-of-two-arrays/README.md b/Algorithms/0349.intersection-of-two-arrays/README.md index 659befacc..e20ac4841 100755 --- a/Algorithms/0349.intersection-of-two-arrays/README.md +++ b/Algorithms/0349.intersection-of-two-arrays/README.md @@ -2,6 +2,21 @@ ## 题目 + +Given two arrays, write a function to compute their intersection. + + +Example: +Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. + + +Note: + +Each element in the result must be unique. +The result can be in any order. + + + ## 解题思路 diff --git a/Algorithms/0350.intersection-of-two-arrays-ii/README.md b/Algorithms/0350.intersection-of-two-arrays-ii/README.md index 66920e166..8cc3c156e 100755 --- a/Algorithms/0350.intersection-of-two-arrays-ii/README.md +++ b/Algorithms/0350.intersection-of-two-arrays-ii/README.md @@ -2,6 +2,29 @@ ## 题目 + +Given two arrays, write a function to compute their intersection. + + +Example: +Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. + + +Note: + +Each element in the result should appear as many times as it shows in both arrays. +The result can be in any order. + + + +Follow up: + +What if the given array is already sorted? How would you optimize your algorithm? +What if nums1's size is small compared to nums2's size? Which algorithm is better? +What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once? + + + ## 解题思路 diff --git a/Algorithms/0352.data-stream-as-disjoint-intervals/README.md b/Algorithms/0352.data-stream-as-disjoint-intervals/README.md index ff19d21a9..764a74e24 100755 --- a/Algorithms/0352.data-stream-as-disjoint-intervals/README.md +++ b/Algorithms/0352.data-stream-as-disjoint-intervals/README.md @@ -2,6 +2,22 @@ ## 题目 + Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. + +For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: +[1, 1] +[1, 1], [3, 3] +[1, 1], [3, 3], [7, 7] +[1, 3], [7, 7] +[1, 3], [6, 7] + + +Follow up: +What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size? + + +Credits:Special thanks to @yunhong for adding this problem and creating most of the test cases. + ## 解题思路 diff --git a/Algorithms/0354.russian-doll-envelopes/README.md b/Algorithms/0354.russian-doll-envelopes/README.md index b8e7f1742..bb6961eb7 100755 --- a/Algorithms/0354.russian-doll-envelopes/README.md +++ b/Algorithms/0354.russian-doll-envelopes/README.md @@ -2,6 +2,16 @@ ## 题目 + You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope. + + +What is the maximum number of envelopes can you Russian doll? (put one inside other) + + +Example: +Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]). + + ## 解题思路 diff --git a/Algorithms/0355.design-twitter/README.md b/Algorithms/0355.design-twitter/README.md index 05c3d419b..cf70c3976 100755 --- a/Algorithms/0355.design-twitter/README.md +++ b/Algorithms/0355.design-twitter/README.md @@ -2,6 +2,45 @@ ## 题目 + Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: + + + +postTweet(userId, tweetId): Compose a new tweet. +getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. +follow(followerId, followeeId): Follower follows a followee. +unfollow(followerId, followeeId): Follower unfollows a followee. + + + +Example: +Twitter twitter = new Twitter(); + +// User 1 posts a new tweet (id = 5). +twitter.postTweet(1, 5); + +// User 1's news feed should return a list with 1 tweet id -> [5]. +twitter.getNewsFeed(1); + +// User 1 follows user 2. +twitter.follow(1, 2); + +// User 2 posts a new tweet (id = 6). +twitter.postTweet(2, 6); + +// User 1's news feed should return a list with 2 tweet ids -> [6, 5]. +// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5. +twitter.getNewsFeed(1); + +// User 1 unfollows user 2. +twitter.unfollow(1, 2); + +// User 1's news feed should return a list with 1 tweet id -> [5], +// since user 1 is no longer following user 2. +twitter.getNewsFeed(1); + + + ## 解题思路 diff --git a/Algorithms/0357.count-numbers-with-unique-digits/README.md b/Algorithms/0357.count-numbers-with-unique-digits/README.md index b998c64ca..56a397c97 100755 --- a/Algorithms/0357.count-numbers-with-unique-digits/README.md +++ b/Algorithms/0357.count-numbers-with-unique-digits/README.md @@ -2,6 +2,15 @@ ## 题目 + Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. + + + Example: +Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) + + +Credits:Special thanks to @memoryless for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0363.max-sum-of-rectangle-no-larger-than-k/README.md b/Algorithms/0363.max-sum-of-rectangle-no-larger-than-k/README.md index d3d5c1029..e79f1a45b 100755 --- a/Algorithms/0363.max-sum-of-rectangle-no-larger-than-k/README.md +++ b/Algorithms/0363.max-sum-of-rectangle-no-larger-than-k/README.md @@ -2,6 +2,28 @@ ## 题目 + Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. + +Example: +Given matrix = [ + [1, 0, 1], + [0, -2, 3] +] +k = 2 + + + +The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2). + +Note: + +The rectangle inside the matrix must have an area > 0. +What if the number of rows is much larger than the number of columns? + + + +Credits:Special thanks to @fujiaozhu for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0365.water-and-jug-problem/README.md b/Algorithms/0365.water-and-jug-problem/README.md index 9de589939..042e2b29a 100755 --- a/Algorithms/0365.water-and-jug-problem/README.md +++ b/Algorithms/0365.water-and-jug-problem/README.md @@ -2,6 +2,34 @@ ## 题目 + You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. +You need to determine whether it is possible to measure exactly z litres using these two jugs. + +If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end. + + +Operations allowed: + +Fill any of the jugs completely with water. +Empty any of the jugs. +Pour water from one jug into another till the other jug is completely full or the first jug itself is empty. + + + +Example 1: (From the famous "Die Hard" example) +Input: x = 3, y = 5, z = 4 +Output: True + + + +Example 2: +Input: x = 2, y = 6, z = 5 +Output: False + + + +Credits:Special thanks to @vinod23 for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0367.valid-perfect-square/README.md b/Algorithms/0367.valid-perfect-square/README.md index ab4a037a1..c55235eae 100755 --- a/Algorithms/0367.valid-perfect-square/README.md +++ b/Algorithms/0367.valid-perfect-square/README.md @@ -2,6 +2,26 @@ ## 题目 + Given a positive integer num, write a function which returns True if num is a perfect square else False. + + +Note: Do not use any built-in library function such as sqrt. + + +Example 1: +Input: 16 +Returns: True + + + +Example 2: +Input: 14 +Returns: False + + + +Credits:Special thanks to @elmirap for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0368.largest-divisible-subset/README.md b/Algorithms/0368.largest-divisible-subset/README.md index b32421891..a26752a30 100755 --- a/Algorithms/0368.largest-divisible-subset/README.md +++ b/Algorithms/0368.largest-divisible-subset/README.md @@ -2,6 +2,29 @@ ## 题目 + +Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. + + +If there are multiple solutions, return any subset is fine. + + +Example 1: +nums: [1,2,3] + +Result: [1,2] (of course, [1,3] will also be ok) + + + +Example 2: +nums: [1,2,4,8] + +Result: [1,2,4,8] + + + +Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0371.sum-of-two-integers/README.md b/Algorithms/0371.sum-of-two-integers/README.md index f8ac992a1..a5d17b1f7 100755 --- a/Algorithms/0371.sum-of-two-integers/README.md +++ b/Algorithms/0371.sum-of-two-integers/README.md @@ -2,6 +2,14 @@ ## 题目 + Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. + +Example: +Given a = 1 and b = 2, return 3. + + +Credits:Special thanks to @fujiaozhu for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0372.super-pow/README.md b/Algorithms/0372.super-pow/README.md index 9fe42d657..540f4f78f 100755 --- a/Algorithms/0372.super-pow/README.md +++ b/Algorithms/0372.super-pow/README.md @@ -2,6 +2,28 @@ ## 题目 + +Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. + + +Example1: +a = 2 +b = [3] + +Result: 8 + + + +Example2: +a = 2 +b = [1,0] + +Result: 1024 + + + +Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0373.find-k-pairs-with-smallest-sums/README.md b/Algorithms/0373.find-k-pairs-with-smallest-sums/README.md index 369740e2b..5abda1b7d 100755 --- a/Algorithms/0373.find-k-pairs-with-smallest-sums/README.md +++ b/Algorithms/0373.find-k-pairs-with-smallest-sums/README.md @@ -2,6 +2,47 @@ ## 题目 + +You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. + + +Define a pair (u,v) which consists of one element from the first array and one element from the second array. + +Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums. + + +Example 1: +Given nums1 = [1,7,11], nums2 = [2,4,6], k = 3 + +Return: [1,2],[1,4],[1,6] + +The first 3 pairs are returned from the sequence: +[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6] + + + +Example 2: +Given nums1 = [1,1,2], nums2 = [1,2,3], k = 2 + +Return: [1,1],[1,1] + +The first 2 pairs are returned from the sequence: +[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3] + + + +Example 3: +Given nums1 = [1,2], nums2 = [3], k = 3 + +Return: [1,3],[2,3] + +All possible pairs are returned from the sequence: +[1,3],[2,3] + + + +Credits:Special thanks to @elmirap and @StefanPochmann for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0374.guess-number-higher-or-lower/README.md b/Algorithms/0374.guess-number-higher-or-lower/README.md index b03b9a802..aa2b781ab 100755 --- a/Algorithms/0374.guess-number-higher-or-lower/README.md +++ b/Algorithms/0374.guess-number-higher-or-lower/README.md @@ -2,6 +2,25 @@ ## 题目 + We are playing the Guess Game. The game is as follows: + +I pick a number from 1 to n. You have to guess which number I picked. + +Every time you guess wrong, I'll tell you whether the number is higher or lower. + +You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0): +-1 : My number is lower + 1 : My number is higher + 0 : Congrats! You got it! + + +Example: +n = 10, I pick 6. + +Return 6. + + + ## 解题思路 diff --git a/Algorithms/0375.guess-number-higher-or-lower-ii/README.md b/Algorithms/0375.guess-number-higher-or-lower-ii/README.md index 90ba7cba8..e4a23638e 100755 --- a/Algorithms/0375.guess-number-higher-or-lower-ii/README.md +++ b/Algorithms/0375.guess-number-higher-or-lower-ii/README.md @@ -2,6 +2,32 @@ ## 题目 + We are playing the Guess Game. The game is as follows: + +I pick a number from 1 to n. You have to guess which number I picked. + +Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. + +However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked. + + +Example: +n = 10, I pick 8. + +First round: You guess 5, I tell you that it's higher. You pay $5. +Second round: You guess 7, I tell you that it's higher. You pay $7. +Third round: You guess 9, I tell you that it's lower. You pay $9. + +Game over. 8 is the number I picked. + +You end up paying $5 + $7 + $9 = $21. + + + +Given a particular n ≥ 1, find out how much money you need to have to guarantee a win. + +Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0376.wiggle-subsequence/README.md b/Algorithms/0376.wiggle-subsequence/README.md index cfe0fdd6e..f9585b28f 100755 --- a/Algorithms/0376.wiggle-subsequence/README.md +++ b/Algorithms/0376.wiggle-subsequence/README.md @@ -2,6 +2,32 @@ ## 题目 + A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence. + +For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero. + +Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order. + +Examples: +Input: [1,7,4,9,2,5] +Output: 6 +The entire sequence is a wiggle sequence. + +Input: [1,17,5,10,13,15,10,5,16,8] +Output: 7 +There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8]. + +Input: [1,2,3,4,5,6,7,8,9] +Output: 2 + + + +Follow up: +Can you do it in O(n) time? + + +Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0377.combination-sum-iv/README.md b/Algorithms/0377.combination-sum-iv/README.md index 9bcb64d37..d58315bd7 100755 --- a/Algorithms/0377.combination-sum-iv/README.md +++ b/Algorithms/0377.combination-sum-iv/README.md @@ -2,6 +2,34 @@ ## 题目 + Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. + +Example: +nums = [1, 2, 3] +target = 4 + +The possible combination ways are: +(1, 1, 1, 1) +(1, 1, 2) +(1, 2, 1) +(1, 3) +(2, 1, 1) +(2, 2) +(3, 1) + +Note that different sequences are counted as different combinations. + +Therefore the output is 7. + + + +Follow up: +What if negative numbers are allowed in the given array? +How does it change the problem? +What limitation we need to add to the question to allow negative numbers? + +Credits:Special thanks to @pbrother for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0378.kth-smallest-element-in-a-sorted-matrix/README.md b/Algorithms/0378.kth-smallest-element-in-a-sorted-matrix/README.md index e9c69dab4..6790edea2 100755 --- a/Algorithms/0378.kth-smallest-element-in-a-sorted-matrix/README.md +++ b/Algorithms/0378.kth-smallest-element-in-a-sorted-matrix/README.md @@ -2,6 +2,27 @@ ## 题目 + Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. + + +Note that it is the kth smallest element in the sorted order, not the kth distinct element. + + +Example: +matrix = [ + [ 1, 5, 9], + [10, 11, 13], + [12, 13, 15] +], +k = 8, + +return 13. + + + +Note: +You may assume k is always valid, 1 ? k ? n2. + ## 解题思路 diff --git a/Algorithms/0380.insert-delete-getrandom-o1/README.md b/Algorithms/0380.insert-delete-getrandom-o1/README.md index d8aba8a0f..eb5b99fc1 100755 --- a/Algorithms/0380.insert-delete-getrandom-o1/README.md +++ b/Algorithms/0380.insert-delete-getrandom-o1/README.md @@ -2,6 +2,43 @@ ## 题目 + Design a data structure that supports all following operations in average O(1) time. + + + +insert(val): Inserts an item val to the set if not already present. +remove(val): Removes an item val from the set if present. +getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. + + + +Example: +// Init an empty set. +RandomizedSet randomSet = new RandomizedSet(); + +// Inserts 1 to the set. Returns true as 1 was inserted successfully. +randomSet.insert(1); + +// Returns false as 2 does not exist in the set. +randomSet.remove(2); + +// Inserts 2 to the set, returns true. Set now contains [1,2]. +randomSet.insert(2); + +// getRandom should return either 1 or 2 randomly. +randomSet.getRandom(); + +// Removes 1 from the set, returns true. Set now contains [2]. +randomSet.remove(1); + +// 2 was already in the set, so return false. +randomSet.insert(2); + +// Since 2 is the only number in the set, getRandom always return 2. +randomSet.getRandom(); + + + ## 解题思路 diff --git a/Algorithms/0381.insert-delete-getrandom-o1-duplicates-allowed/README.md b/Algorithms/0381.insert-delete-getrandom-o1-duplicates-allowed/README.md index 6eee3db98..60dc47ef3 100755 --- a/Algorithms/0381.insert-delete-getrandom-o1-duplicates-allowed/README.md +++ b/Algorithms/0381.insert-delete-getrandom-o1-duplicates-allowed/README.md @@ -2,6 +2,40 @@ ## 题目 + Design a data structure that supports all following operations in average O(1) time. +Note: Duplicate elements are allowed. + + +insert(val): Inserts an item val to the collection. +remove(val): Removes an item val from the collection if present. +getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains. + + + +Example: +// Init an empty collection. +RandomizedCollection collection = new RandomizedCollection(); + +// Inserts 1 to the collection. Returns true as the collection did not contain 1. +collection.insert(1); + +// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1]. +collection.insert(1); + +// Inserts 2 to the collection, returns true. Collection now contains [1,1,2]. +collection.insert(2); + +// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3. +collection.getRandom(); + +// Removes 1 from the collection, returns true. Collection now contains [1,2]. +collection.remove(1); + +// getRandom should return 1 and 2 both equally likely. +collection.getRandom(); + + + ## 解题思路 diff --git a/Algorithms/0382.linked-list-random-node/README.md b/Algorithms/0382.linked-list-random-node/README.md index 65ea953a5..98103bf98 100755 --- a/Algorithms/0382.linked-list-random-node/README.md +++ b/Algorithms/0382.linked-list-random-node/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. + +Follow up: +What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? + + +Example: +// Init a singly linked list [1,2,3]. +ListNode head = new ListNode(1); +head.next = new ListNode(2); +head.next.next = new ListNode(3); +Solution solution = new Solution(head); + +// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning. +solution.getRandom(); + + + ## 解题思路 diff --git a/Algorithms/0383.ransom-note/README.md b/Algorithms/0383.ransom-note/README.md index c4daf0988..5eab8ef2c 100755 --- a/Algorithms/0383.ransom-note/README.md +++ b/Algorithms/0383.ransom-note/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom +note can be constructed from the magazines ; otherwise, it will return false. + + +Each letter in the magazine string can only be used once in your ransom note. + + +Note: +You may assume that both strings contain only lowercase letters. + + +canConstruct("a", "b") -> false +canConstruct("aa", "ab") -> false +canConstruct("aa", "aab") -> true + + + ## 解题思路 diff --git a/Algorithms/0384.shuffle-an-array/README.md b/Algorithms/0384.shuffle-an-array/README.md index e43400d2c..1a5315c3d 100755 --- a/Algorithms/0384.shuffle-an-array/README.md +++ b/Algorithms/0384.shuffle-an-array/README.md @@ -2,6 +2,25 @@ ## 题目 + Shuffle a set of numbers without duplicates. + + +Example: +// Init an array with set 1, 2, and 3. +int[] nums = {1,2,3}; +Solution solution = new Solution(nums); + +// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned. +solution.shuffle(); + +// Resets the array back to its original configuration [1,2,3]. +solution.reset(); + +// Returns the random shuffling of array [1,2,3]. +solution.shuffle(); + + + ## 解题思路 diff --git a/Algorithms/0385.mini-parser/README.md b/Algorithms/0385.mini-parser/README.md index 8bcb24c3d..8187e1a04 100755 --- a/Algorithms/0385.mini-parser/README.md +++ b/Algorithms/0385.mini-parser/README.md @@ -2,6 +2,39 @@ ## 题目 + Given a nested list of integers represented as a string, implement a parser to deserialize it. + +Each element is either an integer, or a list -- whose elements may also be integers or other lists. + +Note: +You may assume that the string is well-formed: + +String is non-empty. +String does not contain white spaces. +String contains only digits 0-9, [, - ,, ]. + + + +Example 1: +Given s = "324", + +You should return a NestedInteger object which contains a single integer 324. + + + +Example 2: +Given s = "[123,[456,[789]]]", + +Return a NestedInteger object containing a nested list with 2 elements: + +1. An integer containing value 123. +2. A nested list containing two elements: + i. An integer containing value 456. + ii. A nested list with one element: + a. An integer containing value 789. + + + ## 解题思路 diff --git a/Algorithms/0386.lexicographical-numbers/README.md b/Algorithms/0386.lexicographical-numbers/README.md index 1306428f1..770c6aec8 100755 --- a/Algorithms/0386.lexicographical-numbers/README.md +++ b/Algorithms/0386.lexicographical-numbers/README.md @@ -2,6 +2,18 @@ ## 题目 + +Given an integer n, return 1 - n in lexicographical order. + + + +For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. + + + +Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. + + ## 解题思路 diff --git a/Algorithms/0387.first-unique-character-in-a-string/README.md b/Algorithms/0387.first-unique-character-in-a-string/README.md index bd0924c7e..0ec30fece 100755 --- a/Algorithms/0387.first-unique-character-in-a-string/README.md +++ b/Algorithms/0387.first-unique-character-in-a-string/README.md @@ -2,6 +2,22 @@ ## 题目 + +Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. + +Examples: +s = "leetcode" +return 0. + +s = "loveleetcode", +return 2. + + + + +Note: You may assume the string contain only lowercase letters. + + ## 解题思路 diff --git a/Algorithms/0388.longest-absolute-file-path/README.md b/Algorithms/0388.longest-absolute-file-path/README.md index ea5f3b0ce..54a701efb 100755 --- a/Algorithms/0388.longest-absolute-file-path/README.md +++ b/Algorithms/0388.longest-absolute-file-path/README.md @@ -2,6 +2,46 @@ ## 题目 + Suppose we abstract our file system by a string in the following manner: + +The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: + +dir + subdir1 + subdir2 + file.ext + + +The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext. + +The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents: + +dir + subdir1 + file1.ext + subsubdir1 + subdir2 + subsubdir2 + file2.ext + + +The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext. + +We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes). + +Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0. + +Note: + +The name of a file contains at least a . and an extension. +The name of a directory or sub-directory will not contain a .. + + + +Time complexity required: O(n) where n is the size of the input string. + +Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png. + ## 解题思路 diff --git a/Algorithms/0389.find-the-difference/README.md b/Algorithms/0389.find-the-difference/README.md index c5a92c0f4..d15f4b715 100755 --- a/Algorithms/0389.find-the-difference/README.md +++ b/Algorithms/0389.find-the-difference/README.md @@ -2,6 +2,25 @@ ## 题目 + +Given two strings s and t which consist of only lowercase letters. + +String t is generated by random shuffling string s and then add one more letter at a random position. + +Find the letter that was added in t. + +Example: +Input: +s = "abcd" +t = "abcde" + +Output: +e + +Explanation: +'e' is the letter that was added. + + ## 解题思路 diff --git a/Algorithms/0390.elimination-game/README.md b/Algorithms/0390.elimination-game/README.md index b063fa547..b10687108 100755 --- a/Algorithms/0390.elimination-game/README.md +++ b/Algorithms/0390.elimination-game/README.md @@ -2,6 +2,28 @@ ## 题目 + +There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list. + +Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers. + +We keep repeating the steps again, alternating left to right and right to left, until a single number remains. + +Find the last number that remains starting with a list of length n. + +Example: +Input: +n = 9, +1 2 3 4 5 6 7 8 9 +2 4 6 8 +2 6 +6 + +Output: +6 + + + ## 解题思路 diff --git a/Algorithms/0391.perfect-rectangle/README.md b/Algorithms/0391.perfect-rectangle/README.md index 0c8b1bb8d..09dff4a52 100755 --- a/Algorithms/0391.perfect-rectangle/README.md +++ b/Algorithms/0391.perfect-rectangle/README.md @@ -2,6 +2,75 @@ ## 题目 + +Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. + + + +Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)). + + + +Example 1: +rectangles = [ + [1,1,3,3], + [3,1,4,2], + [3,2,4,4], + [1,3,2,4], + [2,3,3,4] +] + +Return true. All 5 rectangles together form an exact cover of a rectangular region. + + + + + + +Example 2: +rectangles = [ + [1,1,2,3], + [1,3,2,4], + [3,1,4,2], + [3,2,4,4] +] + +Return false. Because there is a gap between the two rectangular regions. + + + + + + +Example 3: +rectangles = [ + [1,1,3,3], + [3,1,4,2], + [1,3,2,4], + [3,2,4,4] +] + +Return false. Because there is a gap in the top center. + + + + + + +Example 4: +rectangles = [ + [1,1,3,3], + [3,1,4,2], + [1,3,2,4], + [2,2,4,4] +] + +Return false. Because two of the rectangles overlap with each other. + + + + + ## 解题思路 diff --git a/Algorithms/0392.is-subsequence/README.md b/Algorithms/0392.is-subsequence/README.md index 43737b616..94eb52f71 100755 --- a/Algorithms/0392.is-subsequence/README.md +++ b/Algorithms/0392.is-subsequence/README.md @@ -2,6 +2,37 @@ ## 题目 + +Given a string s and a string t, check if s is subsequence of t. + + + +You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100). + + + +A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). + + +Example 1: +s = "abc", t = "ahbgdc" + + +Return true. + + +Example 2: +s = "axc", t = "ahbgdc" + + +Return false. + + +Follow up: +If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? + +Credits:Special thanks to @pbrother for adding this problem and creating all test cases. + ## 解题思路 diff --git a/Algorithms/0393.utf-8-validation/README.md b/Algorithms/0393.utf-8-validation/README.md index d5eac5cc1..c8a90b64c 100755 --- a/Algorithms/0393.utf-8-validation/README.md +++ b/Algorithms/0393.utf-8-validation/README.md @@ -2,6 +2,49 @@ ## 题目 + A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: + +For 1-byte character, the first bit is a 0, followed by its unicode code. +For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10. + +This is how the UTF-8 encoding would work: + + Char. number range | UTF-8 octet sequence + (hexadecimal) | (binary) + --------------------+--------------------------------------------- + 0000 0000-0000 007F | 0xxxxxxx + 0000 0080-0000 07FF | 110xxxxx 10xxxxxx + 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx + 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + + +Given an array of integers representing the data, return whether it is a valid utf-8 encoding. + + +Note: +The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data. + + + +Example 1: +data = [197, 130, 1], which represents the octet sequence: 11000101 10000010 00000001. + +Return true. +It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character. + + + + +Example 2: +data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100. + +Return false. +The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character. +The next byte is a continuation byte which starts with 10 and that's correct. +But the second continuation byte does not start with 10, so it is invalid. + + + ## 解题思路 diff --git a/Algorithms/0394.decode-string/README.md b/Algorithms/0394.decode-string/README.md index a3b125eac..6fd6c8d73 100755 --- a/Algorithms/0394.decode-string/README.md +++ b/Algorithms/0394.decode-string/README.md @@ -2,6 +2,25 @@ ## 题目 + +Given an encoded string, return it's decoded string. + + +The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. + + +You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. + +Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. + + +Examples: +s = "3[a]2[bc]", return "aaabcbc". +s = "3[a2[c]]", return "accaccacc". +s = "2[abc]3[cd]ef", return "abcabccdcdcdef". + + + ## 解题思路 diff --git a/Algorithms/0395.longest-substring-with-at-least-k-repeating-characters/README.md b/Algorithms/0395.longest-substring-with-at-least-k-repeating-characters/README.md index e38d6e03e..c567fcca9 100755 --- a/Algorithms/0395.longest-substring-with-at-least-k-repeating-characters/README.md +++ b/Algorithms/0395.longest-substring-with-at-least-k-repeating-characters/README.md @@ -2,6 +2,32 @@ ## 题目 + +Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times. + + +Example 1: +Input: +s = "aaabb", k = 3 + +Output: +3 + +The longest substring is "aaa", as 'a' is repeated 3 times. + + + +Example 2: +Input: +s = "ababbc", k = 2 + +Output: +5 + +The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times. + + + ## 解题思路 diff --git a/Algorithms/0396.rotate-function/README.md b/Algorithms/0396.rotate-function/README.md index 5053a7cb0..82277dec7 100755 --- a/Algorithms/0396.rotate-function/README.md +++ b/Algorithms/0396.rotate-function/README.md @@ -2,6 +2,36 @@ ## 题目 + +Given an array of integers A and let n to be its length. + + + +Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: + + + +F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]. + +Calculate the maximum value of F(0), F(1), ..., F(n-1). + + +Note: +n is guaranteed to be less than 105. + + +Example: +A = [4, 3, 2, 6] + +F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25 +F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16 +F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23 +F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26 + +So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26. + + + ## 解题思路 diff --git a/Algorithms/0397.integer-replacement/README.md b/Algorithms/0397.integer-replacement/README.md index 723550367..09e2f8498 100755 --- a/Algorithms/0397.integer-replacement/README.md +++ b/Algorithms/0397.integer-replacement/README.md @@ -2,6 +2,49 @@ ## 题目 + +Given a positive integer n and you can do operations as follow: + + + + +If n is even, replace n with n/2. +If n is odd, you can replace n with either n + 1 or n - 1. + + + + +What is the minimum number of replacements needed for n to become 1? + + + + +Example 1: +Input: +8 + +Output: +3 + +Explanation: +8 -> 4 -> 2 -> 1 + + + +Example 2: +Input: +7 + +Output: +4 + +Explanation: +7 -> 8 -> 4 -> 2 -> 1 +or +7 -> 6 -> 3 -> 2 -> 1 + + + ## 解题思路 diff --git a/Algorithms/0398.random-pick-index/README.md b/Algorithms/0398.random-pick-index/README.md index 7081ee0d2..279dd06e7 100755 --- a/Algorithms/0398.random-pick-index/README.md +++ b/Algorithms/0398.random-pick-index/README.md @@ -2,6 +2,27 @@ ## 题目 + +Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. + + + +Note: +The array size can be very large. Solution that uses too much extra space will not pass the judge. + + +Example: +int[] nums = new int[] {1,2,3,3,3}; +Solution solution = new Solution(nums); + +// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning. +solution.pick(3); + +// pick(1) should return 0. Since in the array only nums[0] is equal to 1. +solution.pick(1); + + + ## 解题思路 diff --git a/Algorithms/0399.evaluate-division/README.md b/Algorithms/0399.evaluate-division/README.md index e8879dddf..80f4dafb1 100755 --- a/Algorithms/0399.evaluate-division/README.md +++ b/Algorithms/0399.evaluate-division/README.md @@ -2,6 +2,26 @@ ## 题目 + +Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0. + +Example: +Given a / b = 2.0, b / c = 3.0. queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . return [6.0, 0.5, -1.0, 1.0, -1.0 ]. + + +The input is: vector> equations, vector& values, vector> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector. + + +According to the example above: +equations = [ ["a", "b"], ["b", "c"] ], +values = [2.0, 3.0], +queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. + + + +The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction. + + ## 解题思路 diff --git a/Algorithms/0400.nth-digit/README.md b/Algorithms/0400.nth-digit/README.md index 06c96369b..998c8f17f 100755 --- a/Algorithms/0400.nth-digit/README.md +++ b/Algorithms/0400.nth-digit/README.md @@ -2,6 +2,33 @@ ## 题目 + Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... + +Note: +n is positive and will fit within the range of a 32-bit signed integer (n < 231). + + +Example 1: +Input: +3 + +Output: +3 + + + +Example 2: +Input: +11 + +Output: +0 + +Explanation: +The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10. + + + ## 解题思路 diff --git a/Algorithms/0401.binary-watch/README.md b/Algorithms/0401.binary-watch/README.md index 885c3d6a5..5b5c15295 100755 --- a/Algorithms/0401.binary-watch/README.md +++ b/Algorithms/0401.binary-watch/README.md @@ -2,6 +2,25 @@ ## 题目 + A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). +Each LED represents a zero or one, with the least significant bit on the right. + +For example, the above binary watch reads "3:25". + +Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent. + +Example: +Input: n = 1Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"] + + +Note: + +The order of output does not matter. +The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00". +The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02". + + + ## 解题思路 diff --git a/Algorithms/0402.remove-k-digits/README.md b/Algorithms/0402.remove-k-digits/README.md index d1d5f571c..42c5cf096 100755 --- a/Algorithms/0402.remove-k-digits/README.md +++ b/Algorithms/0402.remove-k-digits/README.md @@ -2,6 +2,38 @@ ## 题目 + Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. + + +Note: + +The length of num is less than 10002 and will be ≥ k. +The given num does not contain any leading zero. + + + + +Example 1: +Input: num = "1432219", k = 3 +Output: "1219" +Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest. + + + +Example 2: +Input: num = "10200", k = 1 +Output: "200" +Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes. + + + +Example 3: +Input: num = "10", k = 2 +Output: "0" +Explanation: Remove all the digits from the number and it is left with nothing which is 0. + + + ## 解题思路 diff --git a/Algorithms/0403.frog-jump/README.md b/Algorithms/0403.frog-jump/README.md index afbb898d4..2e67cdf86 100755 --- a/Algorithms/0403.frog-jump/README.md +++ b/Algorithms/0403.frog-jump/README.md @@ -2,6 +2,44 @@ ## 题目 + A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. + +Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit. + + +If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction. + +Note: + +The number of stones is ≥ 2 and is < 1,100. +Each stone's position will be a non-negative integer < 231. +The first stone's position is always 0. + + + +Example 1: +[0,1,3,5,6,8,12,17] + +There are a total of 8 stones. +The first stone at the 0th unit, second stone at the 1st unit, +third stone at the 3rd unit, and so on... +The last stone at the 17th unit. + +Return true. The frog can jump to the last stone by jumping +1 unit to the 2nd stone, then 2 units to the 3rd stone, then +2 units to the 4th stone, then 3 units to the 6th stone, +4 units to the 7th stone, and 5 units to the 8th stone. + + + +Example 2: +[0,1,2,3,4,8,9,11] + +Return false. There is no way to jump to the last stone as +the gap between the 5th and 6th stone is too large. + + + ## 解题思路 diff --git a/Algorithms/0404.sum-of-left-leaves/README.md b/Algorithms/0404.sum-of-left-leaves/README.md index 2dce0644f..ca549f88a 100755 --- a/Algorithms/0404.sum-of-left-leaves/README.md +++ b/Algorithms/0404.sum-of-left-leaves/README.md @@ -2,6 +2,19 @@ ## 题目 + Find the sum of all left leaves in a given binary tree. + +Example: + 3 + / \ + 9 20 + / \ + 15 7 + +There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. + + + ## 解题思路 diff --git a/Algorithms/0405.convert-a-number-to-hexadecimal/README.md b/Algorithms/0405.convert-a-number-to-hexadecimal/README.md index 210b9a1cb..d5b378a31 100755 --- a/Algorithms/0405.convert-a-number-to-hexadecimal/README.md +++ b/Algorithms/0405.convert-a-number-to-hexadecimal/README.md @@ -2,6 +2,37 @@ ## 题目 + +Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used. + + +Note: + +All letters in hexadecimal (a-f) must be in lowercase. +The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character. +The given number is guaranteed to fit within the range of a 32-bit signed integer. +You must not use any method provided by the library which converts/formats the number to hex directly. + + + +Example 1: +Input: +26 + +Output: +"1a" + + + +Example 2: +Input: +-1 + +Output: +"ffffffff" + + + ## 解题思路 diff --git a/Algorithms/0406.queue-reconstruction-by-height/README.md b/Algorithms/0406.queue-reconstruction-by-height/README.md index 9a1c2553a..3f5061f15 100755 --- a/Algorithms/0406.queue-reconstruction-by-height/README.md +++ b/Algorithms/0406.queue-reconstruction-by-height/README.md @@ -2,6 +2,22 @@ ## 题目 + Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. + + +Note: +The number of people is less than 1,100. + + +Example +Input: +[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] + +Output: +[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] + + + ## 解题思路 diff --git a/Algorithms/0407.trapping-rain-water-ii/README.md b/Algorithms/0407.trapping-rain-water-ii/README.md index 63f282bc7..238c4128e 100755 --- a/Algorithms/0407.trapping-rain-water-ii/README.md +++ b/Algorithms/0407.trapping-rain-water-ii/README.md @@ -2,6 +2,35 @@ ## 题目 + Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. + + +Note: +Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000. + + +Example: +Given the following 3x6 height map: +[ + [1,4,3,1,3,2], + [3,2,1,3,2,4], + [2,3,3,2,3,1] +] + +Return 4. + + + + + +The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain. + + + + +After the rain, water are trapped between the blocks. The total volume of water trapped is 4. + + ## 解题思路 diff --git a/Algorithms/0409.longest-palindrome/README.md b/Algorithms/0409.longest-palindrome/README.md index 3f086498a..60b543770 100755 --- a/Algorithms/0409.longest-palindrome/README.md +++ b/Algorithms/0409.longest-palindrome/README.md @@ -2,6 +2,26 @@ ## 题目 + Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. + +This is case sensitive, for example "Aa" is not considered a palindrome here. + +Note: +Assume the length of given string will not exceed 1,010. + + +Example: +Input: +"abccccdd" + +Output: +7 + +Explanation: +One longest palindrome that can be built is "dccaccd", whose length is 7. + + + ## 解题思路 diff --git a/Algorithms/0410.split-array-largest-sum/README.md b/Algorithms/0410.split-array-largest-sum/README.md index 88d8d1a1a..428196723 100755 --- a/Algorithms/0410.split-array-largest-sum/README.md +++ b/Algorithms/0410.split-array-largest-sum/README.md @@ -2,6 +2,32 @@ ## 题目 + Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. + + +Note: +If n is the length of array, assume the following constraints are satisfied: + +1 ≤ n ≤ 1000 +1 ≤ m ≤ min(50, n) + + + +Examples: +Input: +nums = [7,2,5,10,8] +m = 2 + +Output: +18 + +Explanation: +There are four ways to split nums into two subarrays. +The best way is to split it into [7,2,5] and [10,8], +where the largest sum among the two subarrays is only 18. + + + ## 解题思路 diff --git a/Algorithms/0412.fizz-buzz/README.md b/Algorithms/0412.fizz-buzz/README.md index 0fcbd0e9d..ed1fe6486 100755 --- a/Algorithms/0412.fizz-buzz/README.md +++ b/Algorithms/0412.fizz-buzz/README.md @@ -2,6 +2,34 @@ ## 题目 + Write a program that outputs the string representation of numbers from 1 to n. + +But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. + +Example: +n = 15, + +Return: +[ + "1", + "2", + "Fizz", + "4", + "Buzz", + "Fizz", + "7", + "8", + "Fizz", + "Buzz", + "11", + "Fizz", + "13", + "14", + "FizzBuzz" +] + + + ## 解题思路 diff --git a/Algorithms/0413.arithmetic-slices/README.md b/Algorithms/0413.arithmetic-slices/README.md index 0b2d62405..380a8f98d 100755 --- a/Algorithms/0413.arithmetic-slices/README.md +++ b/Algorithms/0413.arithmetic-slices/README.md @@ -2,6 +2,30 @@ ## 题目 + A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. + +For example, these are arithmetic sequence: +1, 3, 5, 7, 9 +7, 7, 7, 7 +3, -1, -5, -9 + +The following sequence is not arithmetic. 1, 1, 2, 5, 7 + + +A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N. + +A slice (P, Q) of array A is called arithmetic if the sequence: + A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. + +The function should return the number of arithmetic slices in the array A. + + +Example: +A = [1, 2, 3, 4] + +return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself. + + ## 解题思路 diff --git a/Algorithms/0414.third-maximum-number/README.md b/Algorithms/0414.third-maximum-number/README.md index 4dde4af77..70db7004a 100755 --- a/Algorithms/0414.third-maximum-number/README.md +++ b/Algorithms/0414.third-maximum-number/README.md @@ -2,6 +2,36 @@ ## 题目 + Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). + +Example 1: +Input: [3, 2, 1] + +Output: 1 + +Explanation: The third maximum is 1. + + + +Example 2: +Input: [1, 2] + +Output: 2 + +Explanation: The third maximum does not exist, so the maximum (2) is returned instead. + + + +Example 3: +Input: [2, 2, 3, 1] + +Output: 1 + +Explanation: Note that the third maximum here means the third maximum distinct number. +Both numbers with value 2 are both considered as second maximum. + + + ## 解题思路 diff --git a/Algorithms/0415.add-strings/README.md b/Algorithms/0415.add-strings/README.md index 1a0217d5b..76170c3d3 100755 --- a/Algorithms/0415.add-strings/README.md +++ b/Algorithms/0415.add-strings/README.md @@ -2,6 +2,17 @@ ## 题目 + Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. + +Note: + +The length of both num1 and num2 is < 5100. +Both num1 and num2 contains only digits 0-9. +Both num1 and num2 does not contain any leading zero. +You must not use any built-in BigInteger library or convert the inputs to integer directly. + + + ## 解题思路 diff --git a/Algorithms/0416.partition-equal-subset-sum/README.md b/Algorithms/0416.partition-equal-subset-sum/README.md index 2f9d9e9b2..8008a1811 100755 --- a/Algorithms/0416.partition-equal-subset-sum/README.md +++ b/Algorithms/0416.partition-equal-subset-sum/README.md @@ -2,6 +2,34 @@ ## 题目 + Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. + + +Note: + +Each of the array element will not exceed 100. +The array size will not exceed 200. + + + +Example 1: +Input: [1, 5, 11, 5] + +Output: true + +Explanation: The array can be partitioned as [1, 5, 5] and [11]. + + + +Example 2: +Input: [1, 2, 3, 5] + +Output: false + +Explanation: The array cannot be partitioned into equal sum subsets. + + + ## 解题思路 diff --git a/Algorithms/0417.pacific-atlantic-water-flow/README.md b/Algorithms/0417.pacific-atlantic-water-flow/README.md index fb023146e..a55dfc73c 100755 --- a/Algorithms/0417.pacific-atlantic-water-flow/README.md +++ b/Algorithms/0417.pacific-atlantic-water-flow/README.md @@ -2,6 +2,35 @@ ## 题目 + Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges. + +Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower. + +Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean. + +Note: + +The order of returned grid coordinates does not matter. +Both m and n are less than 150. + + +Example: +Given the following 5x5 matrix: + + Pacific ~ ~ ~ ~ ~ + ~ 1 2 2 3 (5) * + ~ 3 2 3 (4) (4) * + ~ 2 4 (5) 3 1 * + ~ (6) (7) 1 4 5 * + ~ (5) 1 1 2 4 * + * * * * * Atlantic + +Return: + +[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix). + + + ## 解题思路 diff --git a/Algorithms/0419.battleships-in-a-board/README.md b/Algorithms/0419.battleships-in-a-board/README.md index ff0dee732..17d2af857 100755 --- a/Algorithms/0419.battleships-in-a-board/README.md +++ b/Algorithms/0419.battleships-in-a-board/README.md @@ -2,6 +2,30 @@ ## 题目 + Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: + + +You receive a valid board, made of only battleships or empty slots. +Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size. +At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships. + + +Example: +X..X +...X +...X + +In the above board there are 2 battleships. + +Invalid Example: +...X +XXXX +...X + +This is an invalid board that you will not receive - as battleships will always have a cell separating between them. + +Follow up:Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board? + ## 解题思路 diff --git a/Algorithms/0420.strong-password-checker/README.md b/Algorithms/0420.strong-password-checker/README.md index 897265cae..1689b77d7 100755 --- a/Algorithms/0420.strong-password-checker/README.md +++ b/Algorithms/0420.strong-password-checker/README.md @@ -2,6 +2,18 @@ ## 题目 + A password is considered strong if below conditions are all met: + + + It has at least 6 characters and at most 20 characters. + It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit. + It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met). + + +Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM change required to make s a strong password. If s is already strong, return 0. + +Insertion, deletion or replace of any one character are all considered as one change. + ## 解题思路 diff --git a/Algorithms/0421.maximum-xor-of-two-numbers-in-an-array/README.md b/Algorithms/0421.maximum-xor-of-two-numbers-in-an-array/README.md index 4b2ac4360..bdbd505ac 100755 --- a/Algorithms/0421.maximum-xor-of-two-numbers-in-an-array/README.md +++ b/Algorithms/0421.maximum-xor-of-two-numbers-in-an-array/README.md @@ -2,6 +2,21 @@ ## 题目 + Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. + +Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. + +Could you do this in O(n) runtime? + +Example: +Input: [3, 10, 5, 25, 2, 8] + +Output: 28 + +Explanation: The maximum result is 5 ^ 25 = 28. + + + ## 解题思路 diff --git a/Algorithms/0423.reconstruct-original-digits-from-english/README.md b/Algorithms/0423.reconstruct-original-digits-from-english/README.md index 113552833..51b14358a 100755 --- a/Algorithms/0423.reconstruct-original-digits-from-english/README.md +++ b/Algorithms/0423.reconstruct-original-digits-from-english/README.md @@ -2,6 +2,30 @@ ## 题目 + Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. + +Note: + +Input contains only lowercase English letters. +Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted. +Input length is less than 50,000. + + + +Example 1: +Input: "owoztneoer" + +Output: "012" + + + +Example 2: +Input: "fviefuro" + +Output: "45" + + + ## 解题思路 diff --git a/Algorithms/0424.longest-repeating-character-replacement/README.md b/Algorithms/0424.longest-repeating-character-replacement/README.md index 5507179c2..1eb2740f9 100755 --- a/Algorithms/0424.longest-repeating-character-replacement/README.md +++ b/Algorithms/0424.longest-repeating-character-replacement/README.md @@ -2,6 +2,39 @@ ## 题目 + Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations. + +Note: +Both the string's length and k will not exceed 104. + + + +Example 1: +Input: +s = "ABAB", k = 2 + +Output: +4 + +Explanation: +Replace the two 'A's with two 'B's or vice versa. + + + + +Example 2: +Input: +s = "AABABBA", k = 1 + +Output: +4 + +Explanation: +Replace the one 'A' in the middle with 'B' and form "AABBBBA". +The substring "BBBB" has the longest repeating letters, which is 4. + + + ## 解题思路 diff --git a/Algorithms/0432.all-oone-data-structure/README.md b/Algorithms/0432.all-oone-data-structure/README.md index 7231ef72e..64f3b206e 100755 --- a/Algorithms/0432.all-oone-data-structure/README.md +++ b/Algorithms/0432.all-oone-data-structure/README.md @@ -2,6 +2,21 @@ ## 题目 + Implement a data structure supporting the following operations: + + + +Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string. +Dec(Key) - If Key's value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty string. +GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string "". +GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string "". + + + + +Challenge: Perform all these in O(1) time complexity. + + ## 解题思路 diff --git a/Algorithms/0434.number-of-segments-in-a-string/README.md b/Algorithms/0434.number-of-segments-in-a-string/README.md index 09043a213..04f773aa8 100755 --- a/Algorithms/0434.number-of-segments-in-a-string/README.md +++ b/Algorithms/0434.number-of-segments-in-a-string/README.md @@ -2,6 +2,16 @@ ## 题目 + Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. + +Please note that the string does not contain any non-printable characters. + +Example: +Input: "Hello, my name is John" +Output: 5 + + + ## 解题思路 diff --git a/Algorithms/0435.non-overlapping-intervals/README.md b/Algorithms/0435.non-overlapping-intervals/README.md index b758aa74f..ce52b43c8 100755 --- a/Algorithms/0435.non-overlapping-intervals/README.md +++ b/Algorithms/0435.non-overlapping-intervals/README.md @@ -2,6 +2,44 @@ ## 题目 + +Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. + + +Note: + +You may assume the interval's end point is always bigger than its start point. +Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other. + + + +Example 1: +Input: [ [1,2], [2,3], [3,4], [1,3] ] + +Output: 1 + +Explanation: [1,3] can be removed and the rest of intervals are non-overlapping. + + + +Example 2: +Input: [ [1,2], [1,2], [1,2] ] + +Output: 2 + +Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping. + + + +Example 3: +Input: [ [1,2], [2,3] ] + +Output: 0 + +Explanation: You don't need to remove any of the intervals since they're already non-overlapping. + + + ## 解题思路 diff --git a/Algorithms/0436.find-right-interval/README.md b/Algorithms/0436.find-right-interval/README.md index 68ae03247..079644fe1 100755 --- a/Algorithms/0436.find-right-interval/README.md +++ b/Algorithms/0436.find-right-interval/README.md @@ -2,6 +2,51 @@ ## 题目 + +Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i. + + + +For any interval i, you need to store the minimum interval j's index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array. + + +Note: + +You may assume the interval's end point is always bigger than its start point. +You may assume none of these intervals have the same start point. + + + +Example 1: +Input: [ [1,2] ] + +Output: [-1] + +Explanation: There is only one interval in the collection, so it outputs -1. + + + +Example 2: +Input: [ [3,4], [2,3], [1,2] ] + +Output: [-1, 0, 1] + +Explanation: There is no satisfied "right" interval for [3,4]. +For [2,3], the interval [3,4] has minimum-"right" start point; +For [1,2], the interval [2,3] has minimum-"right" start point. + + + +Example 3: +Input: [ [1,4], [2,3], [3,4] ] + +Output: [-1, 2, -1] + +Explanation: There is no satisfied "right" interval for [1,4] and [3,4]. +For [2,3], the interval [3,4] has minimum-"right" start point. + + + ## 解题思路 diff --git a/Algorithms/0437.path-sum-iii/README.md b/Algorithms/0437.path-sum-iii/README.md index 2d87fe9a8..39e73b7a1 100755 --- a/Algorithms/0437.path-sum-iii/README.md +++ b/Algorithms/0437.path-sum-iii/README.md @@ -2,6 +2,34 @@ ## 题目 + You are given a binary tree in which each node contains an integer value. + +Find the number of paths that sum to a given value. + +The path does not need to start or end at the root or a leaf, but it must go downwards +(traveling only from parent nodes to child nodes). + +The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000. + +Example: +root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 + + 10 + / \ + 5 -3 + / \ \ + 3 2 11 + / \ \ +3 -2 1 + +Return 3. The paths that sum to 8 are: + +1. 5 -> 3 +2. 5 -> 2 -> 1 +3. -3 -> 11 + + + ## 解题思路 diff --git a/Algorithms/0438.find-all-anagrams-in-a-string/README.md b/Algorithms/0438.find-all-anagrams-in-a-string/README.md index 21a564f73..798dd88a6 100755 --- a/Algorithms/0438.find-all-anagrams-in-a-string/README.md +++ b/Algorithms/0438.find-all-anagrams-in-a-string/README.md @@ -2,6 +2,39 @@ ## 题目 + Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. + +Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. + +The order of output does not matter. + +Example 1: +Input: +s: "cbaebabacd" p: "abc" + +Output: +[0, 6] + +Explanation: +The substring with start index = 0 is "cba", which is an anagram of "abc". +The substring with start index = 6 is "bac", which is an anagram of "abc". + + + +Example 2: +Input: +s: "abab" p: "ab" + +Output: +[0, 1, 2] + +Explanation: +The substring with start index = 0 is "ab", which is an anagram of "ab". +The substring with start index = 1 is "ba", which is an anagram of "ab". +The substring with start index = 2 is "ab", which is an anagram of "ab". + + + ## 解题思路 diff --git a/Algorithms/0440.k-th-smallest-in-lexicographical-order/README.md b/Algorithms/0440.k-th-smallest-in-lexicographical-order/README.md index ce70edc2e..e30f144c4 100755 --- a/Algorithms/0440.k-th-smallest-in-lexicographical-order/README.md +++ b/Algorithms/0440.k-th-smallest-in-lexicographical-order/README.md @@ -2,6 +2,23 @@ ## 题目 + Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. + +Note: 1 ≤ k ≤ n ≤ 109. + +Example: +Input: +n: 13 k: 2 + +Output: +10 + +Explanation: +The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10. + + + + ## 解题思路 diff --git a/Algorithms/0441.arranging-coins/README.md b/Algorithms/0441.arranging-coins/README.md index 6d1be6dd0..b459fb39b 100755 --- a/Algorithms/0441.arranging-coins/README.md +++ b/Algorithms/0441.arranging-coins/README.md @@ -2,6 +2,37 @@ ## 题目 + You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. + +Given n, find the total number of full staircase rows that can be formed. + +n is a non-negative integer and fits within the range of a 32-bit signed integer. + +Example 1: +n = 5 + +The coins can form the following rows: +¤ +¤ ¤ +¤ ¤ + +Because the 3rd row is incomplete, we return 2. + + + +Example 2: +n = 8 + +The coins can form the following rows: +¤ +¤ ¤ +¤ ¤ ¤ +¤ ¤ + +Because the 4th row is incomplete, we return 3. + + + ## 解题思路 diff --git a/Algorithms/0442.find-all-duplicates-in-an-array/README.md b/Algorithms/0442.find-all-duplicates-in-an-array/README.md index 62bb45759..863833f34 100755 --- a/Algorithms/0442.find-all-duplicates-in-an-array/README.md +++ b/Algorithms/0442.find-all-duplicates-in-an-array/README.md @@ -2,6 +2,20 @@ ## 题目 + Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. + +Find all the elements that appear twice in this array. + +Could you do it without extra space and in O(n) runtime? + +Example: +Input: +[4,3,2,7,8,2,3,1] + +Output: +[2,3] + + ## 解题思路 diff --git a/Algorithms/0445.add-two-numbers-ii/README.md b/Algorithms/0445.add-two-numbers-ii/README.md index 89aa7dc09..c8fb7600d 100755 --- a/Algorithms/0445.add-two-numbers-ii/README.md +++ b/Algorithms/0445.add-two-numbers-ii/README.md @@ -2,6 +2,21 @@ ## 题目 + You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. + +You may assume the two numbers do not contain any leading zero, except the number 0 itself. + +Follow up: +What if you cannot modify the input lists? In other words, reversing the lists is not allowed. + + + +Example: +Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) +Output: 7 -> 8 -> 0 -> 7 + + + ## 解题思路 diff --git a/Algorithms/0446.arithmetic-slices-ii-subsequence/README.md b/Algorithms/0446.arithmetic-slices-ii-subsequence/README.md index 73bc08bb5..c28d7ebfd 100755 --- a/Algorithms/0446.arithmetic-slices-ii-subsequence/README.md +++ b/Algorithms/0446.arithmetic-slices-ii-subsequence/README.md @@ -2,6 +2,42 @@ ## 题目 + A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. + +For example, these are arithmetic sequences: +1, 3, 5, 7, 9 +7, 7, 7, 7 +3, -1, -5, -9 + +The following sequence is not arithmetic. 1, 1, 2, 5, 7 + + +A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N. + +A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2. + +The function should return the number of arithmetic subsequence slices in the array A. + +The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1. + + +Example: +Input: [2, 4, 6, 8, 10] + +Output: 7 + +Explanation: +All arithmetic subsequence slices are: +[2,4,6] +[4,6,8] +[6,8,10] +[2,4,6,8] +[4,6,8,10] +[2,4,6,8,10] +[2,6,10] + + + ## 解题思路 diff --git a/Algorithms/0447.number-of-boomerangs/README.md b/Algorithms/0447.number-of-boomerangs/README.md index 9184d5f20..ab17c59f6 100755 --- a/Algorithms/0447.number-of-boomerangs/README.md +++ b/Algorithms/0447.number-of-boomerangs/README.md @@ -2,6 +2,22 @@ ## 题目 + Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters). + +Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive). + +Example: +Input: +[[0,0],[1,0],[2,0]] + +Output: +2 + +Explanation: +The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]] + + + ## 解题思路 diff --git a/Algorithms/0449.serialize-and-deserialize-bst/README.md b/Algorithms/0449.serialize-and-deserialize-bst/README.md index d89e103ab..9e987ac4b 100755 --- a/Algorithms/0449.serialize-and-deserialize-bst/README.md +++ b/Algorithms/0449.serialize-and-deserialize-bst/README.md @@ -2,6 +2,18 @@ ## 题目 + Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. + +Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure. + + +The encoded string should be as compact as possible. + + + +Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless. + + ## 解题思路 diff --git a/Algorithms/0450.delete-node-in-a-bst/README.md b/Algorithms/0450.delete-node-in-a-bst/README.md index 7f5cb50bc..d66a886d6 100755 --- a/Algorithms/0450.delete-node-in-a-bst/README.md +++ b/Algorithms/0450.delete-node-in-a-bst/README.md @@ -2,6 +2,47 @@ ## 题目 + Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. + +Basically, the deletion can be divided into two stages: + +Search for a node to remove. +If the node is found, delete the node. + + + +Note: Time complexity should be O(height of tree). + +Example: +root = [5,3,6,2,4,null,7] +key = 3 + + 5 + / \ + 3 6 + / \ \ +2 4 7 + +Given key to delete is 3. So we find the node with value 3 and delete it. + +One valid answer is [5,4,6,2,null,null,7], shown in the following BST. + + 5 + / \ + 4 6 + / \ +2 7 + +Another valid answer is [5,2,6,null,4,null,7]. + + 5 + / \ + 2 6 + \ \ + 4 7 + + + ## 解题思路 diff --git a/Algorithms/0451.sort-characters-by-frequency/README.md b/Algorithms/0451.sort-characters-by-frequency/README.md index 1ad0eb993..a36740b65 100755 --- a/Algorithms/0451.sort-characters-by-frequency/README.md +++ b/Algorithms/0451.sort-characters-by-frequency/README.md @@ -2,6 +2,47 @@ ## 题目 + Given a string, sort it in decreasing order based on the frequency of characters. + +Example 1: +Input: +"tree" + +Output: +"eert" + +Explanation: +'e' appears twice while 'r' and 't' both appear once. +So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. + + + +Example 2: +Input: +"cccaaa" + +Output: +"cccaaa" + +Explanation: +Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer. +Note that "cacaca" is incorrect, as the same characters must be together. + + + +Example 3: +Input: +"Aabb" + +Output: +"bbAa" + +Explanation: +"bbaA" is also a valid answer, but "Aabb" is incorrect. +Note that 'A' and 'a' are treated as two different characters. + + + ## 解题思路 diff --git a/Algorithms/0452.minimum-number-of-arrows-to-burst-balloons/README.md b/Algorithms/0452.minimum-number-of-arrows-to-burst-balloons/README.md index b8c6036db..c3698851b 100755 --- a/Algorithms/0452.minimum-number-of-arrows-to-burst-balloons/README.md +++ b/Algorithms/0452.minimum-number-of-arrows-to-burst-balloons/README.md @@ -2,6 +2,22 @@ ## 题目 + There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons. + +An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons. + +Example: +Input: +[[10,16], [2,8], [1,6], [7,12]] + +Output: +2 + +Explanation: +One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons). + + + ## 解题思路 diff --git a/Algorithms/0453.minimum-moves-to-equal-array-elements/README.md b/Algorithms/0453.minimum-moves-to-equal-array-elements/README.md index 48658ce40..1956ec10c 100755 --- a/Algorithms/0453.minimum-moves-to-equal-array-elements/README.md +++ b/Algorithms/0453.minimum-moves-to-equal-array-elements/README.md @@ -2,6 +2,22 @@ ## 题目 + Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. + +Example: +Input: +[1,2,3] + +Output: +3 + +Explanation: +Only three moves are needed (remember each move increments two elements): + +[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] + + + ## 解题思路 diff --git a/Algorithms/0454.4sum-ii/README.md b/Algorithms/0454.4sum-ii/README.md index 1030e2e86..762487b72 100755 --- a/Algorithms/0454.4sum-ii/README.md +++ b/Algorithms/0454.4sum-ii/README.md @@ -2,6 +2,27 @@ ## 题目 + Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. + +To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. + +Example: +Input: +A = [ 1, 2] +B = [-2,-1] +C = [-1, 2] +D = [ 0, 2] + +Output: +2 + +Explanation: +The two tuples are: +1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 +2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 + + + ## 解题思路 diff --git a/Algorithms/0455.assign-cookies/README.md b/Algorithms/0455.assign-cookies/README.md index 6fe5b88b3..c694c06be 100755 --- a/Algorithms/0455.assign-cookies/README.md +++ b/Algorithms/0455.assign-cookies/README.md @@ -2,6 +2,37 @@ ## 题目 + +Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. + + +Note: +You may assume the greed factor is always positive. +You cannot assign more than one cookie to one child. + + +Example 1: +Input: [1,2,3], [1,1] + +Output: 1 + +Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. +And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. +You need to output 1. + + + +Example 2: +Input: [1,2], [1,2,3] + +Output: 2 + +Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. +You have 3 cookies and their sizes are big enough to gratify all of the children, +You need to output 2. + + + ## 解题思路 diff --git a/Algorithms/0456.132-pattern/README.md b/Algorithms/0456.132-pattern/README.md index 6c1e91c71..e8b1bea91 100755 --- a/Algorithms/0456.132-pattern/README.md +++ b/Algorithms/0456.132-pattern/README.md @@ -2,6 +2,39 @@ ## 题目 + +Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such +that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. + +Note: n will be less than 15,000. + +Example 1: +Input: [1, 2, 3, 4] + +Output: False + +Explanation: There is no 132 pattern in the sequence. + + + +Example 2: +Input: [3, 1, 4, 2] + +Output: True + +Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. + + + +Example 3: +Input: [-1, 3, 2, 0] + +Output: True + +Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0]. + + + ## 解题思路 diff --git a/Algorithms/0459.repeated-substring-pattern/README.md b/Algorithms/0459.repeated-substring-pattern/README.md index 9f5785528..57565791b 100755 --- a/Algorithms/0459.repeated-substring-pattern/README.md +++ b/Algorithms/0459.repeated-substring-pattern/README.md @@ -2,6 +2,33 @@ ## 题目 + Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000. + +Example 1: +Input: "abab" + +Output: True + +Explanation: It's the substring "ab" twice. + + + +Example 2: +Input: "aba" + +Output: False + + + +Example 3: +Input: "abcabcabcabc" + +Output: True + +Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.) + + + ## 解题思路 diff --git a/Algorithms/0460.lfu-cache/README.md b/Algorithms/0460.lfu-cache/README.md index 5b5483c20..dee321763 100755 --- a/Algorithms/0460.lfu-cache/README.md +++ b/Algorithms/0460.lfu-cache/README.md @@ -2,6 +2,33 @@ ## 题目 + Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put. + + + +get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. +put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted. + + +Follow up: +Could you do both operations in O(1) time complexity? + +Example: +LFUCache cache = new LFUCache( 2 /* capacity */ ); + +cache.put(1, 1); +cache.put(2, 2); +cache.get(1); // returns 1 +cache.put(3, 3); // evicts key 2 +cache.get(2); // returns -1 (not found) +cache.get(3); // returns 3. +cache.put(4, 4); // evicts key 1. +cache.get(1); // returns -1 (not found) +cache.get(3); // returns 3 +cache.get(4); // returns 4 + + + ## 解题思路 diff --git a/Algorithms/0461.hamming-distance/README.md b/Algorithms/0461.hamming-distance/README.md index a85e026a7..0e3a4a68d 100755 --- a/Algorithms/0461.hamming-distance/README.md +++ b/Algorithms/0461.hamming-distance/README.md @@ -2,6 +2,28 @@ ## 题目 + The Hamming distance between two integers is the number of positions at which the corresponding bits are different. + +Given two integers x and y, calculate the Hamming distance. + +Note: +0 ≤ x, y < 231. + + +Example: +Input: x = 1, y = 4 + +Output: 2 + +Explanation: +1 (0 0 0 1) +4 (0 1 0 0) + ? ? + +The above arrows point to positions where the corresponding bits are different. + + + ## 解题思路 diff --git a/Algorithms/0462.minimum-moves-to-equal-array-elements-ii/README.md b/Algorithms/0462.minimum-moves-to-equal-array-elements-ii/README.md index 32fb94c3e..80b81cd48 100755 --- a/Algorithms/0462.minimum-moves-to-equal-array-elements-ii/README.md +++ b/Algorithms/0462.minimum-moves-to-equal-array-elements-ii/README.md @@ -2,6 +2,24 @@ ## 题目 + Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. + +You may assume the array's length is at most 10,000. + +Example: +Input: +[1,2,3] + +Output: +2 + +Explanation: +Only two moves are needed (remember each move increments or decrements one element): + +[1,2,3] => [2,2,3] => [2,2,2] + + + ## 解题思路 diff --git a/Algorithms/0463.island-perimeter/README.md b/Algorithms/0463.island-perimeter/README.md index 95bebe260..3a5a51fad 100755 --- a/Algorithms/0463.island-perimeter/README.md +++ b/Algorithms/0463.island-perimeter/README.md @@ -2,6 +2,20 @@ ## 题目 + You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island. + +Example: +[[0,1,0,0], + [1,1,1,0], + [0,1,0,0], + [1,1,0,0]] + +Answer: 16 +Explanation: The perimeter is the 16 yellow stripes in the image below: + + + + ## 解题思路 diff --git a/Algorithms/0464.can-i-win/README.md b/Algorithms/0464.can-i-win/README.md index 328a8ee69..454e2e13c 100755 --- a/Algorithms/0464.can-i-win/README.md +++ b/Algorithms/0464.can-i-win/README.md @@ -2,6 +2,34 @@ ## 题目 + In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins. + +What if we change the game so that players cannot re-use integers? + +For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100. + +Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally. + +You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300. + + +Example +Input: +maxChoosableInteger = 10 +desiredTotal = 11 + +Output: +false + +Explanation: +No matter which integer the first player choose, the first player will lose. +The first player can choose an integer from 1 up to 10. +If the first player choose 1, the second player can only choose integers from 2 up to 10. +The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal. +Same with other integers chosen by the first player, the second player will always win. + + + ## 解题思路 diff --git a/Algorithms/0466.count-the-repetitions/README.md b/Algorithms/0466.count-the-repetitions/README.md index 83d266346..d56962bea 100755 --- a/Algorithms/0466.count-the-repetitions/README.md +++ b/Algorithms/0466.count-the-repetitions/README.md @@ -2,6 +2,20 @@ ## 题目 + Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc". +On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”. +You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer M such that [S2,M] can be obtained from S1. + +Example: +Input: +s1="acb", n1=4 +s2="ab", n2=2 + +Return: +2 + + + ## 解题思路 diff --git a/Algorithms/0467.unique-substrings-in-wraparound-string/README.md b/Algorithms/0467.unique-substrings-in-wraparound-string/README.md index 09a2639b0..9c8c19f5d 100755 --- a/Algorithms/0467.unique-substrings-in-wraparound-string/README.md +++ b/Algorithms/0467.unique-substrings-in-wraparound-string/README.md @@ -2,6 +2,34 @@ ## 题目 + Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". + +Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s. + +Note: p consists of only lowercase English letters and the size of p might be over 10000. + +Example 1: +Input: "a" +Output: 1 + +Explanation: Only the substring "a" of string "a" is in the string s. + + + +Example 2: +Input: "cac" +Output: 2 +Explanation: There are two substrings "a", "c" of string "cac" in the string s. + + + +Example 3: +Input: "zab" +Output: 6 +Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s. + + + ## 解题思路 diff --git a/Algorithms/0468.validate-ip-address/README.md b/Algorithms/0468.validate-ip-address/README.md index 64401f385..f9d01105b 100755 --- a/Algorithms/0468.validate-ip-address/README.md +++ b/Algorithms/0468.validate-ip-address/README.md @@ -2,6 +2,64 @@ ## 题目 + +Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither. + + + +IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1; + + + +Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid. + + + +IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases). + + + + +However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address. + + + +Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid. + + + +Note: +You may assume there is no extra space or special characters in the input string. + + +Example 1: +Input: "172.16.254.1" + +Output: "IPv4" + +Explanation: This is a valid IPv4 address, return "IPv4". + + + + +Example 2: +Input: "2001:0db8:85a3:0:0:8A2E:0370:7334" + +Output: "IPv6" + +Explanation: This is a valid IPv6 address, return "IPv6". + + + +Example 3: +Input: "256.256.256.256" + +Output: "Neither" + +Explanation: This is neither a IPv4 address nor a IPv6 address. + + + ## 解题思路 diff --git a/Algorithms/0472.concatenated-words/README.md b/Algorithms/0472.concatenated-words/README.md index 7b9961460..f2cb7a052 100755 --- a/Algorithms/0472.concatenated-words/README.md +++ b/Algorithms/0472.concatenated-words/README.md @@ -2,6 +2,27 @@ ## 题目 + Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. +A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array. + +Example: +Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] + +Output: ["catsdogcats","dogcatsdog","ratcatdogcat"] + +Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; "dogcatsdog" can be concatenated by "dog", "cats" and "dog"; "ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat". + + + +Note: + +The number of elements of the given array will not exceed 10,000 +The length sum of elements in the given array will not exceed 600,000. +All the input string will only include lower case letters. +The returned elements order does not matter. + + + ## 解题思路 diff --git a/Algorithms/0473.matchsticks-to-square/README.md b/Algorithms/0473.matchsticks-to-square/README.md index eefc73510..5fc889e25 100755 --- a/Algorithms/0473.matchsticks-to-square/README.md +++ b/Algorithms/0473.matchsticks-to-square/README.md @@ -2,6 +2,33 @@ ## 题目 + Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time. + + Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has. + +Example 1: +Input: [1,1,2,2,2] +Output: true + +Explanation: You can form a square with length 2, one side of the square came two sticks with length 1. + + + +Example 2: +Input: [3,3,3,3,4] +Output: false + +Explanation: You cannot find a way to form a square with all the matchsticks. + + + +Note: + +The length sum of the given matchsticks is in the range of 0 to 10^9. +The length of the given matchstick array will not exceed 15. + + + ## 解题思路 diff --git a/Algorithms/0474.ones-and-zeroes/README.md b/Algorithms/0474.ones-and-zeroes/README.md index a48ea9a6a..62579f194 100755 --- a/Algorithms/0474.ones-and-zeroes/README.md +++ b/Algorithms/0474.ones-and-zeroes/README.md @@ -2,6 +2,37 @@ ## 题目 + In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue. +For now, suppose you are a dominator of m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s. + + +Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. Each 0 and 1 can be used at most once. + + + +Note: + +The given numbers of 0s and 1s will both not exceed 100 +The size of given string array won't exceed 600. + + + +Example 1: +Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3 +Output: 4 + +Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0” + + + +Example 2: +Input: Array = {"10", "0", "1"}, m = 1, n = 1 +Output: 2 + +Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1". + + + ## 解题思路 diff --git a/Algorithms/0475.heaters/README.md b/Algorithms/0475.heaters/README.md index f112867d3..ad2d6e732 100755 --- a/Algorithms/0475.heaters/README.md +++ b/Algorithms/0475.heaters/README.md @@ -2,6 +2,35 @@ ## 题目 + Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. + +Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. + +So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. + +Note: + +Numbers of houses and heaters you are given are non-negative and will not exceed 25000. +Positions of houses and heaters you are given are non-negative and will not exceed 10^9. +As long as a house is in the heaters' warm radius range, it can be warmed. +All the heaters follow your radius standard and the warm radius will the same. + + + +Example 1: +Input: [1,2,3],[2] +Output: 1 +Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed. + + + +Example 2: +Input: [1,2,3,4],[1,4] +Output: 1 +Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed. + + + ## 解题思路 diff --git a/Algorithms/0476.number-complement/README.md b/Algorithms/0476.number-complement/README.md index b3fc92f2b..92645311d 100755 --- a/Algorithms/0476.number-complement/README.md +++ b/Algorithms/0476.number-complement/README.md @@ -2,6 +2,29 @@ ## 题目 + Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. + +Note: + +The given integer is guaranteed to fit within the range of a 32-bit signed integer. +You could assume no leading zero bit in the integer’s binary representation. + + + +Example 1: +Input: 5 +Output: 2 +Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. + + + +Example 2: +Input: 1 +Output: 0 +Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. + + + ## 解题思路 diff --git a/Algorithms/0477.total-hamming-distance/README.md b/Algorithms/0477.total-hamming-distance/README.md index b64ca4f89..d7a45be73 100755 --- a/Algorithms/0477.total-hamming-distance/README.md +++ b/Algorithms/0477.total-hamming-distance/README.md @@ -2,6 +2,29 @@ ## 题目 + The Hamming distance between two integers is the number of positions at which the corresponding bits are different. + +Now your job is to find the total Hamming distance between all pairs of the given numbers. + + +Example: +Input: 4, 14, 2 + +Output: 6 + +Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just +showing the four bits relevant in this case). So the answer will be: +HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. + + + +Note: + +Elements of the given array are in the range of 0 to 10^9 +Length of the array will not exceed 10^4. + + + ## 解题思路 diff --git a/Algorithms/0479.largest-palindrome-product/README.md b/Algorithms/0479.largest-palindrome-product/README.md index 7383f55ae..c7afd6fbe 100755 --- a/Algorithms/0479.largest-palindrome-product/README.md +++ b/Algorithms/0479.largest-palindrome-product/README.md @@ -2,6 +2,22 @@ ## 题目 + Find the largest palindrome made from the product of two n-digit numbers. + Since the result could be very large, you should return the largest palindrome mod 1337. + +Example: +Input: 2 +Output: 987 +Explanation: 99 x 91 = 9009, 9009 % 1337 = 987 + + + + +Note: +The range of n is [1,8]. + + + ## 解题思路 diff --git a/Algorithms/0480.sliding-window-median/README.md b/Algorithms/0480.sliding-window-median/README.md index d17983419..faff16ed8 100755 --- a/Algorithms/0480.sliding-window-median/README.md +++ b/Algorithms/0480.sliding-window-median/README.md @@ -2,6 +2,31 @@ ## 题目 + Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. +Examples: +[2,3,4] , the median is 3 +[2,3], the median is (2 + 3) / 2 = 2.5 + +Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Your job is to output the median array for each window in the original array. + +For example, +Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. + +Window position Median +--------------- ----- +[1 3 -1] -3 5 3 6 7 1 + 1 [3 -1 -3] 5 3 6 7 -1 + 1 3 [-1 -3 5] 3 6 7 -1 + 1 3 -1 [-3 5 3] 6 7 3 + 1 3 -1 -3 [5 3 6] 7 5 + 1 3 -1 -3 5 [3 6 7] 6 + + +Therefore, return the median sliding window as [1,-1,-1,3,5,6]. + +Note: +You may assume k is always valid, ie: k is always smaller than input array's size for non-empty array. + ## 解题思路 diff --git a/Algorithms/0481.magical-string/README.md b/Algorithms/0481.magical-string/README.md index e0ad5fb5a..19a6df78e 100755 --- a/Algorithms/0481.magical-string/README.md +++ b/Algorithms/0481.magical-string/README.md @@ -2,6 +2,51 @@ ## 题目 + +A magical string S consists of only '1' and '2' and obeys the following rules: + + +The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself. + + + +The first few elements of string S is the following: +S = "1221121221221121122……" + + + +If we group the consecutive '1's and '2's in S, it will be: + + +1 22 11 2 1 22 1 22 11 2 11 22 ...... + + +and the occurrences of '1's or '2's in each group are: + + +1 2 2 1 1 2 1 2 2 1 2 2 ...... + + + +You can see that the occurrence sequence above is the S itself. + + + +Given an integer N as input, return the number of '1's in the first N number in the magical string S. + + +Note: +N will not exceed 100,000. + + + +Example 1: +Input: 6 +Output: 3 +Explanation: The first 6 elements of magical string S is "12211" and it contains three 1's, so return 3. + + + ## 解题思路 diff --git a/Algorithms/0482.license-key-formatting/README.md b/Algorithms/0482.license-key-formatting/README.md index 7fb8c25d0..14edbdf6e 100755 --- a/Algorithms/0482.license-key-formatting/README.md +++ b/Algorithms/0482.license-key-formatting/README.md @@ -2,6 +2,40 @@ ## 题目 + Now you are given a string S, which represents a software license key which we would like to format. The string S is composed of alphanumerical characters and dashes. The dashes split the alphanumerical characters within the string into groups. (i.e. if there are M dashes, the string is split into M+1 groups). The dashes in the given string are possibly misplaced. + +We want each group of characters to be of length K (except for possibly the first group, which could be shorter, but still must contain at least one character). To satisfy this requirement, we will reinsert dashes. Additionally, all the lower case letters in the string must be converted to upper case. + +So, you are given a non-empty string S, representing a license key to format, and an integer K. And you need to return the license key formatted according to the description above. + + +Example 1: +Input: S = "2-4A0r7-4k", K = 4 + +Output: "24A0-R74K" + +Explanation: The string S has been split into two parts, each part has 4 characters. + + + + +Example 2: +Input: S = "2-4A0r7-4k", K = 3 + +Output: "24-A0R-74K" + +Explanation: The string S has been split into three parts, each part has 3 characters except the first part as it could be shorter as said above. + + + +Note: + +The length of string S will not exceed 12,000, and K is a positive integer. +String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-). +String S is non-empty. + + + ## 解题思路 diff --git a/Algorithms/0483.smallest-good-base/README.md b/Algorithms/0483.smallest-good-base/README.md index 645e12e78..866f8e36d 100755 --- a/Algorithms/0483.smallest-good-base/README.md +++ b/Algorithms/0483.smallest-good-base/README.md @@ -2,6 +2,37 @@ ## 题目 + For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. +Now given a string representing n, you should return the smallest good base of n in string format. + +Example 1: +Input: "13" +Output: "3" +Explanation: 13 base 3 is 111. + + + +Example 2: +Input: "4681" +Output: "8" +Explanation: 4681 base 8 is 11111. + + + +Example 3: +Input: "1000000000000000000" +Output: "999999999999999999" +Explanation: 1000000000000000000 base 999999999999999999 is 11. + + + +Note: + +The range of n is [3, 10^18]. +The string representing n is always valid and will not have leading zeros. + + + ## 解题思路 diff --git a/Algorithms/0486.predict-the-winner/README.md b/Algorithms/0486.predict-the-winner/README.md index cfb92d829..1e666887e 100755 --- a/Algorithms/0486.predict-the-winner/README.md +++ b/Algorithms/0486.predict-the-winner/README.md @@ -2,6 +2,32 @@ ## 题目 + Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. + +Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. + +Example 1: +Input: [1, 5, 2] +Output: False +Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. + + + +Example 2: +Input: [1, 5, 233, 7] +Output: True +Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. + + + +Note: + +1 <= length of the array <= 20. +Any scores in the given array are non-negative integers and will not exceed 10,000,000. +If the scores of both players are equal, then player 1 is still the winner. + + + ## 解题思路 diff --git a/Algorithms/0488.zuma-game/README.md b/Algorithms/0488.zuma-game/README.md index 456330a77..a4966cef6 100755 --- a/Algorithms/0488.zuma-game/README.md +++ b/Algorithms/0488.zuma-game/README.md @@ -2,6 +2,40 @@ ## 题目 + Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand. + +Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed. + +Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1. + +Examples: +Input: "WRRBBW", "RB" +Output: -1 +Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW + +Input: "WWRRBBWW", "WRBRW" +Output: 2 +Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty + +Input:"G", "GGGGG" +Output: 2 +Explanation: G -> G[G] -> GG[G] -> empty + +Input: "RBYYBBRRB", "YRBGB" +Output: 3 +Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty + + + +Note: + +You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color. +The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input. +The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input. +Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'. + + + ## 解题思路 diff --git a/Algorithms/0491.increasing-subsequences/README.md b/Algorithms/0491.increasing-subsequences/README.md index d47a71c20..5ddf4579c 100755 --- a/Algorithms/0491.increasing-subsequences/README.md +++ b/Algorithms/0491.increasing-subsequences/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 . + + +Example: +Input: [4, 6, 7, 7] +Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]] + + + +Note: + +The length of the given array will not exceed 15. +The range of integer in the given array is [-100,100]. +The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence. + + + ## 解题思路 diff --git a/Algorithms/0492.construct-the-rectangle/README.md b/Algorithms/0492.construct-the-rectangle/README.md index bfd2ade85..844be5ffb 100755 --- a/Algorithms/0492.construct-the-rectangle/README.md +++ b/Algorithms/0492.construct-the-rectangle/README.md @@ -2,6 +2,30 @@ ## 题目 + +For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:1. The area of the rectangular web page you designed must equal to the given target area. +2. The width W should not be larger than the length L, which means L >= W. +3. The difference between length L and width W should be as small as possible. + +You need to output the length L and the width W of the web page you designed in sequence. + + + +Example: +Input: 4 +Output: [2, 2] +Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. +But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2. + + + +Note: + +The given area won't exceed 10,000,000 and is a positive integer +The web page's width and length you designed must be positive integers. + + + ## 解题思路 diff --git a/Algorithms/0493.reverse-pairs/README.md b/Algorithms/0493.reverse-pairs/README.md index 5743e62c3..b9314836f 100755 --- a/Algorithms/0493.reverse-pairs/README.md +++ b/Algorithms/0493.reverse-pairs/README.md @@ -2,6 +2,27 @@ ## 题目 + Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j]. + +You need to return the number of important reverse pairs in the given array. + +Example1: +Input: [1,3,2,3,1] +Output: 2 + + +Example2: +Input: [2,4,3,5,1] +Output: 3 + + +Note: + +The length of the given array will not exceed 50,000. +All the numbers in the input array are in the range of 32-bit integer. + + + ## 解题思路 diff --git a/Algorithms/0494.target-sum/README.md b/Algorithms/0494.target-sum/README.md index a8a788d23..d66dbf946 100755 --- a/Algorithms/0494.target-sum/README.md +++ b/Algorithms/0494.target-sum/README.md @@ -2,6 +2,36 @@ ## 题目 + +You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. + + +Find out how many ways to assign symbols to make sum of integers equal to target S. + + +Example 1: +Input: nums is [1, 1, 1, 1, 1], S is 3. +Output: 5 +Explanation: + +-1+1+1+1+1 = 3 ++1-1+1+1+1 = 3 ++1+1-1+1+1 = 3 ++1+1+1-1+1 = 3 ++1+1+1+1-1 = 3 + +There are 5 ways to assign symbols to make the sum of nums be target 3. + + + +Note: + +The length of the given array is positive and will not exceed 20. +The sum of elements in the given array will not exceed 1000. +Your output answer is guaranteed to be fitted in a 32-bit integer. + + + ## 解题思路 diff --git a/Algorithms/0495.teemo-attacking/README.md b/Algorithms/0495.teemo-attacking/README.md index 4c8a02b4b..4469190f4 100755 --- a/Algorithms/0495.teemo-attacking/README.md +++ b/Algorithms/0495.teemo-attacking/README.md @@ -2,6 +2,35 @@ ## 题目 + +In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition. + + +You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately. + +Example 1: +Input: [1,4], 2 +Output: 4 +Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. This poisoned status will last 2 seconds until the end of time point 2. And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds. So you finally need to output 4. + + + + +Example 2: +Input: [1,2], 2 +Output: 3 +Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. This poisoned status will last 2 seconds until the end of time point 2. However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status. Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3. So you finally need to output 3. + + + + +Note: + +You may assume the length of given time series array won't exceed 10000. +You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000. + + + ## 解题思路 diff --git a/Algorithms/0496.next-greater-element-i/README.md b/Algorithms/0496.next-greater-element-i/README.md index 1cd1dbf5b..f096e4853 100755 --- a/Algorithms/0496.next-greater-element-i/README.md +++ b/Algorithms/0496.next-greater-element-i/README.md @@ -2,6 +2,41 @@ ## 题目 + +You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. + + + +The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number. + + +Example 1: +Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. +Output: [-1,3,-1] +Explanation: + For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. + For number 1 in the first array, the next greater number for it in the second array is 3. + For number 2 in the first array, there is no next greater number for it in the second array, so output -1. + + + +Example 2: +Input: nums1 = [2,4], nums2 = [1,2,3,4]. +Output: [3,-1] +Explanation: + For number 2 in the first array, the next greater number for it in the second array is 3. + For number 4 in the first array, there is no next greater number for it in the second array, so output -1. + + + + +Note: + +All elements in nums1 and nums2 are unique. +The length of both nums1 and nums2 would not exceed 1000. + + + ## 解题思路 diff --git a/Algorithms/0498.diagonal-traverse/README.md b/Algorithms/0498.diagonal-traverse/README.md index b7b17934b..8f99c0690 100755 --- a/Algorithms/0498.diagonal-traverse/README.md +++ b/Algorithms/0498.diagonal-traverse/README.md @@ -2,6 +2,29 @@ ## 题目 + +Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. + + +Example: +Input: +[ + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ] +] +Output: [1,2,4,7,5,3,6,8,9] +Explanation: + + + + +Note: + +The total number of elements of the given matrix will not exceed 10,000. + + + ## 解题思路 diff --git a/Algorithms/0500.keyboard-row/README.md b/Algorithms/0500.keyboard-row/README.md index f957de41c..afc5e3f84 100755 --- a/Algorithms/0500.keyboard-row/README.md +++ b/Algorithms/0500.keyboard-row/README.md @@ -2,6 +2,27 @@ ## 题目 + Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below. + + + + + + + +Example 1: +Input: ["Hello", "Alaska", "Dad", "Peace"] +Output: ["Alaska", "Dad"] + + + +Note: + +You may use one character in the keyboard more than once. +You may assume the input string will only contain letters of alphabet. + + + ## 解题思路 diff --git a/Algorithms/0501.find-mode-in-binary-search-tree/README.md b/Algorithms/0501.find-mode-in-binary-search-tree/README.md index ea6ff9d5b..a18549e0b 100755 --- a/Algorithms/0501.find-mode-in-binary-search-tree/README.md +++ b/Algorithms/0501.find-mode-in-binary-search-tree/README.md @@ -2,6 +2,39 @@ ## 题目 + Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. + + +Assume a BST is defined as follows: + +The left subtree of a node contains only nodes with keys less than or equal to the node's key. +The right subtree of a node contains only nodes with keys greater than or equal to the node's key. +Both the left and right subtrees must also be binary search trees. + + + + +For example: +Given BST [1,null,2,2], + 1 + \ + 2 + / + 2 + + + +return [2]. + + +Note: +If a tree has more than one mode, you can return them in any order. + + +Follow up: +Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count). + + ## 解题思路 diff --git a/Algorithms/0502.ipo/README.md b/Algorithms/0502.ipo/README.md index 308e581f6..5189419e2 100755 --- a/Algorithms/0502.ipo/README.md +++ b/Algorithms/0502.ipo/README.md @@ -2,6 +2,39 @@ ## 题目 + +Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects. + + + +You are given several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. Initially, you have W capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital. + + + +To sum up, pick a list of at most k distinct projects from given projects to maximize your final capital, and output your final maximized capital. + + +Example 1: +Input: k=2, W=0, Profits=[1,2,3], Capital=[0,1,1]. + +Output: 4 + +Explanation: Since your initial capital is 0, you can only start the project indexed 0. + After finishing it you will obtain profit 1 and your capital becomes 1. + With capital 1, you can either start the project indexed 1 or the project indexed 2. + Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital. + Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4. + + + +Note: + +You may assume all numbers in the input are non-negative integers. +The length of Profits array and Capital array will not exceed 50,000. +The answer is guaranteed to fit in a 32-bit signed integer. + + + ## 解题思路 diff --git a/Algorithms/0503.next-greater-element-ii/README.md b/Algorithms/0503.next-greater-element-ii/README.md index 6588d6316..694598190 100755 --- a/Algorithms/0503.next-greater-element-ii/README.md +++ b/Algorithms/0503.next-greater-element-ii/README.md @@ -2,6 +2,21 @@ ## 题目 + +Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number. + + +Example 1: +Input: [1,2,1] +Output: [2,-1,2] +Explanation: The first 1's next greater number is 2; The number 2 can't find next greater number; The second 1's next greater number needs to search circularly, which is also 2. + + + +Note: +The length of given array won't exceed 10000. + + ## 解题思路 diff --git a/Algorithms/0504.base-7/README.md b/Algorithms/0504.base-7/README.md index 04af88b8b..7f0752c3c 100755 --- a/Algorithms/0504.base-7/README.md +++ b/Algorithms/0504.base-7/README.md @@ -2,6 +2,24 @@ ## 题目 + Given an integer, return its base 7 string representation. + +Example 1: +Input: 100 +Output: "202" + + + +Example 2: +Input: -7 +Output: "-10" + + + +Note: +The input will be in range of [-1e7, 1e7]. + + ## 解题思路 diff --git a/Algorithms/0506.relative-ranks/README.md b/Algorithms/0506.relative-ranks/README.md index 01d8eb58d..2690233e7 100755 --- a/Algorithms/0506.relative-ranks/README.md +++ b/Algorithms/0506.relative-ranks/README.md @@ -2,6 +2,24 @@ ## 题目 + +Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal". + +Example 1: +Input: [5, 4, 3, 2, 1] +Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"] +Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". For the left two athletes, you just need to output their relative ranks according to their scores. + + + +Note: + +N is a positive integer and won't exceed 10,000. +All the scores of athletes are guaranteed to be unique. + + + + ## 解题思路 diff --git a/Algorithms/0507.perfect-number/README.md b/Algorithms/0507.perfect-number/README.md index 90a104488..c3e985a48 100755 --- a/Algorithms/0507.perfect-number/README.md +++ b/Algorithms/0507.perfect-number/README.md @@ -2,6 +2,22 @@ ## 题目 + We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. + +Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not. + + +Example: +Input: 28 +Output: True +Explanation: 28 = 1 + 2 + 4 + 7 + 14 + + + +Note: +The input number n will not exceed 100,000,000. (1e8) + + ## 解题思路 diff --git a/Draft/0433.minimum-genetic-mutation/README.md b/Draft/0433.minimum-genetic-mutation/README.md index cfdfac92e..a7752616e 100755 --- a/Draft/0433.minimum-genetic-mutation/README.md +++ b/Draft/0433.minimum-genetic-mutation/README.md @@ -2,6 +2,51 @@ ## 题目 + A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T". + +Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE mutation is defined as ONE single character changed in the gene string. + +For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation. + +Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string. + +Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If there is no such a mutation, return -1. + +Note: + +Starting point is assumed to be valid, so it might not be included in the bank. +If multiple mutations are needed, all mutations during in the sequence must be valid. +You may assume start and end string is not the same. + + + +Example 1: +start: "AACCGGTT" +end: "AACCGGTA" +bank: ["AACCGGTA"] + +return: 1 + + + +Example 2: +start: "AACCGGTT" +end: "AAACGGTA" +bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"] + +return: 2 + + + +Example 3: +start: "AAAAACCC" +end: "AACCCCCC" +bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"] + +return: 3 + + + ## 解题思路 diff --git a/Draft/0457.circular-array-loop/README.md b/Draft/0457.circular-array-loop/README.md index 2d9982104..15a8adfaf 100755 --- a/Draft/0457.circular-array-loop/README.md +++ b/Draft/0457.circular-array-loop/README.md @@ -2,6 +2,30 @@ ## 题目 + +You are given an array of positive and negative integers. If a number n at an index is positive, then move forward n steps. Conversely, if it's negative (-n), move backward n steps. Assume the first element of the array is forward next to the last element, and the last element is backward next to the first element. Determine if there is a loop in this array. A loop starts and ends at a particular index with more than 1 element along the loop. The loop must be "forward" or "backward'. + + +Example 1: + +Given the array [2, -1, 1, 2, 2], there is a loop, from index 0 -> 2 -> 3 -> 0. + + +Example 2: + +Given the array [-1, 2], there is no loop. + + +Note: + +The given array is guaranteed to contain no element "0". + + + +Can you do it in O(n) time complexity and O(1) space complexity? + + + ## 解题思路 diff --git a/Draft/0458.poor-pigs/README.md b/Draft/0458.poor-pigs/README.md index 46f438ba1..1b4958477 100755 --- a/Draft/0458.poor-pigs/README.md +++ b/Draft/0458.poor-pigs/README.md @@ -2,6 +2,21 @@ ## 题目 + +There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket contains the poison within one hour. + + +Answer this question, and write an algorithm for the follow-up general case. + + + +Follow-up: + + + +If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x) you need to figure out the "poison" bucket within p minutes? There is exact one bucket with poison. + + ## 解题思路 diff --git a/Draft/0518.coin-change-2/README.md b/Draft/0518.coin-change-2/README.md index 2686f3c76..f79b0e847 100755 --- a/Draft/0518.coin-change-2/README.md +++ b/Draft/0518.coin-change-2/README.md @@ -2,6 +2,42 @@ ## 题目 + +You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin. + + +Note: +You can assume that + + 0 <= amount <= 5000 + 1 <= coin <= 5000 + the number of coins is less than 500 + the answer is guaranteed to fit into signed 32-bit integer + + + +Example 1: +Input: amount = 5, coins = [1, 2, 5] +Output: 4 +Explanation: there are four ways to make up the amount: +5=5 +5=2+2+1 +5=2+1+1+1 +5=1+1+1+1+1 + + +Example 2: +Input: amount = 3, coins = [2] +Output: 0 +Explanation: the amount of 3 cannot be made up just with coins of 2. + + +Example 3: +Input: amount = 10, coins = [10] +Output: 1 + + + ## 解题思路 diff --git a/Draft/0626.exchange-seats/README.md b/Draft/0626.exchange-seats/README.md index dfb6db675..0c03490be 100755 --- a/Draft/0626.exchange-seats/README.md +++ b/Draft/0626.exchange-seats/README.md @@ -2,6 +2,36 @@ ## 题目 + Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids. +The column id is continuous increment. +Mary wants to change seats for the adjacent students. +Can you write a SQL query to output the result for Mary? ++---------+---------+ +| id | student | ++---------+---------+ +| 1 | Abbot | +| 2 | Doris | +| 3 | Emerson | +| 4 | Green | +| 5 | Jeames | ++---------+---------+ + +For the sample input, the output is: ++---------+---------+ +| id | student | ++---------+---------+ +| 1 | Doris | +| 2 | Abbot | +| 3 | Green | +| 4 | Emerson | +| 5 | Jeames | ++---------+---------+ + + +Note: +If the number of students is odd, there is no need to change the last one's seat. + + ## 解题思路