├── .coveralls.yml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── cgit.bat ├── cgit.sh ├── doc ├── 001-100 │ ├── 001-two-num-sum.md │ ├── 002-add-two-numbers.md │ ├── 003-longest-substring-without-repeating-characters.md │ ├── 004-median-of-two-sorted-arrays.md │ ├── 005-longest-palindromic-substring.md │ ├── 006-zigzag-conversion.md │ ├── 007-reverse-integer.md │ ├── 008-string-to-integer-atoi.md │ ├── 009-palindrome-number.md │ ├── 010-regular-expression-matching.md │ ├── 011-container-with-most-water.md │ ├── 012-integer-to-roman.md │ ├── 013-roman-to-integer.md │ ├── 014-longest-common-prefix.md │ ├── 015-3num.md │ ├── 016-3sum-closest.md │ ├── 017-letter-combinations-of-a-phone-number.md │ ├── 018-4sum.md │ ├── 019-remove-nth-node-from-end-of-list.md │ ├── 020-valid-parentheses.md │ ├── 021-merge-two-sorted-lists.md │ ├── 022-generate-parentheses.md │ ├── 023-merge-k-sorted-lists.md │ ├── 024-swap-nodes-in-pairs.md │ ├── 025-reverse-nodes-in-k-group.md │ ├── 026-remove-duplicates-from-sorted-array.md │ ├── 027-remove-element.md │ ├── 028-implement-strstr.md │ ├── 029-divide-two-integers.md │ ├── 030-substring-with-concatenation-of-all-words.md │ ├── 031-next-permutation.md │ ├── 032-longest-valid-parentheses.md │ ├── 033-search-in-rotated-sorted-array.md │ ├── 034-find-first-and-last-position-of-element-in-sorted-array.md │ ├── 035-search-insert-position.md │ ├── 036-valid-sudoku.md │ ├── 037-sudoku-solver.md │ ├── 038-count-and-say.md │ ├── 039-combination-sum.md │ ├── 040-combination-sum-ii.md │ ├── 041-first-missing-positive.md │ ├── 042-trapping-rain-water.md │ ├── 043-multiply-strings.md │ ├── 044-wildcard-matching.md │ ├── 045-jump-game-ii.md │ ├── 046-permutations.md │ ├── 047-permutations-ii.md │ ├── 048-rotate-image.md │ ├── 049-group-anagrams.md │ ├── 050-powerxn.md │ ├── 051-n-queens.md │ ├── 052-n-queens-ii.md │ ├── 053-maximum-subarray.md │ ├── 054-spiral-matrix.md │ ├── 055-jump-game.md │ ├── 056-merge-intervals.md │ ├── 057-insert-intervals.md │ ├── 058-length-of-last-word.md │ ├── 059-spiral-matrix-ii.md │ ├── 060-permutation-sequence.md │ ├── 061-rotate-list.md │ ├── 062-unique-paths.md │ ├── 063-unique-paths-ii.md │ ├── 064-minimum-path-sum.md │ ├── 065-valid-number.md │ ├── 066-plus-one.md │ ├── 067-add-binary.md │ ├── 068-text-justification.md │ ├── 070-climbing-stairs.md │ ├── 071-edit-distance.md │ ├── 071-simplify-path.md │ ├── 073-set-matrix-zeroes.md │ ├── 074-search-a-2d-matrix.md │ ├── 075-sort-colors.md │ ├── 076-minimum-window-substring.md │ ├── 077-combinations.md │ ├── 078-subsets.md │ ├── 079-word-search.md │ ├── 080-remove-duplicates-from-sorted-array-ii.md │ ├── 081-search-in-rotated-sorted-array-ii.md │ ├── 082-remove-duplicates-from-sorted-list-ii.md │ ├── 083-remove-duplicates-from-sorted-list.md │ ├── 084-largest-rectangle-in-histogram.md │ ├── 085-maximal-rectangle.md │ ├── 086-partition-list.md │ ├── 087-scramble-string.md │ ├── 088-merge-sorted-array.md │ ├── 089-grey-code.md │ ├── 090-subsets-ii.md │ ├── 091-decode-ways.md │ ├── 092-reverse-linked-list-ii.md │ ├── 093-restore-ip-addresses.md │ ├── 094-binary-tree-inorder-traversal.md │ ├── 095-unique-binary-search-trees-ii.md │ ├── 096-unique-binary-search-trees.md │ ├── 097-interleaving-string.md │ ├── 098-validate-binary-search-tree.md │ ├── 099-recover-binary-search-tree.md │ └── 100-same-tree.md ├── 101-200 │ ├── 101-symmetric-tree.md │ ├── 102-binary-tree-level-order-traversal.md │ ├── 103-binary-tree-zigzag-level-order-traversal.md │ ├── 104-maximum-depth-of-binary-tree.md │ ├── 105-construct-binary-tree-from-preorder-and-inorder-traversal.md │ ├── 106-construct-binary-tree-from-inorder-and-postorder-traversal.md │ ├── 107-binary-tree-level-order-traversal-ii.md │ ├── 108-convert-sorted-array-to-binary-search-tree.md │ ├── 109-convert-sorted-list-to-binary-search-tree.md │ ├── 110-balanced-binary-tree.md │ ├── 111-minimum-depth-of-binary-tree.md │ ├── 112-path-sum.md │ ├── 113-path-sum-ii.md │ ├── 114-flatten-binary-tree-to-linked-list.md │ ├── 115-distinct-subsequences.md │ ├── 116-populating-next-right-pointers-in-each-node.md │ ├── 117-populating-next-right-pointers-in-each-node-ii.md │ ├── 118-pascals-triangle.md │ ├── 119-pascals-triangle-ii.md │ ├── 120-triangle.md │ ├── 121-best-time-to-buy-and-sell-stock.md │ ├── 122-best-time-to-buy-and-sell-stock-ii.md │ ├── 123-best-time-to-buy-and-sell-stock-iii.md │ ├── 124-binary-tree-maximum-path-sum.md │ ├── 125-valid-palindrome.md │ ├── 126-word-ladder-ii.md │ ├── 127-word-ladder.md │ ├── 128-longest-consecutive-sequence.md │ ├── 129-sum-root-to-leaf-numbers.md │ ├── 130-surrounded-regions.md │ ├── 131-palindrome-partitioning.md │ ├── 132-palindrome-partitioning-ii.md │ ├── 133-clone-graph.md │ ├── 134-gas-station.md │ ├── 135-candy.md │ ├── 136-single-number.md │ ├── 137-single-number-ii.md │ ├── 138-copy-list-with-random-pointer.md │ ├── 139-word-break.md │ ├── 140-word-break-ii.md │ ├── 141-linked-list-cycle.md │ ├── 143-reorder-list.md │ ├── 144-binary-tree-preorder-traversal.md │ ├── 145-binary-tree-postorder-traversal.md │ ├── 146-lru-cache.md │ ├── 147-insertion-sort-list.md │ ├── 148-sort-list.md │ ├── 149-max-points-on-a-line.md │ ├── 150-evaluate-reverse-polish-notation.md │ ├── 151-reverse-words-in-a-string.md │ ├── 152-maximum-product-subarray.md │ ├── 153-find-minimum-in-rotated-sorted-array.md │ ├── 154-find-minimum-in-rotated-sorted-array-ii.md │ ├── 155-min-stack.md │ ├── 160-intersection-of-two-linked-lists.md │ ├── 162-find-peak-element.md │ ├── 164-maximum-gap.md │ ├── 165-compare-version-numbers.md │ ├── 166-fraction-to-recurring-decimal.md │ ├── 167-two-sum-ii-input-array-is-sorted.md │ ├── 168-excel-sheet-column-title.md │ ├── 169-excel-sheet-column-number.md │ ├── 169-majority-element.md │ ├── 172-factorial-trailing-zeroes.md │ ├── 173-binary-search-tree-iterator.md │ ├── 174-dungeon-game.md │ ├── 179-largest-number.md │ ├── 187-repeated-dna-sequences.md │ ├── 188-best-time-to-buy-and-sell-stock-iv.md │ ├── 189-rotate-array.md │ ├── 190-reverse-bits.md │ ├── 191-number-of-1-bits.md │ ├── 198-house-robber.md │ ├── 199-binary-tree-right-side-view.md │ └── 200-number-of-islands.md ├── 1300-1400 │ ├── 1305-all-elements-in-two-binary-search-trees.md │ └── 1329-sort-the-matrix-diagonally.md ├── 200-300 │ ├── 201-bitwise-and-of-numbers-range.md │ ├── 202-happy-number.md │ ├── 203-remove-linked-list-elements.md │ ├── 204-count-primes.md │ ├── 205-isomorphic-strings.md │ ├── 206-reverse-linked-list.md │ ├── 206-single-number-iii.md │ ├── 207-course-schedule.md │ ├── 208-implement-trie-prefix-tree.md │ ├── 209-minimum-size-subarray-sum.md │ ├── 210-course-schedule-ii.md │ ├── 211-design-add-and-search-words-data-structure.md │ ├── 212-word-search-ii.md │ ├── 213-house-robber-ii.md │ ├── 214-shortest-palindrome.md │ ├── 215-kth-largest-element-in-an-array.md │ ├── 217-contains-duplicate.md │ ├── 229-majority-element-ii.md │ ├── 233-number-of-digit-one.md │ ├── 237-delete-node-in-a-linked-list.md │ └── 257-binary-tree-paths.md ├── 206-reverse-linked-list.md ├── 300-400 │ └── 347-top-k-frequent-elements.md ├── 400-500 │ └── 437-path-sum-iii.md ├── 500-600 │ └── 509-fibonacci-number.md ├── 600-700 │ └── 629-top-k-frequent-words.md ├── README.md └── _BAK │ ├── README.md │ └── README_BAK.md ├── pom.xml ├── release.bat ├── release.sh ├── release_rm.sh └── src ├── main └── java │ └── com │ └── github │ └── houbb │ └── leetcode │ ├── AddTwoNumbers.java │ ├── AddTwoNumbersLeetCodeVersion1.java │ ├── AddTwoNumbersLeetCodeVersion2.java │ ├── AddTwoNumbersLeetCodeVersion3.java │ ├── ClimbStairs.java │ ├── ContainerWithMostWater.java │ ├── DivideTwoIntegers.java │ ├── F000T100 │ ├── T001_TwoNumSum.java │ ├── T002_AddTwoNumbers.java │ ├── T002_AddTwoNumbersV1.java │ ├── T002_AddTwoNumbersV2.java │ ├── T002_AddTwoNumbersV3.java │ ├── T003_LongestSubstringWithoutRepeatingCharacters.java │ ├── T003_LongestSubstringWithoutRepeatingCharactersV2.java │ ├── T003_LongestSubstringWithoutRepeatingCharactersV3.java │ ├── T004_MedianOfTwoSortedArrays.java │ ├── T005_LongestPalindromicSubstring.java │ ├── T005_LongestPalindromicSubstringManacher.java │ ├── T005_LongestPalindromicSubstringV2.java │ ├── T006_ZigzagConversion.java │ ├── T006_ZigzagConversionV2.java │ ├── T006_ZigzagConversionV3.java │ ├── T006_ZigzagConversionV4.java │ ├── T007_ReverseInteger.java │ ├── T007_ReverseIntegerV2.java │ ├── T008_StringToIntegerAtoi.java │ ├── T008_StringToIntegerAtoiV2.java │ ├── T009_PalindromeNumber.java │ ├── T009_PalindromeNumberV1.java │ ├── T009_PalindromeNumberV2.java │ ├── T010_RegularExpressionMatching.java │ ├── T010_RegularExpressionMatchingV2.java │ ├── T011_ContainerWithMostWater.java │ ├── T011_ContainerWithMostWaterV2.java │ ├── T011_ContainerWithMostWaterV3.java │ ├── T012_IntegerToRoman.java │ ├── T012_IntegerToRomanV2.java │ ├── T012_IntegerToRomanV3.java │ ├── T013_RomanToInteger.java │ ├── T014_LongestCommonPrefix.java │ ├── T015_3Num.java │ ├── T015_3NumV2.java │ ├── T016_3SumClosest.java │ ├── T016_3SumClosestV2.java │ ├── T017_LetterCombinationsOfAPhoneNumber.java │ ├── T017_LetterCombinationsOfAPhoneNumberRecursive.java │ ├── T018_FourSum.java │ ├── T018_FourSumV2.java │ ├── T019_RemoveNthNodeFromEndOfList.java │ ├── T019_RemoveNthNodeFromEndOfListV2.java │ ├── T019_RemoveNthNodeFromEndOfListV3.java │ ├── T020_ValidParentheses.java │ ├── T020_ValidParenthesesV2.java │ ├── T021_MergeTwoSortedLists.java │ ├── T021_MergeTwoSortedListsV2.java │ ├── T022_GenerateParenthesesBF.java │ ├── T022_GenerateParenthesesBacktrack.java │ ├── T022_GenerateParenthesesBfs.java │ ├── T022_GenerateParenthesesDfs.java │ ├── T022_GenerateParenthesesDfsMinis.java │ ├── T022_GenerateParenthesesDp.java │ ├── T023_MergeKSortedLists.java │ ├── T023_MergeKSortedListsDivideAndConquer.java │ ├── T023_MergeKSortedListsOneByOne.java │ ├── T023_MergeKSortedListsPriorityQueue.java │ ├── T023_MergeKSortedListsV2.java │ ├── T024_SwapNodesInPairs.java │ ├── T025_ReverseNodesInKGroup.java │ ├── T025_ReverseNodesInKGroupOptimize.java │ ├── T025_ReverseNodesInKGroupV3.java │ ├── T026_RemoveDuplicatesFromSortedArray.java │ ├── T026_RemoveDuplicatesFromSortedArrayV2.java │ ├── T027_RemoveElement.java │ ├── T028_FindTheIndexOfTheFirstOccurrenceInAString.java │ ├── T029_DivideTwoIntegers.java │ ├── T030_SubstringWithConcatenationOfAllWords.java │ ├── T030_SubstringWithConcatenationOfAllWordsSlideWindow.java │ ├── T030_SubstringWithConcatenationOfAllWordsSlideWindowOptimize.java │ ├── T030_SubstringWithConcatenationOfAllWordsV2.java │ ├── T031_NextPermutation.java │ ├── T032_LongestValidParentheses.java │ ├── T032_LongestValidParenthesesV2.java │ ├── T032_LongestValidParenthesesV3.java │ ├── T033_SearchInRotatedSortedArray.java │ ├── T033_SearchInRotatedSortedArrayV2.java │ ├── T034_FindFirstAndLastPosition.java │ ├── T034_FindFirstAndLastPositionV2.java │ ├── T034_FindFirstAndLastPositionV3.java │ ├── T035_SearchInsertPosition.java │ ├── T035_SearchInsertPositionV2.java │ ├── T036_ValidSoduku.java │ ├── T037_SudokuSolver.java │ ├── T037_SudokuSolverV2.java │ ├── T037_SudokuSolver_FAILED.java │ ├── T038_CountAndSay.java │ ├── T039_CombinationSum.java │ ├── T039_CombinationSumV2.java │ ├── T040_CombinationSumII.java │ ├── T040_CombinationSumIIBest.java │ ├── T040_CombinationSumIIV1.java │ ├── T041_FirstMissingPositive.java │ ├── T042_TrappingRainWater.java │ ├── T042_TrappingRainWaterV0.java │ ├── T042_TrappingRainWaterV1.java │ ├── T042_TrappingRainWaterV2.java │ ├── T042_TrappingRainWaterV3.java │ ├── T042_TrappingRainWaterV4.java │ ├── T043_MultiplyStrings.java │ ├── T044_WildcardMatching.java │ ├── T044_WildcardMatchingDp.java │ ├── T044_WildcardMatchingRecursive.java │ ├── T044_WildcardMatchingRecursiveMem.java │ ├── T045_JumpGameII.java │ ├── T046_Permutations.java │ ├── T047_PermutationsII.java │ ├── T047_PermutationsII_V0.java │ ├── T048_RotateImage.java │ ├── T049_GroupAnagrams.java │ ├── T049_GroupAnagramsV2.java │ ├── T050_PowerxN.java │ ├── T051_NQueens.java │ ├── T052_NQueensII.java │ ├── T052_NQueensIIV2.java │ ├── T053_MaximumSubarray.java │ ├── T054_SpiralMatrix.java │ ├── T055_JumpGame.java │ ├── T056_MergeIntervals.java │ ├── T057_InsertIntervals.java │ ├── T057_InsertIntervalsWorst.java │ ├── T058_LengthOfLastWord.java │ ├── T059_SpiralMatrixII.java │ ├── T059_SpiralMatrixIIBasic.java │ ├── T060_PermutationSequence.java │ ├── T060_PermutationSequenceV2.java │ ├── T060_PermutationSequenceV3.java │ ├── T061_RotateList.java │ ├── T061_RotateListV2.java │ ├── T062_UniquePaths.java │ ├── T063_UniquePathsII.java │ ├── T064_MinimumPathSum.java │ ├── T065_ValidNumber.java │ ├── T066_PlusOne.java │ ├── T067_AddBinary.java │ ├── T068_TextJustification.java │ ├── T069_Sqrtx.java │ ├── T069_SqrtxV2.java │ ├── T070_ClimbStairs.java │ ├── T071_SimplifyPath.java │ ├── T072_EditDistance.java │ ├── T072_EditDistanceV2.java │ ├── T072_EditDistanceV3.java │ ├── T073_SetMatrixZeroes.java │ ├── T074_SearchA2dMatrix.java │ ├── T075_SortColors.java │ ├── T076_MinimumWindowSubstring.java │ ├── T076_MinimumWindowSubstringV2.java │ ├── T076_MinimumWindowSubstringV3.java │ ├── T077_Combinations.java │ ├── T078_Subsets.java │ ├── T079_WordSearch.java │ ├── T081_SearchInRotatedSortedArrayII.java │ ├── T082_RemoveDuplicatesFromSortedListII.java │ ├── T082_RemoveDuplicatesFromSortedListIIV2.java │ ├── T082_RemoveDuplicatesFromSortedListIIV3.java │ ├── T083_RemoveDuplicatesFromSortedList.java │ ├── T083_RemoveDuplicatesFromSortedListV2.java │ ├── T084_LargestRectangleInHistogram.java │ ├── T084_LargestRectangleInHistogramV2.java │ ├── T085_MaximalRectangle.java │ ├── T085_MaximalRectangleV2.java │ ├── T085_MaximalRectangleV3.java │ ├── T086_PartitionList.java │ ├── T086_PartitionListV2.java │ ├── T087_ScrambleString.java │ ├── T087_ScrambleStringV2.java │ ├── T087_ScrambleStringV3.java │ ├── T088_MergeSortedArray.java │ ├── T088_MergeSortedArrayV2.java │ ├── T089_GrayCode.java │ ├── T090_SubsetsII.java │ ├── T091_DecodeWays.java │ ├── T092_ReverseLinkedListII.java │ ├── T093_RestoreIpAddresses.java │ ├── T094_BinaryTreeInorderTraversal.java │ ├── T094_BinaryTreeInorderTraversalV2.java │ ├── T095_UniqueBinarySearchTreesII.java │ ├── T096_UniqueBinarySearchTrees.java │ ├── T096_UniqueBinarySearchTreesV2.java │ ├── T097_InterleavingString.java │ ├── T097_InterleavingStringV2.java │ ├── T098_ValidateBinarySearchTree.java │ ├── T099_RecoverBinarySearchTree.java │ ├── T100_SameTree.java │ └── package-info.java │ ├── F1000T1100 │ ├── T1124longestWPI.java │ ├── T1124longestWPI_V2_PrefixSumHashMap.java │ ├── T1182_MIN_COLOR.java │ ├── T1182_MIN_COLOR_BF.java │ ├── T1182_MIN_COLOR_V2.java │ ├── T2459_MinMoveSteps.java │ ├── T2459_MinMoveSteps_V2.java │ ├── T2548_maxPriceBag.java │ ├── T2548_maxPriceBag_V2.java │ ├── T3195_minContainsOneArea.java │ └── T325_maxSubArrayEqualsK_V1.java │ ├── F100T200 │ ├── T101_SymmetricTree.java │ ├── T102_BinaryTreeLevelOrderTraversal.java │ ├── T103_BinaryTreeZigZagLevelOrderTraversal.java │ ├── T104_MaximumDepthOfBinaryTree.java │ ├── T105_ConstructBinaryTreeFromInorderAndPostOrderTraversal.java │ ├── T106_ConstructBinaryTreeFromPreorderAndInorderTraversal.java │ ├── T107_BinaryTreeLevelOrderTraversalII.java │ ├── T108_ConvertSortedArrayToBinarySearchTree.java │ ├── T108_ConvertSortedArrayToBinarySearchTreeV1.java │ ├── T109_ConvertSortedListToBinarySearchTree.java │ ├── T109_ConvertSortedListToBinarySearchTreeV2.java │ ├── T110_BalancedBinaryTree.java │ ├── T111_MinimumDepthOfBinaryTreeBFS.java │ ├── T111_MinimumDepthOfBinaryTreeDFS.java │ ├── T112_PathSum.java │ ├── T113_PathSumII.java │ ├── T113_PathSumIIV2.java │ ├── T113_PathSumIIV3.java │ ├── T114_FlattenBinaryTreeToLinkedList.java │ ├── T114_FlattenBinaryTreeToLinkedListV2.java │ ├── T114_FlattenBinaryTreeToLinkedListV3.java │ ├── T114_FlattenBinaryTreeToLinkedListV4.java │ ├── T115_DistinctSubsequences.java │ ├── T116_PopulatingNextRightPointersInEachNode.java │ ├── T116_PopulatingNextRightPointersInEachNodeV2.java │ ├── T117_PopulatingNextRightPointersInEachNodeII.java │ ├── T117_PopulatingNextRightPointersInEachNodeIIV2.java │ ├── T117_PopulatingNextRightPointersInEachNodeIIV3.java │ ├── T118_PascalsTriangle.java │ ├── T118_PascalsTriangleV2.java │ ├── T119_PascalsTriangleII.java │ ├── T119_PascalsTriangleIIV2.java │ ├── T120_TriangleDp.java │ ├── T120_TriangleDpOptimize.java │ ├── T120_TriangleDpReusage.java │ ├── T120_TriangleGreedy.java │ ├── T121_BestTimeToBuyAndSellStock.java │ ├── T121_BestTimeToBuyAndSellStockV2.java │ ├── T121_BestTimeToBuyAndSellStock_V20241105.java │ ├── T122_BestTimeToBuyAndSellStockII.java │ ├── T122_BestTimeToBuyAndSellStockII_DP.java │ ├── T123_BestTimeToBuyAndSellStockIII.java │ ├── T123_BestTimeToBuyAndSellStockIIIV2.java │ ├── T123_BestTimeToBuyAndSellStockIIIV3.java │ ├── T123_BestTimeToBuyAndSellStockIIIV4.java │ ├── T123_BestTimeToBuyAndSellStockIII_V20241105.java │ ├── T123_BestTimeToBuyAndSellStockIII_V20241105_VDP.java │ ├── T124_BinaryTreeMaximumPathSum.java │ ├── T125_ValidPalindrome.java │ ├── T126_WordLadderII.java │ ├── T126_WordLadderIIV2.java │ ├── T126_WordLadderIIV3.java │ ├── T126_WordLadderIIV_OTHER.java │ ├── T126_WordLadderIIV_OTHER2.java │ ├── T127_WordLadder.java │ ├── T127_WordLadderV2.java │ ├── T128_LongestConsecutiveSequence.java │ ├── T129_SumRootToLeafNums.java │ ├── T129_SumRootToLeafNumsV2.java │ ├── T130_SurroundedRegions.java │ ├── T131_PalindromePartitioning.java │ ├── T131_PalindromePartitioningV2.java │ ├── T131_PalindromePartitioningV3.java │ ├── T132_PalindromePartitioningII.java │ ├── T132_PalindromePartitioningV2.java │ ├── T133_CloneGraph.java │ ├── T133_CloneGraphBFS.java │ ├── T134_GasStation.java │ ├── T135_Candy.java │ ├── T135_CandyV2.java │ ├── T135_CandyV3.java │ ├── T135_CandyV4.java │ ├── T136_SingleNumber.java │ ├── T136_SingleNumberV2.java │ ├── T136_SingleNumberV3.java │ ├── T137_SingleNumberII.java │ ├── T137_SingleNumberIIV2.java │ ├── T138_CopyListWithRandomPointer.java │ ├── T139_WordBreak.java │ ├── T139_WordBreakV2.java │ ├── T139_WordBreakV3.java │ ├── T139_WordBreakV4.java │ ├── T139_WordBreakV5.java │ ├── T139_WordBreakV6.java │ ├── T140_WordBreakII.java │ ├── T141_LinkedListCycle.java │ ├── T141_LinkedListCycle_ERROR.java │ ├── T142_LinkedListCycleII.java │ ├── T143_ReorderList.java │ ├── T144_BinaryTreePreorderTraversal.java │ ├── T144_BinaryTreePreorderTraversalLoop.java │ ├── T145_BinaryTreePostorderTraversal.java │ ├── T145_BinaryTreePostorderTraversalLoop.java │ ├── T146_LRUCache.java │ ├── T146_LRUCacheV2.java │ ├── T146_LRUCacheV3.java │ ├── T147_InsertionSortList.java │ ├── T148_SortList.java │ ├── T149_MaxPointsOnALine.java │ ├── T149_MaxPointsOnALineV2.java │ ├── T149_MaxPointsOnALineV3.java │ ├── T149_MaxPointsOnALineV4.java │ ├── T150_EvaluateReversePolishNotation.java │ ├── T151_ReverseWordsInAString.java │ ├── T151_ReverseWordsInAStringV2.java │ ├── T152_MaximumProductSubarray.java │ ├── T152_MaximumProductSubarrayV2.java │ ├── T152_MaximumProductSubarrayV3.java │ ├── T153_FindMinimumInRotatedSortedArray.java │ ├── T153_FindMinimumInRotatedSortedArrayV2.java │ ├── T154_FindMinimumInRotatedSortedArrayII.java │ ├── T154_FindMinimumInRotatedSortedArrayIIV2.java │ ├── T155_MinStack.java │ ├── T160_IntersectionOfTwoLinkedLists.java │ ├── T162_FindPeakElement.java │ ├── T162_FindPeakElementV2.java │ ├── T164_MaximumGap.java │ ├── T165_CompareVersionNumbers.java │ ├── T166_FractionToRecurringDecimal.java │ ├── T167_TwoSumIIInputArrayIsSorted.java │ ├── T167_TwoSumIIInputArrayIsSortedV2.java │ ├── T167_TwoSumIIInputArrayIsSortedV3.java │ ├── T168_ExcelSheetColumnTitle.java │ ├── T169_MajorityElement.java │ ├── T169_MajorityElementMoerV3.java │ ├── T169_MajorityElementV2.java │ ├── T169_MajorityElementV4.java │ ├── T171_ExcelSheetColumnNumber.java │ ├── T172_FactorialTrailingZeroes.java │ ├── T172_FactorialTrailingZeroesV2.java │ ├── T173_BinarySearchTreeIterator.java │ ├── T174_DungeonGame.java │ ├── T174_DungeonGameV2.java │ ├── T174_DungeonGameV3.java │ ├── T179_LargestNumber.java │ ├── T187_RepeatedDnaSequences.java │ ├── T188_BestTimeToBuyAndSellStockIV.java │ ├── T188_BestTimeToBuyAndSellStockIV_v20241105_VDP.java │ ├── T189_RotateArray.java │ ├── T190_ReverseBits.java │ ├── T191_NumberOf1Bits.java │ ├── T191_NumberOf1BitsV2.java │ ├── T198_HouseRobber.java │ ├── T198_HouseRobberV2.java │ ├── T198_HouseRobberV3.java │ ├── T198_HouseRobberV4.java │ ├── T198_rob_V1_DP.java │ ├── T199_BinaryTreeRightSideView.java │ ├── T200_NumberOfIslands.java │ ├── T200_NumberOfIslandsV2.java │ └── T213_robII_V1_DP.java │ ├── F1300T1400 │ ├── AllElementsInTwoBinarySearchTrees.java │ ├── AllElementsInTwoBinarySearchTrees2.java │ ├── AllElementsInTwoBinarySearchTreesBetter.java │ ├── SortTheMatrixDiagonally.java │ ├── SortTheMatrixDiagonallyBest.java │ ├── SortTheMatrixDiagonallyBetter.java │ ├── T_1371_findTheLongestSubstring_V1.java │ ├── T_1371_findTheLongestSubstring_V2.java │ └── T_1371_findTheLongestSubstring_V3.java │ ├── F200T300 │ ├── BinaryTreePaths.java │ ├── DeleteNodeInALinkedList.java │ ├── KthLargestElementInAnArray.java │ ├── KthLargestElementInAnArrayHeap.java │ ├── MajorityElementII.java │ ├── MajorityElementIIMoerVote.java │ ├── NumberOfDigitOne.java │ ├── NumberOfDigitOneMath.java │ ├── SingleNumIII.java │ ├── SingleNumIIIBest.java │ ├── SingleNumIIIBetter.java │ ├── T201_BitwiseAndOfNumbersRange.java │ ├── T201_BitwiseAndOfNumbersRangeV2.java │ ├── T202_HappyNumber.java │ ├── T202_HappyNumberV2.java │ ├── T203_RemoveLinkedListElements.java │ ├── T204_CountPrimes.java │ ├── T204_CountPrimesV2.java │ ├── T204_CountPrimesV3.java │ ├── T204_CountPrimesV4.java │ ├── T205_IsomorphicStrings.java │ ├── T205_IsomorphicStringsV2.java │ ├── T206_ReverseLinkedList.java │ ├── T206_ReverseLinkedListV2.java │ ├── T207_CourseSchedule.java │ ├── T207_CourseScheduleV2.java │ ├── T208_ImplementTriePrefixTree.java │ ├── T209_MinimumSizeSubarraySum.java │ ├── T209_MinimumSizeSubarraySumV2.java │ ├── T209_MinimumSizeSubarraySumV3.java │ ├── T209_MinimumSizeSubarraySum_V4_PrefixSum.java │ ├── T209_MinimumSizeSubarraySum_V4_PrefixSum_BinarySearch.java │ ├── T209_MinimumSizeSubarraySum_V6_TwoPinter.java │ ├── T210_CourseScheduleII.java │ ├── T211_WordDictionary.java │ ├── T212_WordSearch.java │ ├── T212_WordSearchV2.java │ ├── T212_WordSearchV3.java │ ├── T212_bestTimeToBuyAndSellStock.java │ ├── T212_bestTimeToBuyAndSellStockBest.java │ ├── T212_bestTimeToBuyAndSellStock_II.java │ ├── T213_HouseRobberII.java │ ├── T213_bestTimeToBuyAndSellStock_III.java │ ├── T214_ShortestPalindrome.java │ ├── T214_ShortestPalindromeV2.java │ ├── T214_ShortestPalindromeV3.java │ ├── T214_ShortestPalindromeV4.java │ ├── T214_ShortestPalindromeV5.java │ ├── T216_CombinationSumIII.java │ ├── T217_ContainsDuplicate.java │ ├── T217_ContainsDuplicateV2Bit.java │ ├── T217_ContainsDuplicateV3InsertSort.java │ ├── T217_ContainsDuplicateV4CompareTest.java │ ├── T217_ContainsDuplicateV4InsertSortOptimize.java │ ├── T218_SkyProblem_V1.java │ ├── T219_ContainsDuplicateII_V1.java │ ├── T219_ContainsDuplicateII_V2.java │ ├── T220_ContainsDuplicateIII_V1.java │ ├── T220_ContainsDuplicateIII_V2.java │ ├── T220_ContainsDuplicateIII_V3.java │ ├── T220_ContainsDuplicateIII_V4.java │ ├── T220_ContainsDuplicateIII_V5.java │ ├── T220_ContainsDuplicateIII_V6.java │ ├── T238_ProductOfArrayExceptSelf_V1_PrefixSuffix.java │ └── T238_ProductOfArrayExceptSelf_V2_PrefixSuffixMemOpt.java │ ├── F300T400 │ ├── T302_PrefixSum_RangeSumQueryImmutable_V1.java │ ├── T307_PrefixSum_RangeSumQueryMutable_V1.java │ ├── T309_bestTimeToBuyAndSellStockWithCooldownV1.java │ ├── T309_bestTimeToBuyAndSellStockWithCooldownV2_Dp.java │ ├── T325_LongestSubarraySumK_V1.java │ ├── T325_LongestSubarraySumZero_V2_PrefixSumHashMap.java │ ├── T325_maxSubArrayEqualsK_V1.java │ ├── T377_CombinationSumIV.java │ ├── T377_CombinationSumIVV2.java │ ├── T377_CombinationSumIVV3.java │ ├── T377_CombinationSumIVV4.java │ ├── TopKFrequentElements.java │ ├── TopKFrequentElementsListSort.java │ ├── TopKFrequentElementsMoerVote.java │ └── TopKFrequentElementsTreeMap.java │ ├── F400T500 │ ├── PathSumIII.java │ ├── PathSumIIIBest.java │ └── PathSumIIIRecursive.java │ ├── F600T700 │ ├── T618_ClosestTime.java │ ├── T_MaxStatck.java │ ├── T_minMeetingRooms.java │ ├── T_moveQi.java │ ├── TopKFrequentWords.java │ └── TopKFrequentWordsSort.java │ ├── F700T800 │ ├── F713_bestTimeToBuyAndSellStockWithTransactionFee_DP.java │ └── T713_BestTimeToBuyAndSellStockWithTxFee_V1.java │ ├── FourSum.java │ ├── FourSumBest.java │ ├── GenerateParenthesesBF.java │ ├── GenerateParenthesesBacktrack.java │ ├── GenerateParenthesesBfs.java │ ├── GenerateParenthesesDfs.java │ ├── GenerateParenthesesDfsMinis.java │ ├── GenerateParenthesesDp.java │ ├── ImplementStrStr.java │ ├── IntegerToRoman.java │ ├── IntegerToRomanBest.java │ ├── KSum.java │ ├── LetterCombinationsOfAPhoneNumber.java │ ├── LetterCombinationsOfAPhoneNumberRecursive.java │ ├── ListNode.java │ ├── LongestCommonPrefix.java │ ├── LongestPalindromicSubstring.java │ ├── LongestPalindromicSubstringManacher.java │ ├── LongestSubstringWithoutRepeatingCharacters.java │ ├── MedianOfTwoSortedArrays.java │ ├── MergeKSortedLists.java │ ├── MergeKSortedListsBest.java │ ├── MergeKSortedListsDivideAndConquer.java │ ├── MergeKSortedListsOneByOne.java │ ├── MergeKSortedListsPriorityQueue.java │ ├── MergeTwoSortedLists.java │ ├── MergeTwoSortedListsBest.java │ ├── PalindromeNumber.java │ ├── PalindromeNumberOptimize.java │ ├── RegularExpressionMatching.java │ ├── RegularExpressionMatchingDp.java │ ├── RemoveDuplicatesFromSortedArray.java │ ├── RemoveDuplicatesFromSortedArrayII.java │ ├── RemoveElement.java │ ├── RemoveNthNodeFromEndOfList.java │ ├── ReverseInteger.java │ ├── ReverseLinkedList.java │ ├── ReverseLinkedListBetter.java │ ├── ReverseLinkedListII.java │ ├── ReverseNodesInKGroup.java │ ├── ReverseNodesInKGroupOptimize.java │ ├── RomanToInteger.java │ ├── StringToIntegerAtoi.java │ ├── StringToIntegerAtoiOptimize.java │ ├── SubstringWithConcatenationOfAllWords.java │ ├── SubstringWithConcatenationOfAllWordsBest.java │ ├── SubstringWithConcatenationOfAllWordsSlideWindow.java │ ├── SubstringWithConcatenationOfAllWordsSlideWindowOptimize.java │ ├── SwapNodesInPairs.java │ ├── ThirdNumBasic.java │ ├── ThirdNumOptimize.java │ ├── ThirdNumSortedAndTwoPointer.java │ ├── ThirdSumClosest.java │ ├── ThirdSumClosestOptimize.java │ ├── TwoNumSum.java │ ├── ValidParentheses.java │ ├── ValidParenthesesOptimize.java │ ├── ZigzagConversion.java │ ├── component │ ├── MyTreeSet.java │ ├── Node.java │ └── TreeNode.java │ ├── datastruct │ ├── array │ │ ├── T001_twoSum_V1_BF.java │ │ ├── T001_twoSum_V2_SortBinarySearch.java │ │ ├── T001_twoSum_V2_SortBinarySearch_ERR.java │ │ ├── T001_twoSum_V3_HashMap.java │ │ ├── T001_twoSum_V3_SortTwoPointer.java │ │ ├── T015_threeSum_V1_BF.java │ │ ├── T015_threeSum_V1_SortTwoPointer.java │ │ ├── T015_threeSum_V1_SortTwoPointerOpt.java │ │ ├── T016_threeSumClosest_V0_BF.java │ │ ├── T016_threeSumClosest_V1_SortTwoPointer.java │ │ ├── T018_fourSum_V1_BF.java │ │ ├── T018_fourSum_V2_SortAndTwoPointer.java │ │ ├── T128_longestConsecutiveSequence_V1.java │ │ ├── T136_SingleNumber_V1_HashMap.java │ │ ├── T136_SingleNumber_V2_HashSet.java │ │ ├── T136_SingleNumber_V3_BitOperator.java │ │ ├── T137_SingleNumberII_V1_HashMap.java │ │ ├── T137_SingleNumberII_V2_Sort.java │ │ ├── T137_SingleNumberII_V3_BitOperator.java │ │ ├── T167_twoSum_V1_BF.java │ │ ├── T167_twoSum_V2_SortBinarySearch.java │ │ ├── T167_twoSum_V3_HashMap.java │ │ ├── T167_twoSum_V4_TwoPointer.java │ │ ├── T217_ContainsDup_V1_HashMap.java │ │ ├── T217_ContainsDup_V2_Set.java │ │ ├── T217_ContainsDup_V2_Set2.java │ │ ├── T217_ContainsDup_V3_Sort.java │ │ ├── T219_ContainsDup_V1_HashMap.java │ │ ├── T219_ContainsDup_V2_SlideWindow.java │ │ ├── T220_ContainsDup_V1_SlideWindow.java │ │ ├── T220_ContainsDup_V2_SlideWindowAndSort.java │ │ ├── T220_ContainsDup_V3_SlideWindowAndTreeSet.java │ │ ├── T220_ContainsDup_V4_BucketSort.java │ │ ├── T252_meetingRoom_V1_BF.java │ │ ├── T252_meetingRoom_V2_Sort.java │ │ ├── T252_meetingRoom_V3_SweepLine.java │ │ ├── T252_meetingRoom_V4_PriorityQueue.java │ │ ├── T252_meetingRoot_V1_BF.java │ │ ├── T253_meetingRoom_V1_BF.java │ │ ├── T259_threeSumSmaller_V1_BF.java │ │ ├── T259_threeSumSmaller_V2_SortTwoPointer.java │ │ ├── T260_SingleNumberIII_V1_HashMap.java │ │ ├── T287_findDuplicate_V1_HashSet.java │ │ ├── T287_findDuplicate_V2_Sort.java │ │ ├── T442_findDuplicates_V1_HashMap.java │ │ ├── T442_findDuplicates_V2_HashSet.java │ │ ├── T442_findDuplicates_V3_Sort.java │ │ ├── T454_fourSumII_V1_BF.java │ │ ├── T454_fourSumII_V2_SortAndBinarySearch.java │ │ ├── T454_fourSumII_V3_HashMap.java │ │ ├── T454_fourSumII_V3_HashMapAndComparess.java │ │ ├── T51_maxSumSubArray_V1_PrefixSum.java │ │ ├── T51_maxSumSubArray_V1_TwoPointer.java │ │ ├── T51_maxSumSubArray_V3_Greedy.java │ │ ├── T51_maxSumSubArray_V4_DP.java │ │ ├── T653_twoSumIV_V1_PreOrderHashSet.java │ │ ├── T653_twoSumIV_V2_InOrderHashSet.java │ │ ├── T653_twoSumIV_V3_PostOrderHashSet.java │ │ ├── T653_twoSumIV_V4_levelOrderHashSet.java │ │ └── T653_twoSumIV_V5_levelOrderTwoPointer.java │ ├── package-info.java │ ├── queryopt │ │ ├── T1906_minDifference_V1_BF.java │ │ ├── T1906_minDifference_V2_subArraySort.java │ │ ├── T1906_minDifference_V3_binarySearch.java │ │ ├── T1906_minDifference_V4_prefixSum.java │ │ ├── T1906_minDifference_V5_mos.java │ │ ├── T1906_minDifference_V6_BIT.java │ │ └── T1906_minDifference_V7_SegmentTree.java │ └── sweepline │ │ ├── T1851_minInterval_V1_BF.java │ │ ├── T1851_minInterval_V2_Sort.java │ │ ├── T1851_minInterval_V3_Sort_BinarySearchKey.java │ │ ├── T1851_minInterval_V3_queryPreSort_queryOpt.java │ │ ├── T1851_minInterval_V4_SegmentTree.java │ │ ├── T223_rectangleArea_V1_overlapArea.java │ │ ├── T223_rectangleArea_V2_Sweepline.java │ │ ├── T3047_rectangleBiggestOverlapArea_V1_overlapArea.java │ │ ├── T3047_rectangleBiggestOverlapArea_V2_overlapAreaOpt.java │ │ ├── T3047_rectangleBiggestOverlapArea_V3_Sweepline.java │ │ ├── T391_perfectRectangle_v1_HashMap.java │ │ ├── T391_perfectRectangle_v1_HashSet.java │ │ ├── T836_rectangleOverlap_V1.java │ │ ├── T836_rectangleOverlap_V2_Sweepline.java │ │ ├── T836_rectangleOverlap_V2_overlapArea.java │ │ ├── T850_rectangleArea_v1_split.java │ │ ├── T850_rectangleArea_v2_sweepline.java │ │ └── T850_rectangleArea_v3_sweepline_yOpt.java │ ├── f500t600 │ ├── FibonacciNumber.java │ ├── FibonacciNumberBest.java │ ├── FibonacciNumberDp.java │ ├── FibonacciNumberDpOptimize.java │ ├── FibonacciNumberMath.java │ ├── T523_checkSubarraySum_V1_BF.java │ ├── T523_checkSubarraySum_V2_SameMod.java │ ├── T525_findMaxLength_V1_prefixSum.java │ ├── T525_findMaxLength_V2_prefixSumHashMap.java │ └── T560_subarraySum_V1_prefixSumHashMap.java │ ├── hard │ └── PermutationSequence.java │ ├── medium │ ├── BinaryTreeInorderTraversal.java │ ├── BinaryTreeInorderTraversalLoop.java │ ├── BinaryTreePostorderTraversal.java │ ├── BinaryTreePostorderTraversalLoop.java │ ├── BinaryTreePostorderTraversalLoopDebug.java │ ├── BinaryTreePreorderTraversal.java │ ├── BinaryTreePreorderTraversalLoop.java │ ├── BinaryTreePreorderTraversalLoopDebug.java │ ├── CombinationSum.java │ ├── CombinationSumBest.java │ ├── CombinationSumII.java │ ├── CombinationSumIIBest.java │ ├── Combinations.java │ ├── CountAndSay.java │ ├── DecodeWays.java │ ├── F100T200 │ │ ├── BinaryTreeLevelOrderTraversal.java │ │ ├── BinaryTreeLevelOrderTraversalII.java │ │ ├── BinaryTreeZigZagLevelOrderTraversal.java │ │ ├── CloneGraph.java │ │ ├── CloneGraphBFS.java │ │ ├── ConstructBinaryTreeFromInorderAndPostOrderTraversal.java │ │ ├── ConstructBinaryTreeFromPreorderAndInorderTraversal.java │ │ ├── ConvertSortedListToBinarySearchTree.java │ │ ├── ConvertSortedListToBinarySearchTreeBetter.java │ │ ├── ConvertSortedListToBinarySearchTreeForTest.java │ │ ├── FlattenBinaryTreeToLinkedList.java │ │ ├── FlattenBinaryTreeToLinkedListBetter.java │ │ ├── GasStation.java │ │ ├── PalindromePartitioning.java │ │ ├── PathSum.java │ │ ├── PathSumII.java │ │ ├── PathSumIIBest.java │ │ ├── PathSumIIBetter.java │ │ ├── PopulatingNextRightPointersInEachNode.java │ │ ├── PopulatingNextRightPointersInEachNodeBetter.java │ │ ├── PopulatingNextRightPointersInEachNodeII.java │ │ ├── PopulatingNextRightPointersInEachNodeIIBasic.java │ │ ├── PopulatingNextRightPointersInEachNodeIIBetter.java │ │ ├── SingleNumber.java │ │ ├── SingleNumberBest.java │ │ ├── SingleNumberBetter.java │ │ ├── SingleNumberII.java │ │ ├── SingleNumberIIBest.java │ │ ├── SumRootToLeafNums.java │ │ ├── SumRootToLeafNumsBetter.java │ │ ├── SurroundedRegionsDFS.java │ │ ├── TriangleDp.java │ │ ├── TriangleDpOptimize.java │ │ ├── TriangleDpReusage.java │ │ └── TriangleGreedy.java │ ├── FindFirstAndLastPosition.java │ ├── FindFirstAndLastPositionBest.java │ ├── FindFirstAndLastPositionBetter.java │ ├── GrayCode.java │ ├── GroupAnagrams.java │ ├── GroupAnagramsBest.java │ ├── InsertIntervals.java │ ├── InsertIntervalsWorst.java │ ├── InterleavingString.java │ ├── InterleavingStringBetter.java │ ├── InterleavingStringError.java │ ├── JumpGame.java │ ├── JumpGameII.java │ ├── JumpGameIIForward.java │ ├── MergeIntervals.java │ ├── MinPathSum.java │ ├── MinPathSumRecursion.java │ ├── MultiplyStrings.java │ ├── NextPermutation.java │ ├── PartitionList.java │ ├── PartitionListBetter.java │ ├── Permutations.java │ ├── PermutationsII.java │ ├── PowerxN.java │ ├── RemoveDuplicatesFromSortedListII.java │ ├── RemoveDuplicatesFromSortedListIIBest.java │ ├── RemoveDuplicatesFromSortedListIIWorst.java │ ├── RestoreIpAddresses.java │ ├── RotateImage.java │ ├── RotateList.java │ ├── RotateListBest.java │ ├── SearchA2dMatrix.java │ ├── SearchInRotatedSortedArray.java │ ├── SearchInRotatedSortedArrayII.java │ ├── SearchInRotatedSortedArrayOld.java │ ├── SetMatrixZeroes.java │ ├── SimplifyPath.java │ ├── SortColors.java │ ├── SpiralMatrix.java │ ├── SpiralMatrixII.java │ ├── SpiralMatrixIIBasic.java │ ├── Subsets.java │ ├── SubsetsII.java │ ├── T079_WordSearch.java │ ├── UniqueBinarySearchTrees.java │ ├── UniqueBinarySearchTreesBest.java │ ├── UniqueBinarySearchTreesII.java │ ├── UniquePaths.java │ ├── UniquePathsII.java │ ├── ValidBinarySearchTreeInOrderTraversal.java │ ├── ValidSoduku.java │ └── ValidateBinarySearchTree.java │ ├── package-info.java │ ├── sort │ └── InsertSortTest.java │ ├── topics │ ├── editdistance │ │ ├── T161_isOneEditDistance.java │ │ ├── T2542_twoEditWords_V1_BF.java │ │ ├── T2542_twoEditWords_V2_BF_OPT.java │ │ ├── T72_minDistanceList_V1_DP.java │ │ ├── T72_minDistance_V1_BF.java │ │ ├── T72_minDistance_V2_BF_memory.java │ │ ├── T72_minDistance_V3_DP.java │ │ └── T72_minDistance_V4_DP_memOpt.java │ └── package-info.java │ └── util │ ├── ListNodes.java │ └── MultiArrays.java └── test └── java └── com └── github └── houbb └── leetcode ├── AddTwoNumbersLeetCodeV2Test.java ├── AddTwoNumbersLeetCodeV3Test.java ├── AddTwoNumbersTest.java ├── ContainerWithMostWaterTest.java ├── FourSumTest.java ├── GenerateParenthesesTest.java ├── IntegerToRomanTest.java ├── LongestCommonPrefixTest.java ├── LongestPalindromicSubstringTest.java ├── LongestSubstringWithoutRepeatingCharactersTest.java ├── MedianOfTwoSortedArraysTest.java ├── MergeKSortedListsTest.java ├── MergeTwoSortedListsTest.java ├── RegularExpressionMatchingTest.java ├── RemoveNthNodeFromEndOfListTest.java ├── ReverseIntegerTest.java ├── ReverseNodesInKGroupTest.java ├── RomanToIntegerTest.java ├── SwapNodesInPairsTest.java ├── T008StringToIntegerAtoiTest.java ├── ThirdNumTest.java ├── ThirdSumClosestTest.java ├── ValidParenthesesTest.java ├── ZigzagConversionTest.java └── sort └── InsertSortTest.java /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-ci -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # maven ignore 2 | target/ 3 | *.jar 4 | *.war 5 | *.zip 6 | *.tar 7 | *.tar.gz 8 | 9 | # eclipse ignore 10 | .settings/ 11 | .project 12 | .classpath 13 | 14 | # idea ignore 15 | .idea/ 16 | *.ipr 17 | *.iml 18 | *.iws 19 | 20 | # temp ignore 21 | *.log 22 | *.cache 23 | *.diff 24 | *.patch 25 | *.tmp 26 | *.java~ 27 | *.properties~ 28 | *.xml~ 29 | 30 | # system ignore 31 | .DS_Store 32 | Thumbs.db 33 | 34 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true 5 | script: mvn test 6 | after_success: 7 | - mvn clean cobertura:cobertura coveralls:report 8 | 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 变更日志 2 | 3 | | 类型 | 说明 | 4 | |:----|:----| 5 | | A | 新增 | 6 | | U | 更新 | 7 | | D | 删除 | 8 | | T | 测试 | 9 | | O | 优化 | 10 | | F | 修复BUG | 11 | 12 | # release_0.0.1 13 | 14 | | 序号 | 变更类型 | 说明 | 时间 | 备注 | 15 | |:---|:---|:---|:---|:--| 16 | | 1 | A | 基本功能的实现 | 2020-5-12 21:06:00 | | -------------------------------------------------------------------------------- /cgit.bat: -------------------------------------------------------------------------------- 1 | :: 用于提交当前变更(windows) 2 | :: author: houbb 3 | :: LastUpdateTime: 2018-11-22 09:08:52 4 | :: 用法:双击运行,或者当前路径 cmd 直接输入 .\cgit.bat 5 | 6 | git pull 7 | git add . 8 | git commit -m "[Feature] add for new" 9 | git push 10 | git status 11 | 12 | -------------------------------------------------------------------------------- /cgit.sh: -------------------------------------------------------------------------------- 1 | # 提交 2 | 3 | git pull 4 | git add . 5 | git commit -m "[Feature] add for new" 6 | git push 7 | git status 8 | 9 | # 1. 赋值权限: chmod +x ./cgit.sh 10 | # 2. 执行: ./cgit.sh 11 | # Last Update Time: 2018-11-21 21:55:38 12 | # Author: houbb -------------------------------------------------------------------------------- /doc/001-100/001-two-num-sum.md: -------------------------------------------------------------------------------- 1 | Given an array of integers, return indices of the two numbers such that they add up to a specific target. 2 | 3 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 4 | 5 | Example: 6 | 7 | ``` 8 | Given nums = [2, 7, 11, 15], target = 9, 9 | 10 | Because nums[0] + nums[1] = 2 + 7 = 9, 11 | return [0, 1]. 12 | ``` 13 | 14 | https://leetcode.com/problems/two-sum/ -------------------------------------------------------------------------------- /doc/001-100/002-add-two-numbers.md: -------------------------------------------------------------------------------- 1 | You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 2 | 3 | You may assume the two numbers do not contain any leading zero, except the number 0 itself. 4 | 5 | Example: 6 | 7 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 8 | Output: 7 -> 0 -> 8 9 | Explanation: 342 + 465 = 807. 10 | 11 | https://leetcode.com/problems/add-two-numbers/ -------------------------------------------------------------------------------- /doc/001-100/003-longest-substring-without-repeating-characters.md: -------------------------------------------------------------------------------- 1 | Given a string, find the length of the longest substring without repeating characters. 2 | 3 | Example 1: 4 | 5 | Input: "abcabcbb" 6 | Output: 3 7 | Explanation: The answer is "abc", with the length of 3. 8 | Example 2: 9 | 10 | Input: "bbbbb" 11 | Output: 1 12 | Explanation: The answer is "b", with the length of 1. 13 | Example 3: 14 | 15 | Input: "pwwkew" 16 | Output: 3 17 | Explanation: The answer is "wke", with the length of 3. 18 | Note that the answer must be a substring, "pwke" is a subsequence and not a substring. -------------------------------------------------------------------------------- /doc/001-100/005-longest-palindromic-substring.md: -------------------------------------------------------------------------------- 1 | Given a string s, find the longest palindromic substring(回文子串) in s. You may assume that the maximum length of s is 1000. 2 | 3 | Example 1: 4 | 5 | ``` 6 | Input: "babad" 7 | Output: "bab" 8 | Note: "aba" is also a valid answer. 9 | ``` 10 | 11 | Example 2: 12 | 13 | ``` 14 | Input: "cbbd" 15 | Output: "bb" 16 | ``` 17 | 18 | https://leetcode.com/problems/longest-palindromic-substring/ -------------------------------------------------------------------------------- /doc/001-100/006-zigzag-conversion.md: -------------------------------------------------------------------------------- 1 | The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) 2 | 3 | ``` 4 | P A H N 5 | A P L S I I G 6 | Y I R 7 | ``` 8 | 9 | And then read line by line: "PAHNAPLSIIGYIR" 10 | 11 | Write the code that will take a string and make this conversion given a number of rows: 12 | 13 | string convert(string s, int numRows); 14 | Example 1: 15 | 16 | ``` 17 | Input: s = "PAYPALISHIRING", numRows = 3 18 | Output: "PAHNAPLSIIGYIR" 19 | ``` 20 | 21 | Example 2: 22 | 23 | ``` 24 | Input: s = "PAYPALISHIRING", numRows = 4 25 | Output: "PINALSIGYAHRPI" 26 | Explanation: 27 | 28 | P I N 29 | A L S I G 30 | Y A H R 31 | P I 32 | ``` 33 | 34 | https://leetcode.com/problems/zigzag-conversion/ -------------------------------------------------------------------------------- /doc/001-100/007-reverse-integer.md: -------------------------------------------------------------------------------- 1 | Given a 32-bit signed integer, reverse digits of an integer. 2 | 3 | Example 1: 4 | 5 | ``` 6 | Input: 123 7 | Output: 321 8 | ``` 9 | 10 | Example 2: 11 | 12 | ``` 13 | Input: -123 14 | Output: -321 15 | ``` 16 | 17 | Example 3: 18 | 19 | ``` 20 | Input: 120 21 | Output: 21 22 | ``` 23 | 24 | Note: 25 | Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. -------------------------------------------------------------------------------- /doc/001-100/009-palindrome-number.md: -------------------------------------------------------------------------------- 1 | Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. 2 | 3 | Example 1: 4 | 5 | ``` 6 | Input: 121 7 | Output: true 8 | ``` 9 | 10 | Example 2: 11 | 12 | ``` 13 | Input: -121 14 | Output: false 15 | ``` 16 | 17 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 18 | Example 3: 19 | 20 | ``` 21 | Input: 10 22 | Output: false 23 | ``` 24 | 25 | Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 26 | 27 | Follow up: 28 | 29 | Coud you solve it without converting the integer to a string? -------------------------------------------------------------------------------- /doc/001-100/011-container-with-most-water.md: -------------------------------------------------------------------------------- 1 | Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 2 | 3 | Note: You may not slant(倾斜) the container and n is at least 2. 4 | 5 | ![question_11.jpg](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg) -------------------------------------------------------------------------------- /doc/001-100/014-longest-common-prefix.md: -------------------------------------------------------------------------------- 1 | Write a function to find the longest common prefix string amongst an array of strings. 2 | 3 | If there is no common prefix, return an empty string "". 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: ["flower","flow","flight"] 9 | Output: "fl" 10 | ``` 11 | 12 | Example 2: 13 | 14 | ``` 15 | Input: ["dog","racecar","car"] 16 | Output: "" 17 | ``` 18 | 19 | Explanation: There is no common prefix among the input strings. 20 | 21 | Note: 22 | 23 | All given inputs are in lowercase letters a-z. -------------------------------------------------------------------------------- /doc/001-100/015-3num.md: -------------------------------------------------------------------------------- 1 | Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 2 | 3 | Note: 4 | 5 | The solution set must not contain duplicate triplets. 6 | 7 | Example: 8 | 9 | ``` 10 | Given array nums = [-1, 0, 1, 2, -1, -4], 11 | 12 | A solution set is: 13 | [ 14 | [-1, 0, 1], 15 | [-1, -1, 2] 16 | ] 17 | ``` -------------------------------------------------------------------------------- /doc/001-100/016-3sum-closest.md: -------------------------------------------------------------------------------- 1 | Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. 2 | 3 | You may assume that each input would have exactly one solution. 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: nums = [-1,2,1,-4], target = 1 9 | Output: 2 10 | Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 11 | ``` 12 | 13 | Constraints: 14 | 15 | ``` 16 | 3 <= nums.length <= 10^3 17 | -10^3 <= nums[i] <= 10^3 18 | -10^4 <= target <= 10^4 19 | ``` 20 | -------------------------------------------------------------------------------- /doc/001-100/017-letter-combinations-of-a-phone-number.md: -------------------------------------------------------------------------------- 1 | Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. 2 | 3 | A mapping of digit to letters (just like on the telephone buttons) is given below. 4 | 5 | Note that 1 does not map to any letters. 6 | 7 | Example: 8 | 9 | ``` 10 | Input: "23" 11 | Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 12 | ``` 13 | 14 | Note: 15 | 16 | Although the above answer is in lexicographical order, your answer could be in any order you want. 17 | 18 | -------------------------------------------------------------------------------- /doc/001-100/018-4sum.md: -------------------------------------------------------------------------------- 1 | Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? 2 | 3 | Find all unique quadruplets in the array which gives the sum of target. 4 | 5 | Note: 6 | 7 | The solution set must not contain duplicate quadruplets. 8 | 9 | Example: 10 | 11 | ``` 12 | Given array nums = [1, 0, -1, 0, -2, 2], and target = 0. 13 | 14 | A solution set is: 15 | [ 16 | [-1, 0, 0, 1], 17 | [-2, -1, 1, 2], 18 | [-2, 0, 0, 2] 19 | ] 20 | ``` -------------------------------------------------------------------------------- /doc/001-100/019-remove-nth-node-from-end-of-list.md: -------------------------------------------------------------------------------- 1 | Given a linked list, remove the n-th node from the end of list and return its head. 2 | 3 | Example: 4 | 5 | ``` 6 | Given linked list: 1->2->3->4->5, and n = 2. 7 | 8 | After removing the second node from the end, the linked list becomes 1->2->3->5. 9 | ``` 10 | 11 | Note: 12 | 13 | Given n will always be valid. 14 | 15 | Follow up: 16 | 17 | Could you do this in one pass? -------------------------------------------------------------------------------- /doc/001-100/020-valid-parentheses.md: -------------------------------------------------------------------------------- 1 | Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 2 | 3 | An input string is valid if: 4 | 5 | Open brackets must be closed by the same type of brackets. 6 | 7 | Open brackets must be closed in the correct order. 8 | 9 | Note that an empty string is also considered valid. 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: "()" 15 | Output: true 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: "()[]{}" 22 | Output: true 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: "(]" 29 | Output: false 30 | ``` 31 | 32 | Example 4: 33 | 34 | ``` 35 | Input: "([)]" 36 | Output: false 37 | ``` 38 | 39 | Example 5: 40 | 41 | ``` 42 | Input: "{[]}" 43 | Output: true 44 | ``` 45 | 46 | https://leetcode.com/problems/valid-parentheses/ -------------------------------------------------------------------------------- /doc/001-100/021-merge-two-sorted-lists.md: -------------------------------------------------------------------------------- 1 | Merge two sorted linked lists and return it as a new sorted list. 2 | 3 | The new list should be made by splicing together the nodes of the first two lists. 4 | 5 | Example: 6 | 7 | ``` 8 | Input: 1->2->4, 1->3->4 9 | Output: 1->1->2->3->4->4 10 | ``` 11 | 12 | https://leetcode.com/problems/merge-two-sorted-lists/ -------------------------------------------------------------------------------- /doc/001-100/022-generate-parentheses.md: -------------------------------------------------------------------------------- 1 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 2 | 3 | For example, given n = 3, a solution set is: 4 | 5 | ``` 6 | [ 7 | "((()))", 8 | "(()())", 9 | "(())()", 10 | "()(())", 11 | "()()()" 12 | ] 13 | ``` -------------------------------------------------------------------------------- /doc/001-100/023-merge-k-sorted-lists.md: -------------------------------------------------------------------------------- 1 | Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 2 | 3 | Example: 4 | 5 | ``` 6 | Input: 7 | [ 8 | 1->4->5, 9 | 1->3->4, 10 | 2->6 11 | ] 12 | Output: 1->1->2->3->4->4->5->6 13 | ``` 14 | 15 | -------------------------------------------------------------------------------- /doc/001-100/024-swap-nodes-in-pairs.md: -------------------------------------------------------------------------------- 1 | Given a linked list, swap every two adjacent nodes and return its head. 2 | 3 | You may not modify the values in the list's nodes, only nodes itself may be changed. 4 | 5 | Example: 6 | 7 | ``` 8 | Given 1->2->3->4, you should return the list as 2->1->4->3. 9 | ``` -------------------------------------------------------------------------------- /doc/001-100/025-reverse-nodes-in-k-group.md: -------------------------------------------------------------------------------- 1 | Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. 2 | 3 | k is a positive integer and is less than or equal to the length of the linked list. 4 | 5 | If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. 6 | 7 | Example: 8 | 9 | ``` 10 | Given this linked list: 1->2->3->4->5 11 | 12 | For k = 2, you should return: 2->1->4->3->5 13 | 14 | For k = 3, you should return: 3->2->1->4->5 15 | ``` 16 | 17 | Note: 18 | 19 | Only constant extra memory is allowed. 20 | 21 | You may not alter the values in the list's nodes, only nodes itself may be changed. -------------------------------------------------------------------------------- /doc/001-100/028-implement-strstr.md: -------------------------------------------------------------------------------- 1 | Implement strStr(). 2 | 3 | Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: haystack = "hello", needle = "ll" 9 | Output: 2 10 | ``` 11 | 12 | Example 2: 13 | 14 | ``` 15 | Input: haystack = "aaaaa", needle = "bba" 16 | Output: -1 17 | ``` 18 | 19 | Clarification: 20 | 21 | What should we return when needle is an empty string? This is a great question to ask during an interview. 22 | 23 | For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf(). -------------------------------------------------------------------------------- /doc/001-100/029-divide-two-integers.md: -------------------------------------------------------------------------------- 1 | Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. 2 | 3 | Return the quotient after dividing dividend by divisor. 4 | 5 | The integer division should truncate toward zero, which means losing its fractional part. 6 | 7 | For example, truncate(8.345) = 8 and truncate(-2.7335) = -2. 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: dividend = 10, divisor = 3 13 | Output: 3 14 | Explanation: 10/3 = truncate(3.33333..) = 3. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: dividend = 7, divisor = -3 21 | Output: -2 22 | Explanation: 7/-3 = truncate(-2.33333..) = -2. 23 | ``` 24 | 25 | Note: 26 | 27 | Both dividend and divisor will be 32-bit signed integers. 28 | 29 | The divisor will never be 0. 30 | 31 | Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. 32 | 33 | For the purpose of this problem, assume that your function returns 2^31 − 1 when the division result overflows. 34 | -------------------------------------------------------------------------------- /doc/001-100/030-substring-with-concatenation-of-all-words.md: -------------------------------------------------------------------------------- 1 | 串联所有单词的子串 2 | 3 | 给定一个字符串 s 和一些长度相同的单词 words。 4 | 5 | 找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。 6 | 7 | 注意子串要与words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。 8 | 9 | 示例 1: 10 | 11 | ``` 12 | 输入: 13 | s = "barfoothefoobarman", 14 | words = ["foo","bar"] 15 | 输出:[0,9] 16 | 解释: 17 | 从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。 18 | 输出的顺序不重要, [9,0] 也是有效答案。 19 | ``` 20 | 21 | 示例 2: 22 | 23 | ``` 24 | 输入: 25 | s = "wordgoodgoodgoodbestword", 26 | words = ["word","good","best","word"] 27 | 输出:[] 28 | ``` 29 | 30 | 来源:力扣(LeetCode) 31 | 链接:https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words 32 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 -------------------------------------------------------------------------------- /doc/001-100/032-longest-valid-parentheses.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/longest-valid-parentheses/ 2 | 3 | Given a string containing just the characters '(' and ')', 4 | return the length of the longest valid (well-formed) parentheses substring. 5 | 6 | ## EX 7 | 8 | Example 1: 9 | 10 | ``` 11 | Input: s = "(()" 12 | Output: 2 13 | Explanation: The longest valid parentheses substring is "()". 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: s = ")()())" 20 | Output: 4 21 | Explanation: The longest valid parentheses substring is "()()". 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: s = "" 28 | Output: 0 29 | ``` 30 | 31 | ## Constraints: 32 | 33 | 0 <= s.length <= 3 * 104 34 | s[i] is '(', or ')'. 35 | -------------------------------------------------------------------------------- /doc/001-100/034-find-first-and-last-position-of-element-in-sorted-array.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 2 | 3 | Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. 4 | 5 | If target is not found in the array, return [-1, -1]. 6 | 7 | Follow up: Could you write an algorithm with O(log n) runtime complexity? 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: nums = [5,7,7,8,8,10], target = 8 14 | Output: [3,4] 15 | Example 2: 16 | 17 | Input: nums = [5,7,7,8,8,10], target = 6 18 | Output: [-1,-1] 19 | Example 3: 20 | 21 | Input: nums = [], target = 0 22 | Output: [-1,-1] 23 | 24 | 25 | Constraints: 26 | 27 | 0 <= nums.length <= 105 28 | -109 <= nums[i] <= 109 29 | nums is a non-decreasing array. 30 | -109 <= target <= 109 -------------------------------------------------------------------------------- /doc/001-100/035-search-insert-position.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/search-insert-position/ 2 | 3 | Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 4 | 5 | You must write an algorithm with O(log n) runtime complexity. 6 | 7 | ## EX 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: nums = [1,3,5,6], target = 5 13 | Output: 2 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [1,3,5,6], target = 2 20 | Output: 1 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: nums = [1,3,5,6], target = 7 27 | Output: 4 28 | ``` 29 | 30 | ## Constraints: 31 | 32 | 1 <= nums.length <= 10^4 33 | -10^4 <= nums[i] <= 10^4 34 | nums contains distinct values sorted in ascending order. 35 | -10^4 <= target <= 10^4 36 | -------------------------------------------------------------------------------- /doc/001-100/040-combination-sum-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/combination-sum-ii/ 2 | 3 | 4 | Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. 5 | 6 | Each number in candidates may only be used once in the combination. 7 | 8 | Note: The solution set must not contain duplicate combinations. 9 | 10 | 11 | 12 | Example 1: 13 | 14 | Input: candidates = [10,1,2,7,6,1,5], target = 8 15 | Output: 16 | [ 17 | [1,1,6], 18 | [1,2,5], 19 | [1,7], 20 | [2,6] 21 | ] 22 | Example 2: 23 | 24 | Input: candidates = [2,5,2,1,2], target = 5 25 | Output: 26 | [ 27 | [1,2,2], 28 | [5] 29 | ] 30 | 31 | 32 | Constraints: 33 | 34 | 1 <= candidates.length <= 100 35 | 1 <= candidates[i] <= 50 36 | 1 <= target <= 30 -------------------------------------------------------------------------------- /doc/001-100/041-first-missing-positive.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/first-missing-positive/ 2 | 3 | Given an unsorted integer array nums, return the smallest missing positive integer. 4 | 5 | You must implement an algorithm that runs in O(n) time and uses constant extra space. 6 | 7 | ## Ex 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: nums = [1,2,0] 13 | Output: 3 14 | Explanation: The numbers in the range [1,2] are all in the array. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [3,4,-1,1] 21 | Output: 2 22 | Explanation: 1 is in the array but 2 is missing. 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: nums = [7,8,9,11,12] 29 | Output: 1 30 | Explanation: The smallest positive integer 1 is missing. 31 | ``` 32 | 33 | ## Constraints: 34 | 35 | 1 <= nums.length <= 10^5 36 | 37 | -2^31 <= nums[i] <= 2^31 - 1 38 | -------------------------------------------------------------------------------- /doc/001-100/042-trapping-rain-water.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/trapping-rain-water/ 2 | 3 | 4 | 42. Trapping Rain Water 5 | 6 | Given n non-negative integers representing an elevation map where the width of each bar is 1, 7 | compute how much water it can trap after raining. 8 | 9 | ## EX 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] 15 | Output: 6 16 | Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. 17 | ``` 18 | 19 | Example 2: 20 | 21 | ``` 22 | Input: height = [4,2,0,3,2,5] 23 | Output: 9 24 | ``` 25 | 26 | ## Constraints: 27 | 28 | n == height.length 29 | 30 | 1 <= n <= 2 * 10^4 31 | 32 | 0 <= height[i] <= 10^5 33 | -------------------------------------------------------------------------------- /doc/001-100/043-multiply-strings.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/multiply-strings/ 2 | 3 | 4 | Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. 5 | 6 | Note: You must not use any built-in BigInteger library or convert the inputs to integer directly. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: num1 = "2", num2 = "3" 13 | Output: "6" 14 | 15 | Example 2: 16 | 17 | Input: num1 = "123", num2 = "456" 18 | Output: "56088" 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= num1.length, num2.length <= 200 24 | num1 and num2 consist of digits only. 25 | Both num1 and num2 do not contain any leading zero, except the number 0 itself. 26 | -------------------------------------------------------------------------------- /doc/001-100/044-wildcard-matching.md: -------------------------------------------------------------------------------- 1 | 44. Wildcard Matching 2 | 3 | Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where: 4 | 5 | '?' Matches any single character. 6 | 7 | '*' Matches any sequence of characters (including the empty sequence). 8 | 9 | The matching should cover the entire input string (not partial). 10 | 11 | ## Ex 12 | 13 | Example 1: 14 | 15 | ``` 16 | Input: s = "aa", p = "a" 17 | Output: false 18 | Explanation: "a" does not match the entire string "aa". 19 | ``` 20 | 21 | Example 2: 22 | 23 | ``` 24 | Input: s = "aa", p = "*" 25 | Output: true 26 | Explanation: '*' matches any sequence. 27 | ``` 28 | 29 | Example 3: 30 | 31 | ``` 32 | Input: s = "cb", p = "?a" 33 | Output: false 34 | Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. 35 | ``` 36 | 37 | ## Constraints: 38 | 39 | 0 <= s.length, p.length <= 2000 40 | 41 | s contains only lowercase English letters. 42 | 43 | p contains only lowercase English letters, '?' or '*'. 44 | 45 | -------------------------------------------------------------------------------- /doc/001-100/045-jump-game-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/jump-game-ii/ 2 | 3 | 4 | Given an array of non-negative integers nums, you are initially positioned at the first index of the array. 5 | 6 | Each element in the array represents your maximum jump length at that position. 7 | 8 | Your goal is to reach the last index in the minimum number of jumps. 9 | 10 | You can assume that you can always reach the last index. 11 | 12 | 13 | 14 | Example 1: 15 | 16 | Input: nums = [2,3,1,1,4] 17 | Output: 2 18 | Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. 19 | Example 2: 20 | 21 | Input: nums = [2,3,0,1,4] 22 | Output: 2 23 | 24 | 25 | Constraints: 26 | 27 | 1 <= nums.length <= 1000 28 | 0 <= nums[i] <= 105 -------------------------------------------------------------------------------- /doc/001-100/046-permutations.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/permutations/ 2 | 3 | 4 | Given an array nums of distinct integers, return all the possible permutations. 5 | 6 | You can return the answer in any order. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: nums = [1,2,3] 13 | Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 14 | Example 2: 15 | 16 | Input: nums = [0,1] 17 | Output: [[0,1],[1,0]] 18 | Example 3: 19 | 20 | Input: nums = [1] 21 | Output: [[1]] 22 | 23 | 24 | Constraints: 25 | 26 | 1 <= nums.length <= 6 27 | -10 <= nums[i] <= 10 28 | All the integers of nums are unique. 29 | -------------------------------------------------------------------------------- /doc/001-100/047-permutations-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/permutations-ii/ 2 | 3 | 4 | Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: nums = [1,1,2] 11 | Output: 12 | [[1,1,2], 13 | [1,2,1], 14 | [2,1,1]] 15 | Example 2: 16 | 17 | Input: nums = [1,2,3] 18 | Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= nums.length <= 8 24 | -10 <= nums[i] <= 10 -------------------------------------------------------------------------------- /doc/001-100/048-rotate-image.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/rotate-image/ 2 | 3 | You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). 4 | 5 | You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. 6 | 7 | DO NOT allocate another 2D matrix and do the rotation. 8 | 9 | Example 1: 10 | 11 | ![example1](https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg) 12 | 13 | 14 | ``` 15 | Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 16 | Output: [[7,4,1],[8,5,2],[9,6,3]] 17 | ``` 18 | 19 | Example 2: 20 | 21 | ![mat2](https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg) 22 | 23 | ``` 24 | Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] 25 | Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] 26 | ``` 27 | 28 | ps: 实际上这是图片处理非常有用的一种算法,常见的图片旋转就是这么实现的。 29 | 30 | Constraints: 31 | 32 | matrix.length == n 33 | matrix[i].length == n 34 | 1 <= n <= 20 35 | -1000 <= matrix[i][j] <= 1000 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /doc/001-100/049-group-anagrams.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/group-anagrams/ 2 | 3 | Given an array of strings strs, group the anagrams together. You can return the answer in any order. 4 | 5 | An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: strs = ["eat","tea","tan","ate","nat","bat"] 12 | Output: [["bat"],["nat","tan"],["ate","eat","tea"]] 13 | Example 2: 14 | 15 | Input: strs = [""] 16 | Output: [[""]] 17 | Example 3: 18 | 19 | Input: strs = ["a"] 20 | Output: [["a"]] 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= strs.length <= 104 26 | 0 <= strs[i].length <= 100 27 | strs[i] consists of lower-case English letters. 28 | -------------------------------------------------------------------------------- /doc/001-100/050-powerxn.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/powx-n/ 2 | 3 | Implement pow(x, n), which calculates x raised to the power n (i.e. xn). 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: x = 2.00000, n = 10 10 | Output: 1024.00000 11 | Example 2: 12 | 13 | Input: x = 2.10000, n = 3 14 | Output: 9.26100 15 | Example 3: 16 | 17 | Input: x = 2.00000, n = -2 18 | Output: 0.25000 19 | Explanation: 2-2 = 1/22 = 1/4 = 0.25 20 | 21 | 22 | Constraints: 23 | 24 | -100.0 < x < 100.0 25 | -231 <= n <= 231-1 26 | -104 <= xn <= 104 -------------------------------------------------------------------------------- /doc/001-100/051-n-queens.md: -------------------------------------------------------------------------------- 1 | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. 2 | 3 | Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. 4 | 5 | Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively. 6 | 7 | ## EX 8 | 9 | Example 1: 10 | 11 | ![ex](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg) 12 | 13 | ``` 14 | Input: n = 4 15 | Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 16 | Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above 17 | ``` 18 | 19 | Example 2: 20 | 21 | ``` 22 | Input: n = 1 23 | Output: [["Q"]] 24 | ``` 25 | 26 | ## Constraints: 27 | 28 | 1 <= n <= 9 29 | -------------------------------------------------------------------------------- /doc/001-100/052-n-queens-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/n-queens-ii/ 2 | 3 | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. 4 | 5 | Given an integer n, return the number of distinct solutions to the n-queens puzzle. 6 | 7 | ## EX 8 | 9 | Example 1: 10 | 11 | ![ex](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg) 12 | 13 | ``` 14 | Input: n = 4 15 | Output: 2 16 | Explanation: There are two distinct solutions to the 4-queens puzzle as shown. 17 | ``` 18 | 19 | Example 2: 20 | 21 | ``` 22 | Input: n = 1 23 | Output: 1 24 | ``` 25 | 26 | ## Constraints: 27 | 28 | 1 <= n <= 9 29 | 30 | PS: 个人感觉这一题的设计比较失败,在 51 的基础上,很容易解出来。 31 | -------------------------------------------------------------------------------- /doc/001-100/053-maximum-subarray.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/maximum-subarray/ 2 | 3 | Given an integer array nums, find the subarray with the largest sum, and return its sum. 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] 9 | Output: 6 10 | Explanation: The subarray [4,-1,2,1] has the largest sum 6. 11 | ``` 12 | 13 | Example 2: 14 | 15 | ``` 16 | Input: nums = [1] 17 | Output: 1 18 | Explanation: The subarray [1] has the largest sum 1. 19 | ``` 20 | 21 | Example 3: 22 | 23 | ``` 24 | Input: nums = [5,4,-1,7,8] 25 | Output: 23 26 | Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. 27 | ``` 28 | 29 | Constraints: 30 | 31 | 1 <= nums.length <= 10^5 32 | -104 <= nums[i] <= 10^4 33 | 34 | Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. 35 | -------------------------------------------------------------------------------- /doc/001-100/054-spiral-matrix.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/spiral-matrix 2 | 3 | Given an m x n matrix, return all elements of the matrix in spiral order. 4 | 5 | Example 1: 6 | 7 | ![example1](https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg) 8 | 9 | ``` 10 | Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 11 | Output: [1,2,3,6,9,8,7,4,5] 12 | ``` 13 | 14 | Example 2: 15 | 16 | 17 | ![spiral](https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg) 18 | 19 | ``` 20 | Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 21 | Output: [1,2,3,4,8,12,11,10,9,5,6,7] 22 | ``` 23 | 24 | Constraints: 25 | 26 | m == matrix.length 27 | n == matrix[i].length 28 | 1 <= m, n <= 10 29 | -100 <= matrix[i][j] <= 100 -------------------------------------------------------------------------------- /doc/001-100/055-jump-game.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/jump-game/ 2 | 3 | Given an array of non-negative integers nums, you are initially positioned at the first index of the array. 4 | 5 | Each element in the array represents your maximum jump length at that position. 6 | 7 | Determine if you are able to reach the last index. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: nums = [2,3,1,1,4] 14 | Output: true 15 | Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. 16 | Example 2: 17 | 18 | Input: nums = [3,2,1,0,4] 19 | Output: false 20 | Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 3 * 10^4 26 | 0 <= nums[i] <= 105 -------------------------------------------------------------------------------- /doc/001-100/056-merge-intervals.md: -------------------------------------------------------------------------------- 1 | Given an array of intervals where intervals[i] = [starti, endi], 2 | merge all overlapping intervals, and return an array of the 3 | non-overlapping intervals that cover all the intervals in the input. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: intervals = [[1,3],[2,6],[8,10],[15,18]] 10 | Output: [[1,6],[8,10],[15,18]] 11 | Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. 12 | Example 2: 13 | 14 | Input: intervals = [[1,4],[4,5]] 15 | Output: [[1,5]] 16 | Explanation: Intervals [1,4] and [4,5] are considered overlapping. 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= intervals.length <= 104 22 | intervals[i].length == 2 23 | 0 <= starti <= endi <= 104 -------------------------------------------------------------------------------- /doc/001-100/058-length-of-last-word.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/length-of-last-word/ 2 | 3 | Given a string s consisting of words and spaces, return the length of the last word in the string. 4 | 5 | A word is a maximal substring consisting of non-space characters only. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: s = "Hello World" 11 | Output: 5 12 | Explanation: The last word is "World" with length 5. 13 | ``` 14 | 15 | Example 2: 16 | ``` 17 | Input: s = " fly me to the moon " 18 | Output: 4 19 | Explanation: The last word is "moon" with length 4. 20 | ``` 21 | 22 | Example 3: 23 | 24 | ``` 25 | Input: s = "luffy is still joyboy" 26 | Output: 6 27 | Explanation: The last word is "joyboy" with length 6. 28 | ``` 29 | 30 | Constraints: 31 | 32 | 1 <= s.length <= 10^4 33 | s consists of only English letters and spaces ' '. 34 | There will be at least one word in s. 35 | -------------------------------------------------------------------------------- /doc/001-100/059-spiral-matrix-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/spiral-matrix-ii/ 2 | 3 | Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![spiraln](https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg) 10 | 11 | Input: n = 3 12 | Output: [[1,2,3],[8,9,4],[7,6,5]] 13 | Example 2: 14 | 15 | Input: n = 1 16 | Output: [[1]] 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= n <= 20 -------------------------------------------------------------------------------- /doc/001-100/060-permutation-sequence.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/permutation-sequence/ 2 | 3 | 4 | The set [1, 2, 3, ..., n] contains a total of n! unique permutations. 5 | 6 | By listing and labeling all of the permutations in order, we get the following sequence for n = 3: 7 | 8 | "123" 9 | "132" 10 | "213" 11 | "231" 12 | "312" 13 | "321" 14 | Given n and k, return the kth permutation sequence. 15 | 16 | 17 | 18 | Example 1: 19 | 20 | Input: n = 3, k = 3 21 | Output: "213" 22 | Example 2: 23 | 24 | Input: n = 4, k = 9 25 | Output: "2314" 26 | Example 3: 27 | 28 | Input: n = 3, k = 1 29 | Output: "123" 30 | 31 | 32 | Constraints: 33 | 34 | 1 <= n <= 9 35 | 1 <= k <= n! -------------------------------------------------------------------------------- /doc/001-100/061-rotate-list.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/rotate-list/ 2 | 3 | Given the head of a linked list, rotate the list to the right by k places. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![example1](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg) 10 | 11 | Input: head = [1,2,3,4,5], k = 2 12 | Output: [4,5,1,2,3] 13 | 14 | Constraints: 15 | 16 | The number of nodes in the list is in the range [0, 500]. 17 | -100 <= Node.val <= 100 18 | 0 <= k <= 2 * 109 -------------------------------------------------------------------------------- /doc/001-100/064-minimum-path-sum.md: -------------------------------------------------------------------------------- 1 | https://leetcode-cn.com/problems/minimum-path-sum/ 2 | 3 | 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. 4 | 5 | Note: You can only move either down or right at any point in time. 6 | 7 | Example 1: 8 | 9 | ![minpath](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg) 10 | 11 | Input: grid = [[1,3,1],[1,5,1],[4,2,1]] 12 | Output: 7 13 | Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. 14 | Example 2: 15 | 16 | Input: grid = [[1,2,3],[4,5,6]] 17 | Output: 12 18 | 19 | 20 | Constraints: 21 | 22 | m == grid.length 23 | n == grid[i].length 24 | 1 <= m, n <= 200 25 | 0 <= grid[i][j] <= 100 -------------------------------------------------------------------------------- /doc/001-100/067-add-binary.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/add-binary/ 2 | 3 | Given two binary strings a and b, return their sum as a binary string. 4 | 5 | ## EX 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: a = "11", b = "1" 11 | Output: "100" 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: a = "1010", b = "1011" 18 | Output: "10101" 19 | ``` 20 | 21 | ## Constraints: 22 | 23 | 1 <= a.length, b.length <= 10^4 24 | 25 | a and b consist only of '0' or '1' characters. 26 | 27 | Each string does not contain leading zeros except for the zero itself. -------------------------------------------------------------------------------- /doc/001-100/070-climbing-stairs.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/climbing-stairs/ 2 | 3 | You are climbing a staircase. It takes n steps to reach the top. 4 | 5 | Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: n = 2 12 | Output: 2 13 | Explanation: There are two ways to climb to the top. 14 | 1. 1 step + 1 step 15 | 2. 2 steps 16 | Example 2: 17 | 18 | Input: n = 3 19 | Output: 3 20 | Explanation: There are three ways to climb to the top. 21 | 1. 1 step + 1 step + 1 step 22 | 2. 1 step + 2 steps 23 | 3. 2 steps + 1 step 24 | 25 | 26 | Constraints: 27 | 28 | 1 <= n <= 45 -------------------------------------------------------------------------------- /doc/001-100/073-set-matrix-zeroes.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/set-matrix-zeroes/ 2 | 3 | Given an m x n matrix. If an element is 0, set its entire row and column to 0. Do it in-place. 4 | 5 | Follow up: 6 | 7 | A straight forward solution using O(mn) space is probably a bad idea. 8 | A simple improvement uses O(m + n) space, but still not the best solution. 9 | Could you devise a constant space solution? 10 | 11 | 12 | Example 1: 13 | 14 | ![1](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg) 15 | 16 | 17 | Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] 18 | Output: [[1,0,1],[0,0,0],[1,0,1]] 19 | Example 2: 20 | 21 | ![2](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg) 22 | 23 | Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] 24 | Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] 25 | 26 | 27 | Constraints: 28 | 29 | m == matrix.length 30 | n == matrix[0].length 31 | 1 <= m, n <= 200 32 | -231 <= matrix[i][j] <= 231 - 1 -------------------------------------------------------------------------------- /doc/001-100/074-search-a-2d-matrix.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/search-a-2d-matrix/ 2 | 3 | Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: 4 | 5 | Integers in each row are sorted from left to right. 6 | The first integer of each row is greater than the last integer of the previous row. 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 13 | Output: true 14 | Example 2: 15 | 16 | 17 | Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 18 | Output: false 19 | 20 | 21 | Constraints: 22 | 23 | m == matrix.length 24 | n == matrix[i].length 25 | 1 <= m, n <= 100 26 | -10^4 <= matrix[i][j], target <= 10^4 27 | -------------------------------------------------------------------------------- /doc/001-100/075-sort-colors.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/sort-colors/ 2 | 3 | Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. 4 | 5 | We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [2,0,2,1,1,0] 12 | Output: [0,0,1,1,2,2] 13 | Example 2: 14 | 15 | Input: nums = [2,0,1] 16 | Output: [0,1,2] 17 | Example 3: 18 | 19 | Input: nums = [0] 20 | Output: [0] 21 | Example 4: 22 | 23 | Input: nums = [1] 24 | Output: [1] 25 | 26 | 27 | Constraints: 28 | 29 | n == nums.length 30 | 1 <= n <= 300 31 | nums[i] is 0, 1, or 2. -------------------------------------------------------------------------------- /doc/001-100/077-combinations.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/combinations/ 2 | 3 | 4 | Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 5 | 6 | You may return the answer in any order. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: n = 4, k = 2 13 | Output: 14 | [ 15 | [2,4], 16 | [3,4], 17 | [2,3], 18 | [1,2], 19 | [1,3], 20 | [1,4], 21 | ] 22 | Example 2: 23 | 24 | Input: n = 1, k = 1 25 | Output: [[1]] 26 | 27 | 28 | Constraints: 29 | 30 | 1 <= n <= 20 31 | 1 <= k <= n -------------------------------------------------------------------------------- /doc/001-100/078-subsets.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/subsets/ 2 | 3 | Given an integer array nums of unique elements, return all possible subsets (the power set). 4 | 5 | The solution set must not contain duplicate subsets. Return the solution in any order. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,2,3] 12 | Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] 13 | Example 2: 14 | 15 | Input: nums = [0] 16 | Output: [[],[0]] 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= nums.length <= 10 22 | -10 <= nums[i] <= 10 23 | All the numbers of nums are unique. -------------------------------------------------------------------------------- /doc/001-100/081-search-in-rotated-sorted-array-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ 2 | 3 | There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values). 4 | 5 | Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4]. 6 | 7 | Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: nums = [2,5,6,0,0,1,2], target = 0 14 | Output: true 15 | Example 2: 16 | 17 | Input: nums = [2,5,6,0,0,1,2], target = 3 18 | Output: false 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= nums.length <= 5000 24 | -10^4 <= nums[i] <= 10^4 25 | nums is guaranteed to be rotated at some pivot. 26 | -10^4 <= target <= 10^4 -------------------------------------------------------------------------------- /doc/001-100/082-remove-duplicates-from-sorted-list-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 2 | 3 | Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![e1](https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg) 10 | 11 | Input: head = [1,2,3,3,4,4,5] 12 | Output: [1,2,5] 13 | Example 2: 14 | 15 | ![e2](https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg) 16 | 17 | Input: head = [1,1,1,2,3] 18 | Output: [2,3] 19 | 20 | 21 | Constraints: 22 | 23 | The number of nodes in the list is in the range [0, 300]. 24 | -100 <= Node.val <= 100 25 | The list is guaranteed to be sorted in ascending order. 26 | -------------------------------------------------------------------------------- /doc/001-100/083-remove-duplicates-from-sorted-list.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 2 | 3 | Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. 4 | 5 | 6 | ## EX 7 | 8 | Example 1: 9 | 10 | ![list1](https://assets.leetcode.com/uploads/2021/01/04/list1.jpg) 11 | 12 | ``` 13 | Input: head = [1,1,2] 14 | Output: [1,2] 15 | ``` 16 | 17 | Example 2: 18 | 19 | ![list2](https://assets.leetcode.com/uploads/2021/01/04/list2.jpg) 20 | 21 | ``` 22 | Input: head = [1,1,2,3,3] 23 | Output: [1,2,3] 24 | ``` 25 | 26 | ## Constraints: 27 | 28 | The number of nodes in the list is in the range [0, 300]. 29 | 30 | -100 <= Node.val <= 100 31 | 32 | The list is guaranteed to be sorted in ascending order. 33 | 34 | -------------------------------------------------------------------------------- /doc/001-100/084-largest-rectangle-in-histogram.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/largest-rectangle-in-histogram/description/ 2 | 3 | 84. Largest Rectangle in Histogram 4 | 5 | Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. 6 | 7 | ## EX 8 | 9 | Example 1: 10 | 11 | ![ex1](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg) 12 | 13 | ``` 14 | Input: heights = [2,1,5,6,2,3] 15 | Output: 10 16 | Explanation: The above is a histogram where width of each bar is 1. 17 | The largest rectangle is shown in the red area, which has an area = 10 units. 18 | ``` 19 | 20 | Example 2: 21 | 22 | ![ex2](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg) 23 | 24 | ``` 25 | Input: heights = [2,4] 26 | Output: 4 27 | ``` 28 | 29 | ## Constraints: 30 | 31 | 1 <= heights.length <= 10^5 32 | 33 | 0 <= heights[i] <= 10^4 34 | -------------------------------------------------------------------------------- /doc/001-100/085-maximal-rectangle.md: -------------------------------------------------------------------------------- 1 | # 85. Maximal Rectangle 2 | 3 | Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. 4 | 5 | ## Ex 6 | 7 | Example 1: 8 | 9 | ![ex1](https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg) 10 | 11 | ``` 12 | Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] 13 | Output: 6 14 | Explanation: The maximal rectangle is shown in the above picture. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: matrix = [["0"]] 21 | Output: 0 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: matrix = [["1"]] 28 | Output: 1 29 | ``` 30 | 31 | ## Constraints: 32 | 33 | rows == matrix.length 34 | 35 | cols == matrix[i].length 36 | 37 | 1 <= row, cols <= 200 38 | 39 | matrix[i][j] is '0' or '1'. -------------------------------------------------------------------------------- /doc/001-100/086-partition-list.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/partition-list/ 2 | 3 | Given the head of 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. 4 | 5 | You should preserve the original relative order of the nodes in each of the two partitions. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ![e1](https://assets.leetcode.com/uploads/2021/01/04/partition.jpg) 12 | 13 | Input: head = [1,4,3,2,5,2], x = 3 14 | Output: [1,2,2,4,3,5] 15 | Example 2: 16 | 17 | Input: head = [2,1], x = 2 18 | Output: [1,2] 19 | 20 | 21 | Constraints: 22 | 23 | The number of nodes in the list is in the range [0, 200]. 24 | -100 <= Node.val <= 100 25 | -200 <= x <= 200 -------------------------------------------------------------------------------- /doc/001-100/089-grey-code.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/gray-code/ 2 | 3 | The gray code is a binary numeral system where two successive values differ in only one bit. 4 | 5 | Given an integer n representing the total number of bits in the code, return any sequence of gray code. 6 | 7 | A gray code sequence must begin with 0. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: n = 2 14 | Output: [0,1,3,2] 15 | Explanation: 16 | 00 - 0 17 | 01 - 1 18 | 11 - 3 19 | 10 - 2 20 | [0,2,3,1] is also a valid gray code sequence. 21 | 00 - 0 22 | 10 - 2 23 | 11 - 3 24 | 01 - 1 25 | Example 2: 26 | 27 | Input: n = 1 28 | Output: [0,1] 29 | 30 | 31 | Constraints: 32 | 33 | 1 <= n <= 16 -------------------------------------------------------------------------------- /doc/001-100/090-subsets-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/subsets-ii/ 2 | 3 | Given an integer array nums that may contain duplicates, return all possible subsets (the power set). 4 | 5 | The solution set must not contain duplicate subsets. Return the solution in any order. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,2,2] 12 | Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] 13 | Example 2: 14 | 15 | Input: nums = [0] 16 | Output: [[],[0]] 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= nums.length <= 10 22 | -10 <= nums[i] <= 10 -------------------------------------------------------------------------------- /doc/001-100/092-reverse-linked-list-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/reverse-linked-list-ii/ 2 | 3 | 4 | Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | ![example1](https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg) 11 | 12 | Input: head = [1,2,3,4,5], left = 2, right = 4 13 | Output: [1,4,3,2,5] 14 | Example 2: 15 | 16 | Input: head = [5], left = 1, right = 1 17 | Output: [5] 18 | 19 | 20 | Constraints: 21 | 22 | The number of nodes in the list is n. 23 | 1 <= n <= 500 24 | -500 <= Node.val <= 500 25 | 1 <= left <= right <= n 26 | 27 | 28 | Follow up: Could you do it in one pass? 29 | -------------------------------------------------------------------------------- /doc/001-100/094-binary-tree-inorder-traversal.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/binary-tree-inorder-traversal/ 2 | 3 | Given the root of a binary tree, return the inorder traversal of its nodes' values. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![e1](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg) 10 | 11 | Input: root = [1,null,2,3] 12 | Output: [1,3,2] 13 | Example 2: 14 | 15 | Input: root = [] 16 | Output: [] 17 | Example 3: 18 | 19 | Input: root = [1] 20 | Output: [1] 21 | Example 4: 22 | 23 | ![e4](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg) 24 | 25 | Input: root = [1,2] 26 | Output: [2,1] 27 | Example 5: 28 | 29 | ![e5](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg) 30 | 31 | Input: root = [1,null,2] 32 | Output: [1,2] 33 | 34 | 35 | Constraints: 36 | 37 | The number of nodes in the tree is in the range [0, 100]. 38 | -100 <= Node.val <= 100 39 | 40 | 41 | Follow up: 42 | 43 | Recursive solution is trivial, could you do it iteratively? -------------------------------------------------------------------------------- /doc/001-100/095-unique-binary-search-trees-ii.md: -------------------------------------------------------------------------------- 1 | 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 2 | 3 | 示例: 4 | 5 | 输入: 3 6 | 输出: 5 7 | 解释: 8 | 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 9 | 10 | 1 3 3 2 1 11 | \ / / / \ \ 12 | 3 2 1 1 3 2 13 | / / \ \ 14 | 2 1 2 3 15 | 16 | 来源:力扣(LeetCode) 17 | 链接:https://leetcode-cn.com/problems/unique-binary-search-trees 18 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 -------------------------------------------------------------------------------- /doc/001-100/096-unique-binary-search-trees.md: -------------------------------------------------------------------------------- 1 | Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | 8 | Input: n = 3 9 | Output: 5 10 | Example 2: 11 | 12 | Input: n = 1 13 | Output: 1 14 | 15 | 16 | Constraints: 17 | 18 | 1 <= n <= 19 -------------------------------------------------------------------------------- /doc/001-100/097-interleaving-string.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/interleaving-string/ 2 | 3 | Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2. 4 | 5 | An interleaving of two strings s and t is a configuration where they are divided into non-empty substrings such that: 6 | 7 | s = s1 + s2 + ... + sn 8 | t = t1 + t2 + ... + tm 9 | |n - m| <= 1 10 | 11 | The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ... 12 | 13 | Note: a + b is the concatenation of strings a and b. 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /doc/001-100/098-validate-binary-search-tree.md: -------------------------------------------------------------------------------- 1 | Given the root of a binary tree, determine if it is a valid binary search tree (BST). 2 | 3 | A valid BST is defined as follows: 4 | 5 | The left subtree of a node contains only nodes with keys less than the node's key. 6 | The right subtree of a node contains only nodes with keys greater than the node's key. 7 | Both the left and right subtrees must also be binary search trees. 8 | 9 | 10 | Example 1: 11 | ![e1](https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg) 12 | 13 | Input: root = [2,1,3] 14 | Output: true 15 | Example 2: 16 | ![e2](https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg) 17 | 18 | Input: root = [5,1,4,null,null,3,6] 19 | Output: false 20 | Explanation: The root node's value is 5 but its right child's value is 4. 21 | 22 | 23 | Constraints: 24 | 25 | The number of nodes in the tree is in the range [1, 104]. 26 | -2^31 <= Node.val <= 2^31 - 1 -------------------------------------------------------------------------------- /doc/001-100/100-same-tree.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/same-tree/ 2 | 3 | # 100. same tree 4 | 5 | Given the roots of two binary trees p and q, write a function to check if they are the same or not. 6 | 7 | Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. 8 | 9 | # Example 10 | 11 | Example 1: 12 | 13 | ![ex1](https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg) 14 | 15 | ``` 16 | Input: p = [1,2,3], q = [1,2,3] 17 | Output: true 18 | ``` 19 | 20 | Example 2: 21 | 22 | ![ex1](https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg) 23 | 24 | ``` 25 | Input: p = [1,2], q = [1,null,2] 26 | Output: false 27 | ``` 28 | 29 | Example 3: 30 | 31 | ``` 32 | Input: p = [1,2,1], q = [1,1,2] 33 | Output: false 34 | ``` 35 | 36 | Constraints: 37 | 38 | The number of nodes in both trees is in the range [0, 100]. 39 | -104 <= Node.val <= 104 40 | -------------------------------------------------------------------------------- /doc/101-200/101-symmetric-tree.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/symmetric-tree/ 2 | 3 | Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). 4 | 5 | # example 6 | 7 | example1: 8 | 9 | ![e1](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg) 10 | 11 | ``` 12 | Input: root = [1,2,2,3,4,4,3] 13 | Output: true 14 | ``` 15 | 16 | example2: 17 | 18 | ![e2](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg) 19 | 20 | ``` 21 | Input: root = [1,2,2,null,3,null,3] 22 | Output: false 23 | ``` 24 | 25 | Constraints: 26 | 27 | The number of nodes in the tree is in the range [1, 1000]. 28 | -100 <= Node.val <= 100 29 | 30 | 31 | Follow up: Could you solve it both recursively and iteratively? 32 | 33 | -------------------------------------------------------------------------------- /doc/101-200/102-binary-tree-level-order-traversal.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/binary-tree-level-order-traversal/ 2 | 3 | Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![ex](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) 10 | 11 | Input: root = [3,9,20,null,null,15,7] 12 | Output: [[3],[9,20],[15,7]] 13 | Example 2: 14 | 15 | Input: root = [1] 16 | Output: [[1]] 17 | Example 3: 18 | 19 | Input: root = [] 20 | Output: [] 21 | 22 | 23 | Constraints: 24 | 25 | The number of nodes in the tree is in the range [0, 2000]. 26 | -1000 <= Node.val <= 1000 -------------------------------------------------------------------------------- /doc/101-200/103-binary-tree-zigzag-level-order-traversal.md: -------------------------------------------------------------------------------- 1 | Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between). 2 | 3 | 4 | 5 | Example 1: 6 | 7 | ![e1](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) 8 | 9 | Input: root = [3,9,20,null,null,15,7] 10 | Output: [[3],[20,9],[15,7]] 11 | Example 2: 12 | 13 | Input: root = [1] 14 | Output: [[1]] 15 | Example 3: 16 | 17 | Input: root = [] 18 | Output: [] 19 | 20 | 21 | Constraints: 22 | 23 | The number of nodes in the tree is in the range [0, 2000]. 24 | -100 <= Node.val <= 100 -------------------------------------------------------------------------------- /doc/101-200/104-maximum-depth-of-binary-tree.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ 2 | 3 | # 104. Maximum Depth of Binary Tree 4 | 5 | Given the root of a binary tree, return its maximum depth. 6 | 7 | A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 8 | 9 | # Example 10 | 11 | example 1: 12 | 13 | ![ex1](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg) 14 | 15 | ``` 16 | Input: root = [3,9,20,null,null,15,7] 17 | Output: 3 18 | ``` 19 | 20 | example 2: 21 | 22 | ``` 23 | Input: root = [1,null,2] 24 | Output: 2 25 | ``` 26 | 27 | # Constraints: 28 | 29 | The number of nodes in the tree is in the range [0, 10^4]. 30 | -100 <= Node.val <= 100 -------------------------------------------------------------------------------- /doc/101-200/105-construct-binary-tree-from-preorder-and-inorder-traversal.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 2 | 3 | Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, 4 | construct and return the binary tree. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | ![e1](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg) 11 | 12 | Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] 13 | Output: [3,9,20,null,null,15,7] 14 | Example 2: 15 | 16 | Input: preorder = [-1], inorder = [-1] 17 | Output: [-1] 18 | 19 | 20 | Constraints: 21 | 22 | 1 <= preorder.length <= 3000 23 | inorder.length == preorder.length 24 | -3000 <= preorder[i], inorder[i] <= 3000 25 | preorder and inorder consist of unique values. 26 | Each value of inorder also appears in preorder. 27 | preorder is guaranteed to be the preorder traversal of the tree. 28 | inorder is guaranteed to be the inorder traversal of the tree. -------------------------------------------------------------------------------- /doc/101-200/106-construct-binary-tree-from-inorder-and-postorder-traversal.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 2 | 3 | Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree. 4 | 5 | 6 | 7 | 8 | Example 1: 9 | 10 | ![e1](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg) 11 | 12 | Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] 13 | Output: [3,9,20,null,null,15,7] 14 | Example 2: 15 | 16 | Input: inorder = [-1], postorder = [-1] 17 | Output: [-1] 18 | 19 | 20 | Constraints: 21 | 22 | 1 <= inorder.length <= 3000 23 | postorder.length == inorder.length 24 | -3000 <= inorder[i], postorder[i] <= 3000 25 | inorder and postorder consist of unique values. 26 | Each value of postorder also appears in inorder. 27 | inorder is guaranteed to be the inorder traversal of the tree. 28 | postorder is guaranteed to be the postorder traversal of the tree. -------------------------------------------------------------------------------- /doc/101-200/107-binary-tree-level-order-traversal-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 2 | 3 | 4 | Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root). 5 | 6 | 7 | 8 | Example 1: 9 | 10 | 11 | ![e1](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) 12 | 13 | Input: root = [3,9,20,null,null,15,7] 14 | Output: [[15,7],[9,20],[3]] 15 | Example 2: 16 | 17 | Input: root = [1] 18 | Output: [[1]] 19 | Example 3: 20 | 21 | Input: root = [] 22 | Output: [] 23 | 24 | 25 | Constraints: 26 | 27 | The number of nodes in the tree is in the range [0, 2000]. 28 | -1000 <= Node.val <= 1000 -------------------------------------------------------------------------------- /doc/101-200/108-convert-sorted-array-to-binary-search-tree.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 2 | 3 | Given an integer array nums where the elements are sorted in ascending order, convert it to a 4 | height-balanced binary search tree. 5 | 6 | # Example 7 | 8 | Example 1: 9 | 10 | ![ex1](https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg) 11 | 12 | ``` 13 | Input: nums = [-10,-3,0,5,9] 14 | Output: [0,-3,9,-10,null,5] 15 | Explanation: [0,-10,5,null,-3,null,9] is also accepted: 16 | ``` 17 | 18 | ![bt2](https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg) 19 | 20 | Example 2: 21 | 22 | ![ex2](https://assets.leetcode.com/uploads/2021/02/18/btree.jpg) 23 | 24 | ``` 25 | Input: nums = [1,3] 26 | Output: [3,1] 27 | Explanation: [1,null,3] and [3,1] are both height-balanced BSTs. 28 | ``` 29 | 30 | # Constraints: 31 | 32 | 1 <= nums.length <= 10^4 33 | -10^4 <= nums[i] <= 10^4 34 | nums is sorted in a strictly increasing order. 35 | 36 | PS: 这一题也可以结合无顺序作为考点,不过难度不大。 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /doc/101-200/109-convert-sorted-list-to-binary-search-tree.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 2 | 3 | Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 4 | 5 | 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. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ![ex1](https://assets.leetcode.com/uploads/2020/08/17/linked.jpg) 12 | 13 | Input: head = [-10,-3,0,5,9] 14 | Output: [0,-3,9,-10,null,5] 15 | Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. 16 | Example 2: 17 | 18 | Input: head = [] 19 | Output: [] 20 | Example 3: 21 | 22 | Input: head = [0] 23 | Output: [0] 24 | Example 4: 25 | 26 | Input: head = [1,3] 27 | Output: [3,1] 28 | 29 | 30 | Constraints: 31 | 32 | The number of nodes in head is in the range [0, 2 * 104]. 33 | -10^5 <= Node.val <= 10^5 34 | -------------------------------------------------------------------------------- /doc/101-200/110-balanced-binary-tree.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/balanced-binary-tree/description/ 2 | 3 | Given a binary tree, determine if it is height-balanced . 4 | 5 | # Example 6 | 7 | Example 1: 8 | 9 | 10 | ![e1](https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg) 11 | 12 | ``` 13 | Input: root = [3,9,20,null,null,15,7] 14 | Output: true 15 | ``` 16 | 17 | Example 2: 18 | 19 | ![ex2](https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg) 20 | 21 | ``` 22 | Input: root = [1,2,2,3,3,null,null,4,4] 23 | Output: false 24 | ``` 25 | 26 | Example 3: 27 | 28 | ``` 29 | Input: root = [] 30 | Output: true 31 | ``` 32 | 33 | # Constraints: 34 | 35 | The number of nodes in the tree is in the range [0, 5000]. 36 | -10^4 <= Node.val <= 10^4 37 | 38 | -------------------------------------------------------------------------------- /doc/101-200/111-minimum-depth-of-binary-tree.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/minimum-depth-of-binary-tree/ 2 | 3 | Given a binary tree, find its minimum depth. 4 | 5 | The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 6 | 7 | Note: A leaf is a node with no children. 8 | 9 | # Example 10 | 11 | Example 1: 12 | 13 | ![ex1](https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg) 14 | 15 | ``` 16 | Input: root = [3,9,20,null,null,15,7] 17 | Output: 2 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: root = [2,null,3,null,4,null,5,null,6] 24 | Output: 5 25 | ``` 26 | 27 | # Constraints: 28 | 29 | The number of nodes in the tree is in the range [0, 10^5]. 30 | -1000 <= Node.val <= 1000 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /doc/101-200/112-path-sum.md: -------------------------------------------------------------------------------- 1 | Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. 2 | 3 | A leaf is a node with no children. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![e1](https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg) 10 | 11 | Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 12 | Output: true 13 | Example 2: 14 | 15 | ![e2](https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg) 16 | 17 | Input: root = [1,2,3], targetSum = 5 18 | Output: false 19 | Example 3: 20 | 21 | Input: root = [1,2], targetSum = 0 22 | Output: false 23 | 24 | 25 | Constraints: 26 | 27 | The number of nodes in the tree is in the range [0, 5000]. 28 | -1000 <= Node.val <= 1000 29 | -1000 <= targetSum <= 1000 -------------------------------------------------------------------------------- /doc/101-200/113-path-sum-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/path-sum-ii/ 2 | 3 | Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where each path's sum equals targetSum. 4 | 5 | A leaf is a node with no children. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ![e1](https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg) 12 | 13 | Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 14 | Output: [[5,4,11,2],[5,8,4,5]] 15 | Example 2: 16 | 17 | ![e2](https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg) 18 | 19 | Input: root = [1,2,3], targetSum = 5 20 | Output: [] 21 | Example 3: 22 | 23 | Input: root = [1,2], targetSum = 0 24 | Output: [] 25 | 26 | 27 | Constraints: 28 | 29 | The number of nodes in the tree is in the range [0, 5000]. 30 | -1000 <= Node.val <= 1000 31 | -1000 <= targetSum <= 1000 32 | -------------------------------------------------------------------------------- /doc/101-200/115-distinct-subsequences.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/distinct-subsequences/ 2 | 3 | 4 | Given two strings s and t, return the number of distinct subsequences of s which equals t. 5 | 6 | The test cases are generated so that the answer fits on a 32-bit signed integer. 7 | 8 | 9 | Example 1: 10 | 11 | Input: s = "rabbbit", t = "rabbit" 12 | Output: 3 13 | Explanation: 14 | As shown below, there are 3 ways you can generate "rabbit" from s. 15 | rabbbit 16 | rabbbit 17 | rabbbit 18 | 19 | bbb 这3个去掉任何一个都是方式。所以一共有3种。 20 | 21 | Example 2: 22 | 23 | Input: s = "babgbag", t = "bag" 24 | Output: 5 25 | Explanation: 26 | As shown below, there are 5 ways you can generate "bag" from s. 27 | babgbag 28 | babgbag 29 | babgbag 30 | babgbag 31 | babgbag 32 | 33 | # Constraints: 34 | 35 | 1 <= s.length, t.length <= 1000 36 | s and t consist of English letters 37 | -------------------------------------------------------------------------------- /doc/101-200/118-pascals-triangle.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/pascals-triangle/ 2 | 3 | Given an integer numRows, return the first numRows of Pascal's triangle. 4 | 5 | In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: 6 | 7 | ![PascalTriangleAnimated2](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif) 8 | 9 | ## Example 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: numRows = 5 15 | Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: numRows = 1 22 | Output: [[1]] 23 | ``` 24 | 25 | ## Constraints: 26 | 27 | 1 <= numRows <= 30 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /doc/101-200/119-pascals-triangle-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/pascals-triangle-ii/ 2 | 3 | Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle. 4 | 5 | In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: 6 | 7 | ![PascalTriangleAnimated2](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif) 8 | 9 | ## Ex 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: rowIndex = 3 15 | Output: [1,3,3,1] 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: rowIndex = 0 22 | Output: [1] 23 | ``` 24 | 25 | Example 3: 26 | 27 | ``` 28 | Input: rowIndex = 1 29 | Output: [1,1] 30 | ``` 31 | 32 | ## Constraints: 33 | 34 | 0 <= rowIndex <= 33 35 | 36 | 37 | Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space? -------------------------------------------------------------------------------- /doc/101-200/120-triangle.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/triangle/ 2 | 3 | Given a triangle array, return the minimum path sum from top to bottom. 4 | 5 | For each step, you may move to an adjacent number of the row below. 6 | 7 | More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]] 15 | Output: 11 16 | Explanation: The triangle looks like: 17 | 2 18 | 3 4 19 | 6 5 7 20 | 4 1 8 3 21 | The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above). 22 | ``` 23 | 24 | Example 2: 25 | 26 | ``` 27 | Input: triangle = [[-10]] 28 | Output: -10 29 | ``` 30 | 31 | Constraints: 32 | 33 | 1 <= triangle.length <= 200 34 | triangle[0].length == 1 35 | triangle[i].length == triangle[i - 1].length + 1 36 | -104 <= triangle[i][j] <= 104 37 | 38 | 39 | Follow up: Could you do this using only O(n) extra space, where n is the total number of rows in the triangle? -------------------------------------------------------------------------------- /doc/101-200/121-best-time-to-buy-and-sell-stock.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 2 | 3 | You are given an array prices where prices[i] is the price of a given stock on the ith day. 4 | 5 | You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. 6 | 7 | Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. 8 | 9 | ## EX 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: prices = [7,1,5,3,6,4] 15 | Output: 5 16 | Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. 17 | Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. 18 | ``` 19 | 20 | Example 2: 21 | 22 | ``` 23 | Input: prices = [7,6,4,3,1] 24 | Output: 0 25 | Explanation: In this case, no transactions are done and the max profit = 0. 26 | ``` 27 | 28 | ## Constraints: 29 | 30 | 1 <= prices.length <= 10^5 31 | 0 <= prices[i] <= 10^4 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /doc/101-200/128-longest-consecutive-sequence.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/longest-consecutive-sequence/ 2 | 3 | Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. 4 | 5 | You must write an algorithm that runs in O(n) time. 6 | 7 | ## Ex 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: nums = [100,4,200,1,3,2] 13 | Output: 4 14 | Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [0,3,7,2,5,8,4,6,0,1] 21 | Output: 9 22 | ``` 23 | 24 | ## Constraints: 25 | 26 | 0 <= nums.length <= 10^5 27 | 28 | -10^9 <= nums[i] <= 10^9 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /doc/101-200/131-palindrome-partitioning.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/palindrome-partitioning/ 2 | 3 | Given a string s, partition s such that every substring of the partition is a palindrome. 4 | 5 | Return all possible palindrome partitioning of s. 6 | 7 | A palindrome string is a string that reads the same backward as forward. 8 | 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: s = "aab" 14 | Output: [["a","a","b"],["aa","b"]] 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: s = "a" 21 | Output: [["a"]] 22 | ``` 23 | 24 | Constraints: 25 | 26 | 1 <= s.length <= 16 27 | s contains only lowercase English letters. -------------------------------------------------------------------------------- /doc/101-200/132-palindrome-partitioning-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/palindrome-partitioning-ii/ 2 | 3 | Given a string s, partition s such that every substring of the partition is a palindrome. 4 | 5 | Return the minimum cuts needed for a palindrome partitioning of s. 6 | 7 | ## Ex 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: s = "aab" 13 | Output: 1 14 | Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: s = "a" 21 | Output: 0 22 | ``` 23 | 24 | Example 3: 25 | 26 | 27 | ``` 28 | Input: s = "ab" 29 | Output: 1 30 | ``` 31 | 32 | ## Constraints: 33 | 34 | 1 <= s.length <= 2000 35 | s consists of lowercase English letters only. 36 | 37 | 38 | -------------------------------------------------------------------------------- /doc/101-200/136-single-number.md: -------------------------------------------------------------------------------- 1 | Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. 2 | 3 | Follow up: Could you implement a solution with a linear runtime complexity and without using extra memory? 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: nums = [2,2,1] 10 | Output: 1 11 | Example 2: 12 | 13 | Input: nums = [4,1,2,1,2] 14 | Output: 4 15 | Example 3: 16 | 17 | Input: nums = [1] 18 | Output: 1 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= nums.length <= 3 * 10^4 24 | -3 * 10^4 <= nums[i] <= 3 * 10^4 25 | 26 | Each element in the array appears twice except for one element which appears only once. -------------------------------------------------------------------------------- /doc/101-200/137-single-number-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/single-number-ii/ 2 | 3 | Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: nums = [2,2,3,2] 10 | Output: 3 11 | Example 2: 12 | 13 | Input: nums = [0,1,0,1,0,1,99] 14 | Output: 99 15 | 16 | 17 | Constraints: 18 | 19 | 1 <= nums.length <= 3 * 104 20 | -2^31 <= nums[i] <= 2^31 - 1 21 | 22 | Each element in nums appears exactly three times except for one element which appears once. 23 | 24 | 25 | Follow up: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? -------------------------------------------------------------------------------- /doc/101-200/143-reorder-list.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/reorder-list/ 2 | 3 | You are given the head of a singly linked-list. The list can be represented as: 4 | 5 | L0 → L1 → … → Ln - 1 → Ln 6 | 7 | Reorder the list to be on the following form: 8 | 9 | L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → … 10 | 11 | You may not modify the values in the list's nodes. Only nodes themselves may be changed. 12 | 13 | 14 | ## Ex 15 | 16 | Example 1: 17 | 18 | ``` 19 | Input: head = [1,2,3,4] 20 | Output: [1,4,2,3] 21 | ``` 22 | 23 | Example 2: 24 | 25 | ``` 26 | Input: head = [1,2,3,4,5] 27 | Output: [1,5,2,4,3] 28 | ``` 29 | 30 | ## Constraints: 31 | 32 | The number of nodes in the list is in the range [1, 5 * 10^4]. 33 | 34 | 1 <= Node.val <= 1000 35 | -------------------------------------------------------------------------------- /doc/101-200/144-binary-tree-preorder-traversal.md: -------------------------------------------------------------------------------- 1 | Given the root of a binary tree, return the preorder traversal of its nodes' values. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | ![e1](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg) 8 | 9 | Input: root = [1,null,2,3] 10 | Output: [1,2,3] 11 | Example 2: 12 | 13 | Input: root = [] 14 | Output: [] 15 | Example 3: 16 | 17 | Input: root = [1] 18 | Output: [1] 19 | Example 4: 20 | 21 | ![e4](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg) 22 | 23 | Input: root = [1,2] 24 | Output: [1,2] 25 | Example 5: 26 | 27 | ![e5](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg) 28 | 29 | Input: root = [1,null,2] 30 | Output: [1,2] 31 | 32 | 33 | Constraints: 34 | 35 | The number of nodes in the tree is in the range [0, 100]. 36 | -100 <= Node.val <= 100 37 | 38 | 39 | Follow up: Recursive solution is trivial, could you do it iteratively? 40 | 41 | -------------------------------------------------------------------------------- /doc/101-200/145-binary-tree-postorder-traversal.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/binary-tree-postorder-traversal/ 2 | 3 | Given the root of a binary tree, return the postorder traversal of its nodes' values. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![e1](https://assets.leetcode.com/uploads/2020/08/28/pre1.jpg) 10 | 11 | Input: root = [1,null,2,3] 12 | Output: [3,2,1] 13 | Example 2: 14 | 15 | Input: root = [] 16 | Output: [] 17 | Example 3: 18 | 19 | Input: root = [1] 20 | Output: [1] 21 | Example 4: 22 | 23 | ![e4](https://assets.leetcode.com/uploads/2020/08/28/pre3.jpg) 24 | 25 | Input: root = [1,2] 26 | Output: [2,1] 27 | Example 5: 28 | 29 | ![e5](https://assets.leetcode.com/uploads/2020/08/28/pre2.jpg) 30 | 31 | Input: root = [1,null,2] 32 | Output: [2,1] 33 | 34 | 35 | Constraints: 36 | 37 | The number of the nodes in the tree is in the range [0, 100]. 38 | -100 <= Node.val <= 100 39 | 40 | 41 | Follow up: 42 | 43 | Recursive solution is trivial, could you do it iteratively? -------------------------------------------------------------------------------- /doc/101-200/148-sort-list.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/sort-list/description/ 2 | 3 | Given the head of a linked list, return the list after sorting it in ascending order. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | 10 | Input: head = [4,2,1,3] 11 | Output: [1,2,3,4] 12 | Example 2: 13 | 14 | 15 | Input: head = [-1,5,3,4,0] 16 | Output: [-1,0,3,4,5] 17 | Example 3: 18 | 19 | Input: head = [] 20 | Output: [] 21 | 22 | 23 | Constraints: 24 | 25 | The number of nodes in the list is in the range [0, 5 * 104]. 26 | -105 <= Node.val <= 105 27 | 28 | 29 | Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)? 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /doc/101-200/149-max-points-on-a-line.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/max-points-on-a-line/description/ 2 | 3 | Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ![ex1](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg) 10 | 11 | ``` 12 | Input: points = [[1,1],[2,2],[3,3]] 13 | Output: 3 14 | ``` 15 | 16 | Example 2: 17 | 18 | ![ex2](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg) 19 | 20 | ``` 21 | Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 22 | Output: 4 23 | ``` 24 | 25 | Constraints: 26 | 27 | 1 <= points.length <= 300 28 | points[i].length == 2 29 | -10^4 <= xi, yi <= 10^4 30 | All the points are unique. 31 | -------------------------------------------------------------------------------- /doc/101-200/152-maximum-product-subarray.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/maximum-product-subarray/ 2 | 3 | 4 | Given an integer array nums, find a subarray that has the largest product, and return the product. 5 | 6 | The test cases are generated so that the answer will fit in a 32-bit integer. 7 | 8 | ## EX 9 | 10 | Example 1: 11 | 12 | ``` 13 | Input: nums = [2,3,-2,4] 14 | Output: 6 15 | Explanation: [2,3] has the largest product 6. 16 | ``` 17 | 18 | Example 2: 19 | 20 | ``` 21 | Input: nums = [-2,0,-1] 22 | Output: 0 23 | Explanation: The result cannot be 2, because [-2,-1] is not a subarray. 24 | ``` 25 | 26 | 27 | ## Constraints: 28 | 29 | 1 <= nums.length <= 2 * 10^4 30 | 31 | -10 <= nums[i] <= 10 32 | 33 | The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. -------------------------------------------------------------------------------- /doc/101-200/164-maximum-gap.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/maximum-gap/ 2 | 3 | Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less than two elements, return 0. 4 | 5 | You must write an algorithm that runs in linear time and uses linear extra space. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: nums = [3,6,9,1] 13 | Output: 3 14 | Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3. 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: nums = [10] 21 | Output: 0 22 | Explanation: The array contains less than 2 elements, therefore return 0. 23 | ``` 24 | 25 | Constraints: 26 | 27 | 1 <= nums.length <= 10^5 28 | 29 | 0 <= nums[i] <= 10^9 -------------------------------------------------------------------------------- /doc/101-200/166-fraction-to-recurring-decimal.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/fraction-to-recurring-decimal/ 2 | 3 | Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. 4 | 5 | If the fractional part is repeating, enclose the repeating part in parentheses. 6 | 7 | If multiple answers are possible, return any of them. 8 | 9 | It is guaranteed that the length of the answer string is less than 104 for all the given inputs. 10 | 11 | 12 | ## EX 13 | 14 | Example 1: 15 | 16 | ``` 17 | Input: numerator = 1, denominator = 2 18 | Output: "0.5" 19 | ``` 20 | 21 | Example 2: 22 | 23 | ``` 24 | Input: numerator = 2, denominator = 1 25 | Output: "2" 26 | ``` 27 | 28 | Example 3: 29 | 30 | ``` 31 | Input: numerator = 4, denominator = 333 32 | Output: "0.(012)" 33 | ``` 34 | 35 | ## Constraints: 36 | 37 | -2^31 <= numerator, denominator <= 2^31 - 1 38 | 39 | denominator != 0 -------------------------------------------------------------------------------- /doc/101-200/168-excel-sheet-column-title.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/excel-sheet-column-title/ 2 | 3 | Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet. 4 | 5 | For example: 6 | 7 | A -> 1 8 | B -> 2 9 | C -> 3 10 | ... 11 | Z -> 26 12 | AA -> 27 13 | AB -> 28 14 | ... 15 | 16 | 17 | Example 1: 18 | 19 | Input: columnNumber = 1 20 | Output: "A" 21 | 22 | Example 2: 23 | 24 | Input: columnNumber = 28 25 | Output: "AB" 26 | Example 3: 27 | 28 | Input: columnNumber = 701 29 | Output: "ZY" 30 | 31 | 32 | Constraints: 33 | 34 | 1 <= columnNumber <= 2^31 - 1 -------------------------------------------------------------------------------- /doc/101-200/169-excel-sheet-column-number.md: -------------------------------------------------------------------------------- 1 | Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number. 2 | 3 | For example: 4 | 5 | A -> 1 6 | B -> 2 7 | C -> 3 8 | ... 9 | Z -> 26 10 | AA -> 27 11 | AB -> 28 12 | ... 13 | 14 | 15 | Example 1: 16 | 17 | Input: columnTitle = "A" 18 | Output: 1 19 | Example 2: 20 | 21 | Input: columnTitle = "AB" 22 | Output: 28 23 | Example 3: 24 | 25 | Input: columnTitle = "ZY" 26 | Output: 701 27 | 28 | 29 | Constraints: 30 | 31 | 1 <= columnTitle.length <= 7 32 | columnTitle consists only of uppercase English letters. 33 | columnTitle is in the range ["A", "FXSHRXW"]. -------------------------------------------------------------------------------- /doc/101-200/169-majority-element.md: -------------------------------------------------------------------------------- 1 | https://leetcode-cn.com/problems/majority-element/ 2 | 3 | 给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。 4 | 5 | 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 6 | 7 | 示例 1: 8 | 9 | 输入:[3,2,3] 10 | 输出:3 11 | 示例 2: 12 | 13 | 输入:[2,2,1,1,1,2,2] 14 | 输出:2 15 |   16 | 17 | 进阶: 18 | 19 | 尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。 20 | 21 | 来源:力扣(LeetCode) 22 | 链接:https://leetcode-cn.com/problems/majority-element 23 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 -------------------------------------------------------------------------------- /doc/101-200/172-factorial-trailing-zeroes.md: -------------------------------------------------------------------------------- 1 | Given an integer n, return the number of trailing zeroes in n!. 2 | 3 | Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: n = 3 11 | Output: 0 12 | Explanation: 3! = 6, no trailing zero. 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: n = 5 19 | Output: 1 20 | Explanation: 5! = 120, one trailing zero. 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: n = 0 27 | Output: 0 28 | ``` 29 | 30 | Constraints: 31 | 32 | 0 <= n <= 10^4 33 | 34 | 35 | Follow up: Could you write a solution that works in logarithmic time complexity? -------------------------------------------------------------------------------- /doc/101-200/179-largest-number.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/largest-number/ 2 | 3 | Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. 4 | 5 | Since the result may be very large, so you need to return a string instead of an integer. 6 | 7 | ## EX 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: nums = [10,2] 13 | Output: "210" 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [3,30,34,5,9] 20 | Output: "9534330" 21 | ``` 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 100 26 | 27 | 0 <= nums[i] <= 10^9 -------------------------------------------------------------------------------- /doc/101-200/187-repeated-dna-sequences.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/repeated-dna-sequences/description/ 2 | 3 | The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'. 4 | 5 | For example, "ACGAATTCCG" is a DNA sequence. 6 | When studying DNA, it is useful to identify repeated sequences within the DNA. 7 | 8 | Given a string s that represents a DNA sequence, return all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in any order. 9 | 10 | 11 | 12 | Example 1: 13 | 14 | Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" 15 | Output: ["AAAAACCCCC","CCCCCAAAAA"] 16 | Example 2: 17 | 18 | Input: s = "AAAAAAAAAAAAA" 19 | Output: ["AAAAAAAAAA"] 20 | 21 | 22 | Constraints: 23 | 24 | 1 <= s.length <= 10^5 25 | 26 | s[i] is either 'A', 'C', 'G', or 'T'. 27 | -------------------------------------------------------------------------------- /doc/101-200/189-rotate-array.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/rotate-array/ 2 | 3 | Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: nums = [1,2,3,4,5,6,7], k = 3 9 | Output: [5,6,7,1,2,3,4] 10 | Explanation: 11 | rotate 1 steps to the right: [7,1,2,3,4,5,6] 12 | rotate 2 steps to the right: [6,7,1,2,3,4,5] 13 | rotate 3 steps to the right: [5,6,7,1,2,3,4] 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [-1,-100,3,99], k = 2 20 | Output: [3,99,-1,-100] 21 | Explanation: 22 | rotate 1 steps to the right: [99,-1,-100,3] 23 | rotate 2 steps to the right: [3,99,-1,-100] 24 | ``` 25 | 26 | Constraints: 27 | 28 | 1 <= nums.length <= 10^5 29 | -2^31 <= nums[i] <= 2^31 - 1 30 | 0 <= k <= 10^5 31 | 32 | 33 | Follow up: 34 | 35 | Try to come up with as many solutions as you can. There are at least three different ways to solve this problem. 36 | Could you do it in-place with O(1) extra space? -------------------------------------------------------------------------------- /doc/101-200/198-house-robber.md: -------------------------------------------------------------------------------- 1 | You are a professional robber planning to rob houses along a street. 2 | 3 | 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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. 4 | 5 | Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: nums = [1,2,3,1] 11 | Output: 4 12 | Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). 13 | Total amount you can rob = 1 + 3 = 4. 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: nums = [2,7,9,3,1] 20 | Output: 12 21 | Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). 22 | Total amount you can rob = 2 + 9 + 1 = 12. 23 | ``` 24 | 25 | Constraints: 26 | 27 | 1 <= nums.length <= 100 28 | 29 | 0 <= nums[i] <= 400 30 | -------------------------------------------------------------------------------- /doc/101-200/199-binary-tree-right-side-view.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/binary-tree-right-side-view/ 2 | 3 | Given the root of 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. 4 | 5 | 6 | ## EX 7 | 8 | Example 1: 9 | 10 | ![ex1](https://assets.leetcode.com/uploads/2021/02/14/tree.jpg) 11 | 12 | ``` 13 | Input: root = [1,2,3,null,5,null,4] 14 | Output: [1,3,4] 15 | ``` 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: root = [1,null,3] 21 | Output: [1,3] 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: root = [] 28 | Output: [] 29 | ``` 30 | 31 | ## Constraints: 32 | 33 | The number of nodes in the tree is in the range [0, 100]. 34 | 35 | -100 <= Node.val <= 100 36 | -------------------------------------------------------------------------------- /doc/101-200/200-number-of-islands.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/number-of-islands/ 2 | 3 | Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. 4 | 5 | An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. 6 | 7 | You may assume all four edges of the grid are all surrounded by water. 8 | 9 | ## EX 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: grid = [ 15 | ["1","1","1","1","0"], 16 | ["1","1","0","1","0"], 17 | ["1","1","0","0","0"], 18 | ["0","0","0","0","0"] 19 | ] 20 | Output: 1 21 | ``` 22 | 23 | Example 2: 24 | 25 | ``` 26 | Input: grid = [ 27 | ["1","1","0","0","0"], 28 | ["1","1","0","0","0"], 29 | ["0","0","1","0","0"], 30 | ["0","0","0","1","1"] 31 | ] 32 | 33 | Output: 3 34 | ``` 35 | 36 | ## Constraints: 37 | 38 | m == grid.length 39 | n == grid[i].length 40 | 1 <= m, n <= 300 41 | grid[i][j] is '0' or '1'. -------------------------------------------------------------------------------- /doc/1300-1400/1305-all-elements-in-two-binary-search-trees.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/all-elements-in-two-binary-search-trees/ 2 | 3 | Given two binary search trees root1 and root2. 4 | 5 | Return a list containing all the integers from both trees sorted in ascending order. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ![ex1](https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png) 12 | 13 | Input: root1 = [2,1,4], root2 = [1,0,3] 14 | Output: [0,1,1,2,3,4] 15 | Example 2: 16 | 17 | Input: root1 = [0,-10,10], root2 = [5,1,7,0,2] 18 | Output: [-10,0,0,1,2,5,7,10] 19 | Example 3: 20 | 21 | Input: root1 = [], root2 = [5,1,7,0,2] 22 | Output: [0,1,2,5,7] 23 | Example 4: 24 | 25 | Input: root1 = [0,-10,10], root2 = [] 26 | Output: [-10,0,10] 27 | Example 5: 28 | 29 | ![ex5](https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png) 30 | 31 | Input: root1 = [1,null,8], root2 = [8,1] 32 | Output: [1,1,8,8] 33 | 34 | 35 | Constraints: 36 | 37 | Each tree has at most 5000 nodes. 38 | Each node's value is between [-10^5, 10^5]. -------------------------------------------------------------------------------- /doc/200-300/201-bitwise-and-of-numbers-range.md: -------------------------------------------------------------------------------- 1 | Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: left = 5, right = 7 9 | Output: 4 10 | ``` 11 | 12 | Example 2: 13 | 14 | ``` 15 | Input: left = 0, right = 0 16 | Output: 0 17 | ``` 18 | 19 | Example 3: 20 | 21 | ``` 22 | Input: left = 1, right = 2147483647 23 | Output: 0 24 | ``` 25 | 26 | Constraints: 27 | 28 | 0 <= left <= right <= 2^31 - 1 -------------------------------------------------------------------------------- /doc/200-300/202-happy-number.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/happy-number/description/ 2 | 3 | Write an algorithm to determine if a number n is happy. 4 | 5 | A happy number is a number defined by the following process: 6 | 7 | Starting with any positive integer, replace the number by the sum of the squares of its digits. 8 | 9 | Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. 10 | 11 | Those numbers for which this process ends in 1 are happy. 12 | 13 | Return true if n is a happy number, and false if not. 14 | 15 | Example 1: 16 | 17 | ``` 18 | Input: n = 19 19 | Output: true 20 | Explanation: 21 | 1^2 + 9^2 = 82 22 | 8^2 + 2^2 = 68 23 | 6^2 + 8^2 = 100 24 | 1^2 + 0^2 + 0^2 = 1 25 | ``` 26 | 27 | Example 2: 28 | 29 | ``` 30 | Input: n = 2 31 | Output: false 32 | ``` 33 | 34 | Constraints: 35 | 36 | 1 <= n <= 2^31 - 1 37 | 38 | -------------------------------------------------------------------------------- /doc/200-300/203-remove-linked-list-elements.md: -------------------------------------------------------------------------------- 1 | Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head. 2 | 3 | ## EX 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: head = [1,2,6,3,4,5,6], val = 6 9 | Output: [1,2,3,4,5] 10 | ``` 11 | 12 | Example 2: 13 | 14 | ``` 15 | Input: head = [], val = 1 16 | Output: [] 17 | ``` 18 | 19 | Example 3: 20 | 21 | ``` 22 | Input: head = [7,7,7,7], val = 7 23 | Output: [] 24 | ``` 25 | 26 | ## Constraints: 27 | 28 | The number of nodes in the list is in the range [0, 104]. 29 | 30 | 1 <= Node.val <= 50 31 | 32 | 0 <= val <= 50 -------------------------------------------------------------------------------- /doc/200-300/204-count-primes.md: -------------------------------------------------------------------------------- 1 | Given an integer n, return the number of prime numbers that are strictly less than n. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: n = 10 9 | Output: 4 10 | Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 11 | ``` 12 | 13 | Example 2: 14 | 15 | ``` 16 | Input: n = 0 17 | Output: 0 18 | ``` 19 | 20 | Example 3: 21 | 22 | ``` 23 | Input: n = 1 24 | Output: 0 25 | ``` 26 | 27 | Constraints: 28 | 29 | 0 <= n <= 5 * 10^6 -------------------------------------------------------------------------------- /doc/200-300/205-isomorphic-strings.md: -------------------------------------------------------------------------------- 1 | Given two strings s and t, determine if they are isomorphic. 2 | 3 | Two strings s and t are isomorphic if the characters in s can be replaced to get t. 4 | 5 | All occurrences of a character must be replaced with another character while preserving the order of characters. 6 | 7 | No two characters may map to the same character, but a character may map to itself. 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: s = "egg", t = "add" 13 | Output: true 14 | ``` 15 | 16 | 17 | Example 2: 18 | 19 | ``` 20 | Input: s = "foo", t = "bar" 21 | Output: false 22 | ``` 23 | 24 | Example 3: 25 | 26 | ``` 27 | Input: s = "paper", t = "title" 28 | Output: true 29 | ``` 30 | 31 | Constraints: 32 | 33 | 1 <= s.length <= 5 * 10^4 34 | t.length == s.length 35 | s and t consist of any valid ascii character. -------------------------------------------------------------------------------- /doc/200-300/206-reverse-linked-list.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/reverse-linked-list/ 2 | 3 | Given the head of a singly linked list, reverse the list, and return the reversed list. 4 | 5 | Example 1: 6 | 7 | ![ex1](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg) 8 | 9 | ``` 10 | Input: head = [1,2,3,4,5] 11 | Output: [5,4,3,2,1] 12 | ``` 13 | 14 | Example 2: 15 | 16 | ![ex2](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg) 17 | 18 | ``` 19 | Input: head = [1,2] 20 | Output: [2,1] 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: head = [] 27 | Output: [] 28 | ``` 29 | 30 | Constraints: 31 | 32 | The number of nodes in the list is the range [0, 5000]. 33 | 34 | -5000 <= Node.val <= 5000 35 | 36 | 37 | Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /doc/200-300/206-single-number-iii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/single-number-iii/ 2 | 3 | Given an integer array 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. You can return the answer in any order. 4 | 5 | Follow up: Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity? 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,2,1,3,2,5] 12 | Output: [3,5] 13 | Explanation: [5, 3] is also a valid answer. 14 | Example 2: 15 | 16 | Input: nums = [-1,0] 17 | Output: [-1,0] 18 | Example 3: 19 | 20 | Input: nums = [0,1] 21 | Output: [1,0] 22 | 23 | 24 | Constraints: 25 | 26 | 2 <= nums.length <= 3 * 10^4 27 | -2^31 <= nums[i] <= 2^31 - 1 28 | 29 | Each integer in nums will appear twice, only two integers will appear once. -------------------------------------------------------------------------------- /doc/200-300/208-implement-trie-prefix-tree.md: -------------------------------------------------------------------------------- 1 | Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。 2 | 3 | 这一数据结构有相当多的应用情景,例如自动补完和拼写检查。 4 | 5 | 请你实现 Trie 类: 6 | 7 | Trie() 初始化前缀树对象。 8 | void insert(String word) 向前缀树中插入字符串 word 。 9 | boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。 10 | boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。 11 | 12 | 13 | 示例: 14 | 15 | ``` 16 | 输入 17 | ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] 18 | [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]] 19 | 输出 20 | [null, null, true, false, true, null, true] 21 | 22 | 解释 23 | Trie trie = new Trie(); 24 | trie.insert("apple"); 25 | trie.search("apple"); // 返回 True 26 | trie.search("app"); // 返回 False 27 | trie.startsWith("app"); // 返回 True 28 | trie.insert("app"); 29 | trie.search("app"); // 返回 True 30 | ``` 31 | 32 | 提示: 33 | 34 | 1 <= word.length, prefix.length <= 2000 35 | 36 | word 和 prefix 仅由小写英文字母组成 37 | 38 | insert、search 和 startsWith 调用次数 总计 不超过 3 * 10^4 次 39 | -------------------------------------------------------------------------------- /doc/200-300/209-minimum-size-subarray-sum.md: -------------------------------------------------------------------------------- 1 | Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. 2 | 3 | If there is no such subarray, return 0 instead. 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: target = 7, nums = [2,3,1,2,4,3] 9 | Output: 2 10 | Explanation: The subarray [4,3] has the minimal length under the problem constraint. 11 | ``` 12 | 13 | Example 2: 14 | 15 | ``` 16 | Input: target = 4, nums = [1,4,4] 17 | Output: 1 18 | ``` 19 | 20 | Example 3: 21 | 22 | ``` 23 | Input: target = 11, nums = [1,1,1,1,1,1,1,1] 24 | Output: 0 25 | ``` 26 | 27 | 28 | Constraints: 29 | 30 | 1 <= target <= 10^9 31 | 1 <= nums.length <= 10^5 32 | 1 <= nums[i] <= 10^4 33 | 34 | 35 | Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)). -------------------------------------------------------------------------------- /doc/200-300/213-house-robber-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.cn/problems/house-robber-ii/ 2 | 3 | # 213. 打家劫舍 II 4 | 5 | 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。 6 | 7 | **这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。** 8 | 9 | 同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。 10 | 11 | 给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额。 12 | 13 | ## 例子 14 | 15 | 示例 1: 16 | 17 | ``` 18 | 输入:nums = [2,3,2] 19 | 输出:3 20 | 解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。 21 | ``` 22 | 23 | 示例 2: 24 | 25 | ``` 26 | 输入:nums = [1,2,3,1] 27 | 输出:4 28 | 解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。 29 |   偷窃到的最高金额 = 1 + 3 = 4 。 30 | ``` 31 | 32 | 示例 3: 33 | 34 | ``` 35 | 输入:nums = [1,2,3] 36 | 输出:3 37 | ``` 38 | 39 | ## 提示: 40 | 41 | 1 <= nums.length <= 100 42 | 43 | 0 <= nums[i] <= 1000 44 | -------------------------------------------------------------------------------- /doc/200-300/214-shortest-palindrome.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/shortest-palindrome/ 2 | 3 | You are given a string s. You can convert s to a palindrome by adding characters in front of it. 4 | 5 | Return the shortest palindrome you can find by performing this transformation. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: s = "aacecaaa" 11 | Output: "aaacecaaa" 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: s = "abcd" 18 | Output: "dcbabcd" 19 | ``` 20 | 21 | Constraints: 22 | 23 | 0 <= s.length <= 5 * 10^4 24 | s consists of lowercase English letters only. 25 | 26 | -------------------------------------------------------------------------------- /doc/200-300/215-kth-largest-element-in-an-array.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/kth-largest-element-in-an-array/ 2 | 3 | Given an integer array nums and an integer k, return the kth largest element in the array. 4 | 5 | Note that it is the kth largest element in the sorted order, not the kth distinct element. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [3,2,1,5,6,4], k = 2 12 | Output: 5 13 | Example 2: 14 | 15 | Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 16 | Output: 4 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= k <= nums.length <= 10^4 22 | 23 | -10^4 <= nums[i] <= 10^4 -------------------------------------------------------------------------------- /doc/200-300/217-contains-duplicate.md: -------------------------------------------------------------------------------- 1 | Given an integer array nums, return true if any value appears at least twice in the array, 2 | and return false if every element is distinct. 3 | 4 | 5 | Example 1: 6 | 7 | Input: nums = [1,2,3,1] 8 | Output: true 9 | Example 2: 10 | 11 | Input: nums = [1,2,3,4] 12 | Output: false 13 | Example 3: 14 | 15 | Input: nums = [1,1,1,3,3,4,3,2,4,2] 16 | Output: true 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= nums.length <= 10^5 22 | -10^9 <= nums[i] <= 10^9 23 | 24 | https://leetcode.com/problems/contains-duplicate/ -------------------------------------------------------------------------------- /doc/200-300/229-majority-element-ii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/majority-element-ii/ 2 | 3 | Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. 4 | 5 | Follow-up: Could you solve the problem in linear time and in O(1) space? 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [3,2,3] 12 | Output: [3] 13 | Example 2: 14 | 15 | Input: nums = [1] 16 | Output: [1] 17 | Example 3: 18 | 19 | Input: nums = [1,2] 20 | Output: [1,2] 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 5 * 10^4 26 | 27 | -10^9 <= nums[i] <= 10^9 -------------------------------------------------------------------------------- /doc/200-300/233-number-of-digit-one.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/number-of-digit-one/ 2 | 3 | Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: n = 13 10 | Output: 6 11 | Example 2: 12 | 13 | Input: n = 0 14 | Output: 0 15 | 16 | 17 | Constraints: 18 | 19 | 0 <= n <= 2 * 10^9 -------------------------------------------------------------------------------- /doc/200-300/257-binary-tree-paths.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/binary-tree-paths/ 2 | 3 | Given the root of a binary tree, return all root-to-leaf paths in any order. 4 | 5 | A leaf is a node with no children. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ![e1](https://assets.leetcode.com/uploads/2021/03/12/paths-tree.jpg) 12 | 13 | Input: root = [1,2,3,null,5] 14 | Output: ["1->2->5","1->3"] 15 | Example 2: 16 | 17 | Input: root = [1] 18 | Output: ["1"] 19 | 20 | 21 | Constraints: 22 | 23 | The number of nodes in the tree is in the range [1, 100]. 24 | -100 <= Node.val <= 100 -------------------------------------------------------------------------------- /doc/206-reverse-linked-list.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/reverse-linked-list/ 2 | 3 | Given the head of a singly linked list, reverse the list, and return the reversed list. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | 10 | Input: head = [1,2,3,4,5] 11 | Output: [5,4,3,2,1] 12 | Example 2: 13 | 14 | 15 | Input: head = [1,2] 16 | Output: [2,1] 17 | Example 3: 18 | 19 | Input: head = [] 20 | Output: [] 21 | 22 | 23 | Constraints: 24 | 25 | The number of nodes in the list is the range [0, 5000]. 26 | -5000 <= Node.val <= 5000 27 | 28 | 29 | Follow up: A linked list can be reversed either iteratively or recursively. 30 | 31 | Could you implement both? -------------------------------------------------------------------------------- /doc/300-400/347-top-k-frequent-elements.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/top-k-frequent-elements/ 2 | 3 | Given an integer array nums and an integer k, return the k most frequent elements. 4 | 5 | You may return the answer in any order. 6 | 7 | Example 1: 8 | 9 | Input: nums = [1,1,1,2,2,3], k = 2 10 | Output: [1,2] 11 | 12 | Example 2: 13 | 14 | Input: nums = [1], k = 1 15 | Output: [1] 16 | 17 | 18 | Constraints: 19 | 20 | 1 <= nums.legth <= 10^5 21 | 22 | k is in the range [1, the number of unique elements in the array]. 23 | 24 | It is guaranteed that the answer is unique. 25 | 26 | 27 | Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size. -------------------------------------------------------------------------------- /doc/400-500/437-path-sum-iii.md: -------------------------------------------------------------------------------- 1 | https://leetcode.com/problems/path-sum-iii/ 2 | 3 | You are given a binary tree in which each node contains an integer value. 4 | 5 | Find the number of paths that sum to a given value. 6 | 7 | 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). 8 | 9 | The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000. 10 | 11 | Example: 12 | 13 | ``` 14 | root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 15 | 16 | 10 17 | / \ 18 | 5 -3 19 | / \ \ 20 | 3 2 11 21 | / \ \ 22 | 3 -2 1 23 | ``` 24 | 25 | Return 3. The paths that sum to 8 are: 26 | 27 | 1. 5 -> 3 28 | 2. 5 -> 2 -> 1 29 | 3. -3 -> 11 -------------------------------------------------------------------------------- /doc/500-600/509-fibonacci-number.md: -------------------------------------------------------------------------------- 1 | 斐波那契数,通常用 F(n) 表示,形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: 2 | 3 | F(0) = 0,F(1) = 1 4 | F(n) = F(n - 1) + F(n - 2),其中 n > 1 5 | 给你 n ,请计算 F(n) 。 6 | 7 |   8 | 9 | 示例 1: 10 | 11 | 输入:2 12 | 输出:1 13 | 解释:F(2) = F(1) + F(0) = 1 + 0 = 1 14 | 示例 2: 15 | 16 | 输入:3 17 | 输出:2 18 | 解释:F(3) = F(2) + F(1) = 1 + 1 = 2 19 | 示例 3: 20 | 21 | 输入:4 22 | 输出:3 23 | 解释:F(4) = F(3) + F(2) = 2 + 1 = 3 24 |   25 | 26 | 提示: 27 | 28 | 0 <= n <= 30 29 | 30 | 来源:力扣(LeetCode) 31 | 链接:https://leetcode-cn.com/problems/fibonacci-number 32 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 -------------------------------------------------------------------------------- /doc/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/houbb/leetcode/ca063827c05660d16a1e545d818903068467e2b7/doc/README.md -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T009_PalindromeNumber.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T009_PalindromeNumber { 8 | 9 | /** 10 | * 优化思路 11 | *

12 | * 可以使用字符串,中间往两边均摊的方法。 13 | *

14 | * Runtime: 7 ms, faster than 76.19% of Java online submissions for Palindrome Number. 15 | * Memory Usage: 38.5 MB, less than 92.10% of Java online submissions for Palindrome Number. 16 | * 17 | * @param x 数字 18 | * @return 结果 19 | * @since v1 20 | */ 21 | public boolean isPalindrome(int x) { 22 | String string = String.valueOf(x); 23 | final int length = string.length(); 24 | int mid = length >> 1; 25 | 26 | // 从中间往两边均摊 27 | for (int i = 0; i < mid; i++) { 28 | if (string.charAt(i) != string.charAt(length - i - 1)) { 29 | return false; 30 | } 31 | } 32 | return true; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T009_PalindromeNumberV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T009_PalindromeNumberV2 { 8 | 9 | /** 10 | * 最佳解法 11 | * 12 | * 【效果】 13 | * 14 | * @param x 数字 15 | * @return 结果 16 | * @since best 17 | */ 18 | public boolean isPalindrome(int x) { 19 | int temp = x; 20 | int ds = 0; 21 | while (temp > 0) { 22 | // 直接反向操作,计算逆序的数值 23 | ds = ds * 10 + (temp % 10); 24 | 25 | // 保留的高位 26 | temp /= 10; 27 | } 28 | 29 | return (x - ds) == 0; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T026_RemoveDuplicatesFromSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T026_RemoveDuplicatesFromSortedArray { 8 | 9 | /** 10 | * 思路 11 | * 12 | * 1. 返回的结果要对 13 | * 2. 返回的长度对应的数组要唯一。 14 | * 15 | * 【效果】 16 | * Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Duplicates from Sorted Array. 17 | * Memory Usage: 41.3 MB, less than 59.78% of Java online submissions for Remove Duplicates from Sorted Array. 18 | * 19 | * @param nums 数组 20 | * @return 结果 21 | * @since v1 22 | */ 23 | public int removeDuplicates(int[] nums) { 24 | int i = 0; 25 | for (int n : nums) { 26 | // 将后面不同的元素复制过来 27 | if (i == 0 || n > nums[i-1]) { 28 | nums[i++] = n; 29 | } 30 | } 31 | return i; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T026_RemoveDuplicatesFromSortedArrayV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T026_RemoveDuplicatesFromSortedArrayV2 { 8 | 9 | /** 10 | * 思路 11 | * 12 | * 1. 返回的结果要对 13 | * 2. 返回的长度对应的数组要唯一。 14 | * 15 | * @param nums 数组 16 | * @return 结果 17 | * @since v1 18 | */ 19 | public int removeDuplicates(int[] nums) { 20 | int i = 0; 21 | for (int n : nums) { 22 | // 前兩個元素不用考慮 23 | // 大于上一个,或者等于上一个且大于上上个 24 | if ((i == 0 || i == 1) || (n > nums[i-1] || (n > nums[i-2] && n == nums[i-1]))) { 25 | nums[i++] = n; 26 | } 27 | } 28 | return i; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T035_SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author d 5 | * @since 1.0.0 6 | */ 7 | public class T035_SearchInsertPosition { 8 | 9 | /** 10 | * 先处理 O(n) 的方式,直接遍历,很简单 11 | * 12 | * 100%,老实说,这一题的测试用例没有区分度。 13 | * 14 | * @param nums 15 | * @param target 16 | * @return 17 | */ 18 | public int searchInsert(int[] nums, int target) { 19 | for(int i = 0; i < nums.length; i++) { 20 | int value = nums[i]; 21 | 22 | if(target == value) { 23 | return i; 24 | } 25 | 26 | if(target < value) { 27 | return i; 28 | } 29 | } 30 | 31 | // 放在最后 32 | return nums.length; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T062_UniquePaths.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T062_UniquePaths { 8 | 9 | public int uniquePaths(int m, int n) { 10 | int[][] dp = new int[m][n]; 11 | 12 | for(int i = 0; i < m; i++) { 13 | for(int j = 0; j < n; j++) { 14 | // 这种其实是不符合条件的,不过我们设定初始值 dp[0][0] = 1 15 | if(i == 0 || j == 0) { 16 | dp[i][j] = 1; 17 | } else { 18 | dp[i][j] = dp[i-1][j] + dp[i][j-1]; 19 | } 20 | } 21 | } 22 | 23 | // 返回所有可能的结果 24 | return dp[m-1][n-1]; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T069_Sqrtx.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | public class T069_Sqrtx { 4 | 5 | /** 6 | * math.sqrt 的实现原理到底是什么? 7 | * 8 | * 最简单的方式,从 i=1开始迭代 9 | * @param x 数字 10 | * @return 结果 11 | */ 12 | public int mySqrt(int x) { 13 | if(x == 0) { 14 | return 0; 15 | } 16 | 17 | for(int i = 1; i <= x; i++) { 18 | // 用除法,替代乘法,避免越界 19 | int div = x / i; 20 | if(div == i) { 21 | return i; 22 | } 23 | if(div < i) { 24 | return i-1; 25 | } 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | public static void main(String[] args) { 32 | T069_Sqrtx sqrtx = new T069_Sqrtx(); 33 | 34 | System.out.println(sqrtx.mySqrt(4)); 35 | System.out.println(sqrtx.mySqrt(8)); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T088_MergeSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | public class T088_MergeSortedArray { 8 | 9 | /** 10 | * 合并 11 | * 12 | * @param nums1 13 | * @param m 14 | * @param nums2 15 | * @param n 16 | */ 17 | public void merge(int[] nums1, int m, int[] nums2, int n) { 18 | // 最后的结果放在 nums1 里面... 19 | 20 | List resultList = new ArrayList<>(); 21 | for(int i = 0; i < m; i++) { 22 | resultList.add(nums1[i]); 23 | } 24 | for(int j = 0; j < n; j++) { 25 | resultList.add(nums2[j]); 26 | } 27 | 28 | // 排序 29 | Collections.sort(resultList); 30 | 31 | // 放在 nums1 中 32 | for(int i = 0; i < resultList.size(); i++) { 33 | nums1[i] = resultList.get(i); 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T096_UniqueBinarySearchTrees.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T096_UniqueBinarySearchTrees { 8 | 9 | public int numTrees(int n) { 10 | int[] dp = new int[n+1]; 11 | 12 | // 初始化 13 | dp[0] = 1; 14 | dp[1] = 1; 15 | 16 | // 遍历 17 | for(int i = 2; i <= n; i++) { 18 | int sum = 0; 19 | for(int j = 0; j < i; j++) { 20 | // 左边 * 右边 21 | sum += dp[j] * dp[i-1-j]; 22 | } 23 | 24 | dp[i] =sum ; 25 | } 26 | return dp[n]; 27 | } 28 | 29 | public static void main(String[] args) { 30 | T096_UniqueBinarySearchTrees trees = new T096_UniqueBinarySearchTrees(); 31 | 32 | System.out.println(trees.numTrees(3)); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T096_UniqueBinarySearchTreesV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T096_UniqueBinarySearchTreesV2 { 8 | 9 | public int numTrees(int n) { 10 | // 提示:我们在这里需要用 long 类型防止计算过程中的溢出 11 | long c = 1; 12 | 13 | // 当然,针对 2 的乘法,还可以使用位运算进行优化。 14 | for (int i = 0; i < n; ++i) { 15 | c = c * 2 * (2 * i + 1) / (i + 2); 16 | } 17 | 18 | return (int) c; 19 | } 20 | 21 | public static void main(String[] args) { 22 | T096_UniqueBinarySearchTreesV2 trees = new T096_UniqueBinarySearchTreesV2(); 23 | 24 | System.out.println(trees.numTrees(3)); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/T100_SameTree.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; 2 | 3 | import com.github.houbb.leetcode.component.TreeNode; 4 | 5 | /** 6 | *【思路】 7 | * 8 | * 1. 同时遍历整2棵树 9 | * 2. 对比每一个节点,全部相同则通过 10 | * 11 | */ 12 | public class T100_SameTree { 13 | 14 | /** 15 | * 递归: 16 | * 17 | * (1)当前节点,左,右 18 | * (2)左子树 19 | * (3)右子树 20 | * 21 | * 最大值,最小值 22 | * @param p p 23 | * @param q q 24 | */ 25 | public boolean isSameTree(TreeNode p, TreeNode q) { 26 | if(p == null && q == null) { 27 | return true; 28 | } 29 | 30 | //1. 值对比 31 | if(p != null && q != null) { 32 | if(p.val == q.val) { 33 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 34 | } 35 | return false; 36 | } 37 | 38 | return false; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F000T100/package-info.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F000T100; -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F1000T1100/T2548_maxPriceBag.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F1000T1100; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | public class T2548_maxPriceBag { 7 | 8 | public static void main(String[] args) { 9 | int[][] items = new int[][]{{50,1}, {10, 8}}; 10 | 11 | T2548_maxPriceBag priceBag = new T2548_maxPriceBag(); 12 | System.out.println(priceBag.maxPrice(items, 5)); 13 | } 14 | 15 | // items[i] = [pricei, weighti] 表示第 i 个物品的价格和重量。 16 | public double maxPrice(int[][] items, int capacity) { 17 | Arrays.sort(items, (a, b) -> a[1] * b[0] - a[0] * b[1]); 18 | 19 | double ans = 0; 20 | for (int[] elem : items) { 21 | int price = elem[0], weight = elem[1]; 22 | int val = Math.min(weight, capacity); 23 | ans += val * 1.0 / weight * price; 24 | capacity -= val; 25 | } 26 | 27 | return capacity > 0 ? -1 : ans; 28 | } 29 | 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T104_MaximumDepthOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import com.github.houbb.leetcode.component.TreeNode; 4 | 5 | /** 6 | * 思路: 7 | * 8 | * 1. 遍历一棵树,然后到最后一层,就是最大的长度 9 | * 2. 应该使用哪一种遍历? 10 | */ 11 | public class T104_MaximumDepthOfBinaryTree { 12 | 13 | public int maxDepth(TreeNode root) { 14 | if(root == null) { 15 | return 0; 16 | } 17 | 18 | // 递归左右 19 | // 递归真的是神! 20 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 21 | } 22 | 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T122_BestTimeToBuyAndSellStockII_DP.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T122_BestTimeToBuyAndSellStockII_DP { 4 | 5 | /** 6 | * DP 思路 7 | */ 8 | 9 | public int maxProfit(int[] prices) { 10 | int buy[] = new int[prices.length]; 11 | int sell[] = new int[prices.length]; 12 | 13 | buy[0] = -prices[0]; 14 | 15 | for(int i = 1; i < prices.length; i++) { 16 | // 是否卖出? 不卖; 卖出=上一次买入 + 当前价格 17 | sell[i] = Math.max(sell[i-1], buy[i-1] + prices[i]); 18 | 19 | // 是否买? 不买; 买入=上一次卖出-当前价格 20 | buy[i] = Math.max(buy[i-1], sell[i-1] - prices[i]); 21 | } 22 | 23 | return sell[prices.length-1]; 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T141_LinkedListCycle.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import com.github.houbb.leetcode.ListNode; 4 | 5 | public class T141_LinkedListCycle { 6 | 7 | /** 8 | * 是否有循环 9 | * 10 | * 11 | * 1. 快慢指针 12 | * 13 | * 慢:一次走一步 14 | * 快:一次走2步 15 | * 16 | * 如果二者又重复了,说明存在环。 17 | * 18 | * @param head 19 | * @return 20 | */ 21 | public boolean hasCycle(ListNode head) { 22 | ListNode fastPointer = head; 23 | ListNode slowPointer = head; 24 | 25 | while (fastPointer != null 26 | && fastPointer.next != null) { 27 | fastPointer = fastPointer.next.next; 28 | slowPointer = slowPointer.next; 29 | 30 | // 如果相同 31 | if(fastPointer == slowPointer) { 32 | return true; 33 | } 34 | } 35 | 36 | // 循环结果 37 | return false; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T141_LinkedListCycle_ERROR.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import com.github.houbb.leetcode.ListNode; 4 | 5 | public class T141_LinkedListCycle_ERROR { 6 | 7 | /** 8 | * 是否有循环 9 | * 10 | * 11 | * 1. POS 是什么? 12 | * 13 | * 2. 是什么叫重复呢? 14 | * 15 | * 这种算法是错误的,不可以通过值来判断,因为可能值重复。 16 | * 17 | * @param head 18 | * @return 19 | */ 20 | public boolean hasCycle(ListNode head) { 21 | int preValue = -2; 22 | 23 | while (head != null) { 24 | int value = head.val; 25 | 26 | if(value <= preValue) { 27 | return true; 28 | } 29 | 30 | preValue = value; 31 | head = head.next; 32 | } 33 | 34 | // 循环结果 35 | return false; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T142_LinkedListCycleII.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import com.github.houbb.leetcode.ListNode; 4 | 5 | public class T142_LinkedListCycleII { 6 | 7 | /** 8 | * 是否有循环 9 | * 10 | * 11 | * 1. 快慢指针 12 | * 13 | * 慢:一次走一步 14 | * 快:一次走2步 15 | * 16 | * 如果二者又重复了,说明存在环。 17 | * 18 | * @param head 19 | * @return 20 | */ 21 | public boolean hasCycle(ListNode head) { 22 | ListNode fastPointer = head; 23 | ListNode slowPointer = head; 24 | 25 | while (fastPointer != null 26 | && fastPointer.next != null) { 27 | fastPointer = fastPointer.next.next; 28 | slowPointer = slowPointer.next; 29 | 30 | // 如果相同 31 | if(fastPointer == slowPointer) { 32 | return true; 33 | } 34 | } 35 | 36 | // 循环结果 37 | return false; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T152_MaximumProductSubarrayV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T152_MaximumProductSubarrayV2 { 4 | 5 | /** 6 | * 最粗暴的算法: 7 | * 8 | * 1. 直接暴力计算。 9 | * 10 | * 递归呢? 11 | * 12 | * 直接移动 i, j 两个下标志,然后计算结果。 13 | * 14 | * https://leetcode.com/problems/maximum-product-subarray/solutions/1609493/c-simple-solution-w-explanation-optimization-from-brute-force-to-dp/ 15 | * 16 | * @param nums 17 | * @return 18 | */ 19 | public int maxProduct(int[] nums) { 20 | int maxResult = nums[0]; 21 | 22 | for(int i = 0; i < nums.length; i++) { 23 | int temp = 1; 24 | for(int j = i; j < nums.length; j++) { 25 | // 计算这个循环中的所有子数组,而不是从头计算 26 | temp *= nums[j]; 27 | 28 | maxResult = Math.max(temp, maxResult); 29 | } 30 | } 31 | 32 | return maxResult; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T153_FindMinimumInRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T153_FindMinimumInRotatedSortedArray { 4 | 5 | /** 6 | * o(n) 傻瓜式算法 7 | * 8 | * PS:这个测试用例毫无区分度 9 | * 10 | * @param nums 11 | * @return 12 | */ 13 | public int findMin(int[] nums) { 14 | int min = nums[0]; 15 | 16 | for(int i = 1; i < nums.length; i++) { 17 | min = Math.min(min, nums[i]); 18 | } 19 | 20 | return min; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T154_FindMinimumInRotatedSortedArrayII.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T154_FindMinimumInRotatedSortedArrayII { 4 | 5 | /** 6 | * o(n) 傻瓜式算法 7 | * 8 | * PS:这个测试用例毫无区分度 9 | * 10 | * @param nums 11 | * @return 12 | */ 13 | public int findMin(int[] nums) { 14 | int min = nums[0]; 15 | 16 | for(int i = 1; i < nums.length; i++) { 17 | min = Math.min(min, nums[i]); 18 | } 19 | 20 | return min; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T162_FindPeakElement.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T162_FindPeakElement { 4 | 5 | /** 6 | * 这题 test 没意思,直接 O(n) 的效果就是 100% 7 | * @param nums 8 | * @return 9 | */ 10 | public int findPeakElement(int[] nums) { 11 | if(nums.length == 1) { 12 | return 0; 13 | } 14 | 15 | // 第一个 16 | if(nums[0] > nums[1]) { 17 | return 0; 18 | } 19 | 20 | // 最后一个 21 | final int len = nums.length; 22 | if(nums[len-1] > nums[len-2]) { 23 | return len-1; 24 | } 25 | 26 | // 遍历 27 | for(int i = 1; i < len-1; i++) { 28 | if(nums[i] > nums[i-1] 29 | && nums[i] > nums[i+1]) { 30 | return i; 31 | } 32 | } 33 | 34 | // 如果没有呢? 35 | return 0; 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T164_MaximumGap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import java.util.Arrays; 4 | 5 | public class T164_MaximumGap { 6 | 7 | public int maximumGap(int[] nums) { 8 | //1. 排序 9 | Arrays.sort(nums); 10 | 11 | //2. 遍历 12 | int maxGap = 0; 13 | 14 | for(int i = 1; i < nums.length; i++) { 15 | int differ = nums[i] - nums[i-1]; 16 | 17 | if(differ > maxGap) { 18 | maxGap = differ; 19 | } 20 | } 21 | 22 | return maxGap; 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T167_TwoSumIIInputArrayIsSorted.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T167_TwoSumIIInputArrayIsSorted { 4 | 5 | /** 6 | * 双指针 7 | * 8 | * 19 / 21 9 | * 10 | * @param numbers 数组 11 | * @param target 结果 12 | * @return 13 | */ 14 | public int[] twoSum(int[] numbers, int target) { 15 | // 永远有一个解 16 | int[] result = new int[2]; 17 | 18 | for(int i = 0; i < numbers.length-1; i++) { 19 | for(int j = i+1; j < numbers.length; j++) { 20 | int temp = numbers[i] + numbers[j]; 21 | 22 | if(temp == target) { 23 | result[0] = i+1; 24 | result[1] = j+1; 25 | return result; 26 | } 27 | } 28 | } 29 | 30 | return result; 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T168_ExcelSheetColumnTitle.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T168_ExcelSheetColumnTitle { 4 | 5 | public String convertToTitle(int columnNumber) { 6 | StringBuilder stringBuilder = new StringBuilder(); 7 | 8 | while (columnNumber > 0) { 9 | // excel 从1开始 10 | columnNumber--; 11 | 12 | // 插入值 13 | stringBuilder.append((char) ('A' + columnNumber % 26)); 14 | 15 | columnNumber = columnNumber / 26; 16 | } 17 | 18 | return stringBuilder.reverse().toString(); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T169_MajorityElementMoerV3.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T169_MajorityElementMoerV3 { 8 | 9 | /** 10 | * 11 | * @param nums 12 | * @return 13 | */ 14 | public int majorityElement(int[] nums) { 15 | int major = nums[0]; 16 | int count = 1; 17 | 18 | for(int i = 1; i < nums.length; i++) { 19 | int num = nums[i]; 20 | 21 | // 全部抵消,重新赋值 22 | if(count == 0) { 23 | count++; 24 | major = num; 25 | } else if(major == num) { 26 | // 自己阵营,+1 27 | count++; 28 | } else { 29 | // 敌方阵营,-1 30 | count--; 31 | } 32 | } 33 | 34 | return major; 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T169_MajorityElementV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class T169_MajorityElementV2 { 11 | 12 | /** 13 | * Runtime: 9 ms, faster than 31.63% of Java online submissions for Majority Element. 14 | * Memory Usage: 44.1 MB, less than 38.35% of Java online submissions for Majority Element. 15 | * 16 | * @param nums 17 | * @return 18 | */ 19 | public int majorityElement(int[] nums) { 20 | int limit = nums.length >> 1; 21 | Map countMap = new HashMap<>(limit); 22 | for(int n : nums) { 23 | int count = countMap.getOrDefault(n,0)+1; 24 | if(count > limit) { 25 | return n; 26 | } 27 | 28 | countMap.put(n, count); 29 | } 30 | 31 | // 异常 32 | return -1; 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T169_MajorityElementV4.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * @author binbin.hou 7 | * @since 1.0.0 8 | */ 9 | public class T169_MajorityElementV4 { 10 | 11 | /** 12 | * 13 | * 排序之中间的元素肯定超过一半 14 | * @param nums 15 | * @return 16 | */ 17 | public int majorityElement(int[] nums) { 18 | Arrays.sort(nums); 19 | return nums[nums.length >> 1]; 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T171_ExcelSheetColumnNumber.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T171_ExcelSheetColumnNumber { 4 | 5 | public int titleToNumber(String columnTitle) { 6 | // 从1开始 7 | int sum = 1; 8 | 9 | // 倒叙遍历 10 | int count = 0; 11 | for(int i = columnTitle.length()-1; i >= 0; i--) { 12 | int val = (int)(columnTitle.charAt(i) - 'A'); 13 | 14 | sum += val * Math.pow(26, count); 15 | count++; 16 | } 17 | 18 | return sum; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T172_FactorialTrailingZeroesV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public class T172_FactorialTrailingZeroesV2 { 6 | 7 | public static void main(String[] args) { 8 | T172_FactorialTrailingZeroesV2 factorialTrailingZeroes = new T172_FactorialTrailingZeroesV2(); 9 | 10 | System.out.println(factorialTrailingZeroes.trailingZeroes(7)); 11 | } 12 | 13 | /** 14 | * 末尾零的个数 15 | * 16 | * 1. 计算 n! 17 | * 2. 倒序计算 18 | * 19 | * 2 * 5 = 0; 20 | * 偶数是一定存在的,所以只需要看有多少个 5 就行。 21 | * 22 | * 比如 500 23 | * 24 | * 500 / 5 = 100 25 | * 100 / 5 = 20 26 | * 20 / 5 = 4 27 | * 28 | * 共计:124 个 0 29 | * 30 | * @param n 31 | * @return 32 | */ 33 | public int trailingZeroes(int n) { 34 | int count = 0; 35 | 36 | while (n > 0) { 37 | n = n / 5; 38 | count += n; 39 | } 40 | return count; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T187_RepeatedDnaSequences.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashSet; 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | public class T187_RepeatedDnaSequences { 9 | 10 | /** 11 | * 思路: 12 | * 13 | * 1. 以 10 作为步长,截取 substr 14 | * 15 | * 2. 如果一个 substr 出现多次,则放在结果中 16 | * 17 | * 当然,也可以使用 hashmap 等替代 18 | * 19 | * @param s 20 | * @return 21 | */ 22 | public List findRepeatedDnaSequences(String s) { 23 | Set seen = new HashSet<>(); 24 | Set repeat = new HashSet<>(); 25 | 26 | for(int i = 0; i < s.length()-9; i++) { 27 | String subStr = s.substring(i, i+10); 28 | 29 | // false,说明存在多次。 30 | if(!seen.add(subStr)) { 31 | repeat.add(subStr); 32 | } 33 | } 34 | 35 | return new ArrayList<>(repeat); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T189_RotateArray.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T189_RotateArray { 4 | 5 | /** 6 | * 最简单的思路: 7 | * 8 | * 1. 临时数组,把原始的结果存下来 9 | * 2. 更新 nums 10 | * 11 | * @param nums 12 | * @param k 13 | */ 14 | public void rotate(int[] nums, int k) { 15 | final int len = nums.length; 16 | int offset = k % len; 17 | if(offset == 0) { 18 | return; 19 | } 20 | 21 | 22 | int[] temp = new int[len]; 23 | int tempIndex = 0; 24 | // 偏移 25 | //Input: nums = [1,2,3,4,5,6,7], k = 3 26 | //Output: [5,6,7,1,2,3,4] 27 | for(int i = len-1-offset+1; i < len; i++) { 28 | temp[tempIndex++] = nums[i]; 29 | } 30 | for(int i = 0; i <= len-1-offset; i++) { 31 | temp[tempIndex++] = nums[i]; 32 | } 33 | 34 | // 赋值 35 | System.arraycopy(temp, 0, nums, 0, len); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T190_ReverseBits.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T190_ReverseBits { 4 | 5 | // you need treat n as an unsigned value 6 | public int reverseBits(int n) { 7 | return Integer.reverse(n); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T191_NumberOf1Bits.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T191_NumberOf1Bits { 4 | 5 | // you need to treat n as an unsigned value 6 | public int hammingWeight(int n) { 7 | String binary = Integer.toBinaryString(n); 8 | 9 | int c = 0; 10 | 11 | for(int i = 0; i < binary.length(); i++) { 12 | if('1' == binary.charAt(i)) { 13 | c++; 14 | } 15 | } 16 | return c; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T198_HouseRobber.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T198_HouseRobber { 4 | 5 | public int rob(int[] nums) { 6 | return recursive(nums, nums.length-1); 7 | } 8 | 9 | private int recursive(int[] nums, int i) { 10 | if(i < 0) { 11 | return 0; 12 | } 13 | 14 | int robCur = recursive(nums, i-2) + nums[i]; 15 | int notRobCur = recursive(nums, i-1); 16 | return Math.max(robCur, notRobCur); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T198_HouseRobberV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T198_HouseRobberV2 { 4 | 5 | public int rob(int[] nums) { 6 | Integer[] mem = new Integer[nums.length]; 7 | 8 | return recursive(nums, nums.length-1, mem); 9 | } 10 | 11 | private int recursive(int[] nums, int i, Integer[] mem) { 12 | // 获取 cache 13 | if(mem[i] != null) { 14 | return mem[i]; 15 | } 16 | 17 | 18 | int result = 0; 19 | if(i < 0) { 20 | return result; 21 | } 22 | 23 | int robCur = recursive(nums, i-2, mem) + nums[i]; 24 | int notRobCur = recursive(nums, i-1, mem); 25 | 26 | result = Math.max(robCur, notRobCur); 27 | 28 | // cache 29 | mem[i] = result; 30 | return result; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T198_HouseRobberV3.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T198_HouseRobberV3 { 4 | 5 | public int rob(int[] nums) { 6 | int[] dp = new int[nums.length + 1]; 7 | dp[0] = 0; 8 | dp[1] = nums[0]; 9 | 10 | // 从1开始,避免 i-1 越界。 11 | for(int i = 1; i < nums.length; i++) { 12 | dp[i+1] = Math.max(dp[i], dp[i-1] + nums[i]); 13 | } 14 | 15 | return dp[nums.length]; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F100T200/T198_HouseRobberV4.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F100T200; 2 | 3 | public class T198_HouseRobberV4 { 4 | 5 | public int rob(int[] nums) { 6 | int pre1 = 0; 7 | int pre2 = 0; 8 | 9 | for(int val : nums) { 10 | int temp = pre1; 11 | pre1 = Math.max(pre1, pre2+val); 12 | pre2 = temp; 13 | } 14 | 15 | return pre1; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/DeleteNodeInALinkedList.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | import com.github.houbb.leetcode.ListNode; 4 | 5 | /** 6 | * @author binbin.hou 7 | * @since 1.0.0 8 | */ 9 | public class DeleteNodeInALinkedList { 10 | 11 | /** 12 | * 思路: 13 | * 14 | * 当前节点删除的方式: 15 | * 16 | * 下个节点的值,复制到当前节点。 17 | * 18 | * current.next = next.next; 19 | * 20 | * Runtime: 0 ms, faster than 100.00% of Java online submissions for Delete Node in a Linked List. 21 | * Memory Usage: 38.4 MB, less than 57.18% of Java online submissions for Delete Node in a Linked List. 22 | * 23 | * @param node 24 | */ 25 | public void deleteNode(ListNode node) { 26 | node.val = node.next.val; 27 | node.next = node.next.next; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/KthLargestElementInAnArray.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * @author binbin.hou 7 | * @since 1.0.0 8 | */ 9 | public class KthLargestElementInAnArray { 10 | 11 | public static void main(String[] args) { 12 | int[] nums = new int[]{3, 2, 1, 5, 6, 4}; 13 | System.out.println(findKthLargest(nums, 2)); 14 | 15 | int[] nums2 = new int[]{3,2,3,1,2,4,5,5,6}; 16 | System.out.println(findKthLargest(nums2, 4)); 17 | } 18 | 19 | /** 20 | * Runtime: 1 ms, faster than 98.08% of Java online submissions for Kth Largest Element in an Array. 21 | * Memory Usage: 39.2 MB, less than 60.04% of Java online submissions for Kth Largest Element in an Array. 22 | * 23 | * @param nums 24 | * @param k 25 | * @return 26 | */ 27 | public static int findKthLargest(int[] nums, int k) { 28 | Arrays.sort(nums); 29 | return nums[nums.length - k]; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/NumberOfDigitOneMath.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class NumberOfDigitOneMath { 8 | 9 | int countDigitOne(int n) { 10 | if (n < 1) { 11 | return 0; 12 | } 13 | 14 | long digit = 1; 15 | int high = n / 10, current = n % 10, low = 0; 16 | int count = 0; 17 | 18 | while (high != 0 || current != 0) { 19 | if (current == 0) { 20 | count += high * digit; 21 | } 22 | else if (current == 1) { 23 | count += high * digit + low + 1; 24 | } 25 | else { 26 | count += (high + 1) * digit; 27 | } 28 | 29 | // 更新 30 | low += current * digit; 31 | current = high % 10; 32 | high /= 10; 33 | digit *= 10; 34 | } 35 | 36 | return count; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/SingleNumIII.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class SingleNumIII { 11 | 12 | public int[] singleNumber(int[] nums) { 13 | Map map = new HashMap<>(nums.length); 14 | 15 | for(int num : nums) { 16 | map.put(num, map.getOrDefault(num, 0)+1); 17 | } 18 | 19 | // 獲取結果 20 | int[] results = new int[2]; 21 | int size = 0; 22 | for(Map.Entry entry : map.entrySet()) { 23 | if(entry.getValue() == 1) { 24 | results[size++] = entry.getKey(); 25 | } 26 | } 27 | return results; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/SingleNumIIIBest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class SingleNumIIIBest { 8 | 9 | /** 10 | * Runtime: 0 ms, faster than 100.00% of Java online submissions for Single Number III. 11 | * Memory Usage: 39 MB, less than 82.62% of Java online submissions for Single Number III. 12 | * 13 | * @param nums 14 | * @return 15 | */ 16 | public int[] singleNumber(int[] nums) { 17 | int xor = 0; 18 | for(int num : nums) { 19 | xor ^= num; 20 | } 21 | 22 | // 获取非0位 23 | int bit = 1; 24 | while ((bit & xor) == 0) { 25 | bit <<= 1; 26 | } 27 | 28 | // 重新划分数组 29 | int a = 0; 30 | for(int num : nums) { 31 | if((num & bit) == 0) { 32 | a ^= num; 33 | } 34 | } 35 | 36 | // a ^ xor 就是另一个数 37 | return new int[]{a, a ^ xor}; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/SingleNumIIIBetter.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class SingleNumIIIBetter { 11 | 12 | public int[] singleNumber(int[] nums) { 13 | int xor = 0; 14 | for(int num : nums) { 15 | xor ^= num; 16 | } 17 | 18 | // 获取非0位 19 | int bit = 1; 20 | while ((bit & xor) == 0) { 21 | bit <<= 1; 22 | } 23 | 24 | // 重新划分数组 25 | int a = 0; 26 | int b = 0; 27 | for(int num : nums) { 28 | if((num & bit) == 0) { 29 | a ^= num; 30 | } else { 31 | b ^= num; 32 | } 33 | } 34 | return new int[]{a, b}; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T201_BitwiseAndOfNumbersRange.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | public class T201_BitwiseAndOfNumbersRange { 4 | 5 | public static void main(String[] args) { 6 | T201_BitwiseAndOfNumbersRange range = new T201_BitwiseAndOfNumbersRange(); 7 | int r = range.rangeBitwiseAnd(1, 2147483647); 8 | System.out.println(r); 9 | } 10 | 11 | /** 12 | * 暴力运算,不可行。 13 | * 14 | * 会直接超时 15 | * @param left 16 | * @param right 17 | * @return 18 | */ 19 | public int rangeBitwiseAnd(int left, int right) { 20 | int r = left; 21 | 22 | for(int i = left+1; i <= right; i++) { 23 | r = i & r; 24 | } 25 | 26 | return r; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T201_BitwiseAndOfNumbersRangeV2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | public class T201_BitwiseAndOfNumbersRangeV2 { 4 | 5 | public static void main(String[] args) { 6 | T201_BitwiseAndOfNumbersRangeV2 range = new T201_BitwiseAndOfNumbersRangeV2(); 7 | int r = range.rangeBitwiseAnd(1, 2147483647); 8 | System.out.println(r); 9 | } 10 | 11 | /** 12 | * 13 | * 本质: 14 | * 15 | * 寻找,left right 相同的前缀 16 | * 17 | * https://leetcode.com/problems/bitwise-and-of-numbers-range/solutions/593317/simple-3-line-java-solution-faster-than-100/ 18 | * 19 | * @param left 20 | * @param right 21 | * @return 22 | */ 23 | public int rangeBitwiseAnd(int left, int right) { 24 | int i = 0; 25 | while (left != right){ 26 | left >>= 1; 27 | right >>= 1; 28 | 29 | i++; 30 | } 31 | return right << i; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T203_RemoveLinkedListElements.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | import com.github.houbb.leetcode.ListNode; 4 | 5 | public class T203_RemoveLinkedListElements { 6 | 7 | /** 8 | * 移除 9 | * 10 | * 1. 用一个节点保存头节点 11 | * @param head 12 | * @param val 13 | * @return 14 | */ 15 | public ListNode removeElements(ListNode head, int val) { 16 | ListNode dummy = new ListNode(0); 17 | dummy.next = head; 18 | 19 | ListNode cur = head; 20 | ListNode prev = dummy; 21 | while (cur != null) { 22 | if (cur.val == val) { 23 | prev.next = cur.next; 24 | } else { 25 | prev = prev.next; 26 | } 27 | cur = cur.next; 28 | } 29 | return dummy.next; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T212_bestTimeToBuyAndSellStock.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | public class T212_bestTimeToBuyAndSellStock { 4 | 5 | /** 6 | * 最简单的暴力算法 7 | * @param prices 价格 8 | * @return 结果 9 | */ 10 | public int maxProfit(int[] prices) { 11 | int maxResult = 0; 12 | 13 | for(int i = 0; i < prices.length-1; i++) { 14 | for(int j = i+1; j < prices.length; j++) { 15 | int profit = prices[j] - prices[i]; 16 | maxResult = Math.max(profit, maxResult); 17 | } 18 | } 19 | 20 | return maxResult; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T212_bestTimeToBuyAndSellStockBest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | public class T212_bestTimeToBuyAndSellStockBest { 4 | 5 | public int maxProfit(int[] prices) { 6 | int maxResult = 0; 7 | int minVal = prices[0]; 8 | for(int i = 0; i < prices.length; i++) { 9 | minVal = Math.min(minVal, prices[i]); 10 | maxResult = Math.max(prices[i] - minVal, maxResult); 11 | } 12 | 13 | return maxResult; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T212_bestTimeToBuyAndSellStock_II.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | public class T212_bestTimeToBuyAndSellStock_II { 4 | 5 | /** 6 | * 最简单的暴力算法 7 | * @param prices 价格 8 | * @return 结果 9 | */ 10 | public int maxProfit(int[] prices) { 11 | int maxResult = 0; 12 | int minVal = prices[0]; 13 | int maxVal = prices[0]; 14 | for(int i = 1; i < prices.length; i++) { 15 | int cur = prices[i]; 16 | // 值大于当前值 17 | if(cur > maxVal) { 18 | maxResult = Math.max(maxResult, cur - minVal); 19 | } 20 | // 重置 21 | if(cur < minVal) { 22 | minVal = cur; 23 | maxVal = cur; 24 | } 25 | } 26 | 27 | return maxResult; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T213_bestTimeToBuyAndSellStock_III.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | public class T213_bestTimeToBuyAndSellStock_III { 4 | 5 | /** 6 | * 最简单的暴力算法 7 | * @param prices 价格 8 | * @return 结果 9 | */ 10 | public int maxProfit(int[] prices) { 11 | int maxResult = 0; 12 | int minVal = prices[0]; 13 | int maxVal = prices[0]; 14 | for(int i = 1; i < prices.length; i++) { 15 | int cur = prices[i]; 16 | // 值大于当前值 17 | if(cur > maxVal) { 18 | maxResult = Math.max(maxResult, cur - minVal); 19 | } 20 | // 重置 21 | if(cur < minVal) { 22 | minVal = cur; 23 | maxVal = cur; 24 | } 25 | } 26 | 27 | return maxResult; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T217_ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * 是否存在重复元素 8 | * 9 | * @since 20231117 10 | */ 11 | public class T217_ContainsDuplicate { 12 | 13 | public boolean containsDuplicate(int[] nums) { 14 | final Set hashSet = new HashSet<>(); 15 | 16 | for(int num : nums) { 17 | if(hashSet.contains(num)) { 18 | return true; 19 | } 20 | hashSet.add(num); 21 | } 22 | 23 | return false; 24 | } 25 | 26 | public static void main(String[] args) { 27 | T217_ContainsDuplicate containsDuplicate = new T217_ContainsDuplicate(); 28 | boolean result = containsDuplicate.containsDuplicate(new int[]{1,2,3,1}); 29 | System.out.println(result); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F200T300/T217_ContainsDuplicateV2Bit.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F200T300; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * 是否存在重复元素 8 | * 9 | * @since 20231117 10 | */ 11 | public class T217_ContainsDuplicateV2Bit { 12 | 13 | public boolean containsDuplicate(int[] nums) { 14 | int checker = 0; 15 | for (int num : nums) { 16 | int mask = 1 << num; 17 | if ((checker & mask) != 0) { 18 | return true; // 数字重复出现 19 | } 20 | checker |= mask; 21 | } 22 | return false; // 数字没有重复出现 23 | } 24 | 25 | public static void main(String[] args) { 26 | T217_ContainsDuplicateV2Bit containsDuplicate = new T217_ContainsDuplicateV2Bit(); 27 | boolean result = containsDuplicate.containsDuplicate(new int[]{1,2,3,1}); 28 | System.out.println(result); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F300T400/T302_PrefixSum_RangeSumQueryImmutable_V1.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F300T400; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T302_PrefixSum_RangeSumQueryImmutable_V1 { 8 | 9 | 10 | private int[] sum; 11 | 12 | private int[] nums; 13 | public T302_PrefixSum_RangeSumQueryImmutable_V1(int[] nums) { 14 | sum = new int[nums.length]; 15 | 16 | // 初始化 17 | sum[0] = nums[0]; 18 | for(int i = 1; i < nums.length; i++) { 19 | sum[i] = sum[i-1] + nums[i]; 20 | } 21 | 22 | this.nums = nums; 23 | } 24 | 25 | public int sumRange(int left, int right) { 26 | return sum[right] - sum[left] + nums[left]; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F300T400/T309_bestTimeToBuyAndSellStockWithCooldownV1.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F300T400; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T309_bestTimeToBuyAndSellStockWithCooldownV1 { 8 | 9 | 10 | public int maxProfit(int[] prices) { 11 | int n = prices.length; 12 | int[] dp = new int[n]; 13 | 14 | for (int i = 1; i < n; i ++) { 15 | if (prices[i] <= prices[i-1]) { 16 | dp[i] = dp[i-1]; 17 | continue; 18 | } 19 | if (i < 2) { 20 | dp[i] = prices[1] - prices[0]; 21 | } else { 22 | dp[i] = Math.max(dp[i-1], dp[i-2] + prices[i] - prices[i-1]); 23 | } 24 | } 25 | 26 | return dp[n-1]; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F300T400/T377_CombinationSumIVV4.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F300T400; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class T377_CombinationSumIVV4 { 8 | 9 | /** 10 | * dp 11 | * 12 | * 82% 13 | * @param nums 14 | * @param target 15 | * @return 16 | */ 17 | public int combinationSum4(int[] nums, int target) { 18 | int[] dp = new int[target+1]; 19 | dp[0] = 1; 20 | 21 | for(int i = 1; i <= target; i++) { 22 | // 处理逻辑 23 | for(int num : nums) { 24 | if(num <= i) { 25 | dp[i] += dp[i-num]; 26 | } 27 | } 28 | } 29 | 30 | return dp[target]; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F400T500/PathSumIIIRecursive.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F400T500; 2 | 3 | import com.github.houbb.leetcode.component.TreeNode; 4 | 5 | /** 6 | * 递归 7 | * 8 | * @author binbin.hou 9 | * @since 1.0.0 10 | */ 11 | public class PathSumIIIRecursive { 12 | 13 | public int pathSum(TreeNode root, int sum) { 14 | if(root == null){ 15 | return 0; 16 | } 17 | int result = countPath(root,sum); 18 | int a = pathSum(root.left,sum); 19 | int b = pathSum(root.right,sum); 20 | return result+a+b; 21 | } 22 | 23 | public int countPath(TreeNode root,int sum){ 24 | if(root == null){ 25 | return 0; 26 | } 27 | sum = sum - root.val; 28 | int result = sum == 0 ? 1:0; 29 | return result + countPath(root.left,sum) + countPath(root.right,sum); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F700T800/F713_bestTimeToBuyAndSellStockWithTransactionFee_DP.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F700T800; 2 | 3 | public class F713_bestTimeToBuyAndSellStockWithTransactionFee_DP { 4 | 5 | 6 | public int maxProfit(int[] prices, int fee) { 7 | int buy[] = new int[prices.length]; 8 | int sell[] = new int[prices.length]; 9 | 10 | buy[0] = -prices[0]; 11 | 12 | // 遍历 13 | for(int i = 1; i < prices.length; i++) { 14 | //卖出 不卖出? 卖出 = buy[i-1] + prices[i] - FEE 15 | sell[i] = Math.max(sell[i-1], buy[i-1] + prices[i] - fee); 16 | 17 | // 卖出 不卖出? 卖出 = sell[i-1] - prices[i] 18 | buy[i] = Math.max(buy[i-1], sell[i-1] - prices[i]); 19 | } 20 | 21 | return sell[prices.length-1]; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/F700T800/T713_BestTimeToBuyAndSellStockWithTxFee_V1.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.F700T800; 2 | 3 | public class T713_BestTimeToBuyAndSellStockWithTxFee_V1 { 4 | 5 | public int maxProfit(int[] prices, int fee) { 6 | int maxProfit = 0; 7 | 8 | for(int i = 1; i < prices.length; i++) { 9 | int profit = prices[i] - prices[i-1] - fee; 10 | if(profit > 0) { 11 | maxProfit += profit; 12 | } 13 | } 14 | 15 | return maxProfit; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/PalindromeNumber.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class PalindromeNumber { 8 | 9 | /** 10 | * Runtime: 7 ms, faster than 76.19% of Java online submissions for Palindrome Number. 11 | * Memory Usage: 39.3 MB, less than 22.45% of Java online submissions for Palindrome Number. 12 | * 13 | * @param x 数字 14 | * @return 结果 15 | * @since v1 16 | */ 17 | public boolean isPalindrome(int x) { 18 | if(x < 0) { 19 | return false; 20 | } 21 | // 反转 22 | int reverse = reverse(x); 23 | return x == reverse; 24 | } 25 | 26 | private int reverse(int x) { 27 | int result = 0; 28 | 29 | while (x != 0) { 30 | // 移除最后一位 31 | int pop = x % 10; 32 | x = x / 10; 33 | 34 | // 返回值 35 | result = result * 10 + pop; 36 | } 37 | return result; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/RemoveDuplicatesFromSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class RemoveDuplicatesFromSortedArray { 8 | 9 | /** 10 | * 思路 11 | * 12 | * 1. 返回的结果要对 13 | * 2. 返回的长度对应的数组要唯一。 14 | * 15 | * 【效果】 16 | * Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Duplicates from Sorted Array. 17 | * Memory Usage: 41.3 MB, less than 59.78% of Java online submissions for Remove Duplicates from Sorted Array. 18 | * 19 | * @param nums 数组 20 | * @return 结果 21 | * @since v1 22 | */ 23 | public int removeDuplicates(int[] nums) { 24 | int i = 0; 25 | for (int n : nums) { 26 | // 将后面不同的元素复制过来 27 | if (i == 0 || n > nums[i-1]) { 28 | nums[i++] = n; 29 | } 30 | } 31 | return i; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/RemoveDuplicatesFromSortedArrayII.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class RemoveDuplicatesFromSortedArrayII { 8 | 9 | /** 10 | * 思路 11 | * 12 | * 1. 返回的结果要对 13 | * 2. 返回的长度对应的数组要唯一。 14 | * 15 | * @param nums 数组 16 | * @return 结果 17 | * @since v1 18 | */ 19 | public int removeDuplicates(int[] nums) { 20 | int i = 0; 21 | for (int n : nums) { 22 | // 前兩個元素不用考慮 23 | // 大于上一个,或者等于上一个且大于上上个 24 | if ((i == 0 || i == 1) || (n > nums[i-1] || (n > nums[i-2] && n == nums[i-1]))) { 25 | nums[i++] = n; 26 | } 27 | } 28 | return i; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/ReverseLinkedListBetter.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class ReverseLinkedListBetter { 8 | 9 | /** 10 | * 列表反转的思路 11 | * 12 | * 1->2->3 13 | * 1<-2<-3 14 | * 15 | * Runtime: 0 ms, faster than 100.00% of Java online submissions for Reverse Linked List. 16 | * Memory Usage: 38.5 MB, less than 93.17% of Java online submissions for Reverse Linked List. 17 | * 18 | * @param head 头结点 19 | * @return 结果 20 | */ 21 | public ListNode reverseList(ListNode head) { 22 | ListNode newHead = null; 23 | 24 | while (head != null) { 25 | ListNode next = head.next; 26 | // 当前节点指向上一个节点 27 | head.next = newHead; 28 | // 上一个节点更新为当前节点 29 | newHead = head; 30 | // 当前节点继续向后 31 | head = next; 32 | } 33 | 34 | return newHead; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/component/Node.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.component; 2 | 3 | public class Node { 4 | 5 | public int val; 6 | public Node left; 7 | public Node right; 8 | public Node next; 9 | 10 | public Node() {} 11 | 12 | public Node(int _val) { 13 | val = _val; 14 | } 15 | 16 | public Node(int _val, Node _left, Node _right, Node _next) { 17 | val = _val; 18 | left = _left; 19 | right = _right; 20 | next = _next; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/component/TreeNode.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.component; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class TreeNode { 8 | 9 | public int val; 10 | public TreeNode left; 11 | public TreeNode right; 12 | public TreeNode() {} 13 | public TreeNode(int val) { this.val = val; } 14 | public TreeNode(int val, TreeNode left, TreeNode right) { 15 | this.val = val; 16 | this.left = left; 17 | this.right = right; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "("+val + ": " + left + "," + right+")"; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T001_twoSum_V1_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T001_twoSum_V1_BF { 4 | 5 | 6 | public int[] twoSum(int[] nums, int target) { 7 | int[] res = new int[2]; 8 | 9 | final int n = nums.length; 10 | for(int i = 0; i < n; i++) { 11 | for(int j = i+1; j < n; j++) { 12 | if(nums[i] + nums[j] == target) { 13 | res[0] = i; 14 | res[1] = j; 15 | } 16 | } 17 | } 18 | 19 | return res; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T001_twoSum_V2_SortBinarySearch_ERR.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.Arrays; 4 | 5 | @Deprecated 6 | public class T001_twoSum_V2_SortBinarySearch_ERR { 7 | 8 | 9 | /** 10 | * 错误的代码 11 | * @param nums 12 | * @param target 13 | * @return 14 | */ 15 | public int[] twoSum(int[] nums, int target) { 16 | int[] res = new int[2]; 17 | 18 | Arrays.sort(nums); 19 | 20 | // 遍历+二分 21 | int n = nums.length; 22 | for(int i = 0; i < n; i++) { 23 | // 找另一部分 24 | int t = target - nums[i]; 25 | 26 | // 找到了自己怎么办? 27 | int j = Arrays.binarySearch(nums, t); 28 | if(j > 0) { 29 | res[0] = i; 30 | res[1] = j; 31 | return res; 32 | } 33 | } 34 | 35 | return res; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T001_twoSum_V3_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T001_twoSum_V3_HashMap { 6 | 7 | 8 | /** 9 | * @param nums 10 | * @param target 11 | * @return 12 | */ 13 | public int[] twoSum(int[] nums, int target) { 14 | int n = nums.length; 15 | 16 | HashMap hashMap = new HashMap<>(); 17 | for(int i = 0; i < n; i++) { 18 | int other = target - nums[i]; 19 | if(hashMap.containsKey(other)) { 20 | int j = hashMap.get(other); 21 | return new int[]{i, j}; 22 | } 23 | // 存储 24 | hashMap.put(nums[i], i); 25 | } 26 | 27 | return new int[]{-1, -1}; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T015_threeSum_V1_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | public class T015_threeSum_V1_BF { 7 | 8 | 9 | /** 10 | * @param nums 11 | * @param target 12 | * @return 13 | */ 14 | public List> threeSum(int[] nums) { 15 | Set> res = new HashSet<>(); 16 | 17 | final int n = nums.length; 18 | for(int i = 0; i < n; i++){ 19 | for(int j = i+1; j < n; j++) { 20 | for(int k = j+1; k < n; k++) { 21 | if(nums[i]+nums[j]+nums[k] == 0) { 22 | List list = Arrays.asList(nums[i], nums[j], nums[k]); 23 | Collections.sort(list); 24 | res.add(list); 25 | } 26 | } 27 | } 28 | } 29 | 30 | return new ArrayList<>(res); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T016_threeSumClosest_V0_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.Arrays; 4 | 5 | public class T016_threeSumClosest_V0_BF { 6 | 7 | 8 | /** 9 | * @param nums 10 | * @return 11 | */ 12 | public int threeSumClosest(int[] nums, int target) { 13 | // 处理双指针 14 | final int n = nums.length; 15 | int result = Integer.MAX_VALUE; 16 | int minDis = Integer.MAX_VALUE; 17 | for(int i = 0; i < n; i++) { 18 | for(int j = i+1; j < n; j++) { 19 | for(int k = j+1; k < n; k++) { 20 | int sum = nums[i]+nums[j]+nums[k]; 21 | int dis = Math.abs(sum - target); 22 | if(minDis > dis) { 23 | minDis = dis; 24 | result = sum; 25 | } 26 | } 27 | } 28 | } 29 | return result; 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T018_fourSum_V1_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T018_fourSum_V1_BF { 6 | 7 | 8 | public List> fourSum(int[] nums, int target) { 9 | Set> res = new HashSet<>(); 10 | 11 | final int n = nums.length; 12 | for(int i = 0; i < n; i++) { 13 | for(int j = i+1; j < n; j++) { 14 | for(int k = j+1; k < n; k++) { 15 | for(int l = k+1; l < n; l++) { 16 | if(nums[i] + nums[j] + nums[k] + nums[l] == target) { 17 | List list = Arrays.asList(nums[i], nums[j], nums[k], nums[l]); 18 | Collections.sort(list); 19 | res.add(list); 20 | } 21 | } 22 | } 23 | } 24 | } 25 | 26 | return new ArrayList<>(res); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T128_longestConsecutiveSequence_V1.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.Arrays; 4 | 5 | public class T128_longestConsecutiveSequence_V1 { 6 | 7 | /** 8 | * 是否连续? 9 | * @param nums 数组 10 | * @return 结果 11 | */ 12 | public int longestConsecutive(int[] nums) { 13 | if(nums.length == 0) { 14 | return 0; 15 | } 16 | 17 | // 排序 18 | Arrays.sort(nums); 19 | 20 | int maxLen = 1; 21 | int tempLen = 1; 22 | // 对于连续的定义是什么? 23 | for(int i = 1; i < nums.length; i++) { 24 | int num = nums[i]; 25 | int pre = nums[i-1]; 26 | if(num - pre == 1) { 27 | tempLen++; 28 | } else { 29 | // 断开 30 | tempLen = 1; 31 | } 32 | 33 | maxLen = Math.max(maxLen, tempLen); 34 | } 35 | return maxLen; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T136_SingleNumber_V1_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class T136_SingleNumber_V1_HashMap { 7 | 8 | public int singleNumber(int[] nums) { 9 | // 需要存储对应的历史数据 10 | // 如果不是位运算这种技巧 那么使用 HashMap 最方便 11 | 12 | Map countMap = new HashMap<>(); 13 | for(int num : nums){ 14 | countMap.put(num, countMap.getOrDefault(num, 0) +1); 15 | } 16 | 17 | // 找到 values 为1 的数据 18 | for(Map.Entry entry : countMap.entrySet()) { 19 | int count = entry.getValue(); 20 | if(count == 1) { 21 | return entry.getKey(); 22 | } 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T136_SingleNumber_V2_HashSet.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class T136_SingleNumber_V2_HashSet { 7 | 8 | public int singleNumber(int[] nums) { 9 | // 需要存储对应的历史数据 10 | // 如果不是位运算这种技巧 那么使用 HashMap 最方便 11 | 12 | Set set = new HashSet<>(); 13 | for(int num : nums){ 14 | if(set.contains(num)) { 15 | set.remove(num); 16 | }else { 17 | set.add(num); 18 | } 19 | } 20 | 21 | for(Integer num : set) { 22 | return num; 23 | } 24 | return 0; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T136_SingleNumber_V3_BitOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class T136_SingleNumber_V3_BitOperator { 7 | 8 | /** 9 | * 异或的性质是:相同的数字异或结果为 0,0 和任何数字异或结果为该数字本身。 10 | * 所以异或所有元素,最后的结果就是那个只出现一次的数字。 11 | */ 12 | public int singleNumber(int[] nums) { 13 | int result = 0; 14 | 15 | for(int num : nums){ 16 | result ^= num; 17 | } 18 | 19 | return result; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T137_SingleNumberII_V1_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class T137_SingleNumberII_V1_HashMap { 7 | 8 | public int singleNumber(int[] nums) { 9 | // 需要存储对应的历史数据 10 | // 如果不是位运算这种技巧 那么使用 HashMap 最方便 11 | 12 | Map countMap = new HashMap<>(); 13 | for(int num : nums){ 14 | countMap.put(num, countMap.getOrDefault(num, 0) +1); 15 | } 16 | 17 | // 找到 values 为1 的数据 18 | for(Map.Entry entry : countMap.entrySet()) { 19 | int count = entry.getValue(); 20 | if(count == 1) { 21 | return entry.getKey(); 22 | } 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T137_SingleNumberII_V2_Sort.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class T137_SingleNumberII_V2_Sort { 8 | 9 | public int singleNumber(int[] nums) { 10 | Arrays.sort(nums); // 对数组进行排序 11 | for (int i = 0; i < nums.length; i += 3) { 12 | // 如果当前数字与下一个和下下个不同,说明该数字出现次数为 1 13 | if (i + 1 >= nums.length || nums[i] != nums[i + 1]) { 14 | return nums[i]; 15 | } 16 | } 17 | return -1; // 不会执行到,防止编译器报错 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T137_SingleNumberII_V3_BitOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T137_SingleNumberII_V3_BitOperator { 4 | 5 | public int singleNumber(int[] nums) { 6 | int ones = 0, twos = 0; 7 | 8 | for (int num : nums) { 9 | // 更新 twos 和 ones 10 | ones = (ones ^ num) & ~twos; 11 | twos = (twos ^ num) & ~ones; 12 | } 13 | 14 | return ones; // ones 中的数字就是只出现一次的数字 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T167_twoSum_V1_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T167_twoSum_V1_BF { 4 | 5 | public int[] twoSum(int[] nums, int target) { 6 | int[] res = new int[2]; 7 | 8 | final int n = nums.length; 9 | for(int i = 0; i < n; i++) { 10 | for(int j = i+1; j < n; j++) { 11 | if(nums[i] + nums[j] == target) { 12 | res[0] = i+1; 13 | res[1] = j+1; 14 | } 15 | } 16 | } 17 | 18 | return res; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T167_twoSum_V3_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashMap; 4 | 5 | public class T167_twoSum_V3_HashMap { 6 | 7 | public int[] twoSum(int[] nums, int target) { 8 | int n = nums.length; 9 | HashMap hashMap = new HashMap<>(); 10 | for(int i = 0; i < n; i++) { 11 | int other = target - nums[i]; 12 | if(hashMap.containsKey(other)) { 13 | int j = hashMap.get(other); 14 | return new int[]{j+1, i+1}; 15 | } 16 | // 存储 17 | hashMap.put(nums[i], i); 18 | } 19 | return new int[]{-1, -1}; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T167_twoSum_V4_TwoPointer.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T167_twoSum_V4_TwoPointer { 4 | 5 | public int[] twoSum(int[] nums, int target) { 6 | int n = nums.length; 7 | int left = 0; 8 | int right = n-1; 9 | 10 | while (left < right) { 11 | int sum = nums[left] + nums[right]; 12 | if(sum == target) { 13 | return new int[]{left+1, right+1}; 14 | } 15 | if(sum < target) { 16 | left++; 17 | } 18 | if(sum > target) { 19 | right--; 20 | } 21 | } 22 | 23 | return new int[]{-1, -1}; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T217_ContainsDup_V1_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class T217_ContainsDup_V1_HashMap { 7 | 8 | public boolean containsDuplicate(int[] nums) { 9 | Map countMap = new HashMap<>(); 10 | for(int num : nums){ 11 | countMap.put(num, countMap.getOrDefault(num, 0) +1); 12 | } 13 | 14 | for(Map.Entry entry : countMap.entrySet()) { 15 | if(entry.getValue() != 1) { 16 | return false; 17 | } 18 | } 19 | 20 | return true; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T217_ContainsDup_V2_Set.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class T217_ContainsDup_V2_Set { 7 | 8 | public boolean containsDuplicate(int[] nums) { 9 | Set set = new HashSet<>(); 10 | for(int num : nums){ 11 | set.add(num); 12 | } 13 | return set.size() < nums.length; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T217_ContainsDup_V2_Set2.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class T217_ContainsDup_V2_Set2 { 7 | 8 | public boolean containsDuplicate(int[] nums) { 9 | if (nums==null || nums.length<2) { 10 | return false; 11 | } 12 | 13 | Set set = new HashSet<>(); 14 | for(int num : nums){ 15 | if(set.contains(num)) { 16 | return true; 17 | } 18 | set.add(num); 19 | } 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T217_ContainsDup_V3_Sort.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | public class T217_ContainsDup_V3_Sort { 8 | 9 | public boolean containsDuplicate(int[] nums) { 10 | Arrays.sort(nums); 11 | 12 | for(int i = 0; i < nums.length-1; i++){ 13 | if(nums[i] == nums[i+1]) { 14 | return true; 15 | } 16 | } 17 | 18 | return false; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T219_ContainsDup_V2_SlideWindow.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T219_ContainsDup_V2_SlideWindow { 6 | 7 | public boolean containsNearbyDuplicate(int[] nums, int k) { 8 | Set set = new HashSet<>(); 9 | for(int i = 0; i < nums.length; i++){ 10 | 11 | // 固定窗口大小 12 | if(i > k) { 13 | // 移除最开始的元素 保证窗口最多k 14 | set.remove(nums[i - k - 1]); 15 | } 16 | 17 | if(set.contains(nums[i])) { 18 | return true; 19 | } 20 | set.add(nums[i]); 21 | } 22 | 23 | return false; 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T259_threeSumSmaller_V1_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T259_threeSumSmaller_V1_BF { 4 | 5 | 6 | /** 7 | * @param nums 8 | * @return 9 | */ 10 | public int threeSumSmaller(int[] nums, int target) { 11 | // 处理双指针 12 | final int n = nums.length; 13 | int count = 0; 14 | for(int i = 0; i < n; i++) { 15 | for(int j = i+1; j < n; j++) { 16 | for(int k = j+1; k < n; k++) { 17 | int sum = nums[i]+nums[j]+nums[k]; 18 | if(sum > target) { 19 | count++; 20 | } 21 | } 22 | } 23 | } 24 | return count; 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T260_SingleNumberIII_V1_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class T260_SingleNumberIII_V1_HashMap { 7 | 8 | public int[] singleNumber(int[] nums) { 9 | // 需要存储对应的历史数据 10 | // 如果不是位运算这种技巧 那么使用 HashMap 最方便 11 | 12 | int[] result = new int[2]; 13 | int count = 0; 14 | 15 | Map countMap = new HashMap<>(); 16 | for(int num : nums){ 17 | countMap.put(num, countMap.getOrDefault(num, 0) +1); 18 | } 19 | 20 | // 找到 values 为1 的数据 21 | for(Map.Entry entry : countMap.entrySet()) { 22 | if(entry.getValue() == 1) { 23 | result[count++] = entry.getKey(); 24 | } 25 | } 26 | 27 | return result; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T287_findDuplicate_V1_HashSet.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T287_findDuplicate_V1_HashSet { 6 | 7 | public int findDuplicate(int[] nums) { 8 | Set set = new HashSet<>(); 9 | for(int i = 0; i < nums.length; i++){ 10 | int num = nums[i]; 11 | if(set.contains(num)) { 12 | return num; 13 | } 14 | 15 | set.add(num); 16 | } 17 | 18 | return -1; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T287_findDuplicate_V2_Sort.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.Arrays; 4 | 5 | public class T287_findDuplicate_V2_Sort { 6 | 7 | public int findDuplicate(int[] nums) { 8 | Arrays.sort(nums); 9 | 10 | for(int i = 0; i < nums.length-1; i++){ 11 | if(nums[i] == nums[i+1]) { 12 | return nums[i]; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T442_findDuplicates_V1_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T442_findDuplicates_V1_HashMap { 6 | 7 | public List findDuplicates(int[] nums) { 8 | List list = new ArrayList<>(); 9 | Map map = new HashMap<>(); 10 | for(int num : nums){ 11 | int count = map.getOrDefault(num, 0); 12 | if(count > 0) { 13 | list.add(num); 14 | } 15 | map.put(num, count+1); 16 | } 17 | return list; 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T442_findDuplicates_V2_HashSet.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T442_findDuplicates_V2_HashSet { 6 | 7 | public List findDuplicates(int[] nums) { 8 | List list = new ArrayList<>(); 9 | Set set = new HashSet<>(); 10 | for(int num : nums){ 11 | if(set.contains(num)) { 12 | list.add(num); 13 | } 14 | set.add(num); 15 | } 16 | 17 | return list; 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T442_findDuplicates_V3_Sort.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T442_findDuplicates_V3_Sort { 6 | 7 | public List findDuplicates(int[] nums) { 8 | List list = new ArrayList<>(nums.length); 9 | Arrays.sort(nums); 10 | 11 | for(int i = 0; i < nums.length-1;i++){ 12 | if(nums[i] == nums[i+1]) { 13 | list.add(nums[i]); 14 | i++; 15 | } 16 | } 17 | 18 | return list; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T454_fourSumII_V1_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T454_fourSumII_V1_BF { 6 | 7 | 8 | public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { 9 | // 暴力 10 | int count = 0; 11 | for(int i = 0; i < nums1.length; i++) { 12 | for(int j = 0; j < nums2.length; j++) { 13 | for(int k = 0; k < nums3.length; k++) { 14 | for(int l = 0; l < nums4.length; l++) { 15 | int sum = nums1[i] + nums2[j] + nums3[k] + nums4[l]; 16 | if(sum == 0) { 17 | count++; 18 | } 19 | } 20 | } 21 | } 22 | } 23 | return count; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T454_fourSumII_V3_HashMap.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | import java.util.*; 4 | 5 | public class T454_fourSumII_V3_HashMap { 6 | 7 | 8 | public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { 9 | final int n = nums1.length; 10 | Map sum12Map = new HashMap<>(); 11 | for(int i = 0; i < n; i++) { 12 | for(int j = 0; j < n; j++) { 13 | int sum = nums1[i] + nums2[j]; 14 | sum12Map.put(sum, sum12Map.getOrDefault(sum, 0)+1); 15 | } 16 | } 17 | 18 | int count = 0; 19 | for(int i = 0; i < n; i++) { 20 | for(int j = 0; j < n; j++) { 21 | int sum34 = nums3[i] + nums4[j]; 22 | 23 | if(sum12Map.containsKey(-sum34)) { 24 | count += sum12Map.get(-sum34); 25 | } 26 | } 27 | } 28 | 29 | return count; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T51_maxSumSubArray_V1_PrefixSum.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T51_maxSumSubArray_V1_PrefixSum { 4 | 5 | /** 6 | * 前缀和 7 | * @param nums 8 | * @return 9 | */ 10 | public int maxSubArray(int[] nums) { 11 | final int n = nums.length; 12 | 13 | int[] prefixSum = new int[n]; 14 | prefixSum[0] = nums[0]; 15 | for(int i = 1; i < n; i++) { 16 | prefixSum[i] = prefixSum[i-1] + nums[i]; 17 | } 18 | 19 | // BF 匹配 20 | int maxSum = nums[0]; 21 | for(int i = 0; i < n; i++) { 22 | // 后面的数组 》 前一个标识 23 | for(int j = i; j < n; j++) { 24 | int sum = prefixSum[j] - prefixSum[i] + nums[i]; 25 | 26 | // 更新最大值 27 | maxSum = Math.max(maxSum, sum); 28 | } 29 | } 30 | return maxSum; 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T51_maxSumSubArray_V1_TwoPointer.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | @Deprecated 4 | public class T51_maxSumSubArray_V1_TwoPointer { 5 | 6 | /** 7 | * 前缀和 8 | * @param nums 9 | * @return 10 | */ 11 | public int maxSubArray(int[] nums) { 12 | final int n = nums.length; 13 | 14 | // BF 匹配 15 | int maxSum = nums[0]; 16 | int left = 0; 17 | int right = n-1; 18 | 19 | while (left <= right) { 20 | 21 | } 22 | 23 | return maxSum; 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T51_maxSumSubArray_V3_Greedy.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T51_maxSumSubArray_V3_Greedy { 4 | 5 | /** 6 | * 前缀和 7 | * @param nums 8 | * @return 9 | */ 10 | public int maxSubArray(int[] nums) { 11 | final int n = nums.length; 12 | 13 | // BF 匹配 14 | int maxSum = nums[0]; 15 | int tempSum = nums[0]; 16 | for(int i = 1; i < n; i++) { 17 | int num = nums[i]; 18 | 19 | // 历史数据大于等于0,则保留继续累加 20 | if(tempSum >= 0) { 21 | tempSum += num; 22 | } else { 23 | // 历史和小于 0,直接舍弃。只保留今天 24 | tempSum = num; 25 | } 26 | 27 | maxSum = Math.max(maxSum, tempSum); 28 | } 29 | 30 | return maxSum; 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/array/T51_maxSumSubArray_V4_DP.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.array; 2 | 3 | public class T51_maxSumSubArray_V4_DP { 4 | 5 | /** 6 | * 前缀和 7 | * @param nums 8 | * @return 9 | */ 10 | public int maxSubArray(int[] nums) { 11 | final int n = nums.length; 12 | 13 | int[] dp = new int[n]; 14 | dp[0] = nums[0]; 15 | 16 | for(int i = 1; i < n; i++) { 17 | int num = nums[i]; 18 | dp[i] = Math.max(dp[i-1]+num, num); 19 | } 20 | 21 | return dp[n-1]; 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/package-info.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct; -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/sweepline/T836_rectangleOverlap_V1.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.sweepline; 2 | 3 | public class T836_rectangleOverlap_V1 { 4 | 5 | public static void main(String[] args) { 6 | T836_rectangleOverlap_V1 v1 = new T836_rectangleOverlap_V1(); 7 | System.out.println(v1.isRectangleOverlap(new int[]{7,8,13,15}, new int[]{10,8,12,20})); 8 | } 9 | 10 | public boolean isRectangleOverlap(int[] rec1, int[] rec2) { 11 | // 判断 x 轴和 y 轴投影是否有交集 12 | boolean xOverlap = rec1[2] > rec2[0] && rec1[0] < rec2[2]; 13 | boolean yOverlap = rec1[3] > rec2[1] && rec1[1] < rec2[3]; 14 | 15 | // 两个投影都有交集才算重叠 16 | return xOverlap && yOverlap; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/datastruct/sweepline/T836_rectangleOverlap_V2_overlapArea.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.datastruct.sweepline; 2 | 3 | public class T836_rectangleOverlap_V2_overlapArea { 4 | 5 | public boolean isRectangleOverlap(int[] rec1, int[] rec2) { 6 | // 计算交集矩形的左下角和右上角 7 | int x1 = Math.max(rec1[0], rec2[0]); 8 | int y1 = Math.max(rec1[1], rec2[1]); 9 | int x2 = Math.min(rec1[2], rec2[2]); 10 | int y2 = Math.min(rec1[3], rec2[3]); 11 | 12 | // 计算交集的宽度和高度 13 | int width = x2 - x1; 14 | int height = y2 - y1; 15 | 16 | // 如果交集的宽度和高度都大于 0,说明有重叠 17 | if (width > 0 && height > 0) { 18 | return true; 19 | } 20 | 21 | // 没有重叠 22 | return false; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/f500t600/FibonacciNumber.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.f500t600; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class FibonacciNumber { 8 | 9 | public int fib(int n) { 10 | if(n <= 1) { 11 | return n; 12 | } 13 | 14 | return fib(n-1) + fib(n-2); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/f500t600/FibonacciNumberBest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.f500t600; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class FibonacciNumberBest { 8 | 9 | public static int fib(int n) { 10 | int[] nums = new int[]{ 11 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040 12 | }; 13 | 14 | return nums[n]; 15 | } 16 | 17 | public static void main(String[] args) { 18 | for(int i = 0; i <= 30; i++) { 19 | System.out.print(FibonacciNumberBest.fib(i) + ", "); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/f500t600/FibonacciNumberDp.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.f500t600; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class FibonacciNumberDp { 8 | 9 | public int fib(int n) { 10 | if(n <= 1) { 11 | return n; 12 | } 13 | 14 | int[] dp = new int[n+1]; 15 | dp[0] = 0; 16 | dp[1] = 1; 17 | 18 | for(int i = 2; i <= n; i++) { 19 | dp[i] = dp[i-1] + dp[i-2]; 20 | } 21 | 22 | return dp[n]; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/f500t600/FibonacciNumberDpOptimize.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.f500t600; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class FibonacciNumberDpOptimize { 8 | 9 | public static int fib(int n) { 10 | if(n <= 1) { 11 | return n; 12 | } 13 | 14 | int pre = 0; 15 | int current = 1; 16 | 17 | for(int i = 2; i <= n; i++) { 18 | int temp = current; 19 | current = pre + current; 20 | pre = temp; 21 | } 22 | 23 | return current; 24 | } 25 | 26 | public static void main(String[] args) { 27 | System.out.println(fib(4)); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/f500t600/FibonacciNumberMath.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.f500t600; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class FibonacciNumberMath { 8 | 9 | private static final double FAC_ONE = 1 / Math.sqrt(5); 10 | private static final double FAC_TWO = 0.5 + Math.sqrt(5) / 2; 11 | private static final double FAC_THREE = 0.5 - Math.sqrt(5) / 2; 12 | 13 | public int fib(int n) { 14 | if(n <= 1) { 15 | return n; 16 | } 17 | 18 | return (int) (FAC_ONE * (Math.pow(FAC_TWO, n) - Math.pow(FAC_THREE, n))); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/f500t600/T523_checkSubarraySum_V1_BF.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.f500t600; 2 | 3 | public class T523_checkSubarraySum_V1_BF { 4 | 5 | 6 | /** 7 | * 思路: 8 | * 1. 构建前缀和数组 9 | * 2. 穷举,找到符合结果的数据。 10 | * 11 | * @param nums 12 | * @param k 13 | * @return 14 | */ 15 | public boolean checkSubarraySum(int[] nums, int k) { 16 | final int n = nums.length; 17 | 18 | // 前缀和 19 | int[] prefix = new int[n]; 20 | prefix[0] = nums[0]; 21 | for(int i = 1; i < n; i++) { 22 | prefix[i] = prefix[i-1] + nums[i]; 23 | } 24 | 25 | // 穷举 26 | for(int step = 2; step < n-2; step++) { 27 | for(int i = 0; i < n-2; i++) { 28 | int sum = prefix[i+step] - prefix[i] + nums[i]; 29 | if(sum % k == 0) { 30 | return true; 31 | } 32 | } 33 | } 34 | 35 | 36 | return false; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/f500t600/T525_findMaxLength_V1_prefixSum.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.f500t600; 2 | 3 | public class T525_findMaxLength_V1_prefixSum { 4 | 5 | public int findMaxLength(int[] nums) { 6 | final int n = nums.length; 7 | int[] prefix = new int[n]; 8 | prefix[0] = nums[0]; 9 | for(int i = 1; i < n; i++) { 10 | prefix[i] = prefix[i+1] + nums[i]; 11 | } 12 | 13 | // 从大大小遍历? 14 | for(int step = n; step >=1; step-=2) { 15 | for(int i = 0; i < n - step; i++) { 16 | int next = i + step; 17 | int sum = prefix[next] - prefix[i] + nums[i]; 18 | if(sum == step / 2) { 19 | return step; 20 | } 21 | } 22 | } 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/medium/F100T200/FlattenBinaryTreeToLinkedListBetter.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.medium.F100T200; 2 | 3 | import com.github.houbb.leetcode.component.TreeNode; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class FlattenBinaryTreeToLinkedListBetter { 9 | 10 | 11 | public void flatten(TreeNode root) { 12 | TreeNode curr = root; 13 | while (curr != null) { 14 | // 左子树不为空 15 | if (curr.left != null) { 16 | // 左子树 17 | TreeNode next = curr.left; 18 | // 前继结点 19 | TreeNode predecessor = next; 20 | 21 | while (predecessor.right != null) { 22 | predecessor = predecessor.right; 23 | } 24 | predecessor.right = curr.right; 25 | curr.left = null; 26 | curr.right = next; 27 | } 28 | curr = curr.right; 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/medium/UniqueBinarySearchTrees.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.medium; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class UniqueBinarySearchTrees { 8 | 9 | public int numTrees(int n) { 10 | int[] dp = new int[n+1]; 11 | 12 | // 初始化 13 | dp[0] = 1; 14 | dp[1] = 1; 15 | 16 | // 遍历 17 | for(int i = 2; i <= n; i++) { 18 | int sum = 0; 19 | for(int j = 0; j < i; j++) { 20 | // 左边 * 右边 21 | sum += dp[j] * dp[i-1-j]; 22 | } 23 | 24 | dp[i] =sum ; 25 | } 26 | return dp[n]; 27 | } 28 | 29 | public static void main(String[] args) { 30 | UniqueBinarySearchTrees trees = new UniqueBinarySearchTrees(); 31 | 32 | System.out.println(trees.numTrees(3)); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/medium/UniqueBinarySearchTreesBest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.medium; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class UniqueBinarySearchTreesBest { 8 | 9 | public int numTrees(int n) { 10 | // 提示:我们在这里需要用 long 类型防止计算过程中的溢出 11 | long c = 1; 12 | 13 | // 当然,针对 2 的乘法,还可以使用位运算进行优化。 14 | for (int i = 0; i < n; ++i) { 15 | c = c * 2 * (2 * i + 1) / (i + 2); 16 | } 17 | 18 | return (int) c; 19 | } 20 | 21 | public static void main(String[] args) { 22 | UniqueBinarySearchTreesBest trees = new UniqueBinarySearchTreesBest(); 23 | 24 | System.out.println(trees.numTrees(3)); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/medium/UniquePaths.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.medium; 2 | 3 | /** 4 | * @author binbin.hou 5 | * @since 1.0.0 6 | */ 7 | public class UniquePaths { 8 | 9 | public int uniquePaths(int m, int n) { 10 | int[][] dp = new int[m][n]; 11 | 12 | for(int i = 0; i < m; i++) { 13 | for(int j = 0; j < n; j++) { 14 | // 这种其实是不符合条件的,不过我们设定初始值 dp[0][0] = 1 15 | if(i == 0 || j == 0) { 16 | dp[i][j] = 1; 17 | } else { 18 | dp[i][j] = dp[i-1][j] + dp[i][j-1]; 19 | } 20 | } 21 | } 22 | 23 | // 返回所有可能的结果 24 | return dp[m-1][n-1]; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/package-info.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; -------------------------------------------------------------------------------- /src/main/java/com/github/houbb/leetcode/topics/package-info.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode.topics; -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/AddTwoNumbersLeetCodeV3Test.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Test; 4 | 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class AddTwoNumbersLeetCodeV3Test { 11 | 12 | // [2,4,3] 13 | // [5.6.4] 14 | @Test 15 | public void runTest() { 16 | ListNode numOneNext = new ListNode(4, new ListNode(3)); 17 | ListNode numOneNode = new ListNode(2, numOneNext); 18 | 19 | ListNode numTwoNext = new ListNode(6, new ListNode(4)); 20 | ListNode numTwoNode = new ListNode(5, numTwoNext); 21 | 22 | final AddTwoNumbersLeetCodeVersion3 version2 = new AddTwoNumbersLeetCodeVersion3(); 23 | ListNode resultNode = version2.addTwoNumbers(numOneNode, numTwoNode); 24 | System.out.println(resultNode.getIntegerList()); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/AddTwoNumbersTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Test; 4 | import com.github.houbb.leetcode.AddTwoNumbers.ListNode; 5 | /** 6 | * @author binbin.hou 7 | * @since 1.0.0 8 | */ 9 | public class AddTwoNumbersTest { 10 | 11 | @Test 12 | public void runTest() { 13 | ListNode numOneNext = new ListNode(4, new ListNode(3)); 14 | ListNode numOneNode = new ListNode(2, numOneNext); 15 | 16 | ListNode numTwoNext = new ListNode(6, new ListNode(4)); 17 | ListNode numTwoNode = new ListNode(5, numTwoNext); 18 | 19 | final AddTwoNumbers addTwoNumbers = new AddTwoNumbers(); 20 | ListNode resultNode = addTwoNumbers.addTwoNumbers(numOneNode, numTwoNode); 21 | System.out.println(addTwoNumbers.getIntegerList(resultNode)); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/IntegerToRomanTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class IntegerToRomanTest { 11 | 12 | @Test 13 | public void test() { 14 | IntegerToRoman roman = new IntegerToRoman(); 15 | 16 | // Assert.assertEquals("III", roman.intToRoman(3)); 17 | // Assert.assertEquals("IV", roman.intToRoman(4)); 18 | // Assert.assertEquals("IX", roman.intToRoman(9)); 19 | Assert.assertEquals("LVIII", roman.intToRoman(58)); 20 | Assert.assertEquals("MCMXCIV", roman.intToRoman(1994)); 21 | } 22 | 23 | @Test 24 | public void allCacheTest() { 25 | IntegerToRoman roman = new IntegerToRoman(); 26 | 27 | String template = "CACHE.put(%d, \"%s\");"; 28 | for(int i = 1; i <= 3999; i++) { 29 | System.out.println(String.format(template, i, roman.intToRoman(i))); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/LongestCommonPrefixTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | *

project: leetcode-LongestCommonPrefixTest

8 | *

create on 2020/6/11 22:32

9 | * 10 | * @author binbin.hou 11 | * @since v1 12 | */ 13 | public class LongestCommonPrefixTest { 14 | 15 | @Test 16 | public void test() { 17 | LongestCommonPrefix prefix = new LongestCommonPrefix(); 18 | 19 | Assert.assertEquals("fl", prefix.longestCommonPrefix(new String[]{"flower","flow","flight"})); 20 | Assert.assertEquals("", prefix.longestCommonPrefix(new String[]{"dog","racecar","car"})); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/MedianOfTwoSortedArraysTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class MedianOfTwoSortedArraysTest { 11 | 12 | @Test 13 | public void basicTest() { 14 | MedianOfTwoSortedArrays arrays = new MedianOfTwoSortedArrays(); 15 | 16 | int[] numsOne = new int[]{1,2}; 17 | int[] numsTwo = new int[]{3,4}; 18 | 19 | System.out.println(arrays.findMedianSortedArrays(numsOne, numsTwo)); 20 | } 21 | 22 | @Test 23 | public void leetCodeTest() { 24 | MedianOfTwoSortedArrays arrays = new MedianOfTwoSortedArrays(); 25 | 26 | int[] numsOne = new int[]{1,2}; 27 | int[] numsTwo = new int[]{3,4}; 28 | 29 | System.out.println(arrays.findMedianLeetCode(numsOne, numsTwo)); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/RemoveNthNodeFromEndOfListTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | *

project: leetcode-RemoveNthNodeFromEndOfListTest

9 | *

create on 2020/6/16 22:12

10 | * 11 | * @author binbin.hou 12 | * @since v1 13 | */ 14 | public class RemoveNthNodeFromEndOfListTest { 15 | 16 | @Test 17 | public void basicTest() { 18 | RemoveNthNodeFromEndOfList list = new RemoveNthNodeFromEndOfList(); 19 | 20 | ListNode head = ListNode.buildHead(new int[]{1,2}); 21 | ListNode result = list.removeNthFromEnd(head, 2); 22 | ListNode.print(result); 23 | } 24 | 25 | @Test 26 | public void basic2Test() { 27 | RemoveNthNodeFromEndOfList list = new RemoveNthNodeFromEndOfList(); 28 | 29 | ListNode head = ListNode.buildHead(new int[]{1,2,3,4,5}); 30 | ListNode result = list.removeNthFromEnd(head, 2); 31 | ListNode.print(result); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/ReverseIntegerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class ReverseIntegerTest { 11 | 12 | @Test 13 | public void reverseBasicTest() { 14 | ReverseInteger reverseInteger = new ReverseInteger(); 15 | Assert.assertEquals(321, reverseInteger.reverseBasic(123)); 16 | } 17 | 18 | @Test 19 | public void reverseTest() { 20 | ReverseInteger reverseInteger = new ReverseInteger(); 21 | Assert.assertEquals(321, reverseInteger.reverse(123)); 22 | Assert.assertEquals(-321, reverseInteger.reverse(-123)); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/RomanToIntegerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class RomanToIntegerTest { 11 | 12 | @Test 13 | public void test() { 14 | RomanToInteger roman = new RomanToInteger(); 15 | 16 | Assert.assertEquals(3, roman.romanToInt("III")); 17 | Assert.assertEquals(4, roman.romanToInt("IV")); 18 | Assert.assertEquals(9, roman.romanToInt("IX")); 19 | Assert.assertEquals(58, roman.romanToInt("LVIII")); 20 | Assert.assertEquals(1994, roman.romanToInt("MCMXCIV")); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/SwapNodesInPairsTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * @author binbin.hou 7 | * @since 1.0.0 8 | */ 9 | public class SwapNodesInPairsTest { 10 | 11 | @Test 12 | public void basicTest() { 13 | ListNode head = ListNode.buildHead(new int[]{1,2,3,4}); 14 | 15 | ListNode result = new SwapNodesInPairs().swapPairs(head); 16 | ListNode.print(result); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/github/houbb/leetcode/ThirdSumClosestTest.java: -------------------------------------------------------------------------------- 1 | package com.github.houbb.leetcode; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author binbin.hou 8 | * @since 1.0.0 9 | */ 10 | public class ThirdSumClosestTest { 11 | 12 | @Test 13 | public void basicTest() { 14 | ThirdSumClosest closest = new ThirdSumClosest(); 15 | 16 | Assert.assertEquals(2, closest.threeSumClosest(new int[]{-1,2,1,-4}, 1)); 17 | Assert.assertEquals(3, closest.threeSumClosest(new int[]{1,1,1,1}, 0)); 18 | } 19 | 20 | @Test 21 | public void optimizeTest() { 22 | ThirdSumClosestOptimize closest = new ThirdSumClosestOptimize(); 23 | 24 | Assert.assertEquals(2, closest.threeSumClosest(new int[]{-1,2,1,-4}, 1)); 25 | Assert.assertEquals(3, closest.threeSumClosest(new int[]{1,1,1,1}, 0)); 26 | } 27 | 28 | } 29 | --------------------------------------------------------------------------------