├── .gitconfig ├── README.md ├── files.proj ├── pictures ├── 3个非重复子数组最大和.png ├── 4S分析法 - 存储.png ├── 4S分析法 - 数据结构.png ├── 4S分析法.png ├── 位运算 ├── 位运算1.png ├── 位运算2.png ├── 位运算3.png ├── 位运算4.png ├── 位运算5.png ├── 位运算6.png ├── 位运算7.png ├── 位运算8.png ├── 原子计数.png ├── 字典里面的最长单词 - 题解.png ├── 字典里面的最长单词.png ├── 找二叉树最底层最左边的节点-样例.png ├── 找二叉树最底层最左边的节点.题解.png ├── 服务.png ├── 线段树1.png ├── 线段树2.png ├── 线段树3.png ├── 线段树4.png ├── 线段树5.png ├── 线段树6.png └── 线段树7.png ├── 令狐老师的算法小抄 └── 面试常考算法模板 Cheat Sheet V4.3.pdf ├── 其他 ├── n-gram-map-reduce.md └── readme.md ├── 动态规划 ├── arithmetic-slices-ii-subsequence.md ├── backpack-ii.md ├── backpack-iii.md ├── backpack-iv.md ├── backpack-v.md ├── backpack-vi.md ├── backpack.md ├── binary-tree-maximum-path-sum.md ├── bomb-enemy.md ├── burst-ballons.md ├── burst-balloons.md ├── calculation-the-sum-of-path.md ├── can-i-win.md ├── card-game.md ├── climbing-stairs-ii.md ├── climbing-stairs.md ├── coin-change.md ├── coins-in-a-line-ii.md ├── coins-in-a-line-iii.md ├── coins-in-a-line.md ├── copy-books-ii.md ├── copy-books.md ├── copy-list-with-random-pointer.md ├── count-numbers-with-unique-digits.md ├── count-square-submatrices-with-all-ones.md ├── create-maximum-number.md ├── decode-ways-ii.md ├── decode-ways.md ├── dices-sum.md ├── distinct-subsequences-ii.md ├── distinct-subsequences.md ├── drop-eggs-ii.md ├── drop-eggs.md ├── edit-distance.md ├── frog-jump.md ├── house-robber-ii.md ├── house-robber-iii.md ├── house-robber.md ├── interleaving-string.md ├── jump-game.md ├── k-edit-distance.md ├── largest-divisible-subset.md ├── longest-common-subsequence.md ├── longest-continuous-increasing-subsequence-ii.md ├── longest-continuous-increasing-subsequence.md ├── longest-increasing-continuous-subsequence.md ├── longest-increasing-subsequence.md ├── longest-palindromic-subsequence.md ├── longest-path-on-the-tree.md ├── longest-repeating-subsequence.md ├── longest-valid-parentheses.md ├── maximal-rectangle.md ├── maximal-square-ii.md ├── maximal-square.md ├── maximum-product-subarray.md ├── maximum-subarray-difference.md ├── maximum-subarray-ii.md ├── maximum-subarray-iii.md ├── maximum-subarray-iv.md ├── maximum-subarray-v.md ├── maximum-subarray.md ├── minimum-adjustment-cost.md ├── minimum-cost-to-merge-stones.md ├── minimum-falling-path-sum.md ├── minimum-path-sum.md ├── modern-ludo-i.md ├── non-negative-integers-without-consecutive-ones.md ├── palindrome-partitioning-ii.md ├── partition-equal-subset-sum.md ├── perfect-squares.md ├── post-office-problem.md ├── profitable-schemes.md ├── range-sum-query-2d-immutable.md ├── regular-expression-matching.md ├── russian-doll-envelopes.md ├── scramble-string.md ├── stone-game-ii.md ├── stone-game.md ├── traveling-salesman-problem.md ├── unique-paths-ii.md ├── unique-paths.md ├── unique-substrings-in-wraparound-string.md ├── word-break-ii.md ├── word-break-iii.md └── word-break.md ├── 图论 ├── find-the-connected-component-in-the-undirected-graph.md ├── 宽度优先搜索 │ ├── best-time-to-buy-and-sell-stock-ii.md │ ├── best-time-to-buy-and-sell-stock-iii.md │ ├── best-time-to-buy-and-sell-stock-iv.md │ ├── best-time-to-buy-and-sell-stock.md │ ├── binary-tree-zigzag-level-order-traversal.md │ ├── build-post-office-ii.md │ ├── convert-binary-tree-to-linked-lists-by-depth.md │ ├── knight-shortest-path-ii.md │ ├── knight-shortest-path.md │ ├── maximum-subarray-difference.md │ ├── path-sum.md │ ├── remove-substrings.md │ ├── route-between-two-nodes-in-graph.md │ ├── search-graph-nodes.md │ ├── shortest-path-to-the-destination.md │ ├── shortest-path-visiting-all-nodes.md │ ├── similar-string-groups.md │ ├── six-degrees.md │ ├── sliding-puzzle-ii.md │ ├── surrounded-regions.md │ ├── the-maze-iii.md │ ├── topological-sorting.md │ ├── tree.md │ ├── walls-and-gates.md │ ├── word-ladder-ii.md │ ├── word-ladder.md │ └── zombie-in-matrix.md └── 深度优先搜索 │ ├── add-operators.md │ ├── balanced-binary-tree.md │ ├── binary-tree-longest-consecutive-sequence-ii.md │ ├── binary-tree-longest-consecutive-sequence-iii.md │ ├── binary-tree-longest-consecutive-sequence.md │ ├── binary-tree-maximum-node.md │ ├── binary-tree-maximum-path-sum-ii.md │ ├── binary-tree-maximum-path-sum.md │ ├── binary-tree-path-sum-ii.md │ ├── binary-tree-path-sum-iii.md │ ├── binary-tree-path-sum.md │ ├── clone-binary-tree.md │ ├── clone-graph.md │ ├── combination-sum-ii.md │ ├── combination-sum-iv.md │ ├── combination-sum.md │ ├── combinations.md │ ├── continuous-subarray-sum-ii.md │ ├── continuous-subarray-sum.md │ ├── convert-sorted-array-to-binary-search-tree-with-minimal-height.md │ ├── convert-sorted-array-to-binary-search-tree.md │ ├── convert-sorted-list-to-balanced-bst.md │ ├── convert-sorted-list-to-binary-search-tree.md │ ├── decode-string.md │ ├── driving-problem.md │ ├── expression-evaluation.md │ ├── factorization.md │ ├── find-the-missing-number-ii.md │ ├── flatten-2d-vector.md │ ├── flatten-binary-tree-to-linked-list.md │ ├── flatten-list.md │ ├── flatten-nested-iterate.md │ ├── flatten-nested-list-iterator.md │ ├── flip-game-ii.md │ ├── increasing-subsequences.md │ ├── k-edit-distance.md │ ├── k-sum-ii.md │ ├── k-sum.md │ ├── keys-and-rooms.md │ ├── letter-combinations-of-a-phone-number-ii.md │ ├── letter-combinations-of-a-phone-number.md │ ├── longest-consecutive-sequence.md │ ├── maximum-depth-of-binary-tree.md │ ├── maximum-subarray-difference.md │ ├── maximum-subarray-ii.md │ ├── maximum-subarray-iii.md │ ├── maximum-subarray-iv.md │ ├── maximum-subarray-v.md │ ├── maximum-subarray.md │ ├── maximum-submatrix.md │ ├── maximum-subtree.md │ ├── minimum-depth-of-binary-tree.md │ ├── minimum-subarray.md │ ├── minimum-subtree.md │ ├── n-queens-ii.md │ ├── n-queens.md │ ├── nested-list-weight-sum.md │ ├── next-permutation-ii.md │ ├── next-permutation.md │ ├── number-of-islands.md │ ├── palindrome-partitioning.md │ ├── pascals-triangle.md │ ├── path-sum-ii.md │ ├── permutation-sequence.md │ ├── permutations-ii.md │ ├── permutations.md │ ├── populating-next-right-pointers-in-each-node-ii.md │ ├── prime-factorization.md │ ├── reconstruct-itinerary.md │ ├── recover-binary-search-tree.md │ ├── remove-node-in-binary-search-tree.md │ ├── string-permutation-ii.md │ ├── subsets-ii.md │ ├── subsets.md │ ├── subtree.md │ ├── sudoku-solver.md │ ├── sum-of-left-leaves.md │ ├── sum-root-to-leaf-numbers.md │ ├── symmetric-binary-tree.md │ ├── target-sum.md │ ├── triangle.md │ ├── webpage-crawler.md │ ├── wildcard-matching.md │ ├── word-break-ii.md │ ├── word-break.md │ ├── word-count.md │ ├── word-ladder-ii.md │ ├── word-ladder.md │ └── word-search-ii.md ├── 数据结构 ├── balanced-binary-tree.md ├── count-of-smaller-numbers-after-self.md ├── data-stream-median.md ├── find-median-from-data-stream.md ├── implement-queue-by-two-stacks.md ├── implement-stack-by-two-queues.md ├── kth-largest-element-ii.md ├── kth-largest-element.md ├── kth-largest-in-n-arrays.md ├── kth-smallest-number-in-sorted-matrix.md ├── kth-smallest-sum-in-two-sorted-arrays.md ├── largest-number.md ├── moving-average-from-data-stream.md ├── print-numbers-by-recursion.md ├── range-sum-query-2d-mutable.md ├── range-sum-query-mutable.md ├── segment-tree-build-ii.md ├── segment-tree-build.md ├── segment-tree-modify.md ├── segment-tree-query-ii.md ├── segment-tree-query.md ├── sequence-reconstruction.md ├── ⼆叉搜索树 │ ├── add-binary.md │ ├── balanced-binary-tree.md │ ├── binary-representation.md │ ├── binary-search-tree-iterator.md │ ├── binary-search.md │ ├── binary-tree-flipping.md │ ├── binary-tree-inorder-traversal.md │ ├── binary-tree-leaf-sum.md │ ├── binary-tree-leaves-order-traversal.md │ ├── binary-tree-level-order-traversal-ii.md │ ├── binary-tree-level-order-traversal.md │ ├── binary-tree-level-sum.md │ ├── binary-tree-longest-consecutive-sequence-ii.md │ ├── binary-tree-longest-consecutive-sequence-iii.md │ ├── binary-tree-longest-consecutive-sequence.md │ ├── binary-tree-maximum-node.md │ ├── binary-tree-maximum-path-sum-ii.md │ ├── binary-tree-maximum-path-sum.md │ ├── binary-tree-path-sum-ii.md │ ├── binary-tree-path-sum-iii.md │ ├── binary-tree-path-sum.md │ ├── binary-tree-paths.md │ ├── binary-tree-postorder-traversal.md │ ├── binary-tree-preorder-traversal.md │ ├── binary-tree-right-side-view.md │ ├── binary-tree-serialization.md │ ├── binary-tree-vertical-order-traversal.md │ ├── binary-tree-zigzag-level-order-traversal.md │ ├── binary-watch.md │ ├── boundary-of-binary-tree.md │ ├── classical-binary-search.md │ ├── clone-binary-tree.md │ ├── closest-binary-search-tree-value-ii.md │ ├── closest-binary-search-tree-value.md │ ├── complete-binary-tree.md │ ├── construct-binary-tree-from-inorder-and-postorder-traversal.md │ ├── construct-binary-tree-from-preorder-and-inorder-traversal.md │ ├── convert-binary-search-tree-to-doubly-linked-list.md │ ├── convert-binary-tree-to-linked-lists-by-depth.md │ ├── convert-bst-to-greater-tree.md │ ├── convert-sorted-array-to-binary-search-tree-with-minimal-height.md │ ├── convert-sorted-array-to-binary-search-tree.md │ ├── convert-sorted-list-to-binary-search-tree.md │ ├── count-1-in-binary.md │ ├── flatten-binary-tree-to-linked-list.md │ ├── identical-binary-tree.md │ ├── inorder-successor-in-binary-search-tree.md │ ├── inorder-successor-in-bst.md │ ├── insert-node-in-a-binary-search-tree.md │ ├── insert-node-in-binary-search-tree.md │ ├── invert-binary-tree.md │ ├── kth-smallest-element-in-a-bst.md │ ├── largest-bst-subtree.md │ ├── maximum-depth-of-binary-tree.md │ ├── minimum-depth-of-binary-tree.md │ ├── recover-binary-search-tree.md │ ├── remove-node-in-binary-search-tree.md │ ├── search-range-in-binary-search-tree.md │ ├── serialize-and-deserialize-binary-tree.md │ ├── symmetric-binary-tree.md │ ├── tweaked-identical-binary-tree.md │ ├── unique-binary-search-trees-ii.md │ ├── unique-binary-search-trees.md │ ├── validate-binary-search-tree.md │ └── verify-preorder-serialization-of-a-binary-tree.md ├── 二叉树分治 │ ├── balanced-binary-tree.md │ ├── binary-tree-flipping.md │ ├── binary-tree-inorder-traversal.md │ ├── binary-tree-leaf-sum.md │ ├── binary-tree-leaves-order-traversal.md │ ├── binary-tree-level-order-traversal-ii.md │ ├── binary-tree-level-order-traversal.md │ ├── binary-tree-level-sum.md │ ├── binary-tree-longest-consecutive-sequence-ii.md │ ├── binary-tree-longest-consecutive-sequence-iii.md │ ├── binary-tree-longest-consecutive-sequence.md │ ├── binary-tree-maximum-node.md │ ├── binary-tree-maximum-path-sum-ii.md │ ├── binary-tree-maximum-path-sum.md │ ├── binary-tree-path-sum-ii.md │ ├── binary-tree-path-sum-iii.md │ ├── binary-tree-path-sum.md │ ├── binary-tree-paths.md │ ├── binary-tree-postorder-traversal.md │ ├── binary-tree-preorder-traversal.md │ ├── binary-tree-right-side-view.md │ ├── binary-tree-serialization.md │ ├── binary-tree-vertical-order-traversal.md │ ├── binary-tree-zigzag-level-order-traversal.md │ ├── boundary-of-binary-tree.md │ ├── clone-binary-tree.md │ ├── complete-binary-tree.md │ ├── construct-binary-tree-from-inorder-and-postorder-traversal.md │ ├── construct-binary-tree-from-preorder-and-inorder-traversal.md │ ├── convert-binary-tree-to-linked-lists-by-depth.md │ ├── flatten-binary-tree-to-linked-list.md │ ├── identical-binary-tree.md │ ├── invert-binary-tree.md │ ├── maximum-depth-of-binary-tree.md │ ├── minimum-depth-of-binary-tree.md │ ├── serialize-and-deserialize-binary-tree.md │ ├── symmetric-binary-tree.md │ ├── tweaked-identical-binary-tree.md │ └── verify-preorder-serialization-of-a-binary-tree.md ├── 哈希表 │ ├── anagrams.md │ ├── binary-tree-vertical-order-traversal.md │ ├── binary-tree-zigzag-level-order-traversal.md │ ├── building-outline.md │ ├── clone-graph.md │ ├── consistent-hashing-ii.md │ ├── consistent-hashing.md │ ├── copy-list-with-random-pointer.md │ ├── count-characters.md │ ├── counting-bloom-filter.md │ ├── data-stream-as-disjoint-intervals.md │ ├── find-all-anagrams-in-a-string.md │ ├── first-unique-number-in-data-stream-ii.md │ ├── first-unique-number-in-data-stream.md │ ├── happy-number.md │ ├── hash-function.md │ ├── heart-beat.md │ ├── insert-delete-getrandom-o1-duplicates-allowed.md │ ├── insert-delete-getrandom-o1.md │ ├── island-perimeter.md │ ├── line-reflection.md │ ├── load-balancer.md │ ├── longest-palindrome.md │ ├── majority-number-ii.md │ ├── majority-number-iii.md │ ├── majority-number.md │ ├── max-points-on-a-line.md │ ├── memcache.md │ ├── mini-cassandra.md │ ├── minimum-window-substring.md │ ├── mirror-numbers.md │ ├── name-deduplication.md │ ├── number-of-boomerangs.md │ ├── rehashing.md │ ├── same-number.md │ ├── standard-bloom-filter.md │ ├── strstr-ii.md │ ├── subarray-sum-equals-k-ii.md │ ├── subarray-sum-equals-to-k-ii.md │ ├── tiny-url-ii.md │ ├── tiny-url.md │ ├── top-k-frequent-words.md │ ├── word-search-ii.md │ └── word-search.md ├── 堆 │ ├── heapify.md │ ├── high-five.md │ ├── k-closest-points.md │ ├── kth-largest-element-ii.md │ ├── kth-largest-element.md │ ├── kth-largest-in-n-arrays.md │ ├── kth-smallest-number-in-sorted-matrix.md │ ├── largest-number.md │ ├── merge-k-sorted-arrays.md │ ├── merge-k-sorted-interval-lists.md │ ├── merge-k-sorted-lists.md │ ├── shortest-subarray-with-sum-at-least-k.md │ ├── super-ugly-number.md │ ├── the-skyline-problem.md │ ├── top-k-largest-numbers.md │ ├── trapping-rain-water-ii.md │ └── ugly-number-ii.md ├── 字典树 │ ├── add-and-search-word-data-structure-design.md │ ├── add-and-search-word.md │ ├── boggle-game.md │ ├── implement-trie-prefix-tree.md │ ├── implement-trie.md │ └── word-squares.md ├── 栈 │ ├── 132-pattern.md │ ├── 132-patterns.md │ ├── basic-calculator-iii.md │ ├── basic-calculator.md │ ├── convert-expression-to-polish-notation.md │ ├── convert-expression-to-reverse-polish-notation.md │ ├── evaluate-reverse-polish-notation.md │ ├── expression-tree-build.md │ ├── implement-three-stacks-by-single-array.md │ ├── kth-smallest-sum-in-two-sorted-arrays.md │ ├── largest-rectangle-in-histogram.md │ ├── longest-valid-parentheses.md │ ├── max-stack.md │ ├── max-tree.md │ ├── meeting-rooms.md │ ├── min-stack.md │ ├── mock-hanoi-tower-by-stacks.md │ ├── odd-even-jump.md │ ├── set-of-stacks-ii.md │ ├── set-of-stacks.md │ ├── simplify-path.md │ ├── stack-sorting.md │ └── valid-parentheses.md └── 链表 │ ├── add-two-numbers-ii.md │ ├── add-two-numbers.md │ ├── animal-shelter.md │ ├── convert-array-list-to-linked-list.md │ ├── convert-binary-search-tree-to-doubly-linked-list.md │ ├── convert-binary-tree-to-linked-lists-by-depth.md │ ├── convert-linked-list-to-array-list.md │ ├── count-linked-list-nodes.md │ ├── delete-node-in-the-middle-of-singly-linked-list.md │ ├── find-node-in-linked-list.md │ ├── implement-queue-by-linked-list-ii.md │ ├── implement-queue-by-linked-list.md │ ├── insert-into-a-cyclic-sorted-list.md │ ├── insert-node-in-sorted-linked-list.md │ ├── insertion-sort-list.md │ ├── intersection-of-two-linked-lists.md │ ├── merge-k-sorted-lists.md │ ├── merge-two-sorted-lists.md │ ├── middle-of-linked-list.md │ ├── nth-to-last-node-in-list.md │ ├── palindrome-linked-list.md │ ├── remove-duplicates-from-sorted-list-ii.md │ ├── remove-duplicates-from-sorted-list.md │ ├── remove-duplicates-from-unsorted-list.md │ ├── remove-linked-list-elements.md │ ├── remove-nth-node-from-end-of-list.md │ ├── reorder-list.md │ ├── reverse-linked-list-ii.md │ ├── reverse-linked-list.md │ ├── reverse-nodes-in-k-group.md │ ├── rotate-list.md │ ├── sort-list.md │ ├── swap-nodes-in-pairs.md │ └── swap-two-nodes-in-linked-list.md ├── 算法与数据结构 ├── 位运算面试题总结.md ├── 动态规划十问十答.md └── 线段树知识点总结.md ├── 算法思维题 ├── lowest-common-ancestor-ii.md ├── lowest-common-ancestor-iii.md ├── lowest-common-ancestor.md ├── permutation-sequence.md ├── 二分法 │ ├── build-post-office.md │ ├── classical-binary-search.md │ ├── closest-number-in-sorted-array.md │ ├── continuous-subarray-sum-ii.md │ ├── count-of-smaller-number-before-itself.md │ ├── count-of-smaller-number.md │ ├── divide-two-integers.md │ ├── dungeon-game.md │ ├── find-k-closest-elements.md │ ├── find-minimum-in-rotated-sorted-array-ii.md │ ├── find-minimum-in-rotated-sorted-array.md │ ├── find-peak-element-ii.md │ ├── find-peak-element.md │ ├── find-right-interval.md │ ├── first-bad-version.md │ ├── first-position-of-target.md │ ├── geohash-ii.md │ ├── geohash.md │ ├── h-index-ii.md │ ├── h-index.md │ ├── intersection-of-two-arrays-ii.md │ ├── intersection-of-two-arrays.md │ ├── interval-minimum-number.md │ ├── interval-sum-ii.md │ ├── interval-sum.md │ ├── k-closest-numbers-in-sorted-array.md │ ├── last-position-of-target.md │ ├── maximum-absolute-value.md │ ├── maximum-average-subarray-ii.md │ ├── maximum-average-subarray.md │ ├── maximum-number-in-mountain-sequence.md │ ├── median-of-k-sorted-arrays.md │ ├── powx-n.md │ ├── search-a-2d-matrix.md │ ├── search-for-a-range.md │ ├── search-in-a-big-sorted-array.md │ ├── search-insert-position.md │ ├── smallest-rectangle-enclosing-black-pixels.md │ ├── split-array-largest-sum.md │ ├── sqrt-x.md │ ├── sqrtx-ii.md │ ├── subarray-sum-ii.md │ ├── total-occurrence-of-target.md │ ├── web-logger.md │ ├── wiggle-sort-ii.md │ └── wood-cut.md ├── 位运算 │ ├── a-b-problem.md │ ├── add-binary.md │ ├── add-digits.md │ ├── add-strings.md │ ├── binary-representation.md │ ├── binary-watch.md │ ├── bitwise-and-of-numbers-range.md │ ├── convert-a-number-to-hexadecimal.md │ ├── count-1-in-binary.md │ ├── flip-bits.md │ ├── gray-code.md │ ├── hamming-distance.md │ ├── number-of-1-bits.md │ ├── o1-check-power-of-2.md │ ├── single-number-ii.md │ ├── single-number-iii.md │ ├── single-number.md │ ├── swap-bits.md │ ├── swap-without-extra-variable-only-c.md │ └── update-bits.md ├── 其他 │ ├── alien-dictionary.md │ ├── amicable-pair.md │ ├── assignment-operator-overloading-c-only.md │ ├── convex-polygon.md │ ├── cosine-similarity.md │ ├── delete-and-earn.md │ ├── digit-counts.md │ ├── fibonacci.md │ ├── find-the-celebrity.md │ ├── find-the-missing-number.md │ ├── fizz-buzz.md │ ├── friendship-service.md │ ├── generate-parentheses.md │ ├── gfs-client.md │ ├── guess-number-game.md │ ├── insert-interval.md │ ├── kth-prime-number.md │ ├── lfu-cache.md │ ├── matrix-zigzag-traversal.md │ ├── max-of-3-numbers.md │ ├── narcissistic-number.md │ ├── next-smaller-and-larger-number-with-the-same-1-bits.md │ ├── previous-permutation.md │ ├── rectangle-overlap.md │ ├── remove-element.md │ ├── restore-ip-addresses.md │ ├── reverse-3-digit-integer.md │ ├── reverse-bits.md │ ├── reverse-integer.md │ ├── root-of-equation.md │ ├── rotate-array.md │ ├── search-in-rotated-sorted-array-ii.md │ ├── setter-and-getter.md │ ├── shape-factory.md │ ├── simple-calculator.md │ ├── singleton.md │ ├── strings-homomorphism.md │ ├── strings-serialization.md │ ├── student-id.md │ ├── student-level.md │ ├── substring-rotation.md │ ├── substring-with-concatenation-of-all-words.md │ ├── sudoku-solver.md │ ├── text-justification.md │ ├── toy-factory.md │ ├── trailing-zeros.md │ ├── trie-service.md │ ├── tweaked-identical-binary-tree.md │ ├── two-strings-are-anagrams.md │ ├── ugly-number.md │ ├── unique-binary-search-trees-ii.md │ ├── unique-binary-search-trees.md │ ├── utf-8-validation.md │ ├── valid-number.md │ ├── valid-sudoku.md │ ├── verify-preorder-serialization-of-a-binary-tree.md │ ├── word-count.md │ └── zigzag-conversion.md ├── 分治法 │ ├── fast-power.md │ ├── kth-smallest-numbers-in-unsorted-array.md │ ├── nuts-bolts-problem.md │ ├── sort-colors-ii.md │ ├── subtree-with-maximum-average.md │ ├── symmetric-tree.md │ └── validate-binary-search-tree.md ├── 双指针 │ ├── 3sum-closest.md │ ├── 3sum.md │ ├── 4sum-ii.md │ ├── 4sum.md │ ├── container-with-most-water.md │ ├── continuous-subarray-sum-ii.md │ ├── find-the-duplicate-number.md │ ├── interleaving-positive-and-negative-numbers.md │ ├── linked-list-cycle-ii.md │ ├── linked-list-cycle.md │ ├── longest-substring-with-at-most-k-distinct-characters.md │ ├── longest-substring-without-repeating-characters.md │ ├── maximum-submatrix.md │ ├── merge-sorted-array.md │ ├── minimum-size-subarray-sum.md │ ├── minimum-window-substring.md │ ├── move-zeroes.md │ ├── partition-array-by-odd-and-even.md │ ├── partition-list.md │ ├── remove-duplicate-numbers-in-array.md │ ├── sliding-window-maximum.md │ ├── sliding-window-median.md │ ├── sort-colors.md │ ├── subarray-sum-ii.md │ ├── the-smallest-difference.md │ ├── triangle-count.md │ ├── two-sum-closest-to-target.md │ ├── two-sum-closest.md │ ├── two-sum-difference-equals-to-target.md │ ├── two-sum-greater-than-target.md │ ├── two-sum-input-array-is-sorted.md │ ├── two-sum-less-than-or-equal-to-target.md │ ├── two-sum-unique-pairs.md │ ├── valid-palindrome-ii.md │ ├── valid-palindrome.md │ ├── valid-word-abbreviation.md │ ├── window-sum.md │ ├── zigzag-iterator-ii.md │ └── zigzag-iterator.md ├── 字典MAP │ ├── inverted-index-map-reduce.md │ ├── inverted-index.md │ ├── permutation-index-ii.md │ ├── string-permutation.md │ ├── substring-with-at-least-k-distinct-characters.md │ ├── top-k-frequent-words-ii.md │ ├── two-sum-iii-data-structure-design.md │ ├── two-sum.md │ ├── typeahead.md │ ├── unique-characters.md │ ├── word-abbreviation-set.md │ └── words-abbreviation.md ├── 并查集 │ ├── accounts-merge.md │ ├── connected-component-in-undirected-graph.md │ ├── connecting-graph-ii.md │ ├── connecting-graph-iii.md │ ├── connecting-graph.md │ ├── find-the-weak-connected-component-in-the-directed-graph.md │ ├── graph-valid-tree.md │ ├── minimum-spanning-tree.md │ ├── number-of-islands-ii.md │ └── number-of-islands.md ├── 排序算法 │ ├── data-stream-median.md │ ├── find-median-from-data-stream.md │ ├── insert-into-a-cyclic-sorted-list.md │ ├── insertion-sort-list.md │ ├── median-in-data-stream.md │ ├── median-of-k-sorted-arrays.md │ ├── median-of-two-sorted-arrays.md │ ├── median.md │ ├── meeting-rooms.md │ ├── merge-k-sorted-lists.md │ ├── merge-two-sorted-arrays.md │ ├── merge-two-sorted-interval-lists.md │ ├── merge-two-sorted-lists.md │ ├── nuts-bolts-problem.md │ ├── partition-array-by-odd-and-even.md │ ├── partition-array-ii.md │ ├── partition-array.md │ ├── recover-rotated-sorted-array.md │ ├── remove-duplicates-from-sorted-list.md │ ├── reverse-pairs.md │ ├── sliding-window-median.md │ ├── sort-integers-ii.md │ ├── sort-integers-map-reduce.md │ ├── sort-integers.md │ ├── sort-letters-by-case.md │ ├── sort-list.md │ ├── top-k-frequent-elements.md │ ├── top-k-frequent-words-map-reduce.md │ ├── top-k-largest-numbers.md │ └── wiggle-sort.md └── 贪心算法 │ ├── assign-cookies.md │ ├── course-schedule-ii.md │ ├── course-schedule-iii.md │ ├── course-schedule.md │ ├── delete-digits.md │ ├── gas-station.md │ ├── increasing-triplet-subsequence.md │ ├── jump-game-ii.md │ ├── maximum-gap.md │ ├── minimum-number-of-arrows-to-burst-balloons.md │ ├── queue-reconstruction-by-height.md │ └── russian-doll-envelopes.md ├── 算法面试题解 ├── Google 面试题 | 3个非重复子数组最大和.md ├── Google 面试题 | 原子计数.md ├── Google 面试题 | 合法的IP的地址.md ├── Google 面试题 | 字典里面的最长单词.md └── Google 面试题 | 找二叉树最底层最左边的节点.md ├── 系统设计 Syestem Design └── 如何答好面试中的系统设计题.md ├── 编程新手必刷50题 V1.0.docx ├── 面试常考算法必刷题库.pdf ├── 题解 ├── 132-pattern.md ├── 3sum-closest.md ├── 3sum.md ├── 4sum-ii.md ├── 4sum.md ├── a-b-problem.md ├── accounts-merge.md ├── add-and-search-word-data-structure-design.md ├── add-and-search-word.md ├── add-binary.md ├── add-digits.md ├── add-operators.md ├── add-strings.md ├── add-two-numbers-ii.md ├── add-two-numbers.md ├── alien-dictionary.md ├── amicable-pair.md ├── anagram-map-reduce.md ├── anagrams.md ├── animal-shelter.md ├── arithmetic-slices-ii-subsequence.md ├── arraylist.md ├── assign-cookies.md ├── assignment-operator-overloading-c-only.md ├── backpack-ii.md ├── backpack-iii.md ├── backpack-iv.md ├── backpack-v.md ├── backpack-vi.md ├── backpack.md ├── balanced-binary-tree.md ├── basic-calculator-iii.md ├── basic-calculator.md ├── battleships-in-a-board.md ├── best-time-to-buy-and-sell-stock-ii.md ├── best-time-to-buy-and-sell-stock-iii.md ├── best-time-to-buy-and-sell-stock-iv.md ├── best-time-to-buy-and-sell-stock.md ├── binary-representation.md ├── binary-search-tree-iterator.md ├── binary-tree-flipping.md ├── binary-tree-inorder-traversal.md ├── binary-tree-leaf-sum.md ├── binary-tree-leaves-order-traversal.md ├── binary-tree-level-order-traversal-ii.md ├── binary-tree-level-order-traversal.md ├── binary-tree-level-sum.md ├── binary-tree-longest-consecutive-sequence-ii.md ├── binary-tree-longest-consecutive-sequence-iii.md ├── binary-tree-longest-consecutive-sequence.md ├── binary-tree-maximum-node.md ├── binary-tree-maximum-path-sum-ii.md ├── binary-tree-maximum-path-sum.md ├── binary-tree-path-sum-ii.md ├── binary-tree-path-sum-iii.md ├── binary-tree-path-sum.md ├── binary-tree-paths.md ├── binary-tree-postorder-traversal.md ├── binary-tree-preorder-traversal.md ├── binary-tree-right-side-view.md ├── binary-tree-serialization.md ├── binary-tree-vertical-order-traversal.md ├── binary-tree-zigzag-level-order-traversal.md ├── binary-watch.md ├── bitwise-and-of-numbers-range.md ├── boggle-game.md ├── bomb-enemy.md ├── boundary-of-binary-tree.md ├── build-post-office-ii.md ├── build-post-office.md ├── building-outline.md ├── burst-ballons.md ├── burst-balloons.md ├── calculation-the-sum-of-path.md ├── can-i-win.md ├── can-place-flowers.md ├── candy.md ├── card-game.md ├── char-to-integer.md ├── classical-binary-search.md ├── climbing-stairs-ii.md ├── climbing-stairs.md ├── clone-binary-tree.md ├── clone-graph.md ├── closest-binary-search-tree-value-ii.md ├── closest-binary-search-tree-value.md ├── closest-number-in-sorted-array.md ├── coin-change.md ├── coins-in-a-line-ii.md ├── coins-in-a-line-iii.md ├── coins-in-a-line.md ├── combination-sum-ii.md ├── combination-sum-iv.md ├── combination-sum.md ├── combinations.md ├── compare-strings.md ├── complete-binary-tree.md ├── connected-component-in-undirected-graph.md ├── connecting-graph-ii.md ├── connecting-graph-iii.md ├── connecting-graph.md ├── consistent-hashing-ii.md ├── consistent-hashing.md ├── construct-binary-tree-from-inorder-and-postorder-traversal.md ├── construct-binary-tree-from-preorder-and-inorder-traversal.md ├── container-with-most-water.md ├── continuous-subarray-sum-ii.md ├── continuous-subarray-sum.md ├── convert-a-number-to-hexadecimal.md ├── convert-array-list-to-linked-list.md ├── convert-binary-search-tree-to-doubly-linked-list.md ├── convert-binary-tree-to-linked-lists-by-depth.md ├── convert-bst-to-greater-tree.md ├── convert-expression-to-polish-notation.md ├── convert-expression-to-reverse-polish-notation.md ├── convert-linked-list-to-array-list.md ├── convert-sorted-array-to-binary-search-tree-with-minimal-height.md ├── convert-sorted-array-to-binary-search-tree.md ├── convert-sorted-list-to-binary-search-tree.md ├── convex-polygon.md ├── copy-books-ii.md ├── copy-books.md ├── copy-list-with-random-pointer.md ├── cosine-similarity.md ├── count-1-in-binary.md ├── count-and-say.md ├── count-characters.md ├── count-linked-list-nodes.md ├── count-numbers-with-unique-digits.md ├── count-of-smaller-number-before-itself.md ├── count-of-smaller-number.md ├── count-of-smaller-numbers-after-self.md ├── count-square-submatrices-with-all-ones.md ├── counting-bloom-filter.md ├── course-schedule-ii.md ├── course-schedule-iii.md ├── course-schedule.md ├── create-maximum-number.md ├── data-stream-as-disjoint-intervals.md ├── data-stream-median.md ├── decode-string.md ├── decode-ways-ii.md ├── decode-ways.md ├── delete-and-earn.md ├── delete-digits.md ├── delete-node-in-the-middle-of-singly-linked-list.md ├── dices-sum.md ├── digit-counts.md ├── distinct-subsequences-ii.md ├── distinct-subsequences.md ├── divide-two-integers.md ├── driving-problem.md ├── drop-eggs-ii.md ├── drop-eggs.md ├── dungeon-game.md ├── edit-distance-ii.md ├── edit-distance.md ├── evaluate-reverse-polish-notation.md ├── excel-sheet-column-title.md ├── expression-evaluation.md ├── expression-expand.md ├── expression-tree-build.md ├── factorization.md ├── fast-power.md ├── fibonacci.md ├── find-all-anagrams-in-a-string.md ├── find-k-closest-elements.md ├── find-median-from-data-stream.md ├── find-minimum-in-rotated-sorted-array-ii.md ├── find-minimum-in-rotated-sorted-array.md ├── find-node-in-linked-list.md ├── find-peak-element-ii.md ├── find-peak-element.md ├── find-right-interval.md ├── find-the-celebrity.md ├── find-the-connected-component-in-the-undirected-graph.md ├── find-the-duplicate-number.md ├── find-the-missing-number-ii.md ├── find-the-missing-number.md ├── find-the-weak-connected-component-in-the-directed-graph.md ├── first-bad-version.md ├── first-missing-positive.md ├── first-position-of-target.md ├── first-position-unique-character.md ├── first-unique-character-in-a-string.md ├── first-unique-number-in-data-stream-ii.md ├── first-unique-number-in-data-stream.md ├── fizz-buzz.md ├── flatten-2d-vector.md ├── flatten-binary-tree-to-linked-list.md ├── flatten-list.md ├── flatten-nested-list-iterator.md ├── flip-bits.md ├── flip-game-ii.md ├── flip-string-to-monotone-increasing.md ├── friendship-service.md ├── frog-jump.md ├── gas-station.md ├── generate-arraylist-with-given-size.md ├── generate-parentheses.md ├── geohash-ii.md ├── geohash.md ├── gfs-client.md ├── graph-valid-tree.md ├── gray-code.md ├── guess-number-game.md ├── h-index-ii.md ├── h-index.md ├── hamming-distance.md ├── happy-number.md ├── hash-function.md ├── heapify.md ├── heart-beat.md ├── high-five.md ├── house-robber-ii.md ├── house-robber-iii.md ├── house-robber.md ├── identical-binary-tree.md ├── implement-deque-by-circulant-array.md ├── implement-queue-by-interface.md ├── implement-queue-by-linked-list-ii.md ├── implement-queue-by-linked-list.md ├── implement-queue-by-two-stacks.md ├── implement-stack-by-two-queues.md ├── implement-stack.md ├── implement-strstr.md ├── implement-three-stacks-by-single-array.md ├── implement-trie-prefix-tree.md ├── implement-trie.md ├── increasing-subsequences.md ├── increasing-triplet-subsequence.md ├── inorder-successor-in-binary-search-tree.md ├── inorder-successor-in-bst.md ├── insert-delete-getrandom-o1-duplicates-allowed.md ├── insert-delete-getrandom-o1.md ├── insert-interval.md ├── insert-into-a-cyclic-sorted-list.md ├── insert-node-in-a-binary-search-tree.md ├── insert-node-in-sorted-linked-list.md ├── insertion-sort-list.md ├── integer-to-english-words.md ├── integer-to-roman.md ├── interleaving-positive-and-negative-numbers.md ├── interleaving-string.md ├── intersection-of-arrays.md ├── intersection-of-two-arrays-ii.md ├── intersection-of-two-arrays.md ├── intersection-of-two-linked-lists.md ├── interval-minimum-number.md ├── interval-sum-ii.md ├── interval-sum.md ├── invert-binary-tree.md ├── inverted-index-map-reduce.md ├── inverted-index.md ├── is-subsequence.md ├── island-perimeter.md ├── jump-game-ii.md ├── jump-game.md ├── k-closest-numbers-in-sorted-array.md ├── k-closest-points.md ├── k-edit-distance.md ├── k-sum-ii.md ├── k-sum.md ├── keys-and-rooms.md ├── knight-shortest-path-ii.md ├── knight-shortest-path.md ├── kth-largest-element-ii.md ├── kth-largest-element.md ├── kth-largest-in-n-arrays.md ├── kth-prime-number.md ├── kth-smallest-element-in-a-bst.md ├── kth-smallest-number-in-sorted-matrix.md ├── kth-smallest-numbers-in-unsorted-array.md ├── kth-smallest-sum-in-two-sorted-arrays.md ├── largest-bst-subtree.md ├── largest-divisible-subset.md ├── largest-number.md ├── largest-rectangle-in-histogram.md ├── last-position-of-target.md ├── left-pad.md ├── length-of-last-word.md ├── letter-combinations-of-a-phone-number-ii.md ├── letter-combinations-of-a-phone-number.md ├── lfu-cache.md ├── line-reflection.md ├── linked-list-cycle-ii.md ├── linked-list-cycle.md ├── load-balancer.md ├── longest-common-prefix.md ├── longest-common-subsequence.md ├── longest-common-substring.md ├── longest-consecutive-sequence.md ├── longest-continuous-increasing-subsequence-ii.md ├── longest-continuous-increasing-subsequence.md ├── longest-increasing-continuous-subsequence.md ├── longest-increasing-subsequence.md ├── longest-palindrome.md ├── longest-palindromic-subsequence.md ├── longest-palindromic-substring.md ├── longest-path-on-the-tree.md ├── longest-repeating-subsequence.md ├── longest-substring-with-at-least-k-repeating-characters.md ├── longest-substring-with-at-most-k-distinct-characters.md ├── longest-substring-without-repeating-characters.md ├── longest-valid-parentheses.md ├── longest-words.md ├── lowercase-to-uppercase-ii.md ├── lowercase-to-uppercase.md ├── lowest-common-ancestor-ii.md ├── lowest-common-ancestor-iii.md ├── lowest-common-ancestor.md ├── lru-cache.md ├── majority-number-ii.md ├── majority-number-iii.md ├── majority-number.md ├── matrix-zigzag-traversal.md ├── max-of-3-numbers.md ├── max-of-array.md ├── max-points-on-a-line.md ├── max-stack.md ├── max-tree.md ├── maximal-rectangle.md ├── maximal-square-ii.md ├── maximal-square.md ├── maximum-absolute-value.md ├── maximum-average-subarray-ii.md ├── maximum-average-subarray.md ├── maximum-depth-of-binary-tree.md ├── maximum-gap.md ├── maximum-number-in-mountain-sequence.md ├── maximum-product-subarray.md ├── maximum-subarray-difference.md ├── maximum-subarray-ii.md ├── maximum-subarray-iii.md ├── maximum-subarray-iv.md ├── maximum-subarray-v.md ├── maximum-subarray.md ├── maximum-submatrix.md ├── maximum-subtree.md ├── median-of-k-sorted-arrays.md ├── median-of-two-sorted-arrays.md ├── median.md ├── meeting-rooms-ii.md ├── meeting-rooms.md ├── memcache.md ├── merge-intervals.md ├── merge-k-sorted-arrays.md ├── merge-k-sorted-interval-lists.md ├── merge-k-sorted-lists.md ├── merge-sorted-array-ii.md ├── merge-sorted-array.md ├── merge-two-sorted-arrays.md ├── merge-two-sorted-interval-lists.md ├── merge-two-sorted-lists.md ├── middle-of-linked-list.md ├── min-stack.md ├── mini-cassandra.md ├── mini-twitter.md ├── mini-uber.md ├── mini-yelp.md ├── minimum-adjustment-cost.md ├── minimum-cost-to-merge-stones.md ├── minimum-depth-of-binary-tree.md ├── minimum-falling-path-sum.md ├── minimum-moves-to-equal-array-elements-ii.md ├── minimum-moves-to-equal-array-elements.md ├── minimum-number-of-arrows-to-burst-balloons.md ├── minimum-path-sum.md ├── minimum-size-subarray-sum.md ├── minimum-spanning-tree.md ├── minimum-subarray.md ├── minimum-subtree.md ├── minimum-window-substring.md ├── mirror-numbers.md ├── missing-integer.md ├── missing-interval.md ├── mock-hanoi-tower-by-stacks.md ├── modern-ludo-i.md ├── monochrome-screen.md ├── move-zeroes.md ├── moving-average-from-data-stream.md ├── multiply-strings.md ├── n-gram-map-reduce.md ├── n-queens-ii.md ├── n-queens.md ├── name-deduplication.md ├── narcissistic-number.md ├── nested-list-weight-sum.md ├── next-permutation-ii.md ├── next-permutation.md ├── next-smaller-and-larger-number-with-the-same-1-bits.md ├── non-negative-integers-without-consecutive-ones.md ├── nth-to-last-node-in-list.md ├── null-terminated-string.md ├── number-of-1-bits.md ├── number-of-airplanes-in-the-sky.md ├── number-of-boomerangs.md ├── number-of-islands-ii.md ├── number-of-islands.md ├── number-of-ways-to-represent-n-cents.md ├── number-of-ways-to-stay-in-the-same-place-after-some-steps-ii.md ├── nuts-bolts-problem.md ├── o1-check-power-of-2.md ├── odd-even-jump.md ├── ones-and-zeroes.md ├── optimal-account-balancing.md ├── paint-fence.md ├── paint-house-ii.md ├── paint-house.md ├── palindrome-linked-list.md ├── palindrome-number.md ├── palindrome-partitioning-ii.md ├── palindrome-partitioning.md ├── parking-lot.md ├── partition-array-by-odd-and-even.md ├── partition-array-ii.md ├── partition-array.md ├── partition-equal-subset-sum.md ├── partition-list.md ├── pascals-triangle-ii.md ├── pascals-triangle.md ├── pass-interview.md ├── path-sum-ii.md ├── path-sum.md ├── perfect-squares.md ├── permutation-index-ii.md ├── permutation-index.md ├── permutation-sequence.md ├── permutations-ii.md ├── permutations.md ├── plus-one.md ├── populating-next-right-pointers-in-each-node-ii.md ├── post-office-problem.md ├── powx-n.md ├── previous-permutation.md ├── prime-factorization.md ├── print-numbers-by-recursion.md ├── product-of-array-exclude-itself.md ├── profitable-schemes.md ├── queue-reconstruction-by-height.md ├── range-sum-query-2d-immutable.md ├── range-sum-query-2d-mutable.md ├── range-sum-query-mutable.md ├── rat-jump.md ├── read-n-characters-given-read4-ii-call-multiple-times.md ├── reconstruct-itinerary.md ├── recover-binary-search-tree.md ├── recover-rotated-sorted-array.md ├── rectangle-area.md ├── rectangle-overlap.md ├── reference.md ├── regular-expression-matching.md ├── rehashing.md ├── remove-duplicate-numbers-in-array.md ├── remove-duplicates-from-sorted-array-ii.md ├── remove-duplicates-from-sorted-array.md ├── remove-duplicates-from-sorted-list-ii.md ├── remove-duplicates-from-sorted-list.md ├── remove-duplicates-from-unsorted-list.md ├── remove-element.md ├── remove-linked-list-elements.md ├── remove-node-in-binary-search-tree.md ├── remove-nth-node-from-end-of-list.md ├── remove-substrings.md ├── reorder-array-to-construct-the-minimum-number.md ├── reorder-list.md ├── repeated-dna-sequences.md ├── repeated-substring-pattern.md ├── restore-ip-addresses.md ├── reverse-3-digit-integer.md ├── reverse-bits.md ├── reverse-integer.md ├── reverse-linked-list-ii.md ├── reverse-linked-list.md ├── reverse-nodes-in-k-group.md ├── reverse-pairs.md ├── reverse-words-in-a-string.md ├── roman-to-integer.md ├── root-of-equation.md ├── rotate-array.md ├── rotate-image.md ├── rotate-list.md ├── rotate-string.md ├── route-between-two-nodes-in-graph.md ├── russian-doll-envelopes.md ├── same-number.md ├── scramble-string.md ├── search-a-2d-matrix-ii.md ├── search-a-2d-matrix.md ├── search-for-a-range.md ├── search-graph-nodes.md ├── search-in-a-big-sorted-array.md ├── search-in-rotated-sorted-array-ii.md ├── search-in-rotated-sorted-array.md ├── search-insert-position.md ├── search-range-in-binary-search-tree.md ├── second-max-of-array.md ├── segment-tree-build-ii.md ├── segment-tree-build.md ├── segment-tree-modify.md ├── segment-tree-query-ii.md ├── segment-tree-query.md ├── sequence-reconstruction.md ├── serialize-and-deserialize-binary-tree.md ├── set-matrix-zeroes.md ├── set-of-stacks-ii.md ├── set-of-stacks.md ├── setter-and-getter.md ├── shape-factory.md ├── shortest-palindrome.md ├── shortest-path-to-the-destination.md ├── shortest-path-visiting-all-nodes.md ├── shortest-subarray-with-sum-at-least-k.md ├── similar-string-groups.md ├── simple-calculator.md ├── simplify-path.md ├── single-number-ii.md ├── single-number-iii.md ├── single-number.md ├── singleton.md ├── six-degrees.md ├── sliding-puzzle-ii.md ├── sliding-window-matrix-maximum.md ├── sliding-window-maximum.md ├── sliding-window-median.md ├── smallest-rectangle-enclosing-black-pixels.md ├── sort-colors-ii.md ├── sort-colors.md ├── sort-integers-ii.md ├── sort-integers-map-reduce.md ├── sort-integers.md ├── sort-letters-by-case.md ├── sort-list.md ├── space-replacement.md ├── sparse-matrix-multiplication.md ├── spiral-matrix-ii.md ├── spiral-matrix.md ├── split-array-largest-sum.md ├── sqrt-x.md ├── sqrtx-ii.md ├── stack-sorting.md ├── standard-bloom-filter.md ├── stone-game-ii.md ├── stone-game.md ├── string-compression.md ├── string-permutation-ii.md ├── string-permutation.md ├── string-to-integer-atoi.md ├── string-to-integer.md ├── strings-homomorphism.md ├── strings-serialization.md ├── strstr-ii.md ├── student-id.md ├── student-level.md ├── subarray-sum-closest.md ├── subarray-sum-equals-k-ii.md ├── subarray-sum-equals-to-k-ii.md ├── subarray-sum-ii.md ├── subarray-sum.md ├── submatrix-sum.md ├── subsets-ii.md ├── subsets.md ├── substring-rotation.md ├── substring-with-at-least-k-distinct-characters.md ├── substring-with-concatenation-of-all-words.md ├── subtree-with-maximum-average.md ├── subtree.md ├── sudoku-solver.md ├── sum-of-left-leaves.md ├── sum-root-to-leaf-numbers.md ├── super-ugly-number.md ├── surrounded-regions.md ├── swap-bits.md ├── swap-nodes-in-pairs.md ├── swap-two-integers-in-array.md ├── swap-two-nodes-in-linked-list.md ├── swap-without-extra-variable-only-c.md ├── symmetric-binary-tree.md ├── symmetric-tree.md ├── system-longest-file-path.md ├── target-sum.md ├── text-justification.md ├── the-maze-iii.md ├── the-skyline-problem.md ├── the-smallest-difference.md ├── tiny-url-ii.md ├── tiny-url.md ├── top-k-frequent-elements.md ├── top-k-frequent-words-ii.md ├── top-k-frequent-words-map-reduce.md ├── top-k-frequent-words.md ├── top-k-largest-numbers.md ├── topological-sorting.md ├── total-occurrence-of-target.md ├── toy-factory.md ├── trailing-zeros.md ├── trapping-rain-water-ii.md ├── trapping-rain-water.md ├── traveling-salesman-problem.md ├── tree.md ├── triangle-count.md ├── triangle.md ├── trie-service.md ├── tweaked-identical-binary-tree.md ├── two-strings-are-anagrams.md ├── two-sum-closest-to-target.md ├── two-sum-closest.md ├── two-sum-difference-equals-to-target.md ├── two-sum-greater-than-target.md ├── two-sum-iii-data-structure-design.md ├── two-sum-input-array-is-sorted.md ├── two-sum-less-than-or-equal-to-target.md ├── two-sum-unique-pairs.md ├── two-sum.md ├── typeahead.md ├── ugly-number-ii.md ├── ugly-number.md ├── unique-binary-search-trees-ii.md ├── unique-binary-search-trees.md ├── unique-characters.md ├── unique-paths-ii.md ├── unique-paths.md ├── unique-substrings-in-wraparound-string.md ├── update-bits.md ├── url-parser.md ├── utf-8-validation.md ├── valid-number.md ├── valid-palindrome-ii.md ├── valid-palindrome.md ├── valid-parentheses.md ├── valid-sudoku.md ├── valid-word-abbreviation.md ├── validate-binary-search-tree.md ├── validate-ip-address.md ├── verify-preorder-serialization-of-a-binary-tree.md ├── walls-and-gates.md ├── web-logger.md ├── webpage-crawler.md ├── wiggle-sort-ii.md ├── wiggle-sort.md ├── wildcard-matching.md ├── window-sum.md ├── wood-cut.md ├── word-abbreviation-set.md ├── word-break-ii.md ├── word-break-iii.md ├── word-break.md ├── word-count.md ├── word-ladder-ii.md ├── word-ladder.md ├── word-pattern-ii.md ├── word-search-ii.md ├── word-search.md ├── word-squares.md ├── words-abbreviation.md ├── zigzag-conversion.md ├── zigzag-iterator-ii.md ├── zigzag-iterator.md └── zombie-in-matrix.md └── 高频 ├── anagrams.md ├── char-to-integer.md ├── compare-strings.md ├── count-and-say.md ├── edit-distance-ii.md ├── excel-sheet-column-title.md ├── expression-expand.md ├── expression-tree-build.md ├── first-position-unique-character.md ├── first-unique-character-in-a-string.md ├── implement-strstr.md ├── integer-to-english-words.md ├── integer-to-roman.md ├── is-subsequence.md ├── left-pad.md ├── length-of-last-word.md ├── longest-common-prefix.md ├── longest-common-substring.md ├── longest-palindromic-substring.md ├── longest-substring-with-at-least-k-repeating-characters.md ├── longest-words.md ├── lowercase-to-uppercase-ii.md ├── lowercase-to-uppercase.md ├── lru-cache.md ├── mini-twitter.md ├── mini-yelp.md ├── monochrome-screen.md ├── multiply-strings.md ├── null-terminated-string.md ├── palindrome-number.md ├── parking-lot.md ├── pascals-triangle-ii.md ├── pascals-triangle.md ├── pass-interview.md ├── permutation-index.md ├── plus-one.md ├── prime-factorization.md ├── rat-jump.md ├── read-n-characters-given-read4-ii-call-multiple-times.md ├── readme.md ├── rectangle-area.md ├── reference.md ├── repeated-dna-sequences.md ├── repeated-substring-pattern.md ├── reverse-words-in-a-string.md ├── roman-to-integer.md ├── rotate-string.md ├── shortest-palindrome.md ├── space-replacement.md ├── string-compression.md ├── string-to-integer-atoi.md ├── string-to-integer.md ├── system-longest-file-path.md ├── url-parser.md ├── validate-ip-address.md ├── 数组题 ├── arraylist.md ├── battleships-in-a-board.md ├── best-time-to-buy-and-sell-stock-ii.md ├── best-time-to-buy-and-sell-stock-iii.md ├── best-time-to-buy-and-sell-stock-iv.md ├── best-time-to-buy-and-sell-stock.md ├── can-place-flowers.md ├── candy.md ├── continuous-subarray-sum-ii.md ├── continuous-subarray-sum.md ├── convert-a-number-to-hexadecimal.md ├── first-missing-positive.md ├── flatten-2d-vector.md ├── flip-string-to-monotone-increasing.md ├── generate-arraylist-with-given-size.md ├── implement-deque-by-circulant-array.md ├── implement-stack.md ├── intersection-of-arrays.md ├── k-closest-numbers-in-sorted-array.md ├── max-of-array.md ├── minimum-moves-to-equal-array-elements-ii.md ├── minimum-moves-to-equal-array-elements.md ├── missing-interval.md ├── number-of-airplanes-in-the-sky.md ├── product-of-array-exclude-itself.md ├── remove-duplicates-from-sorted-array-ii.md ├── remove-duplicates-from-sorted-array.md ├── reorder-array-to-construct-the-minimum-number.md ├── rotate-image.md ├── search-a-2d-matrix-ii.md ├── search-in-rotated-sorted-array.md ├── second-max-of-array.md ├── set-matrix-zeroes.md ├── sliding-window-matrix-maximum.md ├── sparse-matrix-multiplication.md ├── spiral-matrix-ii.md ├── spiral-matrix.md ├── subarray-sum-closest.md ├── subarray-sum.md ├── submatrix-sum.md ├── swap-two-integers-in-array.md └── trapping-rain-water.md └── 算法面试必刷100题.xlsx /.gitconfig: -------------------------------------------------------------------------------- 1 | [remote "ninechapter-algorithm"] 2 | url = https://github.com/ninechapter-algorithm/linghu-algorithm-templete.git 3 | fetch = +refs/heads/*:refs/remotes/ninechapter-algorithm/* 4 | [remote "origin"] 5 | url = git@github.com:huangyingw/ninechapter-algorithm_linghu-algorithm-templete.git 6 | fetch = +refs/heads/*:refs/remotes/origin/* 7 | [push] 8 | remote = origin 9 | [gsync] 10 | remote = ninechapter-algorithm 11 | branch = master 12 | [merge] 13 | allow = true 14 | [branch "dev"] 15 | remote = origin 16 | merge = refs/heads/dev 17 | [branch "master"] 18 | remote = origin 19 | merge = refs/heads/master 20 | [gdif] 21 | ignore = *.gdio.diff 22 | [deploy] 23 | host = localhost 24 | path = ~/myproject/git/interview/ninechapter-algorithm/linghu-algorithm-templete 25 | -------------------------------------------------------------------------------- /pictures/3个非重复子数组最大和.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/3个非重复子数组最大和.png -------------------------------------------------------------------------------- /pictures/4S分析法 - 存储.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/4S分析法 - 存储.png -------------------------------------------------------------------------------- /pictures/4S分析法 - 数据结构.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/4S分析法 - 数据结构.png -------------------------------------------------------------------------------- /pictures/4S分析法.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/4S分析法.png -------------------------------------------------------------------------------- /pictures/位运算: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /pictures/位运算1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算1.png -------------------------------------------------------------------------------- /pictures/位运算2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算2.png -------------------------------------------------------------------------------- /pictures/位运算3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算3.png -------------------------------------------------------------------------------- /pictures/位运算4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算4.png -------------------------------------------------------------------------------- /pictures/位运算5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算5.png -------------------------------------------------------------------------------- /pictures/位运算6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算6.png -------------------------------------------------------------------------------- /pictures/位运算7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算7.png -------------------------------------------------------------------------------- /pictures/位运算8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/位运算8.png -------------------------------------------------------------------------------- /pictures/原子计数.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/原子计数.png -------------------------------------------------------------------------------- /pictures/字典里面的最长单词 - 题解.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/字典里面的最长单词 - 题解.png -------------------------------------------------------------------------------- /pictures/字典里面的最长单词.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/字典里面的最长单词.png -------------------------------------------------------------------------------- /pictures/找二叉树最底层最左边的节点-样例.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/找二叉树最底层最左边的节点-样例.png -------------------------------------------------------------------------------- /pictures/找二叉树最底层最左边的节点.题解.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/找二叉树最底层最左边的节点.题解.png -------------------------------------------------------------------------------- /pictures/服务.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/服务.png -------------------------------------------------------------------------------- /pictures/线段树1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/线段树1.png -------------------------------------------------------------------------------- /pictures/线段树2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/线段树2.png -------------------------------------------------------------------------------- /pictures/线段树3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/线段树3.png -------------------------------------------------------------------------------- /pictures/线段树4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/线段树4.png -------------------------------------------------------------------------------- /pictures/线段树5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/线段树5.png -------------------------------------------------------------------------------- /pictures/线段树6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/线段树6.png -------------------------------------------------------------------------------- /pictures/线段树7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/pictures/线段树7.png -------------------------------------------------------------------------------- /令狐老师的算法小抄/面试常考算法模板 Cheat Sheet V4.3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huangyingw/ninechapter-algorithm_linghu-algorithm-templete/ff7284350de9097925a41b59f293f1f1dda25622/令狐老师的算法小抄/面试常考算法模板 Cheat Sheet V4.3.pdf -------------------------------------------------------------------------------- /其他/readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /动态规划/backpack-iv.md: -------------------------------------------------------------------------------- 1 | # 背包问题 IV 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/backpack-iv/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出 n 个物品, 以及一个数组, `nums[i]`代表第i个物品的大小, 保证大小均为正数并且没有重复, 正整数 `target` 表示背包的大小, 找到能填满背包的方案数。 5 | `每一个物品可以使用无数次` 6 | ### 样例说明 7 | **样例1** 8 | 9 | ``` 10 | 输入: nums = [2,3,6,7] 和 target = 7 11 | 输出: 2 12 | 解释: 13 | 方案有: 14 | [7] 15 | [2, 2, 3] 16 | ``` 17 | 18 | **样例2** 19 | 20 | ``` 21 | 输入: nums = [2,3,4,5] 和 target = 7 22 | 输出: 3 23 | 解释: 24 | 方案有: 25 | [2, 5] 26 | [3, 4] 27 | [2, 2, 3] 28 | ``` 29 | ### 参考代码 30 | 效率比较高的版本。时间复杂度 $O(n * target)$ 31 | ```python 32 | class Solution: 33 | """ 34 | @param nums: an integer array and all positive numbers, no duplicates 35 | @param target: An integer 36 | @return: An integer 37 | """ 38 | def backPackIV(self, nums, target): 39 | dp = [0] * (target + 1) 40 | dp[0] = 1 41 | 42 | for num in nums: 43 | for i in range(num, target + 1): 44 | dp[i] += dp[i - num] 45 | 46 | return dp[target] 47 | ``` 48 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/backpack-v.md: -------------------------------------------------------------------------------- 1 | # 背包问题 V 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/backpack-v/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出 n 个物品, 以及一个数组, `nums[i]` 代表第i个物品的大小, 保证大小均为正数, 正整数 `target` 表示背包的大小, 找到能填满背包的方案数。 5 | `每一个物品只能使用一次` 6 | ### 样例说明 7 | 给出候选物品集合 `[1,2,3,3,7]` 以及 target `7` 8 | ``` 9 | 结果的集合为: 10 | [7] 11 | [1,3,3] 12 | ``` 13 | 返回 `2` 14 | ### 参考代码 15 | 带一点优化的动态规划。 16 | 17 | - 滚动数组。优化了空间。 18 | - 前缀和。优化了时间。没有必要每次都算到 target 那么多的值。 19 | ```python 20 | class Solution: 21 | """ 22 | @param nums: an integer array and all positive numbers 23 | @param target: An integer 24 | @return: An integer 25 | """ 26 | def backPackV(self, nums, target): 27 | f = [[0] * (target + 1), [0] * (target + 1)] 28 | f[0][0] = 1 29 | n = len(nums) 30 | prefix_sum = 0 31 | for i in range(1, n + 1): 32 | f[i % 2][0] = 1 33 | prefix_sum += nums[i - 1] 34 | for j in range(1, min(target, prefix_sum) + 1): 35 | f[i % 2][j] = f[(i - 1) % 2][j] 36 | if j >= nums[i - 1]: 37 | f[i % 2][j] += f[(i - 1) % 2][j - nums[i - 1]] 38 | 39 | return f[n % 2][target] 40 | ``` 41 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/backpack-vi.md: -------------------------------------------------------------------------------- 1 | # 组合总和 IV 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/backpack-vi/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出一个都是正整数的数组 `nums`,其中没有重复的数。从中找出所有的和为 `target` 的组合个数。 5 | ### 样例说明 6 | 7 | ### 参考代码 8 | dp求解,dp[i]表示当前和为i的解法个数 9 | ```python 10 | class Solution: 11 | # @param {int[]} nums an integer array and all positive numbers, no duplicates 12 | # @param {int} target an integer 13 | # @return {int} an integer 14 | def backPackVI(self, nums, target): 15 | # Write your code here 16 | dp = [0 for i in xrange(target + 1)] 17 | dp[0] = 1 18 | 19 | for j in xrange(1, target+1): 20 | for a in nums: 21 | if j >= a: 22 | dp[j] += dp[j - a] 23 | 24 | return dp[target] 25 | ``` 26 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/backpack.md: -------------------------------------------------------------------------------- 1 | # 背包问题 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/backpack/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |
在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i]
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: [3,4,8,5], backpack size=10 9 | 输出: 9 10 | 11 | 样例 2: 12 | 输入: [2,3,5,7], backpack size=12 13 | 输出: 12 14 | 15 | ``` 16 | ### 参考代码 17 | 传统的背包问题解法。二维动态规划。 18 | ```python 19 | class Solution: 20 | """ 21 | @param m: An integer m denotes the size of a backpack 22 | @param A: Given n items with size A[i] 23 | @return: The maximum size 24 | """ 25 | def backPack(self, m, A): 26 | n = len(A) 27 | f = [[False] * (m + 1) for _ in range(n + 1)] 28 | 29 | f[0][0] = True 30 | for i in range(1, n + 1): 31 | f[i][0] = True 32 | for j in range(1, m + 1): 33 | if j >= A[i - 1]: 34 | f[i][j] = f[i - 1][j] or f[i - 1][j - A[i - 1]] 35 | else: 36 | f[i][j] = f[i - 1][j] 37 | 38 | for i in range(m, -1, -1): 39 | if f[n][i]: 40 | return i 41 | return 0 42 | ``` 43 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/climbing-stairs-ii.md: -------------------------------------------------------------------------------- 1 | # 爬楼梯 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/climbing-stairs-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 一个小孩爬一个 n 层台阶的楼梯。他可以每次跳 1 步, 2 步 或者 3 步。实现一个方法来统计总共有多少种不同的方式爬到最顶层的台阶。 5 | 6 | ### 样例说明 7 | 8 | ### 参考代码 9 | 本题采用动态规划的思想,每一步的答案可以由上三步转移而来。 10 | 初始状态和状态转移方程如下: 11 | $f_0=1,\ f_1 =1,\ f_2=2,f_n=f_{n-1}+f_{n-2}+f_{n-3}(n \geq 3)$ 12 | ```java 13 | public class Solution { 14 | /** 15 | * @param n an integer 16 | * @return an integer 17 | */ 18 | public int climbStairs2(int n) { 19 | int[] f = new int[n+1]; 20 | f[0] = 1; 21 | for (int i = 0; i <= n; i++) 22 | for (int j = 1; j <= 3; j++) { 23 | if (i >= j) { 24 | f[i] += f[i-j]; 25 | } 26 | } 27 | return f[n]; 28 | } 29 | } 30 | ``` 31 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/coins-in-a-line.md: -------------------------------------------------------------------------------- 1 | # 硬币排成线 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/coins-in-a-line/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 有 `n` 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。 5 | 6 | 请判定 **先手玩家** 必胜还是必败? 7 | 8 | 若必胜, 返回 `true`, 否则返回 `false`. 9 | ### 样例说明 10 | **样例 1:** 11 | 12 | ``` 13 | 输入: 1 14 | 输出: true 15 | ``` 16 | 17 | **样例 2:** 18 | 19 | ``` 20 | 输入: 4 21 | 输出: true 22 | 解释: 23 | 先手玩家第一轮拿走一个硬币, 此时还剩三个. 24 | 这时无论后手玩家拿一个还是两个, 下一次先手玩家都可以把剩下的硬币拿完. 25 | ``` 26 | ### 参考代码 27 | 可以证明, 当硬币数目是3的倍数的时候, 先手玩家必败, 否则他必胜. 28 | 29 | 当硬币数目是3的倍数时, 每一轮先手者拿a个, 后手者拿3-a个即可, 后手必胜. 30 | 31 | 若不是3的倍数, 先手者可以拿1或2个, 此时剩余硬币个数就变成了3的倍数. 32 | ```cpp 33 | class Solution { 34 | public: 35 | /** 36 | * @param n: An integer 37 | * @return: A boolean which equals to true if the first player will win 38 | */ 39 | bool firstWillWin(int n) { 40 | return n % 3; 41 | } 42 | }; 43 | ``` 44 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/drop-eggs.md: -------------------------------------------------------------------------------- 1 | # 丢鸡蛋 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/drop-eggs/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 楼有 `n` 层高,鸡蛋若从 *k* 层或以上扔,就会碎。从 *k* 层以下扔,就不会碎。 5 | 6 | 现在给两个鸡蛋,用最少的扔的次数找到 *k*。返回最坏情况下次数。 7 | ### 样例说明 8 | **样例 1:** 9 | ``` 10 | 输入:100 11 | 输出:14 12 | ``` 13 | **样例 2:** 14 | ``` 15 | 输入:10 16 | 输出:4 17 | ``` 18 | ### 参考代码 19 | ```cpp 20 | class Solution { 21 | public: 22 | /** 23 | * @param n an integer 24 | * @return an integer 25 | */ 26 | int dropEggs(int n) { 27 | // Write your code here 28 | long long ans = 0; 29 | int x = 0; 30 | while (ans < n) { 31 | x += 1; 32 | ans += x; 33 | } 34 | return x; 35 | } 36 | }; 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/house-robber.md: -------------------------------------------------------------------------------- 1 | # 打劫房屋 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/house-robber/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 假设你是一个专业的窃贼,准备沿着一条街打劫房屋。每个房子都存放着特定金额的钱。你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 **当相邻的两个房子同一天被打劫时,该系统会自动报警**。 5 | 6 | 给定一个非负整数列表,表示每个房子中存放的钱, 算一算,如果今晚去打劫,**在不触动报警装置的情况下**, 你最多可以得到多少钱 。 7 | ### 样例说明 8 | **样例 1:** 9 | 10 | ``` 11 | 输入: [3, 8, 4] 12 | 输出: 8 13 | 解释: 仅仅打劫第二个房子. 14 | ``` 15 | 16 | **样例 2:** 17 | 18 | ``` 19 | 输入: [5, 2, 1, 3] 20 | 输出: 8 21 | 解释: 抢第一个和最后一个房子 22 | ``` 23 | ### 参考代码 24 | 使用滚动数组的做法,将每个下标直接 % 3。 25 | 时间复杂度 $O(n)$,空间复杂度 $O(1)$ 26 | ```python 27 | class Solution: 28 | """ 29 | @param A: An array of non-negative integers 30 | @return: The maximum amount of money you can rob tonight 31 | """ 32 | def houseRobber(self, A): 33 | if not A: 34 | return 0 35 | if len(A) <= 2: 36 | return max(A) 37 | 38 | f = [0] * 3 39 | f[0], f[1] = A[0], max(A[0], A[1]) 40 | 41 | for i in range(2, len(A)): 42 | f[i % 3] = max(f[(i - 1) % 3], f[(i - 2) % 3] + A[i]) 43 | 44 | return f[(len(A) - 1) % 3] 45 | ``` 46 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/longest-common-subsequence.md: -------------------------------------------------------------------------------- 1 | # 最长公共子序列 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/longest-common-subsequence/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: "ABCD" and "EDCA" 9 | 输出: 1 10 | 11 | 解释: 12 | LCS 是 'A' 或 'D' 或 'C' 13 | 14 | 15 | 样例 2: 16 | 输入: "ABCD" and "EACB" 17 | 输出: 2 18 | 19 | 解释: 20 | LCS 是 "AC" 21 | ``` 22 | 23 | ### 参考代码 24 | 使用九章算法强化班和动态规划专题班中讲的匹配型动态规划 25 | ```python 26 | class Solution: 27 | """ 28 | @param A: A string 29 | @param B: A string 30 | @return: The length of longest common subsequence of A and B 31 | """ 32 | def longestCommonSubsequence(self, A, B): 33 | n, m = len(A), len(B) 34 | f = [[0] * (m + 1) for i in range(n + 1)] 35 | for i in range(1, n + 1): 36 | for j in range(1, m + 1): 37 | f[i][j] = max(f[i][j - 1], f[i - 1][j]) 38 | if A[i - 1] == B[j - 1]: 39 | f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1) 40 | return f[n][m] 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /动态规划/maximum-subarray.md: -------------------------------------------------------------------------------- 1 | # 最大子数组 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/maximum-subarray/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。 5 | ### 样例说明 6 | **样例1:** 7 | 8 | ``` 9 | 输入:[−2,2,−3,4,−1,2,1,−5,3] 10 | 输出:6 11 | 解释:符合要求的子数组为[4,−1,2,1],其最大和为 6。 12 | ``` 13 | 14 | **样例2:** 15 | 16 | ``` 17 | 输入:[1,2,3,4] 18 | 输出:10 19 | 解释:符合要求的子数组为[1,2,3,4],其最大和为 10。 20 | ``` 21 | ### 参考代码 22 | 使用前缀和的方法,计算每个位置为结尾的 subarray 的最大值是多少。 23 | ```python 24 | class Solution: 25 | """ 26 | @param nums: A list of integers 27 | @return: A integer indicate the sum of max subarray 28 | """ 29 | def maxSubArray(self, nums): 30 | #prefix_sum记录前i个数的和,max_Sum记录全局最大值,min_Sum记录前i个数中0-k的最小值 31 | min_sum, max_sum = 0, -sys.maxsize 32 | prefix_sum = 0 33 | 34 | for num in nums: 35 | prefix_sum += num 36 | max_sum = max(max_sum, prefix_sum - min_sum) 37 | min_sum = min(min_sum, prefix_sum) 38 | 39 | return max_sum 40 | ``` 41 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /图论/宽度优先搜索/best-time-to-buy-and-sell-stock-ii.md: -------------------------------------------------------------------------------- 1 | # 买卖股票的最佳时机 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/best-time-to-buy-and-sell-stock-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个数组 `prices` 表示一支股票每天的价格. 5 | 6 | 你可以完成任意次数的交易, 不过你不能同时参与多个交易 (也就是说, 如果你已经持有这支股票, 在再次购买之前, 你必须先卖掉它). 7 | 8 | 设计一个算法求出最大的利润. 9 | ### 样例说明 10 | **样例 1:** 11 | 12 | ``` 13 | 输入: [2, 1, 2, 0, 1] 14 | 输出: 2 15 | 解释: 16 | 1. 在第 2 天以 1 的价格买入, 然后在第 3 天以 2 的价格卖出, 利润 1 17 | 2. 在第 4 天以 0 的价格买入, 然后在第 5 天以 1 的价格卖出, 利润 1 18 | 总利润 2. 19 | ``` 20 | 21 | **样例 2:** 22 | 23 | ``` 24 | 输入: [4, 3, 2, 1] 25 | 输出: 0 26 | 解释: 不进行任何交易, 利润为0. 27 | ``` 28 | ### 参考代码 29 | 贪心法: 只要相邻的两天股票的价格是上升的, 我们就进行一次交易, 获得一定利润. 30 | 31 | 这样的策略不一定是最小的交易次数, 但是一定会得到最大的利润. 32 | ```java 33 | public class Solution { 34 | public int maxProfit(int[] prices) { 35 | int profit = 0; 36 | for (int i = 0; i < prices.length - 1; i++) { 37 | int diff = prices[i + 1] - prices[i]; 38 | if (diff > 0) { 39 | profit += diff; 40 | } 41 | } 42 | return profit; 43 | } 44 | } 45 | ``` 46 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /图论/宽度优先搜索/path-sum.md: -------------------------------------------------------------------------------- 1 | # 路径和 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/path-sum/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定二叉树和求和,确定树是否具有根到叶路径,使得沿路径的所有值相加等于给定的总和。 5 | ### 样例说明 6 | 7 | ### 参考代码 8 | 使用深度搜索的方法,当搜索到叶子节点时,比较与目标和的值,若相同,则结果为真,若搜索完毕没有出现目标和,返回假 9 | ```java 10 | /** 11 | * Definition of TreeNode: 12 | * public class TreeNode { 13 | * public int val; 14 | * public TreeNode left, right; 15 | * public TreeNode(int val) { 16 | * this.val = val; 17 | * this.left = this.right = null; 18 | * } 19 | * } 20 | */ 21 | 22 | public class Solution { 23 | /** 24 | * @param root: the tree 25 | * @param sum: the sum 26 | * @return: if the tree has a root-to-leaf path 27 | */ 28 | public boolean pathSum(TreeNode root, int sum) { 29 | // Write your code here. 30 | if (root == null) return false; 31 | else if (root.val == sum && root.left == null && root.right == null) return true; 32 | else return pathSum(root.left, sum - root.val) || pathSum(root.right, sum - root.val); 33 | } 34 | } 35 | ``` 36 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /图论/深度优先搜索/combination-sum-iv.md: -------------------------------------------------------------------------------- 1 | # 组合总和 IV 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/combination-sum-iv/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出一个都是正整数的数组 `nums`,其中没有重复的数。从中找出所有的和为 `target` 的组合个数。 5 | ### 样例说明 6 | **样例1** 7 | 8 | ``` 9 | 输入: nums = [1, 2, 4] 和 target = 4 10 | 输出: 6 11 | 解释: 12 | 可能的所有组合有: 13 | [1, 1, 1, 1] 14 | [1, 1, 2] 15 | [1, 2, 1] 16 | [2, 1, 1] 17 | [2, 2] 18 | [4] 19 | ``` 20 | 21 | **样例2** 22 | 23 | ``` 24 | 输入: nums = [1, 2] 和 target = 4 25 | 输出: 5 26 | 解释: 27 | 可能的所有组合有: 28 | [1, 1, 1, 1] 29 | [1, 1, 2] 30 | [1, 2, 1] 31 | [2, 1, 1] 32 | [2, 2] 33 | ``` 34 | ### 参考代码 35 | ```cpp 36 | class Solution { 37 | public: 38 | int combinationSum4(vector给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
5 | ### 样例说明 6 | **样例1:** 7 | ``` 8 | 输入:["lint", "intl", "inlt", "code"] 9 | 输出:["lint", "inlt", "intl"] 10 | ``` 11 | 12 | **样例 2:** 13 | ``` 14 | 输入:["ab", "ba", "cd", "dc", "e"] 15 | 输出: ["ab", "ba", "cd", "dc"] 16 | ``` 17 | ### 参考代码 18 | Given an array of strings, return all groups of strings that are anagrams. 19 | 20 | Note: All inputs will be in lower-case 21 | ```python 22 | class Solution: 23 | # @param strs: A list of strings 24 | # @return: A list of strings 25 | def anagrams(self, strs): 26 | # write your code here 27 | dict = {} 28 | for word in strs: 29 | sortedword = ''.join(sorted(word)) 30 | dict[sortedword] = [word] if sortedword not in dict else dict[sortedword] + [word] 31 | res = [] 32 | for item in dict: 33 | if len(dict[item]) >= 2: 34 | res += dict[item] 35 | return res 36 | 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /数据结构/哈希表/count-characters.md: -------------------------------------------------------------------------------- 1 | # 字符计数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/count-characters/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 对一个字符串中的字符进行计数, 返回一个hashmap, key为字符, value是这个字符出现的次数. 5 | ### 样例说明 6 | 7 | 例1: 8 | ``` 9 | 输入: 10 | str = "abca" 11 | 12 | 输出: 13 | { 14 | "a": 2, 15 | "b": 1, 16 | "c": 1 17 | } 18 | 19 | ``` 20 | 例2: 21 | ``` 22 | 输入: 23 | str = "ab" 24 | 25 | 输出: 26 | { 27 | "a": 1, 28 | "b": 1 29 | } 30 | 31 | ``` 32 | 33 | ### 参考代码 34 | 利用map统计即可 35 | ```java 36 | public class Solution { 37 | /* 38 | * @param : a string 39 | * @return: a hash map 40 | */ 41 | public Map给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。
翻转一个链表
5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: 1->2->3->null 10 | 输出: 3->2->1->null 11 | ``` 12 | 13 | **样例 2:** 14 | 15 | ``` 16 | 输入: 1->2->3->4->null 17 | 输出: 4->3->2->1->null 18 | ``` 19 | ### 参考代码 20 | http://www.lintcode.com/zh-cn/problem/reverse-linked-list/ 21 | ```python 22 | class Solution: 23 | 24 | def reverse(self, head): 25 | #curt表示前继节点 26 | curt = None 27 | while head != None: 28 | #temp记录下一个节点,head是当前节点 29 | temp = head.next 30 | head.next = curt 31 | curt = head 32 | head = temp 33 | return curt 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/二分法/closest-number-in-sorted-array.md: -------------------------------------------------------------------------------- 1 | # 排序数组中最接近元素 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/closest-number-in-sorted-array/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 在一个排好序的数组 *A* 中找到 *i* 使得 *A[i]* 最接近 *target* 5 | ### 样例说明 6 | 样例1: 7 | ``` 8 | 输入:[1, 2, 3] ,target = 2 9 | 输出: 1. 10 | ``` 11 | 样例2: 12 | ``` 13 | 输入:[1, 4, 6], target = 3 14 | 输出: 1. 15 | ``` 16 | 样例3: 17 | ``` 18 | 输入:[1, 4, 6],target = 5, 19 | 输出: 1 或 2. 20 | ``` 21 | ### 参考代码 22 | 利用二分查找的思想即可 23 | ```python 24 | class Solution: 25 | # @param {int[]} A an integer array sorted in ascending order 26 | # @param {int} target an integer 27 | # @return {int} an integer 28 | def closestNumber(self, A, target): 29 | if not A: 30 | return -1 31 | start, end = 0, len(A) - 1 32 | while start + 1 < end: 33 | mid = (start + end) / 2 34 | if A[mid] == target: 35 | return mid 36 | elif A[mid] > target: 37 | end = mid 38 | else: 39 | start = mid 40 | if A[end] - target > target - A[start]: 41 | return start 42 | else: 43 | return end 44 | ``` 45 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/二分法/maximum-average-subarray.md: -------------------------------------------------------------------------------- 1 | # 子数组的最大平均值 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/maximum-average-subarray/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个由`n`个整数组成的数组,找到给定长度`k`的连续子数组,该子数组具有最大平均值。你需要输出最大平均值。 5 | ### 样例说明 6 | **样例1** 7 | 8 | ``` 9 | 输入: nums = [1,12,-5,-6,50,3] and k = 4 10 | 输出: 12.75 11 | 解释: 12 | 最大平均为(12-5-6+50)/4 = 51/4 = 12.75 13 | ``` 14 | 15 | 16 | 17 | **样例2** 18 | 19 | ``` 20 | 输入: nums = [4,2,1,3,3] and k = 2 21 | 输出: 3.00 22 | 解释: 23 | 最大平均为(3+3)/2 = 6/2 = 3.00 24 | ``` 25 | ### 参考代码 26 | ```java 27 | public class Solution { 28 | /** 29 | * @param nums: an array 30 | * @param k: an integer 31 | * @return: the maximum average value 32 | */ 33 | public double findMaxAverage(int[] nums, int k) { 34 | // Write your code here 35 | int n = nums.length; 36 | int[] sum = new int[n + 1]; 37 | for(int i = 1; i <= n; i++){ 38 | sum[i] = sum[i - 1] + nums[i - 1]; 39 | } 40 | int ans = Integer.MIN_VALUE; 41 | for(int i = k; i <= n; i++){ 42 | ans = Math.max(ans, sum[i] - sum[i - k]); 43 | } 44 | return ans * 1.0 / k; 45 | } 46 | } 47 | ``` 48 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/二分法/sqrtx-ii.md: -------------------------------------------------------------------------------- 1 | # 对x开根II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/sqrtx-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 实现 `double sqrt(double x)` 并且 `x >= 0`。 5 | 计算并返回x开根后的值。 6 | ### 样例说明 7 | 例1: 8 | ``` 9 | 输入: n = 2 10 | 输出: 1.41421356 11 | ``` 12 | 例2: 13 | ``` 14 | 输入: n = 3 15 | 输出: 1.73205081 16 | ``` 17 | 18 | ### 参考代码 19 | ```python 20 | class Solution: 21 | """ 22 | @param: x: a double 23 | @return: the square root of x 24 | """ 25 | def sqrt(self, x): 26 | if x >= 1: 27 | start, end = 1, x 28 | else: 29 | start, end = x, 1 30 | 31 | while end - start > 1e-10: 32 | mid = (start + end) / 2 33 | if mid * mid < x: 34 | start = mid 35 | else: 36 | end = mid 37 | 38 | return start 39 | ``` 40 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/add-strings.md: -------------------------------------------------------------------------------- 1 | # 大整数加法 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/add-strings/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 以字符串的形式给出两个非负整数 `num1` 和 `num2`,返回 `num1` 和 `num2` 的和。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入 : num1 = "123", num2 = "45" 9 | 输出 : "168" 10 | ``` 11 | ### 参考代码 12 | 将两个数解析后相加即可 13 | ```java 14 | public class Solution { 15 | public String addStrings(String num1, String num2) { 16 | String res = ""; 17 | int m = num1.length(), n = num2.length(), i = m - 1, j = n - 1, flag = 0; 18 | while(i >= 0 || j >= 0){ 19 | int a, b; 20 | if(i >= 0){ 21 | a = num1.charAt(i--) - '0'; 22 | } 23 | else{ 24 | a = 0; 25 | } 26 | if(j >= 0){ 27 | b = num2.charAt(j--) - '0'; 28 | } 29 | else{ 30 | b = 0; 31 | } 32 | int sum = a + b + flag; 33 | res =(char)(sum % 10 + '0') + res; 34 | flag = sum / 10; 35 | } 36 | return flag == 1 ? "1" + res: res; 37 | } 38 | } 39 | ``` 40 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/bitwise-and-of-numbers-range.md: -------------------------------------------------------------------------------- 1 | # 数字范围内位的与 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/bitwise-and-of-numbers-range/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定范围[m,n],其中0 <= m <= n <= 2147483647,输出此范围内所有数字的按位AND,包括端值。 5 | 6 | 例如,给定范围[5,7],输出返回4。 7 | ### 样例说明 8 | **样例1** 9 | 10 | ``` 11 | 输入: m=5, n=7 12 | 输出: 4 13 | ``` 14 | 15 | **样例2** 16 | 17 | ``` 18 | 输入: m=14, n=15 19 | 输出: 14 20 | ``` 21 | ### 参考代码 22 | 本题中,我们需要得到[m,n]所有元素按位与的结果。举个例子,当m=26,n=30时,它们的二进制表示为为: 23 | **11**010 **11**011 **11**100 **11**101 **11**110 24 | 这个样例的答案是**11**000,易见我们发现我们只需要找到m和n最左边的公共部分即可。 25 | 26 | 每次都将n与n-1按位与,当n的二进制为1010时,1010 & 1001 = 1000,相当于把二进制位的最后一个1去掉了。因此我们不断的做n^n-1的操作,直到n小于m相等即可。 27 | ```java 28 | public class Solution { 29 | /** 30 | * @param m: an Integer 31 | * @param n: an Integer 32 | * @return: the bitwise AND of all numbers in [m,n] 33 | */ 34 | public int rangeBitwiseAnd(int m, int n) { 35 | // Write your code here 36 | while(m < n) { 37 | n = n & (n-1); 38 | } 39 | return n; 40 | 41 | } 42 | } 43 | ``` 44 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/count-1-in-binary.md: -------------------------------------------------------------------------------- 1 | # 二进制中有多少个1 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/count-1-in-binary/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 计算在一个 32 位的整数的二进制表示中有多少个 `1`。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入:32 9 | 输出:1 10 | 解释: 11 | 32(100000),返回 1。 12 | ``` 13 | **样例 2:** 14 | ``` 15 | 输入:5 16 | 输出:2 17 | 解释: 18 | 5(101),返回 2。 19 | ``` 20 | ### 参考代码 21 | ```java 22 | public class Solution { 23 | /** 24 | * @param num: an integer 25 | * @return: an integer, the number of ones in num 26 | */ 27 | public int countOnes(int num) { 28 | int count = 0; 29 | while (num != 0) { 30 | num = num & (num - 1); 31 | count++; 32 | } 33 | return count; 34 | } 35 | }; 36 | 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/flip-bits.md: -------------------------------------------------------------------------------- 1 | # 将整数A转换为B 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/flip-bits/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 如果要将整数n转换为m,需要改变多少个bit位? 5 | ### 样例说明 6 | 7 | ### 参考代码 8 | a ^ b 得到的结果的二进制中,1的个数就是a和b相异的位数。 9 | ```java 10 | class Solution { 11 | /** 12 | *@param a, b: Two integer 13 | *return: An integer 14 | */ 15 | public static int bitSwapRequired(int a, int b) { 16 | // write your code here 17 | int count = 0; 18 | for (int c = a ^ b; c != 0; c = c >>> 1) { 19 | count += c & 1; 20 | } 21 | return count; 22 | } 23 | }; 24 | ``` 25 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/hamming-distance.md: -------------------------------------------------------------------------------- 1 | # Hamming距离 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/hamming-distance/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 两个整数的Hamming距离是对应比特位不同的个数。 5 | 给定两个整数x和y,计算两者的Hamming距离。 6 | ### 样例说明 7 | **样例1** 8 | 9 | ``` 10 | 输入: x = 1 和 y = 4 11 | 输出: 2 12 | 解释: 13 | 1的二进制表示是001 14 | 4的二进制表示是100 15 | 共有2位不同 16 | ``` 17 | 18 | 19 | 20 | **样例2** 21 | 22 | ``` 23 | 输入: x = 5 和 y = 2 24 | 输出: 3 25 | 解释: 26 | 5的二进制表示是101 27 | 2的二进制表示是010 28 | 共有3位不同 29 | ``` 30 | ### 参考代码 31 | ```java 32 | public class Solution { 33 | public int hammingDistance(int x, int y) { 34 | int Distance=0; 35 | 36 | while ( x != 0 || y != 0 ) { 37 | if ( x % 2 != y % 2 ) { 38 | Distance ++; 39 | } 40 | x /= 2; 41 | y /= 2; 42 | } 43 | return Distance; 44 | } 45 | } 46 | ``` 47 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/number-of-1-bits.md: -------------------------------------------------------------------------------- 1 | # 判断一个整数中有多少个1 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/number-of-1-bits/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 写一个函数,其以无符号整数为输入,而输出对应二进制数所具有的“1”的位数(也被称为汉明权重) 5 | ### 样例说明 6 | **样例 1** 7 | ``` 8 | 输入:n = 11 9 | 输出:3 10 | 解析:11(10) = 1011(2), 返回 3 11 | ``` 12 | **样例 2** 13 | ``` 14 | 输入:n = 7 15 | 输出:3 16 | 解析:7(10) = 111(2), 返回 3 17 | ``` 18 | ### 参考代码 19 | 使用位运算符号‘&’,与1做与操作 20 | 并将n右移一位( >>1 ) 21 | 直到n为0 22 | ```java 23 | public class Solution { 24 | /** 25 | * @param n: an unsigned integer 26 | * @return: the number of ’1' bits 27 | */ 28 | public int hammingWeight(int n) { 29 | int ones = 0; 30 | while(n!=0) { 31 | ones += (n & 1); 32 | n = n>>1; 33 | } 34 | return ones; 35 | } 36 | } 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/o1-check-power-of-2.md: -------------------------------------------------------------------------------- 1 | # O(1)时间检测2的幂次 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/o1-check-power-of-2/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 用 O(*1*) 时间检测整数 *n* 是否是 *2* 的幂次。 5 | ### 样例说明 6 | 7 | ### 参考代码 8 | 2的幂的数字,其二进制中只有一个1。 9 | $n&(n-1) == 0$ 说明只有1个1。 10 | ```cpp 11 | class Solution { 12 | public: 13 | /* 14 | * @param n: An integer 15 | * @return: True or false 16 | */ 17 | bool checkPowerOf2(int n) { 18 | // write your code here 19 | if (n==0) 20 | return false; 21 | while (n % 2 == 0) 22 | n /=2 ; 23 | return n == 1; 24 | } 25 | }; 26 | ``` 27 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/single-number.md: -------------------------------------------------------------------------------- 1 | # 落单的数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/single-number/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出 `2 * n + 1`个数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入:[1,1,2,2,3,4,4] 9 | 输出:3 10 | 解释: 11 | 仅3出现一次 12 | ``` 13 | **样例 2:** 14 | ``` 15 | 输入:[0,0,1] 16 | 输出:1 17 | 解释: 18 | 仅1出现一次 19 | ``` 20 | 21 | ### 参考代码 22 | Given an array of integers, every element appears twice except for one. Find that single one. 23 | 考点: 24 | * 异或位运算 25 | 26 | Note: 27 | Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 28 | 异或运算具有很好的性质,相同数字异或运算后为0,并且具有交换律和结合律,故将所有数字异或运算后即可得到只出现一次的数字。 29 | ```python 30 | class Solution: 31 | """ 32 | @param A : an integer array 33 | @return : a integer 34 | """ 35 | def singleNumber(self, A): 36 | # write your code here 37 | ans = 0; 38 | for x in A: 39 | ans = ans ^ x 40 | return ans 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/swap-bits.md: -------------------------------------------------------------------------------- 1 | # 交换奇偶二进制位 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/swap-bits/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 设计一个方法,用尽可能少的指令,将一个整数中奇数数位和偶数数位的数字交换 (如,数位 0 和数位 1 交换,数位 2 和数位 3 交换,等等)。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入:0 9 | 输出:0 10 | 解释: 11 | 0 = 0(2) -> 0(2) = 0 12 | ``` 13 | **样例 2:** 14 | ``` 15 | 输入:5 16 | 输出:10 17 | 解释: 18 | 5 = 101(2) -> 1010(2) = 10 19 | ``` 20 | ### 参考代码 21 | ```cpp 22 | class Solution { 23 | public: 24 | /** 25 | * @param x an integer 26 | * @return an integer 27 | */ 28 | int swapOddEvenBits(int x) { 29 | // Write your code here 30 | return ( ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1) ); 31 | } 32 | }; 33 | ``` 34 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/位运算/swap-without-extra-variable-only-c.md: -------------------------------------------------------------------------------- 1 | # 交换不借助额外变量(仅C++) 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/swap-without-extra-variable-only-c/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出两个变量,x 和 y ,不使用第三个变量交换这两个变量的值。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入 : x = 10, y = 5 9 | 输出 : x = 5, y = 10 10 | ``` 11 | ### 参考代码 12 | 通过位运算实现。 13 | ```cpp 14 | class Solution { 15 | public: 16 | /** 17 | * @param x an integer 18 | * @param y an integer 19 | * @return nothing 20 | */ 21 | void swap(int &x, int &y) { 22 | // Write your code here 23 | x = x ^ y; 24 | y = x ^ y; 25 | x = x ^ y; 26 | } 27 | }; 28 | ``` 29 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/max-of-3-numbers.md: -------------------------------------------------------------------------------- 1 | # 三数之中的最大值 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/max-of-3-numbers/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给三个整数,求他们中的最大值。 5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: num1 = 1, num2 = 9, num3 = 0 9 | 输出: 9 10 | 11 | 样例解释: 12 | 返回三个数中最大的数。 13 | 14 | 样例 2: 15 | 输入: num1 = 1, num2 = 2, num3 = 3 16 | 输出: 3 17 | 18 | 样例解释: 19 | 返回三个中最大的数字。 20 | 21 | ``` 22 | ### 参考代码 23 | if选择语句的基本操作 24 | ```java 25 | public class Solution { 26 | /** 27 | * @param a an integer 28 | * @param b an integer 29 | * @param c an integer 30 | * @return an integer 31 | */ 32 | public int maxOfThreeNumbers(int a, int b, int c) { 33 | if (a >= b && a >= c) { 34 | return a; 35 | } else if (b >= a && b >= c) { 36 | return b; 37 | } else { 38 | return c; 39 | } 40 | } 41 | } 42 | ``` 43 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/remove-element.md: -------------------------------------------------------------------------------- 1 | # 删除元素 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/remove-element/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度。
元素的顺序可以改变,并且对新的数组不会有影响。
5 | ### 样例说明 6 | 7 | ### 参考代码 8 | Given an array and a value, remove all instances of that value in place and return the new length. 9 | 10 | The order of elements can be changed. It doesn't matter what you leave beyond the new length. 11 | 12 | 从前往后,遇到等于value的就跳过,把后面的元素填到前面的位置里。 13 | ```java 14 | public class Solution { 15 | public int removeElement(int[] A, int elem) { 16 | int i = 0; 17 | int pointer = A.length - 1; 18 | while(i <= pointer){ 19 | if(A[i] == elem){ 20 | A[i] = A[pointer]; 21 | pointer--; 22 | } else { 23 | i++; 24 | } 25 | } 26 | return pointer + 1; 27 | } 28 | } 29 | ``` 30 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/reverse-3-digit-integer.md: -------------------------------------------------------------------------------- 1 | # 反转一个3位整数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/reverse-3-digit-integer/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 反转一个只有3位数的整数。 5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: number = 123 10 | 输出: 321 11 | ``` 12 | **样例 2:** 13 | 14 | ``` 15 | 输入: number = 900 16 | 输出: 9 17 | ``` 18 | 19 | ### 参考代码 20 | 硅谷求职算法集训营上课版本 21 | ```java 22 | public class Solution { 23 | /* 24 | * @param number: A 3-digit number. 25 | * @return: Reversed number. 26 | */ 27 | public int reverseInteger(int number) { 28 | // write your code here 29 | //获得个位数 30 | int num1 = number % 10; 31 | //获得十位数 32 | int num2 = (number / 10) % 10; 33 | //获得百位数 34 | int num3 = ((number / 10) / 10) % 10; 35 | //相加 36 | return num3 + num2 * 10 + num1 * 100; 37 | } 38 | } 39 | ``` 40 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/reverse-bits.md: -------------------------------------------------------------------------------- 1 | # 颠倒二进制位 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/reverse-bits/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 颠倒给定的 32 位无符号整数的二进制位。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入: 00000010100101000001111010011100 9 | 输出: 00111001011110000010100101000000 10 | 解释: 11 | 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596, 12 | 因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。 13 | ``` 14 | **样例 2:** 15 | ``` 16 | 输入:11111111111111111111111111111101 17 | 输出:10111111111111111111111111111111 18 | 解释: 19 | 输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293, 20 | 因此返回 3221225471 其二进制表示形式为 10101111110010110010011101101001。 21 | ``` 22 | ### 参考代码 23 | 分离出输入数字的每一位,最低位左移31bit,次低位左移30bit,以此类推。 24 | ```java 25 | public class Solution { 26 | // you need treat n as an unsigned value 27 | public long reverseBits(int n) { 28 | int reversed = 0; 29 | for (int i = 0; i < 32; i++) { 30 | reversed = (reversed << 1) | (n & 1); 31 | n = (n >> 1); 32 | } 33 | return reversed; 34 | } 35 | } 36 | ``` 37 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/search-in-rotated-sorted-array-ii.md: -------------------------------------------------------------------------------- 1 | # 搜索旋转排序数组 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |跟进“搜索旋转排序数组”,假如有重复元素又将如何?
是否会影响运行时间复杂度?
如何影响?
为何会影响?
写出一个函数判断给定的目标值是否出现在数组中。
5 | ### 样例说明 6 | 例1: 7 | ``` 8 | 输入: 9 | [] 10 | 1 11 | 输出: 12 | false 13 | ``` 14 | 例2: 15 | ``` 16 | 输入: 17 | [3,4,4,5,7,0,1,2] 18 | 4 19 | 输出: 20 | true 21 | ``` 22 | 23 | ### 参考代码 24 | 由于出现重复元素,故不能使用二分法。 25 | 所以暴力比较即可。 26 | ```java 27 | public class Solution { 28 | // 这个问题在面试中不会让实现完整程序 29 | // 只需要举出能够最坏情况的数据是 [1,1,1,1... 1] 里有一个0即可。 30 | // 在这种情况下是无法使用二分法的,复杂度是O(n) 31 | // 因此写个for循环最坏也是O(n),那就写个for循环就好了 32 | // 如果你觉得,不是每个情况都是最坏情况,你想用二分法解决不是最坏情况的情况,那你就写一个二分吧。 33 | // 反正面试考的不是你在这个题上会不会用二分法。这个题的考点是你想不想得到最坏情况。 34 | public boolean search(int[] A, int target) { 35 | for (int i = 0; i < A.length; i ++) { 36 | if (A[i] == target) { 37 | return true; 38 | } 39 | } 40 | return false; 41 | } 42 | } 43 | ``` 44 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/singleton.md: -------------------------------------------------------------------------------- 1 | # 单例 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/singleton/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | **单例** 是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。 5 | 6 | 你的任务是设计一个 `getInstance` 方法,对于给定的类,每次调用 `getInstance` 时,都可得到同一个实例。 7 | ### 样例说明 8 | ``` 9 | 在 Java 中: 10 | 11 | A a = A.getInstance(); 12 | A b = A.getInstance(); 13 | 14 | a 应等于 b. 15 | 16 | ``` 17 | ### 参考代码 18 | 创建私有静态成员对象,每次获取时返回此对象。 19 | ```java 20 | class Solution { 21 | 22 | 23 | /** 24 | * @return: The same instance of this class every time 25 | */ 26 | public static Solution instance = null; 27 | public static Solution getInstance() { 28 | if (instance == null) { 29 | instance = new Solution(); 30 | } 31 | return instance; 32 | } 33 | }; 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/trailing-zeros.md: -------------------------------------------------------------------------------- 1 | # 尾部的零 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/trailing-zeros/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |设计一个算法,计算出n阶乘中尾部零的个数
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: 11 9 | 输出: 2 10 | 11 | 样例解释: 12 | 11! = 39916800, 结尾的0有2个。 13 | 14 | 样例 2: 15 | 输入: 5 16 | 输出: 1 17 | 18 | 样例解释: 19 | 5! = 120, 结尾的0有1个。 20 | 21 | ``` 22 | ### 参考代码 23 | 可以将每个数拆分成其素因子的乘积,可以发现,0是由`2*5`产生的,而5的数量一定小于2的数量,因此5的个数决定了结尾0的个数。 24 | 25 | 只要计算n的阶乘中,5这个素因子出现多少次即可。 26 | ```java 27 | class Solution { 28 | /* 29 | * param n: As desciption 30 | * return: An integer, denote the number of trailing zeros in n! 31 | */ 32 | public long trailingZeros(long n) { 33 | long sum = 0; 34 | while (n != 0) { 35 | sum += n / 5; 36 | n /= 5; 37 | } 38 | return sum; 39 | } 40 | }; 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/其他/ugly-number.md: -------------------------------------------------------------------------------- 1 | # 丑数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/ugly-number/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 写一个程序来检测一个整数是不是`丑数`。 5 | 6 | 丑数的定义是,只包含质因子 `2, 3, 5` 的正整数。比如 6, 8 就是丑数,但是 14 不是丑数因为他包含了质因子 7。 7 | ### 样例说明 8 | 例1: 9 | ``` 10 | 输入: num = 8 11 | 输出: true 12 | 解释: 13 | 8=2*2*2 14 | ``` 15 | 例2: 16 | ``` 17 | 输入: num = 14 18 | 输出: false 19 | 解释: 20 | 14=2*7 21 | ``` 22 | 23 | ### 参考代码 24 | 看这个数是否只有2,3,5的因子 25 | ```java 26 | public class Solution { 27 | /** 28 | * @param num an integer 29 | * @return true if num is an ugly number or false 30 | */ 31 | public boolean isUgly(int num) { 32 | if (num <= 0) return false; 33 | if (num == 1) return true; 34 | 35 | while (num >= 2 && num % 2 == 0) num /= 2; 36 | while (num >= 3 && num % 3 == 0) num /= 3; 37 | while (num >= 5 && num % 5 == 0) num /= 5; 38 | 39 | return num == 1; 40 | } 41 | } 42 | ``` 43 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/双指针/linked-list-cycle.md: -------------------------------------------------------------------------------- 1 | # 带环链表 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/linked-list-cycle/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |给定一个链表,判断它是否有环。
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: 21->10->4->5, then tail connects to node index 1(value 10). 9 | 输出: true 10 | 11 | 样例 2: 12 | 输入: 21->10->4->5->null 13 | 输出: false 14 | 15 | ``` 16 | 17 | ### 参考代码 18 | Given a linked list, determine if it has a cycle in it. 19 | 20 | Follow up: 21 | Can you solve it without using extra space? 22 | 23 | 快慢指针的经典题。 24 | 快指针每次走两步,慢指针一次走一步。 25 | 在慢指针进入环之后,快慢指针之间的距离每次缩小1,所以最终能相遇。 26 | ```java 27 | public class Solution { 28 | public Boolean hasCycle(ListNode head) { 29 | if (head == null || head.next == null) { 30 | return false; 31 | } 32 | 33 | ListNode fast, slow; 34 | fast = head.next; 35 | slow = head; 36 | while (fast != slow) { 37 | if(fast==null || fast.next==null) 38 | return false; 39 | fast = fast.next.next; 40 | slow = slow.next; 41 | } 42 | return true; 43 | } 44 | } 45 | ``` 46 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/双指针/window-sum.md: -------------------------------------------------------------------------------- 1 | # 滑动窗口内数的和 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/window-sum/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给你一个大小为n的整型数组和一个大小为k的滑动窗口,将滑动窗口从头移到尾,输出从开始到结束每一个时刻滑动窗口内的数的和。 5 | ### 样例说明 6 | **样例 1** 7 | ``` 8 | 输入:array = [1,2,7,8,5], k = 3 9 | 输出:[10,17,20] 10 | 解析: 11 | 1 + 2 + 7 = 10 12 | 2 + 7 + 8 = 17 13 | 7 + 8 + 5 = 20 14 | ``` 15 | ### 参考代码 16 | ```python 17 | class Solution: 18 | # @param nums {int[]} a list of integers 19 | # @param k {int} size of window 20 | # @return {int[]} the sum of element inside the window at each moving 21 | def winSum(self, nums, k): 22 | # Write your code here 23 | n = len(nums) 24 | if n < k or k <= 0: 25 | return [] 26 | sums = [0] * (n - k + 1) 27 | for i in range(k): 28 | sums[0] += nums[i]; 29 | 30 | for i in range(1, n - k + 1): 31 | sums[i] = sums[i - 1] - nums[i - 1] + nums[i + k - 1] 32 | 33 | return sums 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/字典MAP/string-permutation.md: -------------------------------------------------------------------------------- 1 | # 字符串置换 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/string-permutation/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换。 5 | 6 | 置换的意思是,通过改变顺序可以使得两个字符串相等。 7 | ### 样例说明 8 | 9 | ### 参考代码 10 | 统计字符数量,组成成分一致即可置换得到。 11 | ```java 12 | public class Solution { 13 | /** 14 | * @param A a string 15 | * @param B a string 16 | * @return a boolean 17 | */ 18 | public boolean stringPermutation(String A, String B) { 19 | // Write your code here 20 | int[] cnt = new int[1000]; 21 | for (int i = 0; i < A.length(); ++i) 22 | cnt[(int)A.charAt(i)] += 1; 23 | for (int i = 0; i < B.length(); ++i) 24 | cnt[(int)B.charAt(i)] -= 1; 25 | for (int i = 0; i < 1000; ++i) 26 | if (cnt[i] != 0) 27 | return false; 28 | return true; 29 | } 30 | } 31 | ``` 32 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/字典MAP/unique-characters.md: -------------------------------------------------------------------------------- 1 | # 判断字符串是否没有重复字符 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/unique-characters/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 实现一个算法确定字符串中的字符是否均唯一出现 5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: "abc_____" 10 | 输出: false 11 | ``` 12 | 13 | **样例 2:** 14 | 15 | ``` 16 | 输入: "abc" 17 | 输出: true 18 | ``` 19 | ### 参考代码 20 | 使用一个map或数组标记每个字符是否出现过。 21 | ```java 22 | public class Solution { 23 | /** 24 | * @param str: a string 25 | * @return: a boolean 26 | */ 27 | public boolean isUnique(String str) { 28 | // write your code here 29 | boolean[] char_set = new boolean[256]; 30 | for (int i = 0; i < str.length(); i++) { 31 | int val = str.charAt(i); 32 | if (char_set[val]) return false; 33 | char_set[val] = true; 34 | } 35 | return true; 36 | } 37 | } 38 | ``` 39 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/排序算法/insertion-sort-list.md: -------------------------------------------------------------------------------- 1 | # 链表插入排序 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/insertion-sort-list/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 用插入排序对链表排序 5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: 0->null 9 | 输出: 0->null 10 | 11 | 12 | 样例 2: 13 | 输入: 1->3->2->0->null 14 | 输出 :0->1->2->3->null 15 | 16 | 17 | ``` 18 | 19 | ### 参考代码 20 | Sort a linked list using insertion sort. 21 | 22 | 与数组普通插入排序一样的做法。 23 | ```java 24 | public class Solution { 25 | public ListNode insertionSortList(ListNode head) { 26 | ListNode dummy = new ListNode(0); 27 | // 这个dummy的作用是,把head开头的链表一个个的插入到dummy开头的链表里 28 | // 所以这里不需要dummy.next = head; 29 | 30 | while (head != null) { 31 | ListNode node = dummy; 32 | while (node.next != null && node.next.val < head.val) { 33 | node = node.next; 34 | } 35 | ListNode temp = head.next; 36 | head.next = node.next; 37 | node.next = head; 38 | head = temp; 39 | } 40 | 41 | return dummy.next; 42 | } 43 | } 44 | ``` 45 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/排序算法/recover-rotated-sorted-array.md: -------------------------------------------------------------------------------- 1 | # 恢复旋转排序数组 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/recover-rotated-sorted-array/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个**旋转**排序数组,在原地恢复其排序。(升序) 5 | ### 样例说明 6 | 7 | ### 参考代码 8 | Given a rotated sorted array, recover it to sorted array in-place. 9 | 10 | Example 11 | [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] 12 | ```python 13 | class Solution: 14 | """ 15 | @param nums: The rotated sorted array 16 | @return: The recovered sorted array 17 | """ 18 | def recoverRotatedSortedArray(self, nums): 19 | nums.sort() 20 | ``` 21 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/排序算法/remove-duplicates-from-sorted-list.md: -------------------------------------------------------------------------------- 1 | # 删除排序链表中的重复元素 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/remove-duplicates-from-sorted-list/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个排序链表,删除所有重复的元素每个元素只留下一个。 5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: null 9 | 输出: null 10 | 11 | 12 | 样例 2: 13 | 输入: 1->1->2->null 14 | 输出: 1->2->null 15 | 16 | 样例 3: 17 | 输入: 1->1->2->3->3->null 18 | 输出: 1->2->3->null 19 | 20 | 21 | ``` 22 | ### 参考代码 23 | Given a sorted linked list, delete all duplicates such that each element appear only once. 24 | 25 | For example, 26 | Given 1->1->2, return 1->2. 27 | Given 1->1->2->3->3, return 1->2->3. 28 | 29 | 跳过相同的元素,指向再后一个元素。 30 | ```java 31 | public class Solution { 32 | public ListNode deleteDuplicates(ListNode head) { 33 | if (head == null) { 34 | return null; 35 | } 36 | 37 | ListNode node = head; 38 | while (node.next != null) { 39 | if (node.val == node.next.val) { 40 | node.next = node.next.next; 41 | } else { 42 | node = node.next; 43 | } 44 | } 45 | return head; 46 | } 47 | } 48 | ``` 49 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/排序算法/sort-letters-by-case.md: -------------------------------------------------------------------------------- 1 | # 字符大小写排序 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/sort-letters-by-case/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: "abAcD" 9 | 输出: "acbAD" 10 | 11 | 样例 2: 12 | 输入: "ABC" 13 | 输出: "ABC" 14 | 15 | ``` 16 | ### 参考代码 17 | ```java 18 | public class Solution { 19 | public void sortLetters(char[] chars) { 20 | int i = 0, j = chars.length - 1; 21 | 22 | while (i <= j) { 23 | while (i <= j && Character.isLowerCase(chars[i])) { 24 | i++; 25 | } 26 | while (i <= j && Character.isUpperCase(chars[j])) { 27 | j--; 28 | } 29 | if (i <= j) { 30 | char tmp = chars[i]; 31 | chars[i] = chars[j]; 32 | chars[j] = tmp; 33 | i++; 34 | j--; 35 | } 36 | } 37 | } 38 | } 39 | ``` 40 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /算法思维题/贪心算法/assign-cookies.md: -------------------------------------------------------------------------------- 1 | # 分饼干 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/assign-cookies/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 假设你是一个了不起的家长,准备给你的孩子们一些饼干吃,但是你只能给每个孩子至多一块饼干。每一块饼干$j$都有一个尺寸$s_{j}$;同时每一个孩子$i$都有一个贪吃指数$g_{i}$,代表了能使他满足的最小的饼干尺寸。如果$s_{j} \geq g_{i}$,那么就可以将饼干$j$分给孩子$i$使他得到满足。你的目标是使最多的孩子得到满足,输出这个最大值。 5 | ### 样例说明 6 | **样例1:** 7 | 8 | ``` 9 | 输入:[1,2,3], [1,1] 10 | 输出:1 11 | 说明:你有三个孩子和两块饼干,三个孩子的贪吃指数分别是1,2,3 12 | 虽然你有两块饼干,但是因为它们的大小都为1,你只能满足让贪吃指数为1的孩子满足,因此你应该输出1 13 | ``` 14 | 15 | **样例2:** 16 | 17 | ``` 18 | 输入:[1,2], [1,2,3] 19 | 输出:2 20 | 说明:你有两个孩子和三块饼干,两个孩子的贪吃指数分别是1和2 21 | 这三块饼干的大小足以满足所有的孩子,因此你应该输出2 22 | ``` 23 | ### 参考代码 24 |25 | ```cpp 26 | class Solution { 27 | public: 28 | int findContentChildren(vector
给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
5 | ### 样例说明 6 | **样例1:** 7 | ``` 8 | 输入:["lint", "intl", "inlt", "code"] 9 | 输出:["lint", "inlt", "intl"] 10 | ``` 11 | 12 | **样例 2:** 13 | ``` 14 | 输入:["ab", "ba", "cd", "dc", "e"] 15 | 输出: ["ab", "ba", "cd", "dc"] 16 | ``` 17 | ### 参考代码 18 | Given an array of strings, return all groups of strings that are anagrams. 19 | 20 | Note: All inputs will be in lower-case 21 | ```python 22 | class Solution: 23 | # @param strs: A list of strings 24 | # @return: A list of strings 25 | def anagrams(self, strs): 26 | # write your code here 27 | dict = {} 28 | for word in strs: 29 | sortedword = ''.join(sorted(word)) 30 | dict[sortedword] = [word] if sortedword not in dict else dict[sortedword] + [word] 31 | res = [] 32 | for item in dict: 33 | if len(dict[item]) >= 2: 34 | res += dict[item] 35 | return res 36 | 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/assign-cookies.md: -------------------------------------------------------------------------------- 1 | # 分饼干 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/assign-cookies/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 假设你是一个了不起的家长,准备给你的孩子们一些饼干吃,但是你只能给每个孩子至多一块饼干。每一块饼干$j$都有一个尺寸$s_{j}$;同时每一个孩子$i$都有一个贪吃指数$g_{i}$,代表了能使他满足的最小的饼干尺寸。如果$s_{j} \geq g_{i}$,那么就可以将饼干$j$分给孩子$i$使他得到满足。你的目标是使最多的孩子得到满足,输出这个最大值。 5 | ### 样例说明 6 | **样例1:** 7 | 8 | ``` 9 | 输入:[1,2,3], [1,1] 10 | 输出:1 11 | 说明:你有三个孩子和两块饼干,三个孩子的贪吃指数分别是1,2,3 12 | 虽然你有两块饼干,但是因为它们的大小都为1,你只能满足让贪吃指数为1的孩子满足,因此你应该输出1 13 | ``` 14 | 15 | **样例2:** 16 | 17 | ``` 18 | 输入:[1,2], [1,2,3] 19 | 输出:2 20 | 说明:你有两个孩子和三块饼干,两个孩子的贪吃指数分别是1和2 21 | 这三块饼干的大小足以满足所有的孩子,因此你应该输出2 22 | ``` 23 | ### 参考代码 24 |25 | ```cpp 26 | class Solution { 27 | public: 28 | int findContentChildren(vector
在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i]
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: [3,4,8,5], backpack size=10 9 | 输出: 9 10 | 11 | 样例 2: 12 | 输入: [2,3,5,7], backpack size=12 13 | 输出: 12 14 | 15 | ``` 16 | ### 参考代码 17 | 传统的背包问题解法。二维动态规划。 18 | ```python 19 | class Solution: 20 | """ 21 | @param m: An integer m denotes the size of a backpack 22 | @param A: Given n items with size A[i] 23 | @return: The maximum size 24 | """ 25 | def backPack(self, m, A): 26 | n = len(A) 27 | f = [[False] * (m + 1) for _ in range(n + 1)] 28 | 29 | f[0][0] = True 30 | for i in range(1, n + 1): 31 | f[i][0] = True 32 | for j in range(1, m + 1): 33 | if j >= A[i - 1]: 34 | f[i][j] = f[i - 1][j] or f[i - 1][j - A[i - 1]] 35 | else: 36 | f[i][j] = f[i - 1][j] 37 | 38 | for i in range(m, -1, -1): 39 | if f[n][i]: 40 | return i 41 | return 0 42 | ``` 43 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/best-time-to-buy-and-sell-stock-ii.md: -------------------------------------------------------------------------------- 1 | # 买卖股票的最佳时机 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/best-time-to-buy-and-sell-stock-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个数组 `prices` 表示一支股票每天的价格. 5 | 6 | 你可以完成任意次数的交易, 不过你不能同时参与多个交易 (也就是说, 如果你已经持有这支股票, 在再次购买之前, 你必须先卖掉它). 7 | 8 | 设计一个算法求出最大的利润. 9 | ### 样例说明 10 | **样例 1:** 11 | 12 | ``` 13 | 输入: [2, 1, 2, 0, 1] 14 | 输出: 2 15 | 解释: 16 | 1. 在第 2 天以 1 的价格买入, 然后在第 3 天以 2 的价格卖出, 利润 1 17 | 2. 在第 4 天以 0 的价格买入, 然后在第 5 天以 1 的价格卖出, 利润 1 18 | 总利润 2. 19 | ``` 20 | 21 | **样例 2:** 22 | 23 | ``` 24 | 输入: [4, 3, 2, 1] 25 | 输出: 0 26 | 解释: 不进行任何交易, 利润为0. 27 | ``` 28 | ### 参考代码 29 | 贪心法: 只要相邻的两天股票的价格是上升的, 我们就进行一次交易, 获得一定利润. 30 | 31 | 这样的策略不一定是最小的交易次数, 但是一定会得到最大的利润. 32 | ```java 33 | public class Solution { 34 | public int maxProfit(int[] prices) { 35 | int profit = 0; 36 | for (int i = 0; i < prices.length - 1; i++) { 37 | int diff = prices[i + 1] - prices[i]; 38 | if (diff > 0) { 39 | profit += diff; 40 | } 41 | } 42 | return profit; 43 | } 44 | } 45 | ``` 46 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/binary-tree-leaf-sum.md: -------------------------------------------------------------------------------- 1 | # 二叉树叶子节点之和 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/binary-tree-leaf-sum/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 计算二叉树的叶子节点之和 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入:{1,2,3,4} 9 | 输出:7 10 | 解释: 11 | 1 12 | / \ 13 | 2 3 14 | / 15 | 4 16 | 3+4=7 17 | ``` 18 | **样例 2:** 19 | ``` 20 | 输入:{1,#,3} 21 | 输出:3 22 | 解释: 23 | 1 24 | \ 25 | 3 26 | ``` 27 | ### 参考代码 28 | 递归求和即可 29 | ```python 30 | """ 31 | Definition of TreeNode: 32 | class TreeNode: 33 | def __init__(self, val): 34 | this.val = val 35 | this.left, this.right = None, None 36 | """ 37 | class Solution: 38 | # @param {TreeNode} root the root of the binary tree 39 | # @return {int} an integer 40 | def leafSum(self, root): 41 | # Write your code here 42 | p = [] 43 | self.dfs(root, p) 44 | return sum(p) 45 | def dfs(self, root, p): 46 | if root is None: 47 | return 48 | if root.left is None and root.right is None: 49 | p.append(root.val) 50 | self.dfs(root.left, p) 51 | self.dfs(root.right, p) 52 | ``` 53 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/binary-tree-maximum-path-sum-ii.md: -------------------------------------------------------------------------------- 1 | # 二叉树的最大路径和 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/binary-tree-maximum-path-sum-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给一棵二叉树,找出从根节点出发的路径中,和最大的一条。 5 | 6 | 这条路径可以在任何二叉树中的节点结束,但是必须包含至少一个点(也就是根了)。 7 | 8 | ### 样例说明 9 | **样例 1:** 10 | 11 | ``` 12 | 输入: {1,2,3} 13 | 输出: 4 14 | 解释: 15 | 1 16 | / \ 17 | 2 3 18 | 1+3=4 19 | ``` 20 | 21 | **样例 2:** 22 | 23 | ``` 24 | 输入: {1,-1,-1} 25 | 输出: 1 26 | 解释: 27 | 1 28 | / \ 29 | -1 -1 30 | ``` 31 | ### 参考代码 32 | 如果从动态规划的角度思考, 可以是这样的: 33 | 34 | - 状态: f[i] 表示从节点i往下走得到的最大路径和 35 | - 状态转移方程: f[i] = max{f[lc[i]], f[rc[i]], 0} + val[i] 36 | 37 | 不过这道题目是二叉树, 比经典的数字三角形要简单, 无需记忆化/定义额外空间储存答案. 38 | 39 | 一次递归遍历整棵二叉树即可. 40 | ```java 41 | public class Solution { 42 | /** 43 | * @param root the root of binary tree. 44 | * @return an integer 45 | */ 46 | public int maxPathSum2(TreeNode root) { 47 | if (root == null) { 48 | return Integer.MIN_VALUE; 49 | } 50 | 51 | int left = maxPathSum2(root.left); 52 | int right = maxPathSum2(root.right); 53 | return root.val + Math.max(0, Math.max(left, right)); 54 | } 55 | } 56 | ``` 57 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/bitwise-and-of-numbers-range.md: -------------------------------------------------------------------------------- 1 | # 数字范围内位的与 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/bitwise-and-of-numbers-range/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定范围[m,n],其中0 <= m <= n <= 2147483647,输出此范围内所有数字的按位AND,包括端值。 5 | 6 | 例如,给定范围[5,7],输出返回4。 7 | ### 样例说明 8 | **样例1** 9 | 10 | ``` 11 | 输入: m=5, n=7 12 | 输出: 4 13 | ``` 14 | 15 | **样例2** 16 | 17 | ``` 18 | 输入: m=14, n=15 19 | 输出: 14 20 | ``` 21 | ### 参考代码 22 | 本题中,我们需要得到[m,n]所有元素按位与的结果。举个例子,当m=26,n=30时,它们的二进制表示为为: 23 | **11**010 **11**011 **11**100 **11**101 **11**110 24 | 这个样例的答案是**11**000,易见我们发现我们只需要找到m和n最左边的公共部分即可。 25 | 26 | 每次都将n与n-1按位与,当n的二进制为1010时,1010 & 1001 = 1000,相当于把二进制位的最后一个1去掉了。因此我们不断的做n^n-1的操作,直到n小于m相等即可。 27 | ```java 28 | public class Solution { 29 | /** 30 | * @param m: an Integer 31 | * @param n: an Integer 32 | * @return: the bitwise AND of all numbers in [m,n] 33 | */ 34 | public int rangeBitwiseAnd(int m, int n) { 35 | // Write your code here 36 | while(m < n) { 37 | n = n & (n-1); 38 | } 39 | return n; 40 | 41 | } 42 | } 43 | ``` 44 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/can-place-flowers.md: -------------------------------------------------------------------------------- 1 | # 能否放置花 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/can-place-flowers/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 假设你有一个长花圃,其中有些地块已经被种植了,但是有些地块没有。但是,花不能够在相邻的地块下种植 - 他们会争夺水从而导致两者的死亡。 5 | 6 | 给定一个花圃(用一个包含0和1的数组来表示,其中0代表空,1代表非空),和一个数字**n**,返回n朵新的花在这个花圃上以能否在不违反“无相邻花”的规则种植。 7 | ### 样例说明 8 | **样例1** 9 | ``` 10 | 输入: flowerbed = [1,0,0,0,1], n = 1 11 | 输出: True 12 | ``` 13 | **样例2** 14 | ``` 15 | 输入: flowerbed = [1,0,0,0,1], n = 2 16 | 输出: False 17 | ``` 18 | ### 参考代码 19 | 比较基础的模拟题,按照题目要求从头开始模拟种植即可。 20 | ```java 21 | public class Solution { 22 | public boolean canPlaceFlowers(int[] flowerbed, int n) { 23 | int count = 0; 24 | for (int i = 0; i < flowerbed.length; i++) { 25 | if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) 26 | && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) { 27 | flowerbed[i] = 1; 28 | count++; 29 | } 30 | if (count >= n) { 31 | return true; 32 | } 33 | } 34 | return false; 35 | } 36 | } 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/char-to-integer.md: -------------------------------------------------------------------------------- 1 | # 字符转整数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/char-to-integer/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 将字符转换为一个整数。你可以假设字符是ASCII码,也就是说转换后的整数在0~255之间。 5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: 'a' 9 | 输出: 97 10 | 11 | 样例解释: 12 | 返回ASCII码中对应的数字. 13 | 14 | 样例 2: 15 | 输入: '%' 16 | 输出: 37 17 | 18 | 样例解释: 19 | 返回ASCII码中对应的数字. 20 | 21 | ``` 22 | ### 参考代码 23 | 类型转换 24 | ```java 25 | public class Solution { 26 | /** 27 | * @param character a character 28 | * @return an integer 29 | */ 30 | public int charToInteger(char character) { 31 | return (int) character; 32 | } 33 | } 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/climbing-stairs-ii.md: -------------------------------------------------------------------------------- 1 | # 爬楼梯 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/climbing-stairs-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 一个小孩爬一个 n 层台阶的楼梯。他可以每次跳 1 步, 2 步 或者 3 步。实现一个方法来统计总共有多少种不同的方式爬到最顶层的台阶。 5 | 6 | ### 样例说明 7 | 8 | ### 参考代码 9 | 本题采用动态规划的思想,每一步的答案可以由上三步转移而来。 10 | 初始状态和状态转移方程如下: 11 | $f_0=1,\ f_1 =1,\ f_2=2,f_n=f_{n-1}+f_{n-2}+f_{n-3}(n \geq 3)$ 12 | ```java 13 | public class Solution { 14 | /** 15 | * @param n an integer 16 | * @return an integer 17 | */ 18 | public int climbStairs2(int n) { 19 | int[] f = new int[n+1]; 20 | f[0] = 1; 21 | for (int i = 0; i <= n; i++) 22 | for (int j = 1; j <= 3; j++) { 23 | if (i >= j) { 24 | f[i] += f[i-j]; 25 | } 26 | } 27 | return f[n]; 28 | } 29 | } 30 | ``` 31 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/closest-number-in-sorted-array.md: -------------------------------------------------------------------------------- 1 | # 排序数组中最接近元素 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/closest-number-in-sorted-array/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 在一个排好序的数组 *A* 中找到 *i* 使得 *A[i]* 最接近 *target* 5 | ### 样例说明 6 | 样例1: 7 | ``` 8 | 输入:[1, 2, 3] ,target = 2 9 | 输出: 1. 10 | ``` 11 | 样例2: 12 | ``` 13 | 输入:[1, 4, 6], target = 3 14 | 输出: 1. 15 | ``` 16 | 样例3: 17 | ``` 18 | 输入:[1, 4, 6],target = 5, 19 | 输出: 1 或 2. 20 | ``` 21 | ### 参考代码 22 | 利用二分查找的思想即可 23 | ```python 24 | class Solution: 25 | # @param {int[]} A an integer array sorted in ascending order 26 | # @param {int} target an integer 27 | # @return {int} an integer 28 | def closestNumber(self, A, target): 29 | if not A: 30 | return -1 31 | start, end = 0, len(A) - 1 32 | while start + 1 < end: 33 | mid = (start + end) / 2 34 | if A[mid] == target: 35 | return mid 36 | elif A[mid] > target: 37 | end = mid 38 | else: 39 | start = mid 40 | if A[end] - target > target - A[start]: 41 | return start 42 | else: 43 | return end 44 | ``` 45 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/coins-in-a-line.md: -------------------------------------------------------------------------------- 1 | # 硬币排成线 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/coins-in-a-line/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 有 `n` 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。 5 | 6 | 请判定 **先手玩家** 必胜还是必败? 7 | 8 | 若必胜, 返回 `true`, 否则返回 `false`. 9 | ### 样例说明 10 | **样例 1:** 11 | 12 | ``` 13 | 输入: 1 14 | 输出: true 15 | ``` 16 | 17 | **样例 2:** 18 | 19 | ``` 20 | 输入: 4 21 | 输出: true 22 | 解释: 23 | 先手玩家第一轮拿走一个硬币, 此时还剩三个. 24 | 这时无论后手玩家拿一个还是两个, 下一次先手玩家都可以把剩下的硬币拿完. 25 | ``` 26 | ### 参考代码 27 | 可以证明, 当硬币数目是3的倍数的时候, 先手玩家必败, 否则他必胜. 28 | 29 | 当硬币数目是3的倍数时, 每一轮先手者拿a个, 后手者拿3-a个即可, 后手必胜. 30 | 31 | 若不是3的倍数, 先手者可以拿1或2个, 此时剩余硬币个数就变成了3的倍数. 32 | ```cpp 33 | class Solution { 34 | public: 35 | /** 36 | * @param n: An integer 37 | * @return: A boolean which equals to true if the first player will win 38 | */ 39 | bool firstWillWin(int n) { 40 | return n % 3; 41 | } 42 | }; 43 | ``` 44 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/combination-sum-iv.md: -------------------------------------------------------------------------------- 1 | # 组合总和 IV 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/combination-sum-iv/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出一个都是正整数的数组 `nums`,其中没有重复的数。从中找出所有的和为 `target` 的组合个数。 5 | ### 样例说明 6 | **样例1** 7 | 8 | ``` 9 | 输入: nums = [1, 2, 4] 和 target = 4 10 | 输出: 6 11 | 解释: 12 | 可能的所有组合有: 13 | [1, 1, 1, 1] 14 | [1, 1, 2] 15 | [1, 2, 1] 16 | [2, 1, 1] 17 | [2, 2] 18 | [4] 19 | ``` 20 | 21 | **样例2** 22 | 23 | ``` 24 | 输入: nums = [1, 2] 和 target = 4 25 | 输出: 5 26 | 解释: 27 | 可能的所有组合有: 28 | [1, 1, 1, 1] 29 | [1, 1, 2] 30 | [1, 2, 1] 31 | [2, 1, 1] 32 | [2, 2] 33 | ``` 34 | ### 参考代码 35 | ```cpp 36 | class Solution { 37 | public: 38 | int combinationSum4(vector给定一个链表,判断它是否有环。
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: 21->10->4->5, then tail connects to node index 1(value 10). 9 | 输出: true 10 | 11 | 样例 2: 12 | 输入: 21->10->4->5->null 13 | 输出: false 14 | 15 | ``` 16 | 17 | ### 参考代码 18 | Given a linked list, determine if it has a cycle in it. 19 | 20 | Follow up: 21 | Can you solve it without using extra space? 22 | 23 | 快慢指针的经典题。 24 | 快指针每次走两步,慢指针一次走一步。 25 | 在慢指针进入环之后,快慢指针之间的距离每次缩小1,所以最终能相遇。 26 | ```java 27 | public class Solution { 28 | public Boolean hasCycle(ListNode head) { 29 | if (head == null || head.next == null) { 30 | return false; 31 | } 32 | 33 | ListNode fast, slow; 34 | fast = head.next; 35 | slow = head; 36 | while (fast != slow) { 37 | if(fast==null || fast.next==null) 38 | return false; 39 | fast = fast.next.next; 40 | slow = slow.next; 41 | } 42 | return true; 43 | } 44 | } 45 | ``` 46 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/longest-common-subsequence.md: -------------------------------------------------------------------------------- 1 | # 最长公共子序列 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/longest-common-subsequence/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: "ABCD" and "EDCA" 9 | 输出: 1 10 | 11 | 解释: 12 | LCS 是 'A' 或 'D' 或 'C' 13 | 14 | 15 | 样例 2: 16 | 输入: "ABCD" and "EACB" 17 | 输出: 2 18 | 19 | 解释: 20 | LCS 是 "AC" 21 | ``` 22 | 23 | ### 参考代码 24 | 使用九章算法强化班和动态规划专题班中讲的匹配型动态规划 25 | ```python 26 | class Solution: 27 | """ 28 | @param A: A string 29 | @param B: A string 30 | @return: The length of longest common subsequence of A and B 31 | """ 32 | def longestCommonSubsequence(self, A, B): 33 | n, m = len(A), len(B) 34 | f = [[0] * (m + 1) for i in range(n + 1)] 35 | for i in range(1, n + 1): 36 | for j in range(1, m + 1): 37 | f[i][j] = max(f[i][j - 1], f[i - 1][j]) 38 | if A[i - 1] == B[j - 1]: 39 | f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1) 40 | return f[n][m] 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/longest-words.md: -------------------------------------------------------------------------------- 1 | # 最长单词 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/longest-words/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给一个词典,找出其中所有最长的单词。 5 | ### 样例说明 6 | 7 | ### 参考代码 8 | ```java 9 | class Solution { 10 | /** 11 | * @param dictionary: an array of strings 12 | * @return: an arraylist of strings 13 | */ 14 | ArrayList给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。
合并两个有序升序的整数数组A和B变成一个新的数组。新数组也要有序。
5 | ### 样例说明 6 | 7 | ### 参考代码 8 | ```cpp 9 | class Solution { 10 | public: 11 | /** 12 | * @param A and B: sorted integer array A and B. 13 | * @return: A new sorted integer array 14 | */ 15 | vector给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度。
元素的顺序可以改变,并且对新的数组不会有影响。
5 | ### 样例说明 6 | 7 | ### 参考代码 8 | Given an array and a value, remove all instances of that value in place and return the new length. 9 | 10 | The order of elements can be changed. It doesn't matter what you leave beyond the new length. 11 | 12 | 从前往后,遇到等于value的就跳过,把后面的元素填到前面的位置里。 13 | ```java 14 | public class Solution { 15 | public int removeElement(int[] A, int elem) { 16 | int i = 0; 17 | int pointer = A.length - 1; 18 | while(i <= pointer){ 19 | if(A[i] == elem){ 20 | A[i] = A[pointer]; 21 | pointer--; 22 | } else { 23 | i++; 24 | } 25 | } 26 | return pointer + 1; 27 | } 28 | } 29 | ``` 30 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/reverse-3-digit-integer.md: -------------------------------------------------------------------------------- 1 | # 反转一个3位整数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/reverse-3-digit-integer/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 反转一个只有3位数的整数。 5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: number = 123 10 | 输出: 321 11 | ``` 12 | **样例 2:** 13 | 14 | ``` 15 | 输入: number = 900 16 | 输出: 9 17 | ``` 18 | 19 | ### 参考代码 20 | 硅谷求职算法集训营上课版本 21 | ```java 22 | public class Solution { 23 | /* 24 | * @param number: A 3-digit number. 25 | * @return: Reversed number. 26 | */ 27 | public int reverseInteger(int number) { 28 | // write your code here 29 | //获得个位数 30 | int num1 = number % 10; 31 | //获得十位数 32 | int num2 = (number / 10) % 10; 33 | //获得百位数 34 | int num3 = ((number / 10) / 10) % 10; 35 | //相加 36 | return num3 + num2 * 10 + num1 * 100; 37 | } 38 | } 39 | ``` 40 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/reverse-bits.md: -------------------------------------------------------------------------------- 1 | # 颠倒二进制位 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/reverse-bits/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 颠倒给定的 32 位无符号整数的二进制位。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入: 00000010100101000001111010011100 9 | 输出: 00111001011110000010100101000000 10 | 解释: 11 | 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596, 12 | 因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。 13 | ``` 14 | **样例 2:** 15 | ``` 16 | 输入:11111111111111111111111111111101 17 | 输出:10111111111111111111111111111111 18 | 解释: 19 | 输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293, 20 | 因此返回 3221225471 其二进制表示形式为 10101111110010110010011101101001。 21 | ``` 22 | ### 参考代码 23 | 分离出输入数字的每一位,最低位左移31bit,次低位左移30bit,以此类推。 24 | ```java 25 | public class Solution { 26 | // you need treat n as an unsigned value 27 | public long reverseBits(int n) { 28 | int reversed = 0; 29 | for (int i = 0; i < 32; i++) { 30 | reversed = (reversed << 1) | (n & 1); 31 | n = (n >> 1); 32 | } 33 | return reversed; 34 | } 35 | } 36 | ``` 37 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/reverse-linked-list.md: -------------------------------------------------------------------------------- 1 | # 翻转链表 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/reverse-linked-list/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |翻转一个链表
5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: 1->2->3->null 10 | 输出: 3->2->1->null 11 | ``` 12 | 13 | **样例 2:** 14 | 15 | ``` 16 | 输入: 1->2->3->4->null 17 | 输出: 4->3->2->1->null 18 | ``` 19 | ### 参考代码 20 | http://www.lintcode.com/zh-cn/problem/reverse-linked-list/ 21 | ```python 22 | class Solution: 23 | 24 | def reverse(self, head): 25 | #curt表示前继节点 26 | curt = None 27 | while head != None: 28 | #temp记录下一个节点,head是当前节点 29 | temp = head.next 30 | head.next = curt 31 | curt = head 32 | head = temp 33 | return curt 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/search-in-rotated-sorted-array-ii.md: -------------------------------------------------------------------------------- 1 | # 搜索旋转排序数组 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |跟进“搜索旋转排序数组”,假如有重复元素又将如何?
是否会影响运行时间复杂度?
如何影响?
为何会影响?
写出一个函数判断给定的目标值是否出现在数组中。
5 | ### 样例说明 6 | 例1: 7 | ``` 8 | 输入: 9 | [] 10 | 1 11 | 输出: 12 | false 13 | ``` 14 | 例2: 15 | ``` 16 | 输入: 17 | [3,4,4,5,7,0,1,2] 18 | 4 19 | 输出: 20 | true 21 | ``` 22 | 23 | ### 参考代码 24 | 由于出现重复元素,故不能使用二分法。 25 | 所以暴力比较即可。 26 | ```java 27 | public class Solution { 28 | // 这个问题在面试中不会让实现完整程序 29 | // 只需要举出能够最坏情况的数据是 [1,1,1,1... 1] 里有一个0即可。 30 | // 在这种情况下是无法使用二分法的,复杂度是O(n) 31 | // 因此写个for循环最坏也是O(n),那就写个for循环就好了 32 | // 如果你觉得,不是每个情况都是最坏情况,你想用二分法解决不是最坏情况的情况,那你就写一个二分吧。 33 | // 反正面试考的不是你在这个题上会不会用二分法。这个题的考点是你想不想得到最坏情况。 34 | public boolean search(int[] A, int target) { 35 | for (int i = 0; i < A.length; i ++) { 36 | if (A[i] == target) { 37 | return true; 38 | } 39 | } 40 | return false; 41 | } 42 | } 43 | ``` 44 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/second-max-of-array.md: -------------------------------------------------------------------------------- 1 | # 数组第二大数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/second-max-of-array/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 在数组中找到第二大的数。 5 | ### 样例说明 6 | 例1: 7 | ``` 8 | 输入:[1,3,2,4] 9 | 输出:3 10 | ``` 11 | 例2: 12 | ``` 13 | 输入:[1,1,2,2] 14 | 输出:2 15 | ``` 16 | ### 参考代码 17 | 维护最大数和此大数即可 18 | ```java 19 | public class Solution { 20 | /** 21 | * @param nums: An integer array. 22 | * @return: The second max number in the array. 23 | */ 24 | public int secondMax(int[] nums) { 25 | int max = Math.max(nums[0], nums[1]); 26 | int second = Math.min(nums[0], nums[1]); 27 | 28 | for (int i = 2; i < nums.length; i++) { 29 | if (nums[i] > max) { 30 | second = max; 31 | max = nums[i]; 32 | } else if (nums[i] > second) { 33 | second = nums[i]; 34 | } 35 | } 36 | 37 | return second; 38 | } 39 | } 40 | ``` 41 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/simplify-path.md: -------------------------------------------------------------------------------- 1 | # 简化路径 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/simplify-path/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个文件的绝对路径(Unix-style),请进行路径简化。 5 | 6 | Unix中, `.` 表示当前目录, `..` 表示父目录。 7 | 8 | 结果必须以 `/` 开头,并且两个目录名之间有且只有一个 `/`。最后一个目录名(如果存在)后不能出现 `/` 。你需要保证结果是正确表示路径的最短的字符串。 9 | ### 样例说明 10 | **样例 1:** 11 | 12 | ``` 13 | 输入: "/home/" 14 | 输出: "/home" 15 | ``` 16 | 17 | **样例 2:** 18 | 19 | ``` 20 | 输入: "/a/./../../c/" 21 | 输出: "/c" 22 | 解释: "/" 没有上级目录, "/../" 的结果就是 "/". 23 | ``` 24 | ### 参考代码 25 | 用栈处理即可. 26 | 27 | 将原字符串以 `'/'` 分隔, 然后遍历: 28 | 29 | - 遇到正常的目录名, 入栈 30 | - 遇到 `'.'` 或 空名称 (对应 `"//"`) 则忽略 31 | - 遇到 `".."` 则从栈顶弹出一个元素 (如果栈为空则不弹栈, 对应 `"/../"`) 32 | 33 | 最后将栈中的元素以 `'/'` 连接得到结果. 34 | ```python 35 | class Solution: 36 | """ 37 | @param path: the original path 38 | @return: the simplified path 39 | """ 40 | def simplifyPath(self, path): 41 | path = path.split('/') 42 | stack = [] 43 | for i in path: 44 | if i == '..': 45 | if len(stack): 46 | stack.pop() 47 | elif i != '.' and i != '': 48 | stack.append(i) 49 | return '/' + '/'.join(stack) 50 | ``` 51 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/single-number.md: -------------------------------------------------------------------------------- 1 | # 落单的数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/single-number/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出 `2 * n + 1`个数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入:[1,1,2,2,3,4,4] 9 | 输出:3 10 | 解释: 11 | 仅3出现一次 12 | ``` 13 | **样例 2:** 14 | ``` 15 | 输入:[0,0,1] 16 | 输出:1 17 | 解释: 18 | 仅1出现一次 19 | ``` 20 | 21 | ### 参考代码 22 | Given an array of integers, every element appears twice except for one. Find that single one. 23 | 考点: 24 | * 异或位运算 25 | 26 | Note: 27 | Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 28 | 异或运算具有很好的性质,相同数字异或运算后为0,并且具有交换律和结合律,故将所有数字异或运算后即可得到只出现一次的数字。 29 | ```python 30 | class Solution: 31 | """ 32 | @param A : an integer array 33 | @return : a integer 34 | """ 35 | def singleNumber(self, A): 36 | # write your code here 37 | ans = 0; 38 | for x in A: 39 | ans = ans ^ x 40 | return ans 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/singleton.md: -------------------------------------------------------------------------------- 1 | # 单例 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/singleton/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | **单例** 是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。 5 | 6 | 你的任务是设计一个 `getInstance` 方法,对于给定的类,每次调用 `getInstance` 时,都可得到同一个实例。 7 | ### 样例说明 8 | ``` 9 | 在 Java 中: 10 | 11 | A a = A.getInstance(); 12 | A b = A.getInstance(); 13 | 14 | a 应等于 b. 15 | 16 | ``` 17 | ### 参考代码 18 | 创建私有静态成员对象,每次获取时返回此对象。 19 | ```java 20 | class Solution { 21 | 22 | 23 | /** 24 | * @return: The same instance of this class every time 25 | */ 26 | public static Solution instance = null; 27 | public static Solution getInstance() { 28 | if (instance == null) { 29 | instance = new Solution(); 30 | } 31 | return instance; 32 | } 33 | }; 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/sort-letters-by-case.md: -------------------------------------------------------------------------------- 1 | # 字符大小写排序 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/sort-letters-by-case/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: "abAcD" 9 | 输出: "acbAD" 10 | 11 | 样例 2: 12 | 输入: "ABC" 13 | 输出: "ABC" 14 | 15 | ``` 16 | ### 参考代码 17 | ```java 18 | public class Solution { 19 | public void sortLetters(char[] chars) { 20 | int i = 0, j = chars.length - 1; 21 | 22 | while (i <= j) { 23 | while (i <= j && Character.isLowerCase(chars[i])) { 24 | i++; 25 | } 26 | while (i <= j && Character.isUpperCase(chars[j])) { 27 | j--; 28 | } 29 | if (i <= j) { 30 | char tmp = chars[i]; 31 | chars[i] = chars[j]; 32 | chars[j] = tmp; 33 | i++; 34 | j--; 35 | } 36 | } 37 | } 38 | } 39 | ``` 40 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/sqrtx-ii.md: -------------------------------------------------------------------------------- 1 | # 对x开根II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/sqrtx-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 实现 `double sqrt(double x)` 并且 `x >= 0`。 5 | 计算并返回x开根后的值。 6 | ### 样例说明 7 | 例1: 8 | ``` 9 | 输入: n = 2 10 | 输出: 1.41421356 11 | ``` 12 | 例2: 13 | ``` 14 | 输入: n = 3 15 | 输出: 1.73205081 16 | ``` 17 | 18 | ### 参考代码 19 | ```python 20 | class Solution: 21 | """ 22 | @param: x: a double 23 | @return: the square root of x 24 | """ 25 | def sqrt(self, x): 26 | if x >= 1: 27 | start, end = 1, x 28 | else: 29 | start, end = x, 1 30 | 31 | while end - start > 1e-10: 32 | mid = (start + end) / 2 33 | if mid * mid < x: 34 | start = mid 35 | else: 36 | end = mid 37 | 38 | return start 39 | ``` 40 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/string-permutation.md: -------------------------------------------------------------------------------- 1 | # 字符串置换 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/string-permutation/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换。 5 | 6 | 置换的意思是,通过改变顺序可以使得两个字符串相等。 7 | ### 样例说明 8 | 9 | ### 参考代码 10 | 统计字符数量,组成成分一致即可置换得到。 11 | ```java 12 | public class Solution { 13 | /** 14 | * @param A a string 15 | * @param B a string 16 | * @return a boolean 17 | */ 18 | public boolean stringPermutation(String A, String B) { 19 | // Write your code here 20 | int[] cnt = new int[1000]; 21 | for (int i = 0; i < A.length(); ++i) 22 | cnt[(int)A.charAt(i)] += 1; 23 | for (int i = 0; i < B.length(); ++i) 24 | cnt[(int)B.charAt(i)] -= 1; 25 | for (int i = 0; i < 1000; ++i) 26 | if (cnt[i] != 0) 27 | return false; 28 | return true; 29 | } 30 | } 31 | ``` 32 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/subarray-sum.md: -------------------------------------------------------------------------------- 1 | # 子数组之和 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/subarray-sum/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个整数数组,找到和为 $0$ 的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置 5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: [-3, 1, 2, -3, 4] 10 | 输出: [0,2] 或 [1,3] 11 | 样例解释: 返回任意一段和为0的区间即可。 12 | ``` 13 | 14 | **样例 2:** 15 | 16 | ``` 17 | 输入: [-3, 1, -4, 2, -3, 4] 18 | 输出: [1,5] 19 | ``` 20 | ### 参考代码 21 | 用九章算法班里讲过的前缀和 22 | 23 | 某一段[l, r]的和为0, 则其对应presum[l-1] = presum[r]. 24 | presum 为数组前缀和。只要保存每个前缀和,找是否有相同的前缀和即可。 25 | ```python 26 | class Solution: 27 | """ 28 | @param nums: A list of integers 29 | @return: A list of integers includes the index of the first number and the index of the last number 30 | """ 31 | def subarraySum(self, nums): 32 | prefix_hash = {0: -1} 33 | prefix_sum = 0 34 | for i, num in enumerate(nums): 35 | prefix_sum += num 36 | if prefix_sum in prefix_hash: 37 | return prefix_hash[prefix_sum] + 1, i 38 | prefix_hash[prefix_sum] = i 39 | 40 | return -1, -1 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/subsets-ii.md: -------------------------------------------------------------------------------- 1 | # 子集 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/subsets-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个可能具有重复数字的列表,返回其所有可能的子集。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入:[0] 9 | 输出: 10 | [ 11 | [], 12 | [0] 13 | ] 14 | ``` 15 | **样例 2:** 16 | ``` 17 | 输入:[1,2,2] 18 | 输出: 19 | [ 20 | [2], 21 | [1], 22 | [1,2,2], 23 | [2,2], 24 | [1,2], 25 | [] 26 | ] 27 | ``` 28 | ### 参考代码 29 | 九章算法班中的方法 30 | ```python 31 | class Solution: 32 | """ 33 | @param nums: A set of numbers. 34 | @return: A list of lists. All valid subsets. 35 | """ 36 | def subsetsWithDup(self, nums): 37 | nums = sorted(nums) 38 | subsets = [] 39 | self.dfs(nums, 0, [], subsets) 40 | return subsets 41 | 42 | def dfs(self, nums, index, subset, subsets): 43 | subsets.append(list(subset)) 44 | 45 | for i in range(index, len(nums)): 46 | if i > index and nums[i] == nums[i - 1]: 47 | continue 48 | subset.append(nums[i]) 49 | self.dfs(nums, i + 1, subset, subsets) 50 | subset.pop() 51 | ``` 52 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/swap-bits.md: -------------------------------------------------------------------------------- 1 | # 交换奇偶二进制位 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/swap-bits/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 设计一个方法,用尽可能少的指令,将一个整数中奇数数位和偶数数位的数字交换 (如,数位 0 和数位 1 交换,数位 2 和数位 3 交换,等等)。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入:0 9 | 输出:0 10 | 解释: 11 | 0 = 0(2) -> 0(2) = 0 12 | ``` 13 | **样例 2:** 14 | ``` 15 | 输入:5 16 | 输出:10 17 | 解释: 18 | 5 = 101(2) -> 1010(2) = 10 19 | ``` 20 | ### 参考代码 21 | ```cpp 22 | class Solution { 23 | public: 24 | /** 25 | * @param x an integer 26 | * @return an integer 27 | */ 28 | int swapOddEvenBits(int x) { 29 | // Write your code here 30 | return ( ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1) ); 31 | } 32 | }; 33 | ``` 34 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/swap-two-integers-in-array.md: -------------------------------------------------------------------------------- 1 | # 交换数组两个元素 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/swap-two-integers-in-array/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给你一个数组和两个索引,交换下标为这两个索引的数字 5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: [1, 2, 3, 4], index1 = 2, index2 = 3 10 | 输出: 交换后你的数组应该是[1, 2, 4, 3], 不需要返回任何值,只要就地对数组进行交换即可。 11 | 样例解释: 就地交换,不需要返回值。 12 | ``` 13 | **样例 2:** 14 | 15 | ``` 16 | 输入: [1, 2, 2, 2], index1 = 0, index2 = 3 17 | 输出: 交换后你的数组应该是[2, 2, 2, 1], 不需要返回任何值,只要就地对数组进行交换即可。 18 | 样例解释: 就地交换,不需要返回值。 19 | ``` 20 | ### 参考代码 21 | 模拟即可 22 | ```java 23 | public class Solution { 24 | /** 25 | * @param A an integer array 26 | * @param index1 the first index 27 | * @param index2 the second index 28 | * @return void 29 | */ 30 | public void swapIntegers(int[] A, int index1, int index2) { 31 | // Write your code here 32 | int tmp = A[index1]; 33 | A[index1] = A[index2]; 34 | A[index2] = tmp; 35 | } 36 | } 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/swap-without-extra-variable-only-c.md: -------------------------------------------------------------------------------- 1 | # 交换不借助额外变量(仅C++) 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/swap-without-extra-variable-only-c/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出两个变量,x 和 y ,不使用第三个变量交换这两个变量的值。 5 | ### 样例说明 6 | **样例 1:** 7 | ``` 8 | 输入 : x = 10, y = 5 9 | 输出 : x = 5, y = 10 10 | ``` 11 | ### 参考代码 12 | 通过位运算实现。 13 | ```cpp 14 | class Solution { 15 | public: 16 | /** 17 | * @param x an integer 18 | * @param y an integer 19 | * @return nothing 20 | */ 21 | void swap(int &x, int &y) { 22 | // Write your code here 23 | x = x ^ y; 24 | y = x ^ y; 25 | x = x ^ y; 26 | } 27 | }; 28 | ``` 29 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/trailing-zeros.md: -------------------------------------------------------------------------------- 1 | # 尾部的零 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/trailing-zeros/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |设计一个算法,计算出n阶乘中尾部零的个数
5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: 11 9 | 输出: 2 10 | 11 | 样例解释: 12 | 11! = 39916800, 结尾的0有2个。 13 | 14 | 样例 2: 15 | 输入: 5 16 | 输出: 1 17 | 18 | 样例解释: 19 | 5! = 120, 结尾的0有1个。 20 | 21 | ``` 22 | ### 参考代码 23 | 可以将每个数拆分成其素因子的乘积,可以发现,0是由`2*5`产生的,而5的数量一定小于2的数量,因此5的个数决定了结尾0的个数。 24 | 25 | 只要计算n的阶乘中,5这个素因子出现多少次即可。 26 | ```java 27 | class Solution { 28 | /* 29 | * param n: As desciption 30 | * return: An integer, denote the number of trailing zeros in n! 31 | */ 32 | public long trailingZeros(long n) { 33 | long sum = 0; 34 | while (n != 0) { 35 | sum += n / 5; 36 | n /= 5; 37 | } 38 | return sum; 39 | } 40 | }; 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/ugly-number-ii.md: -------------------------------------------------------------------------------- 1 | # 丑数 II 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/ugly-number-ii/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 设计一个算法,找出只含素因子`2`,`3`,`5` 的第 *n* 小的数。 5 | 6 | 符合条件的数如:`1, 2, 3, 4, 5, 6, 8, 9, 10, 12...` 7 | 8 | ### 样例说明 9 | **样例 1:** 10 | ``` 11 | 输入:9 12 | 输出:10 13 | ``` 14 | **样例 2:** 15 | ``` 16 | 输入:1 17 | 输出:1 18 | ``` 19 | ### 参考代码 20 | 使用九章算法班中讲的 heap 21 | 时间复杂度 $O(nlogn)$ 22 | ```python 23 | import heapq 24 | 25 | class Solution: 26 | """ 27 | @param {int} n an integer. 28 | @return {int} the nth prime number as description. 29 | """ 30 | def nthUglyNumber(self, n): 31 | heap = [1] 32 | visited = set([1]) 33 | 34 | val = None 35 | for i in range(n): 36 | val = heapq.heappop(heap) 37 | for factor in [2, 3, 5]: 38 | if val * factor not in visited: 39 | visited.add(val * factor) 40 | heapq.heappush(heap, val * factor) 41 | 42 | return val 43 | ``` 44 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/ugly-number.md: -------------------------------------------------------------------------------- 1 | # 丑数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/ugly-number/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 写一个程序来检测一个整数是不是`丑数`。 5 | 6 | 丑数的定义是,只包含质因子 `2, 3, 5` 的正整数。比如 6, 8 就是丑数,但是 14 不是丑数因为他包含了质因子 7。 7 | ### 样例说明 8 | 例1: 9 | ``` 10 | 输入: num = 8 11 | 输出: true 12 | 解释: 13 | 8=2*2*2 14 | ``` 15 | 例2: 16 | ``` 17 | 输入: num = 14 18 | 输出: false 19 | 解释: 20 | 14=2*7 21 | ``` 22 | 23 | ### 参考代码 24 | 看这个数是否只有2,3,5的因子 25 | ```java 26 | public class Solution { 27 | /** 28 | * @param num an integer 29 | * @return true if num is an ugly number or false 30 | */ 31 | public boolean isUgly(int num) { 32 | if (num <= 0) return false; 33 | if (num == 1) return true; 34 | 35 | while (num >= 2 && num % 2 == 0) num /= 2; 36 | while (num >= 3 && num % 3 == 0) num /= 3; 37 | while (num >= 5 && num % 5 == 0) num /= 5; 38 | 39 | return num == 1; 40 | } 41 | } 42 | ``` 43 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/unique-characters.md: -------------------------------------------------------------------------------- 1 | # 判断字符串是否没有重复字符 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/unique-characters/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 实现一个算法确定字符串中的字符是否均唯一出现 5 | ### 样例说明 6 | **样例 1:** 7 | 8 | ``` 9 | 输入: "abc_____" 10 | 输出: false 11 | ``` 12 | 13 | **样例 2:** 14 | 15 | ``` 16 | 输入: "abc" 17 | 输出: true 18 | ``` 19 | ### 参考代码 20 | 使用一个map或数组标记每个字符是否出现过。 21 | ```java 22 | public class Solution { 23 | /** 24 | * @param str: a string 25 | * @return: a boolean 26 | */ 27 | public boolean isUnique(String str) { 28 | // write your code here 29 | boolean[] char_set = new boolean[256]; 30 | for (int i = 0; i < str.length(); i++) { 31 | int val = str.charAt(i); 32 | if (char_set[val]) return false; 33 | char_set[val] = true; 34 | } 35 | return true; 36 | } 37 | } 38 | ``` 39 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /题解/window-sum.md: -------------------------------------------------------------------------------- 1 | # 滑动窗口内数的和 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/window-sum/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给你一个大小为n的整型数组和一个大小为k的滑动窗口,将滑动窗口从头移到尾,输出从开始到结束每一个时刻滑动窗口内的数的和。 5 | ### 样例说明 6 | **样例 1** 7 | ``` 8 | 输入:array = [1,2,7,8,5], k = 3 9 | 输出:[10,17,20] 10 | 解析: 11 | 1 + 2 + 7 = 10 12 | 2 + 7 + 8 = 17 13 | 7 + 8 + 5 = 20 14 | ``` 15 | ### 参考代码 16 | ```python 17 | class Solution: 18 | # @param nums {int[]} a list of integers 19 | # @param k {int} size of window 20 | # @return {int[]} the sum of element inside the window at each moving 21 | def winSum(self, nums, k): 22 | # Write your code here 23 | n = len(nums) 24 | if n < k or k <= 0: 25 | return [] 26 | sums = [0] * (n - k + 1) 27 | for i in range(k): 28 | sums[0] += nums[i]; 29 | 30 | for i in range(1, n - k + 1): 31 | sums[i] = sums[i - 1] - nums[i - 1] + nums[i + k - 1] 32 | 33 | return sums 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /高频/anagrams.md: -------------------------------------------------------------------------------- 1 | # 乱序字符串 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/anagrams/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 |给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
5 | ### 样例说明 6 | **样例1:** 7 | ``` 8 | 输入:["lint", "intl", "inlt", "code"] 9 | 输出:["lint", "inlt", "intl"] 10 | ``` 11 | 12 | **样例 2:** 13 | ``` 14 | 输入:["ab", "ba", "cd", "dc", "e"] 15 | 输出: ["ab", "ba", "cd", "dc"] 16 | ``` 17 | ### 参考代码 18 | Given an array of strings, return all groups of strings that are anagrams. 19 | 20 | Note: All inputs will be in lower-case 21 | ```python 22 | class Solution: 23 | # @param strs: A list of strings 24 | # @return: A list of strings 25 | def anagrams(self, strs): 26 | # write your code here 27 | dict = {} 28 | for word in strs: 29 | sortedword = ''.join(sorted(word)) 30 | dict[sortedword] = [word] if sortedword not in dict else dict[sortedword] + [word] 31 | res = [] 32 | for item in dict: 33 | if len(dict[item]) >= 2: 34 | res += dict[item] 35 | return res 36 | 37 | ``` 38 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /高频/char-to-integer.md: -------------------------------------------------------------------------------- 1 | # 字符转整数 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/char-to-integer/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 将字符转换为一个整数。你可以假设字符是ASCII码,也就是说转换后的整数在0~255之间。 5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: 'a' 9 | 输出: 97 10 | 11 | 样例解释: 12 | 返回ASCII码中对应的数字. 13 | 14 | 样例 2: 15 | 输入: '%' 16 | 输出: 37 17 | 18 | 样例解释: 19 | 返回ASCII码中对应的数字. 20 | 21 | ``` 22 | ### 参考代码 23 | 类型转换 24 | ```java 25 | public class Solution { 26 | /** 27 | * @param character a character 28 | * @return an integer 29 | */ 30 | public int charToInteger(char character) { 31 | return (int) character; 32 | } 33 | } 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /高频/compare-strings.md: -------------------------------------------------------------------------------- 1 | # 比较字符串 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/compare-strings/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 **大写字母** 5 | ### 样例说明 6 | 给出 A = `"ABCD"` B = `"ACD"`,返回 `true` 7 | 8 | 给出 A = `"ABCD"` B = `"AABC"`, 返回 `false` 9 | ### 参考代码 10 | ```python 11 | class Solution: 12 | """ 13 | @param A : A string includes Upper Case letters 14 | @param B : A string includes Upper Case letters 15 | @return : if string A contains all of the characters in B return True else return False 16 | """ 17 | def compareStrings(self, A, B): 18 | # write your code here 19 | if len(B) == 0: 20 | return True 21 | if len(A) == 0: 22 | return False 23 | #trackTable首先记录A中所有的字符以及它们的个数,然后遍历B,如果出现trackTable[i]小于0的情况,说明B中该字符出现的次数 24 | #大于在A中出现的次数 25 | trackTable = [0 for _ in range(26)] 26 | for i in A: 27 | trackTable[ord(i) - 65] += 1 28 | for i in B: 29 | if trackTable[ord(i) - 65] == 0: 30 | return False 31 | else: 32 | trackTable[ord(i) -65] -= 1 33 | return True 34 | ``` 35 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /高频/excel-sheet-column-title.md: -------------------------------------------------------------------------------- 1 | # Excel表列标题 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/excel-sheet-column-title/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定一个正整数,返回相应的列标题,如Excel表中所示。 5 | ### 样例说明 6 | **样例1** 7 | 8 | ``` 9 | 输入: 28 10 | 输出: "AB" 11 | ``` 12 | 13 | **样例2** 14 | 15 | ``` 16 | 输入: 29 17 | 输出: "AC" 18 | ``` 19 | ### 参考代码 20 | ```cpp 21 | class Solution { 22 | public: 23 | string convertToTitle(int n) { 24 | if (n == 0) { 25 | return ""; 26 | } 27 | return convertToTitle((n - 1) / 26) + (char)((n - 1) % 26 + 'A'); 28 | } 29 | }; 30 | ``` 31 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /高频/first-unique-character-in-a-string.md: -------------------------------------------------------------------------------- 1 | # 第一个只出现一次的字符 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/first-unique-character-in-a-string/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给出一个字符串,找出第一个只出现一次的字符。 5 | ### 样例说明 6 | ``` 7 | 样例 1: 8 | 输入: "abaccdeff" 9 | 输出: 'b' 10 | 11 | 解释: 12 | 'b' 是第一个出现一次的字符 13 | 14 | 15 | 样例 2: 16 | 输入: "aabccd" 17 | 输出: 'b' 18 | 19 | 解释: 20 | 'b' 是第一个出现一次的字符 21 | 22 | 23 | ``` 24 | ### 参考代码 25 | 辅助数组记录字符出现次数。 26 | ```python 27 | class Solution: 28 | """ 29 | @param str: str: the given string 30 | @return: char: the first unique character in a given string 31 | """ 32 | def firstUniqChar(self, str): 33 | counter = {} 34 | 35 | for c in str: 36 | counter[c] = counter.get(c, 0) + 1 37 | 38 | for c in str: 39 | if counter[c] == 1: 40 | return c 41 | ``` 42 | 更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz) -------------------------------------------------------------------------------- /高频/is-subsequence.md: -------------------------------------------------------------------------------- 1 | # 是子序列吗? 2 | > 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/is-subsequence/?utm_source=sc-github-wzz) 3 | ## 题目描述 4 | 给定字符串`s`和`t`,判断`s`是否为`t`的子序列。 5 | 6 | 你可以认为在`s`和`t`中都只包含小写字母。`t`可能是一个非常长(`length ~= 500,000`)的字符串,而`s`是一个较短的字符串(`length <= 100`)。 7 | 8 | 一个字符串的子序列是在原字符串中删去一些字符(也可以不删除)后,不改变剩余字符的相对位置形成的新字符串(例如,`"ace"`是`"abcde"`的子序列而`"aec"`不是)。 9 | ### 样例说明 10 | **样例1:** 11 | ``` 12 | 输入:s = "abc",t = "ahbgdc" 13 | 输出:true 14 | ``` 15 | **样例2:** 16 | ``` 17 | 输入:s = "axc",t = "ahbgdc" 18 | 输出:false 19 | ``` 20 | ### 参考代码 21 | 直接按s串的顺序去遍历t串即可。 22 | ```java 23 | public class Solution { 24 | public boolean isSubsequence(String s, String t) { 25 | int i = 0, j = 0; 26 | while(i