├── 187-4sum ├── README.md ├── Yiqi.js └── Yiqi.java ├── 234-3sum ├── README.md ├── Yiqi.js └── Yiqi.java ├── 127-powx-n └── README.md ├── 169-sqrtx └── README.md ├── 204-candy └── README.md ├── 098-subsets ├── README.md ├── Yiqi.js └── Yiqi.java ├── 129-h-index └── README.md ├── 225-two-sum └── README.md ├── 001-nim-game ├── README.md ├── crossken.js ├── bxbbok.js ├── byenow.js ├── yrok.js ├── glenye.js ├── hpoenixf.js ├── boyus.java ├── Yiqi_Ou.js ├── newraina.js └── Yiqi_Ou.java ├── 008-same-tree ├── README.md ├── Yiqi_Ou.java └── byenow.js ├── 043-gray-code └── README.md ├── 074-plus-one ├── README.md ├── yuanweiwu.js └── yuanweiwu.cpp ├── 089-path-sum └── README.md ├── 110-triangle ├── README.md ├── Yiqi.java └── Yiqi.js ├── 126-jump-game ├── README.md └── Yiqi.java ├── 145-n-queens └── README.md ├── 151-anagrams └── README.md ├── 176-sort-list └── README.md ├── 210-min-stack └── README.md ├── 243-lru-cache └── README.md ├── 002-add-digits ├── README.md ├── yrok.js ├── crossken.js ├── Yiqi_Ou.java ├── Yiqi_Ou.js ├── byenow.js ├── boyus.java ├── glen.js └── newraina.js ├── 006-move-zeroes ├── README.md ├── Yiqi_Ou.js ├── glen.js ├── byenow.js ├── newraina.js └── Yiqi_Ou.java ├── 021-n-queens-ii └── README.md ├── 037-ugly-number └── README.md ├── 051-sort-colors ├── README.md └── Yiqi.java ├── 075-h-index-ii └── README.md ├── 104-subsets-ii ├── README.md ├── Yiqi.js └── Yiqi.java ├── 131-path-sum-ii └── README.md ├── 142-gas-station └── README.md ├── 149-add-binary └── README.md ├── 150-maximum-gap └── README.md ├── 166-clone-graph └── README.md ├── 170-word-break └── README.md ├── 194-rotate-list └── README.md ├── 205-word-search ├── README.md └── Yiqi.java ├── 230-word-ladder └── README.md ├── 239-decode-ways └── README.md ├── 039-happy-number └── README.md ├── 040-unique-paths ├── README.md ├── Yiqi.java └── Yiqi.js ├── 049-permutations └── README.md ├── 052-power-of-two ├── README.md ├── yuanweiwu.js └── yuanweiwu.cpp ├── 056-rotate-image ├── README.md ├── Yiqi.java └── Yiqi.js ├── 062-game-of-life ├── README.md └── Yiqi.java ├── 064-combinations └── README.md ├── 069-house-robber └── README.md ├── 102-reverse-bits └── README.md ├── 108-valid-sudoku └── README.md ├── 124-3sum-closest ├── README.md └── Yiqi.java ├── 134-word-pattern └── README.md ├── 163-jump-game-ii ├── README.md └── Yiqi.java ├── 192-count-primes └── README.md ├── 199-reorder-list └── README.md ├── 228-rotate-array └── README.md ├── 229-dungeon-game └── README.md ├── 253-valid-number └── README.md ├── 003-single-number ├── README.md ├── byenow.js ├── Yiqi_Ou.js ├── boyus.java ├── glen.js ├── Yiqi_Ou.java └── newraina.js ├── 012-valid-anagram ├── README.md ├── Yiqi.java └── byenow.js ├── 020-missing-number ├── README.md └── Yiqi.js ├── 026-bulb-switcher └── README.md ├── 060-symmetric-tree └── README.md ├── 065-remove-element └── README.md ├── 094-burst-balloons └── README.md ├── 112-partition-list └── README.md ├── 113-rectangle-area └── README.md ├── 125-edit-distance └── README.md ├── 136-count-and-say └── README.md ├── 141-bulls-and-cows └── README.md ├── 162-ugly-number-ii └── README.md ├── 177-sudoku-solver └── README.md ├── 197-maximal-square └── README.md ├── 198-summary-ranges └── README.md ├── 206-spiral-matrix ├── README.md └── Yiqi.java ├── 213-simplify-path └── README.md ├── 231-word-break-ii └── README.md ├── 235-largest-number └── README.md ├── 237-word-search-ii └── README.md ├── 250-word-ladder-ii └── README.md ├── 015-majority-element ├── README.md ├── Yiqi.js └── Yiqi.java ├── 019-number-of-1-bits └── README.md ├── 022-roman-to-integer └── README.md ├── 031-single-number-ii └── README.md ├── 032-integer-to-roman └── README.md ├── 034-climbing-stairs └── README.md ├── 036-maximum-subarray ├── README.md └── Yiqi.java ├── 053-minimum-path-sum ├── README.md └── Yiqi.java ├── 054-spiral-matrix-ii ├── README.md └── Yiqi.java ├── 072-peeking-iterator └── README.md ├── 077-pascals-triangle └── README.md ├── 092-perfect-squares └── README.md ├── 100-combination-sum ├── README.md ├── Yiqi.js └── Yiqi.java ├── 107-house-robber-ii └── README.md ├── 111-unique-paths-ii ├── README.md ├── Yiqi.js └── Yiqi.java ├── 139-permutations-ii └── README.md ├── 153-scramble-string └── README.md ├── 154-next-permutation ├── README.md ├── Yiqi.js └── Yiqi.java ├── 165-course-schedule └── README.md ├── 172-merge-intervals └── README.md ├── 174-additive-number └── README.md ├── 175-implement-strstr └── README.md ├── 178-reverse-integer └── README.md ├── 184-valid-palindrome └── README.md ├── 186-insert-interval ├── README.md └── Yiqi.java ├── 195-multiply-strings └── README.md ├── 207-add-two-numbers └── README.md ├── 227-basic-calculator └── README.md ├── 009-single-number-iii ├── README.md ├── Yiqi_Ou.js └── Yiqi_Ou.java ├── 024-linked-list-cycle └── README.md ├── 063-find-peak-element ├── README.md └── Yiqi.java ├── 067-set-matrix-zeroes ├── README.md ├── Yiqi.js └── Yiqi.java ├── 091-palindrome-number └── README.md ├── 109-super-ugly-number └── README.md ├── 123-valid-parentheses └── README.md ├── 155-number-of-islands └── README.md ├── 157-binary-tree-paths └── README.md ├── 185-maximal-rectangle └── README.md ├── 188-zigzag-conversion └── README.md ├── 209-first-bad-version └── README.md ├── 240-wildcard-matching └── README.md ├── 007-invert-binary-tree ├── README.md ├── glen.js ├── byenow.js ├── Yiqi_Ou.java └── newraina.js ├── 013-contains-duplicate ├── README.md ├── byenow.js ├── Yiqi.js └── Yiqi.java ├── 025-reverse-linked-list └── README.md ├── 050-swap-nodes-in-pairs └── README.md ├── 059-combination-sum-iii ├── README.md └── Yiqi.java ├── 061-search-a-2d-matrix ├── README.md ├── Yiqi.java └── Yiqi.js ├── 083-trapping-rain-water ├── README.md └── Yiqi.java ├── 087-pascals-triangle-ii └── README.md ├── 099-merge-sorted-array └── README.md ├── 115-length-of-last-word └── README.md ├── 117-search-for-a-range ├── README.md └── Yiqi.java ├── 118-insertion-sort-list └── README.md ├── 128-isomorphic-strings └── README.md ├── 143-combination-sum-ii ├── README.md ├── Yiqi.js └── Yiqi.java ├── 167-majority-element-ii ├── README.md └── Yiqi.java ├── 182-number-of-digit-one └── README.md ├── 189-basic-calculator-ii └── README.md ├── 201-interleaving-string └── README.md ├── 217-course-schedule-ii └── README.md ├── 226-the-skyline-problem └── README.md ├── 232-shortest-palindrome └── README.md ├── 245-surrounded-regions └── README.md ├── 246-text-justification └── README.md ├── 247-divide-two-integers └── README.md ├── 041-generate-parentheses └── README.md ├── 058-balanced-binary-tree └── README.md ├── 076-search-a-2d-matrix-ii └── README.md ├── 080-linked-list-cycle-ii └── README.md ├── 116-contains-duplicate-ii └── README.md ├── 122-distinct-subsequences └── README.md ├── 140-longest-common-prefix └── README.md ├── 168-minimum-height-trees └── README.md ├── 171-permutation-sequence └── README.md ├── 193-restore-ip-addresses └── README.md ├── 196-merge-k-sorted-lists └── README.md ├── 249-max-points-on-a-line └── README.md ├── 252-create-maximum-number └── README.md ├── 030-search-insert-position ├── README.md ├── Yiqi.java └── Yiqi.js ├── 047-merge-two-sorted-lists └── README.md ├── 138-reverse-linked-list-ii └── README.md ├── 152-palindrome-linked-list └── README.md ├── 160-sliding-window-maximum └── README.md ├── 180-first-missing-positive ├── README.md └── Yiqi.java ├── 183-repeated-dna-sequences └── README.md ├── 236-contains-duplicate-iii └── README.md ├── 251-string-to-integer-atoi └── README.md ├── 081-sum-root-to-leaf-numbers └── README.md ├── 130-palindrome-partitioning └── README.md ├── 144-reverse-nodes-in-k-group └── README.md ├── 200-expression-add-operators └── README.md ├── 212-maximum-product-subarray ├── README.md ├── Yiqi.java └── Yiqi.js ├── 221-excel-sheet-column-title ├── README.md └── byenow.js ├── 222-minimum-window-substring └── README.md ├── 223-remove-duplicate-letters └── README.md ├── 238-integer-to-english-words └── README.md ├── 241-compare-version-numbers └── README.md ├── 242-range-sum-query-mutable └── README.md ├── 014-excel-sheet-column-number ├── README.md └── byenow.js ├── 027-find-the-duplicate-number ├── README.md └── Yiqi.java ├── 029-unique-binary-search-trees └── README.md ├── 057-container-with-most-water ├── README.md └── Yiqi.java ├── 085-factorial-trailing-zeroes └── README.md ├── 106-remove-invalid-parentheses └── README.md ├── 156-recover-binary-search-tree └── README.md ├── 158-minimum-size-subarray-sum ├── README.md ├── Yiqi.js └── Yiqi.java ├── 161-implement-trie-prefix-tree └── README.md ├── 173-range-sum-query-immutable └── README.md ├── 181-count-complete-tree-nodes └── README.md ├── 202-longest-valid-parentheses └── README.md ├── 214-palindrome-partitioning-ii └── README.md ├── 244-reverse-words-in-a-string └── README.md ├── 073-binary-search-tree-iterator └── README.md ├── 079-binary-tree-right-side-view └── README.md ├── 135-remove-linked-list-elements └── README.md ├── 208-regular-expression-matching └── README.md ├── 218-validate-binary-search-tree └── README.md ├── 233-median-of-two-sorted-arrays └── README.md ├── 004-maximum-depth-of-binary-tree ├── README.md ├── glen.js ├── byenow.js ├── Yiqi_Ou.js ├── newraina.js └── Yiqi_Ou.java ├── 005-delete-node-in-a-linked-list ├── README.md ├── byenow.js ├── newraina.js ├── glen.js └── Yiqi_Ou.java ├── 011-product-of-array-except-self ├── README.md ├── Yiqi.java └── Yiqi.js ├── 018-binary-tree-inorder-traversal └── README.md ├── 044-kth-smallest-element-in-a-bst └── README.md ├── 048-implement-queue-using-stacks └── README.md ├── 086-longest-consecutive-sequence ├── README.md └── Yiqi.java ├── 090-implement-stack-using-queues └── README.md ├── 097-minimum-depth-of-binary-tree └── README.md ├── 105-unique-binary-search-trees-ii └── README.md ├── 114-bitwise-and-of-numbers-range └── README.md ├── 148-copy-list-with-random-pointer └── README.md ├── 191-binary-tree-maximum-path-sum └── README.md ├── 203-longest-palindromic-substring └── README.md ├── 215-range-sum-query-2d-immutable └── README.md ├── 224-find-median-from-data-stream └── README.md ├── 248-fraction-to-recurring-decimal └── README.md ├── 016-binary-tree-preorder-traversal ├── README.md ├── Yiqi_Ou.js └── Yiqi_Ou.java ├── 023-maximum-product-of-word-lengths └── README.md ├── 042-best-time-to-buy-and-sell-stock ├── README.md └── Yiqi.java ├── 046-binary-tree-postorder-traversal └── README.md ├── 070-longest-increasing-subsequence └── README.md ├── 093-kth-largest-element-in-an-array └── README.md ├── 101-search-in-rotated-sorted-array └── README.md ├── 179-largest-rectangle-in-histogram ├── README.md └── Yiqi.java ├── 095-intersection-of-two-linked-lists └── README.md ├── 119-remove-nth-node-from-end-of-list └── README.md ├── 190-evaluate-reverse-polish-notation └── README.md ├── 010-best-time-to-buy-and-sell-stock-ii ├── README.md └── byenow.js ├── 033-remove-duplicates-from-sorted-list └── README.md ├── 082-search-in-rotated-sorted-array-ii └── README.md ├── 084-different-ways-to-add-parentheses └── README.md ├── 088-binary-tree-level-order-traversal └── README.md ├── 096-flatten-binary-tree-to-linked-list └── README.md ├── 216-best-time-to-buy-and-sell-stock-iv └── README.md ├── 038-find-minimum-in-rotated-sorted-array ├── README.md ├── Yiqi.java └── Yiqi.js ├── 068-binary-tree-level-order-traversal-ii └── README.md ├── 071-remove-duplicates-from-sorted-array └── README.md ├── 147-count-of-smaller-numbers-after-self └── README.md ├── 159-best-time-to-buy-and-sell-stock-iii ├── README.md └── Yiqi.java ├── 137-letter-combinations-of-a-phone-number └── README.md ├── 146-remove-duplicates-from-sorted-list-ii └── README.md ├── 164-serialize-and-deserialize-binary-tree └── README.md ├── 055-find-minimum-in-rotated-sorted-array-ii ├── README.md ├── Yiqi.js └── Yiqi.java ├── 078-remove-duplicates-from-sorted-array-ii ├── README.md ├── Yiqi.js └── Yiqi.java ├── 121-lowest-common-ancestor-of-a-binary-tree └── README.md ├── 103-convert-sorted-list-to-binary-search-tree └── README.md ├── 133-binary-tree-zigzag-level-order-traversal └── README.md ├── 219-add-and-search-word-data-structure-design └── README.md ├── 220-substring-with-concatenation-of-all-words └── README.md ├── 035-convert-sorted-array-to-binary-search-tree └── README.md ├── 028-populating-next-right-pointers-in-each-node └── README.md ├── 017-lowest-common-ancestor-of-a-binary-search-tree └── README.md ├── 045-best-time-to-buy-and-sell-stock-with-cooldown └── README.md ├── 066-populating-next-right-pointers-in-each-node-ii └── README.md ├── 211-longest-substring-without-repeating-characters └── README.md ├── 132-construct-binary-tree-from-preorder-and-inorder-traversal ├── README.md ├── Yiqi.js └── Yiqi.java ├── 120-construct-binary-tree-from-inorder-and-postorder-traversal ├── README.md ├── Yiqi.js └── Yiqi.java └── README.md /187-4sum/README.md: -------------------------------------------------------------------------------- 1 | - [187-4sum](https://leetcode.com/problems/4sum/) -------------------------------------------------------------------------------- /234-3sum/README.md: -------------------------------------------------------------------------------- 1 | - [234-3sum](https://leetcode.com/problems/3sum/) -------------------------------------------------------------------------------- /127-powx-n/README.md: -------------------------------------------------------------------------------- 1 | - [127-powx-n](https://leetcode.com/problems/powx-n/) -------------------------------------------------------------------------------- /169-sqrtx/README.md: -------------------------------------------------------------------------------- 1 | - [169-sqrtx](https://leetcode.com/problems/sqrtx/) -------------------------------------------------------------------------------- /204-candy/README.md: -------------------------------------------------------------------------------- 1 | - [204-candy](https://leetcode.com/problems/candy/) -------------------------------------------------------------------------------- /098-subsets/README.md: -------------------------------------------------------------------------------- 1 | - [098-subsets](https://leetcode.com/problems/subsets/) -------------------------------------------------------------------------------- /129-h-index/README.md: -------------------------------------------------------------------------------- 1 | - [129-h-index](https://leetcode.com/problems/h-index/) -------------------------------------------------------------------------------- /225-two-sum/README.md: -------------------------------------------------------------------------------- 1 | - [225-two-sum](https://leetcode.com/problems/two-sum/) -------------------------------------------------------------------------------- /001-nim-game/README.md: -------------------------------------------------------------------------------- 1 | - [001-nim-game](https://leetcode.com/problems/nim-game/) -------------------------------------------------------------------------------- /008-same-tree/README.md: -------------------------------------------------------------------------------- 1 | - [008-same-tree](https://leetcode.com/problems/same-tree/) -------------------------------------------------------------------------------- /043-gray-code/README.md: -------------------------------------------------------------------------------- 1 | - [043-gray-code](https://leetcode.com/problems/gray-code/) -------------------------------------------------------------------------------- /074-plus-one/README.md: -------------------------------------------------------------------------------- 1 | - [074-plus-one](https://leetcode.com/problems/plus-one/) -------------------------------------------------------------------------------- /089-path-sum/README.md: -------------------------------------------------------------------------------- 1 | - [089-path-sum](https://leetcode.com/problems/path-sum/) -------------------------------------------------------------------------------- /110-triangle/README.md: -------------------------------------------------------------------------------- 1 | - [110-triangle](https://leetcode.com/problems/triangle/) -------------------------------------------------------------------------------- /126-jump-game/README.md: -------------------------------------------------------------------------------- 1 | - [126-jump-game](https://leetcode.com/problems/jump-game/) -------------------------------------------------------------------------------- /145-n-queens/README.md: -------------------------------------------------------------------------------- 1 | - [145-n-queens](https://leetcode.com/problems/n-queens/) -------------------------------------------------------------------------------- /151-anagrams/README.md: -------------------------------------------------------------------------------- 1 | - [151-anagrams](https://leetcode.com/problems/anagrams/) -------------------------------------------------------------------------------- /176-sort-list/README.md: -------------------------------------------------------------------------------- 1 | - [176-sort-list](https://leetcode.com/problems/sort-list/) -------------------------------------------------------------------------------- /210-min-stack/README.md: -------------------------------------------------------------------------------- 1 | - [210-min-stack](https://leetcode.com/problems/min-stack/) -------------------------------------------------------------------------------- /243-lru-cache/README.md: -------------------------------------------------------------------------------- 1 | - [243-lru-cache](https://leetcode.com/problems/lru-cache/) -------------------------------------------------------------------------------- /001-nim-game/crossken.js: -------------------------------------------------------------------------------- 1 | var canWinNim = function(n) { 2 | return !!(n%4); 3 | }; -------------------------------------------------------------------------------- /002-add-digits/README.md: -------------------------------------------------------------------------------- 1 | - [002-add-digits](https://leetcode.com/problems/add-digits/) -------------------------------------------------------------------------------- /006-move-zeroes/README.md: -------------------------------------------------------------------------------- 1 | - [006-move-zeroes](https://leetcode.com/problems/move-zeroes/) -------------------------------------------------------------------------------- /021-n-queens-ii/README.md: -------------------------------------------------------------------------------- 1 | - [021-n-queens-ii](https://leetcode.com/problems/n-queens-ii/) -------------------------------------------------------------------------------- /037-ugly-number/README.md: -------------------------------------------------------------------------------- 1 | - [037-ugly-number](https://leetcode.com/problems/ugly-number/) -------------------------------------------------------------------------------- /051-sort-colors/README.md: -------------------------------------------------------------------------------- 1 | - [051-sort-colors](https://leetcode.com/problems/sort-colors/) -------------------------------------------------------------------------------- /075-h-index-ii/README.md: -------------------------------------------------------------------------------- 1 | - [075-h-index-ii](https://leetcode.com/problems/h-index-ii/) -------------------------------------------------------------------------------- /104-subsets-ii/README.md: -------------------------------------------------------------------------------- 1 | - [104-subsets-ii](https://leetcode.com/problems/subsets-ii/) -------------------------------------------------------------------------------- /131-path-sum-ii/README.md: -------------------------------------------------------------------------------- 1 | - [131-path-sum-ii](https://leetcode.com/problems/path-sum-ii/) -------------------------------------------------------------------------------- /142-gas-station/README.md: -------------------------------------------------------------------------------- 1 | - [142-gas-station](https://leetcode.com/problems/gas-station/) -------------------------------------------------------------------------------- /149-add-binary/README.md: -------------------------------------------------------------------------------- 1 | - [149-add-binary](https://leetcode.com/problems/add-binary/) -------------------------------------------------------------------------------- /150-maximum-gap/README.md: -------------------------------------------------------------------------------- 1 | - [150-maximum-gap](https://leetcode.com/problems/maximum-gap/) -------------------------------------------------------------------------------- /166-clone-graph/README.md: -------------------------------------------------------------------------------- 1 | - [166-clone-graph](https://leetcode.com/problems/clone-graph/) -------------------------------------------------------------------------------- /170-word-break/README.md: -------------------------------------------------------------------------------- 1 | - [170-word-break](https://leetcode.com/problems/word-break/) -------------------------------------------------------------------------------- /194-rotate-list/README.md: -------------------------------------------------------------------------------- 1 | - [194-rotate-list](https://leetcode.com/problems/rotate-list/) -------------------------------------------------------------------------------- /205-word-search/README.md: -------------------------------------------------------------------------------- 1 | - [205-word-search](https://leetcode.com/problems/word-search/) -------------------------------------------------------------------------------- /230-word-ladder/README.md: -------------------------------------------------------------------------------- 1 | - [230-word-ladder](https://leetcode.com/problems/word-ladder/) -------------------------------------------------------------------------------- /239-decode-ways/README.md: -------------------------------------------------------------------------------- 1 | - [239-decode-ways](https://leetcode.com/problems/decode-ways/) -------------------------------------------------------------------------------- /039-happy-number/README.md: -------------------------------------------------------------------------------- 1 | - [039-happy-number](https://leetcode.com/problems/happy-number/) -------------------------------------------------------------------------------- /040-unique-paths/README.md: -------------------------------------------------------------------------------- 1 | - [040-unique-paths](https://leetcode.com/problems/unique-paths/) -------------------------------------------------------------------------------- /049-permutations/README.md: -------------------------------------------------------------------------------- 1 | - [049-permutations](https://leetcode.com/problems/permutations/) -------------------------------------------------------------------------------- /052-power-of-two/README.md: -------------------------------------------------------------------------------- 1 | - [052-power-of-two](https://leetcode.com/problems/power-of-two/) -------------------------------------------------------------------------------- /056-rotate-image/README.md: -------------------------------------------------------------------------------- 1 | - [056-rotate-image](https://leetcode.com/problems/rotate-image/) -------------------------------------------------------------------------------- /062-game-of-life/README.md: -------------------------------------------------------------------------------- 1 | - [062-game-of-life](https://leetcode.com/problems/game-of-life/) -------------------------------------------------------------------------------- /064-combinations/README.md: -------------------------------------------------------------------------------- 1 | - [064-combinations](https://leetcode.com/problems/combinations/) -------------------------------------------------------------------------------- /069-house-robber/README.md: -------------------------------------------------------------------------------- 1 | - [069-house-robber](https://leetcode.com/problems/house-robber/) -------------------------------------------------------------------------------- /102-reverse-bits/README.md: -------------------------------------------------------------------------------- 1 | - [102-reverse-bits](https://leetcode.com/problems/reverse-bits/) -------------------------------------------------------------------------------- /108-valid-sudoku/README.md: -------------------------------------------------------------------------------- 1 | - [108-valid-sudoku](https://leetcode.com/problems/valid-sudoku/) -------------------------------------------------------------------------------- /124-3sum-closest/README.md: -------------------------------------------------------------------------------- 1 | - [124-3sum-closest](https://leetcode.com/problems/3sum-closest/) -------------------------------------------------------------------------------- /134-word-pattern/README.md: -------------------------------------------------------------------------------- 1 | - [134-word-pattern](https://leetcode.com/problems/word-pattern/) -------------------------------------------------------------------------------- /163-jump-game-ii/README.md: -------------------------------------------------------------------------------- 1 | - [163-jump-game-ii](https://leetcode.com/problems/jump-game-ii/) -------------------------------------------------------------------------------- /192-count-primes/README.md: -------------------------------------------------------------------------------- 1 | - [192-count-primes](https://leetcode.com/problems/count-primes/) -------------------------------------------------------------------------------- /199-reorder-list/README.md: -------------------------------------------------------------------------------- 1 | - [199-reorder-list](https://leetcode.com/problems/reorder-list/) -------------------------------------------------------------------------------- /228-rotate-array/README.md: -------------------------------------------------------------------------------- 1 | - [228-rotate-array](https://leetcode.com/problems/rotate-array/) -------------------------------------------------------------------------------- /229-dungeon-game/README.md: -------------------------------------------------------------------------------- 1 | - [229-dungeon-game](https://leetcode.com/problems/dungeon-game/) -------------------------------------------------------------------------------- /253-valid-number/README.md: -------------------------------------------------------------------------------- 1 | - [253-valid-number](https://leetcode.com/problems/valid-number/) -------------------------------------------------------------------------------- /003-single-number/README.md: -------------------------------------------------------------------------------- 1 | - [003-single-number](https://leetcode.com/problems/single-number/) -------------------------------------------------------------------------------- /012-valid-anagram/README.md: -------------------------------------------------------------------------------- 1 | - [012-valid-anagram](https://leetcode.com/problems/valid-anagram/) -------------------------------------------------------------------------------- /020-missing-number/README.md: -------------------------------------------------------------------------------- 1 | - [020-missing-number](https://leetcode.com/problems/missing-number/) -------------------------------------------------------------------------------- /026-bulb-switcher/README.md: -------------------------------------------------------------------------------- 1 | - [026-bulb-switcher](https://leetcode.com/problems/bulb-switcher/) -------------------------------------------------------------------------------- /060-symmetric-tree/README.md: -------------------------------------------------------------------------------- 1 | - [060-symmetric-tree](https://leetcode.com/problems/symmetric-tree/) -------------------------------------------------------------------------------- /065-remove-element/README.md: -------------------------------------------------------------------------------- 1 | - [065-remove-element](https://leetcode.com/problems/remove-element/) -------------------------------------------------------------------------------- /094-burst-balloons/README.md: -------------------------------------------------------------------------------- 1 | - [094-burst-balloons](https://leetcode.com/problems/burst-balloons/) -------------------------------------------------------------------------------- /112-partition-list/README.md: -------------------------------------------------------------------------------- 1 | - [112-partition-list](https://leetcode.com/problems/partition-list/) -------------------------------------------------------------------------------- /113-rectangle-area/README.md: -------------------------------------------------------------------------------- 1 | - [113-rectangle-area](https://leetcode.com/problems/rectangle-area/) -------------------------------------------------------------------------------- /125-edit-distance/README.md: -------------------------------------------------------------------------------- 1 | - [125-edit-distance](https://leetcode.com/problems/edit-distance/) -------------------------------------------------------------------------------- /136-count-and-say/README.md: -------------------------------------------------------------------------------- 1 | - [136-count-and-say](https://leetcode.com/problems/count-and-say/) -------------------------------------------------------------------------------- /141-bulls-and-cows/README.md: -------------------------------------------------------------------------------- 1 | - [141-bulls-and-cows](https://leetcode.com/problems/bulls-and-cows/) -------------------------------------------------------------------------------- /162-ugly-number-ii/README.md: -------------------------------------------------------------------------------- 1 | - [162-ugly-number-ii](https://leetcode.com/problems/ugly-number-ii/) -------------------------------------------------------------------------------- /177-sudoku-solver/README.md: -------------------------------------------------------------------------------- 1 | - [177-sudoku-solver](https://leetcode.com/problems/sudoku-solver/) -------------------------------------------------------------------------------- /197-maximal-square/README.md: -------------------------------------------------------------------------------- 1 | - [197-maximal-square](https://leetcode.com/problems/maximal-square/) -------------------------------------------------------------------------------- /198-summary-ranges/README.md: -------------------------------------------------------------------------------- 1 | - [198-summary-ranges](https://leetcode.com/problems/summary-ranges/) -------------------------------------------------------------------------------- /206-spiral-matrix/README.md: -------------------------------------------------------------------------------- 1 | - [206-spiral-matrix](https://leetcode.com/problems/spiral-matrix/) -------------------------------------------------------------------------------- /213-simplify-path/README.md: -------------------------------------------------------------------------------- 1 | - [213-simplify-path](https://leetcode.com/problems/simplify-path/) -------------------------------------------------------------------------------- /231-word-break-ii/README.md: -------------------------------------------------------------------------------- 1 | - [231-word-break-ii](https://leetcode.com/problems/word-break-ii/) -------------------------------------------------------------------------------- /235-largest-number/README.md: -------------------------------------------------------------------------------- 1 | - [235-largest-number](https://leetcode.com/problems/largest-number/) -------------------------------------------------------------------------------- /237-word-search-ii/README.md: -------------------------------------------------------------------------------- 1 | - [237-word-search-ii](https://leetcode.com/problems/word-search-ii/) -------------------------------------------------------------------------------- /250-word-ladder-ii/README.md: -------------------------------------------------------------------------------- 1 | - [250-word-ladder-ii](https://leetcode.com/problems/word-ladder-ii/) -------------------------------------------------------------------------------- /015-majority-element/README.md: -------------------------------------------------------------------------------- 1 | - [015-majority-element](https://leetcode.com/problems/majority-element/) -------------------------------------------------------------------------------- /019-number-of-1-bits/README.md: -------------------------------------------------------------------------------- 1 | - [019-number-of-1-bits](https://leetcode.com/problems/number-of-1-bits/) -------------------------------------------------------------------------------- /022-roman-to-integer/README.md: -------------------------------------------------------------------------------- 1 | - [022-roman-to-integer](https://leetcode.com/problems/roman-to-integer/) -------------------------------------------------------------------------------- /031-single-number-ii/README.md: -------------------------------------------------------------------------------- 1 | - [031-single-number-ii](https://leetcode.com/problems/single-number-ii/) -------------------------------------------------------------------------------- /032-integer-to-roman/README.md: -------------------------------------------------------------------------------- 1 | - [032-integer-to-roman](https://leetcode.com/problems/integer-to-roman/) -------------------------------------------------------------------------------- /034-climbing-stairs/README.md: -------------------------------------------------------------------------------- 1 | - [034-climbing-stairs](https://leetcode.com/problems/climbing-stairs/) -------------------------------------------------------------------------------- /036-maximum-subarray/README.md: -------------------------------------------------------------------------------- 1 | - [036-maximum-subarray](https://leetcode.com/problems/maximum-subarray/) -------------------------------------------------------------------------------- /053-minimum-path-sum/README.md: -------------------------------------------------------------------------------- 1 | - [053-minimum-path-sum](https://leetcode.com/problems/minimum-path-sum/) -------------------------------------------------------------------------------- /054-spiral-matrix-ii/README.md: -------------------------------------------------------------------------------- 1 | - [054-spiral-matrix-ii](https://leetcode.com/problems/spiral-matrix-ii/) -------------------------------------------------------------------------------- /072-peeking-iterator/README.md: -------------------------------------------------------------------------------- 1 | - [072-peeking-iterator](https://leetcode.com/problems/peeking-iterator/) -------------------------------------------------------------------------------- /077-pascals-triangle/README.md: -------------------------------------------------------------------------------- 1 | - [077-pascals-triangle](https://leetcode.com/problems/pascals-triangle/) -------------------------------------------------------------------------------- /092-perfect-squares/README.md: -------------------------------------------------------------------------------- 1 | - [092-perfect-squares](https://leetcode.com/problems/perfect-squares/) -------------------------------------------------------------------------------- /100-combination-sum/README.md: -------------------------------------------------------------------------------- 1 | - [100-combination-sum](https://leetcode.com/problems/combination-sum/) -------------------------------------------------------------------------------- /107-house-robber-ii/README.md: -------------------------------------------------------------------------------- 1 | - [107-house-robber-ii](https://leetcode.com/problems/house-robber-ii/) -------------------------------------------------------------------------------- /111-unique-paths-ii/README.md: -------------------------------------------------------------------------------- 1 | - [111-unique-paths-ii](https://leetcode.com/problems/unique-paths-ii/) -------------------------------------------------------------------------------- /139-permutations-ii/README.md: -------------------------------------------------------------------------------- 1 | - [139-permutations-ii](https://leetcode.com/problems/permutations-ii/) -------------------------------------------------------------------------------- /153-scramble-string/README.md: -------------------------------------------------------------------------------- 1 | - [153-scramble-string](https://leetcode.com/problems/scramble-string/) -------------------------------------------------------------------------------- /154-next-permutation/README.md: -------------------------------------------------------------------------------- 1 | - [154-next-permutation](https://leetcode.com/problems/next-permutation/) -------------------------------------------------------------------------------- /165-course-schedule/README.md: -------------------------------------------------------------------------------- 1 | - [165-course-schedule](https://leetcode.com/problems/course-schedule/) -------------------------------------------------------------------------------- /172-merge-intervals/README.md: -------------------------------------------------------------------------------- 1 | - [172-merge-intervals](https://leetcode.com/problems/merge-intervals/) -------------------------------------------------------------------------------- /174-additive-number/README.md: -------------------------------------------------------------------------------- 1 | - [174-additive-number](https://leetcode.com/problems/additive-number/) -------------------------------------------------------------------------------- /175-implement-strstr/README.md: -------------------------------------------------------------------------------- 1 | - [175-implement-strstr](https://leetcode.com/problems/implement-strstr/) -------------------------------------------------------------------------------- /178-reverse-integer/README.md: -------------------------------------------------------------------------------- 1 | - [178-reverse-integer](https://leetcode.com/problems/reverse-integer/) -------------------------------------------------------------------------------- /184-valid-palindrome/README.md: -------------------------------------------------------------------------------- 1 | - [184-valid-palindrome](https://leetcode.com/problems/valid-palindrome/) -------------------------------------------------------------------------------- /186-insert-interval/README.md: -------------------------------------------------------------------------------- 1 | - [186-insert-interval](https://leetcode.com/problems/insert-interval/) -------------------------------------------------------------------------------- /195-multiply-strings/README.md: -------------------------------------------------------------------------------- 1 | - [195-multiply-strings](https://leetcode.com/problems/multiply-strings/) -------------------------------------------------------------------------------- /207-add-two-numbers/README.md: -------------------------------------------------------------------------------- 1 | - [207-add-two-numbers](https://leetcode.com/problems/add-two-numbers/) -------------------------------------------------------------------------------- /227-basic-calculator/README.md: -------------------------------------------------------------------------------- 1 | - [227-basic-calculator](https://leetcode.com/problems/basic-calculator/) -------------------------------------------------------------------------------- /009-single-number-iii/README.md: -------------------------------------------------------------------------------- 1 | - [009-single-number-iii](https://leetcode.com/problems/single-number-iii/) -------------------------------------------------------------------------------- /024-linked-list-cycle/README.md: -------------------------------------------------------------------------------- 1 | - [024-linked-list-cycle](https://leetcode.com/problems/linked-list-cycle/) -------------------------------------------------------------------------------- /063-find-peak-element/README.md: -------------------------------------------------------------------------------- 1 | - [063-find-peak-element](https://leetcode.com/problems/find-peak-element/) -------------------------------------------------------------------------------- /067-set-matrix-zeroes/README.md: -------------------------------------------------------------------------------- 1 | - [067-set-matrix-zeroes](https://leetcode.com/problems/set-matrix-zeroes/) -------------------------------------------------------------------------------- /091-palindrome-number/README.md: -------------------------------------------------------------------------------- 1 | - [091-palindrome-number](https://leetcode.com/problems/palindrome-number/) -------------------------------------------------------------------------------- /109-super-ugly-number/README.md: -------------------------------------------------------------------------------- 1 | - [109-super-ugly-number](https://leetcode.com/problems/super-ugly-number/) -------------------------------------------------------------------------------- /123-valid-parentheses/README.md: -------------------------------------------------------------------------------- 1 | - [123-valid-parentheses](https://leetcode.com/problems/valid-parentheses/) -------------------------------------------------------------------------------- /155-number-of-islands/README.md: -------------------------------------------------------------------------------- 1 | - [155-number-of-islands](https://leetcode.com/problems/number-of-islands/) -------------------------------------------------------------------------------- /157-binary-tree-paths/README.md: -------------------------------------------------------------------------------- 1 | - [157-binary-tree-paths](https://leetcode.com/problems/binary-tree-paths/) -------------------------------------------------------------------------------- /185-maximal-rectangle/README.md: -------------------------------------------------------------------------------- 1 | - [185-maximal-rectangle](https://leetcode.com/problems/maximal-rectangle/) -------------------------------------------------------------------------------- /188-zigzag-conversion/README.md: -------------------------------------------------------------------------------- 1 | - [188-zigzag-conversion](https://leetcode.com/problems/zigzag-conversion/) -------------------------------------------------------------------------------- /209-first-bad-version/README.md: -------------------------------------------------------------------------------- 1 | - [209-first-bad-version](https://leetcode.com/problems/first-bad-version/) -------------------------------------------------------------------------------- /240-wildcard-matching/README.md: -------------------------------------------------------------------------------- 1 | - [240-wildcard-matching](https://leetcode.com/problems/wildcard-matching/) -------------------------------------------------------------------------------- /007-invert-binary-tree/README.md: -------------------------------------------------------------------------------- 1 | - [007-invert-binary-tree](https://leetcode.com/problems/invert-binary-tree/) -------------------------------------------------------------------------------- /013-contains-duplicate/README.md: -------------------------------------------------------------------------------- 1 | - [013-contains-duplicate](https://leetcode.com/problems/contains-duplicate/) -------------------------------------------------------------------------------- /025-reverse-linked-list/README.md: -------------------------------------------------------------------------------- 1 | - [025-reverse-linked-list](https://leetcode.com/problems/reverse-linked-list/) -------------------------------------------------------------------------------- /050-swap-nodes-in-pairs/README.md: -------------------------------------------------------------------------------- 1 | - [050-swap-nodes-in-pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) -------------------------------------------------------------------------------- /059-combination-sum-iii/README.md: -------------------------------------------------------------------------------- 1 | - [059-combination-sum-iii](https://leetcode.com/problems/combination-sum-iii/) -------------------------------------------------------------------------------- /061-search-a-2d-matrix/README.md: -------------------------------------------------------------------------------- 1 | - [061-search-a-2d-matrix](https://leetcode.com/problems/search-a-2d-matrix/) -------------------------------------------------------------------------------- /083-trapping-rain-water/README.md: -------------------------------------------------------------------------------- 1 | - [083-trapping-rain-water](https://leetcode.com/problems/trapping-rain-water/) -------------------------------------------------------------------------------- /087-pascals-triangle-ii/README.md: -------------------------------------------------------------------------------- 1 | - [087-pascals-triangle-ii](https://leetcode.com/problems/pascals-triangle-ii/) -------------------------------------------------------------------------------- /099-merge-sorted-array/README.md: -------------------------------------------------------------------------------- 1 | - [099-merge-sorted-array](https://leetcode.com/problems/merge-sorted-array/) -------------------------------------------------------------------------------- /115-length-of-last-word/README.md: -------------------------------------------------------------------------------- 1 | - [115-length-of-last-word](https://leetcode.com/problems/length-of-last-word/) -------------------------------------------------------------------------------- /117-search-for-a-range/README.md: -------------------------------------------------------------------------------- 1 | - [117-search-for-a-range](https://leetcode.com/problems/search-for-a-range/) -------------------------------------------------------------------------------- /118-insertion-sort-list/README.md: -------------------------------------------------------------------------------- 1 | - [118-insertion-sort-list](https://leetcode.com/problems/insertion-sort-list/) -------------------------------------------------------------------------------- /128-isomorphic-strings/README.md: -------------------------------------------------------------------------------- 1 | - [128-isomorphic-strings](https://leetcode.com/problems/isomorphic-strings/) -------------------------------------------------------------------------------- /143-combination-sum-ii/README.md: -------------------------------------------------------------------------------- 1 | - [143-combination-sum-ii](https://leetcode.com/problems/combination-sum-ii/) -------------------------------------------------------------------------------- /167-majority-element-ii/README.md: -------------------------------------------------------------------------------- 1 | - [167-majority-element-ii](https://leetcode.com/problems/majority-element-ii/) -------------------------------------------------------------------------------- /182-number-of-digit-one/README.md: -------------------------------------------------------------------------------- 1 | - [182-number-of-digit-one](https://leetcode.com/problems/number-of-digit-one/) -------------------------------------------------------------------------------- /189-basic-calculator-ii/README.md: -------------------------------------------------------------------------------- 1 | - [189-basic-calculator-ii](https://leetcode.com/problems/basic-calculator-ii/) -------------------------------------------------------------------------------- /201-interleaving-string/README.md: -------------------------------------------------------------------------------- 1 | - [201-interleaving-string](https://leetcode.com/problems/interleaving-string/) -------------------------------------------------------------------------------- /217-course-schedule-ii/README.md: -------------------------------------------------------------------------------- 1 | - [217-course-schedule-ii](https://leetcode.com/problems/course-schedule-ii/) -------------------------------------------------------------------------------- /226-the-skyline-problem/README.md: -------------------------------------------------------------------------------- 1 | - [226-the-skyline-problem](https://leetcode.com/problems/the-skyline-problem/) -------------------------------------------------------------------------------- /232-shortest-palindrome/README.md: -------------------------------------------------------------------------------- 1 | - [232-shortest-palindrome](https://leetcode.com/problems/shortest-palindrome/) -------------------------------------------------------------------------------- /245-surrounded-regions/README.md: -------------------------------------------------------------------------------- 1 | - [245-surrounded-regions](https://leetcode.com/problems/surrounded-regions/) -------------------------------------------------------------------------------- /246-text-justification/README.md: -------------------------------------------------------------------------------- 1 | - [246-text-justification](https://leetcode.com/problems/text-justification/) -------------------------------------------------------------------------------- /247-divide-two-integers/README.md: -------------------------------------------------------------------------------- 1 | - [247-divide-two-integers](https://leetcode.com/problems/divide-two-integers/) -------------------------------------------------------------------------------- /041-generate-parentheses/README.md: -------------------------------------------------------------------------------- 1 | - [041-generate-parentheses](https://leetcode.com/problems/generate-parentheses/) -------------------------------------------------------------------------------- /058-balanced-binary-tree/README.md: -------------------------------------------------------------------------------- 1 | - [058-balanced-binary-tree](https://leetcode.com/problems/balanced-binary-tree/) -------------------------------------------------------------------------------- /076-search-a-2d-matrix-ii/README.md: -------------------------------------------------------------------------------- 1 | - [076-search-a-2d-matrix-ii](https://leetcode.com/problems/search-a-2d-matrix-ii/) -------------------------------------------------------------------------------- /080-linked-list-cycle-ii/README.md: -------------------------------------------------------------------------------- 1 | - [080-linked-list-cycle-ii](https://leetcode.com/problems/linked-list-cycle-ii/) -------------------------------------------------------------------------------- /116-contains-duplicate-ii/README.md: -------------------------------------------------------------------------------- 1 | - [116-contains-duplicate-ii](https://leetcode.com/problems/contains-duplicate-ii/) -------------------------------------------------------------------------------- /122-distinct-subsequences/README.md: -------------------------------------------------------------------------------- 1 | - [122-distinct-subsequences](https://leetcode.com/problems/distinct-subsequences/) -------------------------------------------------------------------------------- /140-longest-common-prefix/README.md: -------------------------------------------------------------------------------- 1 | - [140-longest-common-prefix](https://leetcode.com/problems/longest-common-prefix/) -------------------------------------------------------------------------------- /168-minimum-height-trees/README.md: -------------------------------------------------------------------------------- 1 | - [168-minimum-height-trees](https://leetcode.com/problems/minimum-height-trees/) -------------------------------------------------------------------------------- /171-permutation-sequence/README.md: -------------------------------------------------------------------------------- 1 | - [171-permutation-sequence](https://leetcode.com/problems/permutation-sequence/) -------------------------------------------------------------------------------- /193-restore-ip-addresses/README.md: -------------------------------------------------------------------------------- 1 | - [193-restore-ip-addresses](https://leetcode.com/problems/restore-ip-addresses/) -------------------------------------------------------------------------------- /196-merge-k-sorted-lists/README.md: -------------------------------------------------------------------------------- 1 | - [196-merge-k-sorted-lists](https://leetcode.com/problems/merge-k-sorted-lists/) -------------------------------------------------------------------------------- /249-max-points-on-a-line/README.md: -------------------------------------------------------------------------------- 1 | - [249-max-points-on-a-line](https://leetcode.com/problems/max-points-on-a-line/) -------------------------------------------------------------------------------- /252-create-maximum-number/README.md: -------------------------------------------------------------------------------- 1 | - [252-create-maximum-number](https://leetcode.com/problems/create-maximum-number/) -------------------------------------------------------------------------------- /030-search-insert-position/README.md: -------------------------------------------------------------------------------- 1 | - [030-search-insert-position](https://leetcode.com/problems/search-insert-position/) -------------------------------------------------------------------------------- /047-merge-two-sorted-lists/README.md: -------------------------------------------------------------------------------- 1 | - [047-merge-two-sorted-lists](https://leetcode.com/problems/merge-two-sorted-lists/) -------------------------------------------------------------------------------- /138-reverse-linked-list-ii/README.md: -------------------------------------------------------------------------------- 1 | - [138-reverse-linked-list-ii](https://leetcode.com/problems/reverse-linked-list-ii/) -------------------------------------------------------------------------------- /152-palindrome-linked-list/README.md: -------------------------------------------------------------------------------- 1 | - [152-palindrome-linked-list](https://leetcode.com/problems/palindrome-linked-list/) -------------------------------------------------------------------------------- /160-sliding-window-maximum/README.md: -------------------------------------------------------------------------------- 1 | - [160-sliding-window-maximum](https://leetcode.com/problems/sliding-window-maximum/) -------------------------------------------------------------------------------- /180-first-missing-positive/README.md: -------------------------------------------------------------------------------- 1 | - [180-first-missing-positive](https://leetcode.com/problems/first-missing-positive/) -------------------------------------------------------------------------------- /183-repeated-dna-sequences/README.md: -------------------------------------------------------------------------------- 1 | - [183-repeated-dna-sequences](https://leetcode.com/problems/repeated-dna-sequences/) -------------------------------------------------------------------------------- /236-contains-duplicate-iii/README.md: -------------------------------------------------------------------------------- 1 | - [236-contains-duplicate-iii](https://leetcode.com/problems/contains-duplicate-iii/) -------------------------------------------------------------------------------- /251-string-to-integer-atoi/README.md: -------------------------------------------------------------------------------- 1 | - [251-string-to-integer-atoi](https://leetcode.com/problems/string-to-integer-atoi/) -------------------------------------------------------------------------------- /081-sum-root-to-leaf-numbers/README.md: -------------------------------------------------------------------------------- 1 | - [081-sum-root-to-leaf-numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) -------------------------------------------------------------------------------- /130-palindrome-partitioning/README.md: -------------------------------------------------------------------------------- 1 | - [130-palindrome-partitioning](https://leetcode.com/problems/palindrome-partitioning/) -------------------------------------------------------------------------------- /144-reverse-nodes-in-k-group/README.md: -------------------------------------------------------------------------------- 1 | - [144-reverse-nodes-in-k-group](https://leetcode.com/problems/reverse-nodes-in-k-group/) -------------------------------------------------------------------------------- /200-expression-add-operators/README.md: -------------------------------------------------------------------------------- 1 | - [200-expression-add-operators](https://leetcode.com/problems/expression-add-operators/) -------------------------------------------------------------------------------- /212-maximum-product-subarray/README.md: -------------------------------------------------------------------------------- 1 | - [212-maximum-product-subarray](https://leetcode.com/problems/maximum-product-subarray/) -------------------------------------------------------------------------------- /221-excel-sheet-column-title/README.md: -------------------------------------------------------------------------------- 1 | - [221-excel-sheet-column-title](https://leetcode.com/problems/excel-sheet-column-title/) -------------------------------------------------------------------------------- /222-minimum-window-substring/README.md: -------------------------------------------------------------------------------- 1 | - [222-minimum-window-substring](https://leetcode.com/problems/minimum-window-substring/) -------------------------------------------------------------------------------- /223-remove-duplicate-letters/README.md: -------------------------------------------------------------------------------- 1 | - [223-remove-duplicate-letters](https://leetcode.com/problems/remove-duplicate-letters/) -------------------------------------------------------------------------------- /238-integer-to-english-words/README.md: -------------------------------------------------------------------------------- 1 | - [238-integer-to-english-words](https://leetcode.com/problems/integer-to-english-words/) -------------------------------------------------------------------------------- /241-compare-version-numbers/README.md: -------------------------------------------------------------------------------- 1 | - [241-compare-version-numbers](https://leetcode.com/problems/compare-version-numbers/) -------------------------------------------------------------------------------- /242-range-sum-query-mutable/README.md: -------------------------------------------------------------------------------- 1 | - [242-range-sum-query-mutable](https://leetcode.com/problems/range-sum-query-mutable/) -------------------------------------------------------------------------------- /014-excel-sheet-column-number/README.md: -------------------------------------------------------------------------------- 1 | - [014-excel-sheet-column-number](https://leetcode.com/problems/excel-sheet-column-number/) -------------------------------------------------------------------------------- /027-find-the-duplicate-number/README.md: -------------------------------------------------------------------------------- 1 | - [027-find-the-duplicate-number](https://leetcode.com/problems/find-the-duplicate-number/) -------------------------------------------------------------------------------- /029-unique-binary-search-trees/README.md: -------------------------------------------------------------------------------- 1 | - [029-unique-binary-search-trees](https://leetcode.com/problems/unique-binary-search-trees/) -------------------------------------------------------------------------------- /057-container-with-most-water/README.md: -------------------------------------------------------------------------------- 1 | - [057-container-with-most-water](https://leetcode.com/problems/container-with-most-water/) -------------------------------------------------------------------------------- /085-factorial-trailing-zeroes/README.md: -------------------------------------------------------------------------------- 1 | - [085-factorial-trailing-zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) -------------------------------------------------------------------------------- /106-remove-invalid-parentheses/README.md: -------------------------------------------------------------------------------- 1 | - [106-remove-invalid-parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) -------------------------------------------------------------------------------- /156-recover-binary-search-tree/README.md: -------------------------------------------------------------------------------- 1 | - [156-recover-binary-search-tree](https://leetcode.com/problems/recover-binary-search-tree/) -------------------------------------------------------------------------------- /158-minimum-size-subarray-sum/README.md: -------------------------------------------------------------------------------- 1 | - [158-minimum-size-subarray-sum](https://leetcode.com/problems/minimum-size-subarray-sum/) -------------------------------------------------------------------------------- /161-implement-trie-prefix-tree/README.md: -------------------------------------------------------------------------------- 1 | - [161-implement-trie-prefix-tree](https://leetcode.com/problems/implement-trie-prefix-tree/) -------------------------------------------------------------------------------- /173-range-sum-query-immutable/README.md: -------------------------------------------------------------------------------- 1 | - [173-range-sum-query-immutable](https://leetcode.com/problems/range-sum-query-immutable/) -------------------------------------------------------------------------------- /181-count-complete-tree-nodes/README.md: -------------------------------------------------------------------------------- 1 | - [181-count-complete-tree-nodes](https://leetcode.com/problems/count-complete-tree-nodes/) -------------------------------------------------------------------------------- /202-longest-valid-parentheses/README.md: -------------------------------------------------------------------------------- 1 | - [202-longest-valid-parentheses](https://leetcode.com/problems/longest-valid-parentheses/) -------------------------------------------------------------------------------- /214-palindrome-partitioning-ii/README.md: -------------------------------------------------------------------------------- 1 | - [214-palindrome-partitioning-ii](https://leetcode.com/problems/palindrome-partitioning-ii/) -------------------------------------------------------------------------------- /244-reverse-words-in-a-string/README.md: -------------------------------------------------------------------------------- 1 | - [244-reverse-words-in-a-string](https://leetcode.com/problems/reverse-words-in-a-string/) -------------------------------------------------------------------------------- /073-binary-search-tree-iterator/README.md: -------------------------------------------------------------------------------- 1 | - [073-binary-search-tree-iterator](https://leetcode.com/problems/binary-search-tree-iterator/) -------------------------------------------------------------------------------- /079-binary-tree-right-side-view/README.md: -------------------------------------------------------------------------------- 1 | - [079-binary-tree-right-side-view](https://leetcode.com/problems/binary-tree-right-side-view/) -------------------------------------------------------------------------------- /135-remove-linked-list-elements/README.md: -------------------------------------------------------------------------------- 1 | - [135-remove-linked-list-elements](https://leetcode.com/problems/remove-linked-list-elements/) -------------------------------------------------------------------------------- /208-regular-expression-matching/README.md: -------------------------------------------------------------------------------- 1 | - [208-regular-expression-matching](https://leetcode.com/problems/regular-expression-matching/) -------------------------------------------------------------------------------- /218-validate-binary-search-tree/README.md: -------------------------------------------------------------------------------- 1 | - [218-validate-binary-search-tree](https://leetcode.com/problems/validate-binary-search-tree/) -------------------------------------------------------------------------------- /233-median-of-two-sorted-arrays/README.md: -------------------------------------------------------------------------------- 1 | - [233-median-of-two-sorted-arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) -------------------------------------------------------------------------------- /004-maximum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 | - [004-maximum-depth-of-binary-tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) -------------------------------------------------------------------------------- /005-delete-node-in-a-linked-list/README.md: -------------------------------------------------------------------------------- 1 | - [005-delete-node-in-a-linked-list](https://leetcode.com/problems/delete-node-in-a-linked-list/) -------------------------------------------------------------------------------- /011-product-of-array-except-self/README.md: -------------------------------------------------------------------------------- 1 | - [011-product-of-array-except-self](https://leetcode.com/problems/product-of-array-except-self/) -------------------------------------------------------------------------------- /018-binary-tree-inorder-traversal/README.md: -------------------------------------------------------------------------------- 1 | - [018-binary-tree-inorder-traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) -------------------------------------------------------------------------------- /044-kth-smallest-element-in-a-bst/README.md: -------------------------------------------------------------------------------- 1 | - [044-kth-smallest-element-in-a-bst](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) -------------------------------------------------------------------------------- /048-implement-queue-using-stacks/README.md: -------------------------------------------------------------------------------- 1 | - [048-implement-queue-using-stacks](https://leetcode.com/problems/implement-queue-using-stacks/) -------------------------------------------------------------------------------- /086-longest-consecutive-sequence/README.md: -------------------------------------------------------------------------------- 1 | - [086-longest-consecutive-sequence](https://leetcode.com/problems/longest-consecutive-sequence/) -------------------------------------------------------------------------------- /090-implement-stack-using-queues/README.md: -------------------------------------------------------------------------------- 1 | - [090-implement-stack-using-queues](https://leetcode.com/problems/implement-stack-using-queues/) -------------------------------------------------------------------------------- /097-minimum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 | - [097-minimum-depth-of-binary-tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) -------------------------------------------------------------------------------- /105-unique-binary-search-trees-ii/README.md: -------------------------------------------------------------------------------- 1 | - [105-unique-binary-search-trees-ii](https://leetcode.com/problems/unique-binary-search-trees-ii/) -------------------------------------------------------------------------------- /114-bitwise-and-of-numbers-range/README.md: -------------------------------------------------------------------------------- 1 | - [114-bitwise-and-of-numbers-range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) -------------------------------------------------------------------------------- /148-copy-list-with-random-pointer/README.md: -------------------------------------------------------------------------------- 1 | - [148-copy-list-with-random-pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) -------------------------------------------------------------------------------- /191-binary-tree-maximum-path-sum/README.md: -------------------------------------------------------------------------------- 1 | - [191-binary-tree-maximum-path-sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) -------------------------------------------------------------------------------- /203-longest-palindromic-substring/README.md: -------------------------------------------------------------------------------- 1 | - [203-longest-palindromic-substring](https://leetcode.com/problems/longest-palindromic-substring/) -------------------------------------------------------------------------------- /215-range-sum-query-2d-immutable/README.md: -------------------------------------------------------------------------------- 1 | - [215-range-sum-query-2d-immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) -------------------------------------------------------------------------------- /224-find-median-from-data-stream/README.md: -------------------------------------------------------------------------------- 1 | - [224-find-median-from-data-stream](https://leetcode.com/problems/find-median-from-data-stream/) -------------------------------------------------------------------------------- /248-fraction-to-recurring-decimal/README.md: -------------------------------------------------------------------------------- 1 | - [248-fraction-to-recurring-decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) -------------------------------------------------------------------------------- /016-binary-tree-preorder-traversal/README.md: -------------------------------------------------------------------------------- 1 | - [016-binary-tree-preorder-traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) -------------------------------------------------------------------------------- /023-maximum-product-of-word-lengths/README.md: -------------------------------------------------------------------------------- 1 | - [023-maximum-product-of-word-lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) -------------------------------------------------------------------------------- /042-best-time-to-buy-and-sell-stock/README.md: -------------------------------------------------------------------------------- 1 | - [042-best-time-to-buy-and-sell-stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) -------------------------------------------------------------------------------- /046-binary-tree-postorder-traversal/README.md: -------------------------------------------------------------------------------- 1 | - [046-binary-tree-postorder-traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) -------------------------------------------------------------------------------- /070-longest-increasing-subsequence/README.md: -------------------------------------------------------------------------------- 1 | - [070-longest-increasing-subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) -------------------------------------------------------------------------------- /093-kth-largest-element-in-an-array/README.md: -------------------------------------------------------------------------------- 1 | - [093-kth-largest-element-in-an-array](https://leetcode.com/problems/kth-largest-element-in-an-array/) -------------------------------------------------------------------------------- /101-search-in-rotated-sorted-array/README.md: -------------------------------------------------------------------------------- 1 | - [101-search-in-rotated-sorted-array](https://leetcode.com/problems/search-in-rotated-sorted-array/) -------------------------------------------------------------------------------- /179-largest-rectangle-in-histogram/README.md: -------------------------------------------------------------------------------- 1 | - [179-largest-rectangle-in-histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) -------------------------------------------------------------------------------- /001-nim-game/bxbbok.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {boolean} 4 | */ 5 | var canWinNim = function(n) { 6 | return n%4===0; 7 | }; 8 | -------------------------------------------------------------------------------- /001-nim-game/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {boolean} 4 | */ 5 | var canWinNim = function(n) { 6 | return n%4!==0; 7 | }; 8 | -------------------------------------------------------------------------------- /095-intersection-of-two-linked-lists/README.md: -------------------------------------------------------------------------------- 1 | - [095-intersection-of-two-linked-lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) -------------------------------------------------------------------------------- /119-remove-nth-node-from-end-of-list/README.md: -------------------------------------------------------------------------------- 1 | - [119-remove-nth-node-from-end-of-list](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) -------------------------------------------------------------------------------- /190-evaluate-reverse-polish-notation/README.md: -------------------------------------------------------------------------------- 1 | - [190-evaluate-reverse-polish-notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) -------------------------------------------------------------------------------- /010-best-time-to-buy-and-sell-stock-ii/README.md: -------------------------------------------------------------------------------- 1 | - [010-best-time-to-buy-and-sell-stock-ii](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) -------------------------------------------------------------------------------- /033-remove-duplicates-from-sorted-list/README.md: -------------------------------------------------------------------------------- 1 | - [033-remove-duplicates-from-sorted-list](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) -------------------------------------------------------------------------------- /082-search-in-rotated-sorted-array-ii/README.md: -------------------------------------------------------------------------------- 1 | - [082-search-in-rotated-sorted-array-ii](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) -------------------------------------------------------------------------------- /084-different-ways-to-add-parentheses/README.md: -------------------------------------------------------------------------------- 1 | - [084-different-ways-to-add-parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) -------------------------------------------------------------------------------- /088-binary-tree-level-order-traversal/README.md: -------------------------------------------------------------------------------- 1 | - [088-binary-tree-level-order-traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) -------------------------------------------------------------------------------- /096-flatten-binary-tree-to-linked-list/README.md: -------------------------------------------------------------------------------- 1 | - [096-flatten-binary-tree-to-linked-list](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) -------------------------------------------------------------------------------- /216-best-time-to-buy-and-sell-stock-iv/README.md: -------------------------------------------------------------------------------- 1 | - [216-best-time-to-buy-and-sell-stock-iv](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/) -------------------------------------------------------------------------------- /038-find-minimum-in-rotated-sorted-array/README.md: -------------------------------------------------------------------------------- 1 | - [038-find-minimum-in-rotated-sorted-array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) -------------------------------------------------------------------------------- /068-binary-tree-level-order-traversal-ii/README.md: -------------------------------------------------------------------------------- 1 | - [068-binary-tree-level-order-traversal-ii](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) -------------------------------------------------------------------------------- /071-remove-duplicates-from-sorted-array/README.md: -------------------------------------------------------------------------------- 1 | - [071-remove-duplicates-from-sorted-array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) -------------------------------------------------------------------------------- /147-count-of-smaller-numbers-after-self/README.md: -------------------------------------------------------------------------------- 1 | - [147-count-of-smaller-numbers-after-self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) -------------------------------------------------------------------------------- /159-best-time-to-buy-and-sell-stock-iii/README.md: -------------------------------------------------------------------------------- 1 | - [159-best-time-to-buy-and-sell-stock-iii](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) -------------------------------------------------------------------------------- /001-nim-game/yrok.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {boolean} 4 | */ 5 | var canWinNim = function(n) { 6 | return (n%4)?true:false; 7 | 8 | }; -------------------------------------------------------------------------------- /137-letter-combinations-of-a-phone-number/README.md: -------------------------------------------------------------------------------- 1 | - [137-letter-combinations-of-a-phone-number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) -------------------------------------------------------------------------------- /146-remove-duplicates-from-sorted-list-ii/README.md: -------------------------------------------------------------------------------- 1 | - [146-remove-duplicates-from-sorted-list-ii](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) -------------------------------------------------------------------------------- /164-serialize-and-deserialize-binary-tree/README.md: -------------------------------------------------------------------------------- 1 | - [164-serialize-and-deserialize-binary-tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) -------------------------------------------------------------------------------- /055-find-minimum-in-rotated-sorted-array-ii/README.md: -------------------------------------------------------------------------------- 1 | - [055-find-minimum-in-rotated-sorted-array-ii](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) -------------------------------------------------------------------------------- /078-remove-duplicates-from-sorted-array-ii/README.md: -------------------------------------------------------------------------------- 1 | - [078-remove-duplicates-from-sorted-array-ii](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) -------------------------------------------------------------------------------- /121-lowest-common-ancestor-of-a-binary-tree/README.md: -------------------------------------------------------------------------------- 1 | - [121-lowest-common-ancestor-of-a-binary-tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) -------------------------------------------------------------------------------- /103-convert-sorted-list-to-binary-search-tree/README.md: -------------------------------------------------------------------------------- 1 | - [103-convert-sorted-list-to-binary-search-tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) -------------------------------------------------------------------------------- /133-binary-tree-zigzag-level-order-traversal/README.md: -------------------------------------------------------------------------------- 1 | - [133-binary-tree-zigzag-level-order-traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) -------------------------------------------------------------------------------- /219-add-and-search-word-data-structure-design/README.md: -------------------------------------------------------------------------------- 1 | - [219-add-and-search-word-data-structure-design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) -------------------------------------------------------------------------------- /220-substring-with-concatenation-of-all-words/README.md: -------------------------------------------------------------------------------- 1 | - [220-substring-with-concatenation-of-all-words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) -------------------------------------------------------------------------------- /035-convert-sorted-array-to-binary-search-tree/README.md: -------------------------------------------------------------------------------- 1 | - [035-convert-sorted-array-to-binary-search-tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) -------------------------------------------------------------------------------- /028-populating-next-right-pointers-in-each-node/README.md: -------------------------------------------------------------------------------- 1 | - [028-populating-next-right-pointers-in-each-node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) -------------------------------------------------------------------------------- /017-lowest-common-ancestor-of-a-binary-search-tree/README.md: -------------------------------------------------------------------------------- 1 | - [017-lowest-common-ancestor-of-a-binary-search-tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) -------------------------------------------------------------------------------- /045-best-time-to-buy-and-sell-stock-with-cooldown/README.md: -------------------------------------------------------------------------------- 1 | - [045-best-time-to-buy-and-sell-stock-with-cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) -------------------------------------------------------------------------------- /066-populating-next-right-pointers-in-each-node-ii/README.md: -------------------------------------------------------------------------------- 1 | - [066-populating-next-right-pointers-in-each-node-ii](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) -------------------------------------------------------------------------------- /211-longest-substring-without-repeating-characters/README.md: -------------------------------------------------------------------------------- 1 | - [211-longest-substring-without-repeating-characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) -------------------------------------------------------------------------------- /001-nim-game/glenye.js: -------------------------------------------------------------------------------- 1 | 2 | //书读的少,复杂的知识不知道,简单模拟了一下 3 | //当轮到对面一波团时,恰好剩下4个头,而对面只能取不多于3个,于是我方赢 4 | //另外从4的倍数往后一位数,则规律应是我方先手分别取1,2,3个能赢的一波团 5 | var canWinNim = function(n) { 6 | return n % 4 !== 0 7 | } -------------------------------------------------------------------------------- /132-construct-binary-tree-from-preorder-and-inorder-traversal/README.md: -------------------------------------------------------------------------------- 1 | - [132-construct-binary-tree-from-preorder-and-inorder-traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) -------------------------------------------------------------------------------- /001-nim-game/hpoenixf.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {boolean} 4 | */ 5 | var canWinNim = function(n) { 6 | if(n%4){ 7 | return true 8 | } else{ 9 | return false 10 | } 11 | }; -------------------------------------------------------------------------------- /120-construct-binary-tree-from-inorder-and-postorder-traversal/README.md: -------------------------------------------------------------------------------- 1 | - [120-construct-binary-tree-from-inorder-and-postorder-traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) -------------------------------------------------------------------------------- /002-add-digits/yrok.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} num 3 | * @return {number} 4 | */ 5 | var addDigits = function(num) { 6 | if(num===0){ 7 | return 0; 8 | }else{ 9 | return (num%9)?num%9:9; 10 | } 11 | 12 | }; -------------------------------------------------------------------------------- /003-single-number/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var singleNumber = function(nums) { 6 | var num; 7 | for(var i=0;i 0 && !(n & n - 1); 7 | }; 8 | 9 | console.log(isPowerOfTwo(4)); 10 | console.log(isPowerOfTwo(3)); 11 | console.log(isPowerOfTwo(0)); -------------------------------------------------------------------------------- /002-add-digits/crossken.js: -------------------------------------------------------------------------------- 1 | var addDigits = function(num) { 2 | var sum, arr = num.toString().split(''); 3 | while (arr.length>1) { 4 | sum = arr.reduce(function(prev,next){ 5 | return parseInt(prev) + parseInt(next); 6 | }); 7 | arr = sum.toString().split(''); 8 | } 9 | return parseInt(arr[0]); 10 | }; -------------------------------------------------------------------------------- /014-excel-sheet-column-number/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {number} 4 | */ 5 | var titleToNumber = function(s) { 6 | var arr=s.split(""),num=0; 7 | for(var i=0;i= 10) { 4 | int tmp = 0; 5 | while(num > 0) { 6 | tmp += num%10; 7 | num /= 10; 8 | } 9 | num = tmp; 10 | } 11 | return num; 12 | } 13 | } -------------------------------------------------------------------------------- /003-single-number/boyus.java: -------------------------------------------------------------------------------- 1 | /** 2 | * File name should be Solution.java 3 | * To differ from others' solution files, rename to yourusername.java 4 | */ 5 | 6 | public class Solution { 7 | public int singleNumber(int[] nums) { 8 | int result = 0; 9 | for(int i : nums) { 10 | result ^= i; 11 | } 12 | return result; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /002-add-digits/Yiqi_Ou.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} num 3 | * @return {number} 4 | */ 5 | var addDigits = function(num) { 6 | var tmp = 0; 7 | while(num >= 10) { 8 | while(num > 0) { 9 | tmp += (num % 10); 10 | num = Math.floor(num/10); 11 | } 12 | num = tmp; 13 | tmp = 0; 14 | } 15 | return num; 16 | }; 17 | -------------------------------------------------------------------------------- /013-contains-duplicate/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {boolean} 4 | */ 5 | var containsDuplicate = function(nums) { 6 | nums.sort(function arrSort(a,b){ 7 | return a-b; 8 | }); 9 | for(var i=0;i=10){ 7 | var minNum=0; 8 | var arr=num.toString().split(""); 9 | for(var i=0,len=arr.length;i= nums.length - 1) 8 | return true; 9 | max = Math.max(max, i+nums[i]); 10 | } 11 | return false; 12 | } 13 | } -------------------------------------------------------------------------------- /010-best-time-to-buy-and-sell-stock-ii/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} prices 3 | * @return {number} 4 | */ 5 | var maxProfit = function(prices) { 6 | if(prices.length===0){ 7 | return 0; 8 | } 9 | var profit=0; 10 | for(var i=0;i set = new HashSet(); 4 | if(nums == null || nums.length == 0) 5 | return false; 6 | for(int i = 0; i < nums.length; i++) { 7 | if(!set.contains(nums[i])) 8 | set.add(nums[i]); 9 | else 10 | return true; 11 | } 12 | return false; 13 | } 14 | } -------------------------------------------------------------------------------- /005-delete-node-in-a-linked-list/glen.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} node 10 | * @return {void} Do not return anything, modify node in-place instead. 11 | *///链表的操作有点麻烦,还是要多看几遍 12 | var deleteNode = function(node) { 13 | if (node === null) return; 14 | node.val = node.next.val; 15 | node.next = node.next.next; 16 | }; 17 | -------------------------------------------------------------------------------- /221-excel-sheet-column-title/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {string} 4 | */ 5 | var convertToTitle = function(n) { 6 | if(n<=26){ 7 | str=String.fromCharCode(n+64); 8 | return str; 9 | } 10 | var arr=[],m; 11 | while(n>=27){ 12 | m=(n-1)%26; 13 | arr.unshift(String.fromCharCode(m+65)); 14 | n-=m; 15 | n/=26; 16 | } 17 | arr.unshift(String.fromCharCode(n+64)); 18 | return arr.join(""); 19 | }; 20 | -------------------------------------------------------------------------------- /074-plus-one/yuanweiwu.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} digits 3 | * @return {number[]} 4 | */ 5 | var plusOne = function(digits) { 6 | for (var i = digits.length - 1; i >= 0; i--) { 7 | if (digits[i] == 9) 8 | digits[i] = 0; 9 | else { 10 | digits[i]++; 11 | return digits; 12 | } 13 | } 14 | 15 | digits.unshift(1); 16 | return digits; 17 | }; 18 | 19 | console.log(plusOne([ 9, 9 ])); 20 | console.log(plusOne([ 1, 9, 9 ])); 21 | -------------------------------------------------------------------------------- /030-search-insert-position/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int searchInsert(int[] nums, int target) { 3 | int left = 0, right = nums.length-1; 4 | while(left <= right) { 5 | int mid = (left + right) / 2; 6 | if(nums[mid] == target) 7 | return mid; 8 | else if(nums[mid] < target) 9 | left = mid + 1; 10 | else 11 | right = mid - 1; 12 | } 13 | return left; 14 | } 15 | } -------------------------------------------------------------------------------- /012-valid-anagram/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isAnagram(String s, String t) { 3 | int[] alpha = new int[26]; 4 | for(int i = 0; i < s.length(); i++) { 5 | alpha[s.charAt(i)-'a']++; 6 | } 7 | for(int i = 0; i < t.length(); i++) { 8 | alpha[t.charAt(i)-'a']--; 9 | } 10 | for(int i = 0; i < 26; i++) { 11 | if(alpha[i] != 0) 12 | return false; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /078-remove-duplicates-from-sorted-array-ii/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var removeDuplicates = function(nums) { 6 | var idx = 0, count; 7 | for(var i = 0; i < nums.length; i++) { 8 | if(i !== 0 && nums[i] === nums[i-1]) { 9 | count++; 10 | if(count >= 3) 11 | continue; 12 | } 13 | else { 14 | count = 1; 15 | } 16 | nums[idx++] = nums[i]; 17 | } 18 | return idx; 19 | }; -------------------------------------------------------------------------------- /030-search-insert-position/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number} 5 | */ 6 | var searchInsert = function(nums, target) { 7 | var left = 0, right = nums.length-1; 8 | while(left <= right) { 9 | var mid = Math.floor((left + right) / 2); 10 | if(nums[mid] === target) 11 | return mid; 12 | else if(nums[mid] < target) 13 | left = mid+1; 14 | else 15 | right = mid-1; 16 | } 17 | return left; 18 | }; -------------------------------------------------------------------------------- /056-rotate-image/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void rotate(int[][] matrix) { 3 | int n = matrix.length; 4 | for(int i = 0; i <= (n-1)/2; i++) { 5 | for(int j = i; j < n-1-i; j++) { 6 | int tmp = matrix[n-1-j][i]; 7 | matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; 8 | matrix[n-1-i][n-1-j] = matrix[j][n-1-i]; 9 | matrix[j][n-1-i] = matrix[i][j]; 10 | matrix[i][j] = tmp; 11 | } 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /001-nim-game/Yiqi_Ou.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {boolean} 4 | */ 5 | var canWinNim = function(n) { 6 | if(n >= 134882061) 7 | return n%4 !== 0; 8 | var state1 = true, state2 = true, state3 = true; 9 | var res = true; 10 | for(var i = 4; i <= n; i++) { 11 | if(state1 && state2 && state3) 12 | res = false; 13 | else 14 | res = true; 15 | state1 = state2; 16 | state2 = state3; 17 | state3 = res; 18 | } 19 | return res; 20 | }; -------------------------------------------------------------------------------- /098-subsets/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[][]} 4 | */ 5 | var subsets = function(nums) { 6 | var res = []; 7 | res.push([]); 8 | nums.sort(function(val1, val2) { 9 | return val1-val2; 10 | }); 11 | for(var i = 0; i < nums.length; i++) { 12 | var size = res.length; 13 | for(var j = 0; j < size; j++) { 14 | var set = [].concat(res[j]); 15 | set.push(nums[i]); 16 | res.push(set); 17 | } 18 | } 19 | return res; 20 | }; -------------------------------------------------------------------------------- /007-invert-binary-tree/glen.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {TreeNode} 11 | */ 12 | var invertTree = function(root) { 13 | if (root === null ) { 14 | return root; 15 | } 16 | else { 17 | var n = root.left; 18 | root.left = invertTree(root.right); 19 | root.right = invertTree(n); 20 | } 21 | return root; 22 | }; 23 | -------------------------------------------------------------------------------- /006-move-zeroes/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {void} Do not return anything, modify nums in-place instead. 4 | */ 5 | var moveZeroes = function(nums) { 6 | if(nums.length!==0){ 7 | //i用于遍历数组,index用于定位 8 | for(var i=0,index=0,len=nums.length;i= nums[left] && nums[left] > nums[right]) 9 | left = mid + 1; 10 | else 11 | right = mid; 12 | } 13 | return 0; 14 | } 15 | } -------------------------------------------------------------------------------- /011-product-of-array-except-self/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] productExceptSelf(int[] nums) { 3 | int[] output = new int[nums.length]; 4 | output[0] = 1; 5 | for(int i = 1; i < nums.length; i++) { 6 | output[i] = output[i-1] * nums[i-1]; 7 | } 8 | int outputAfter = nums[nums.length-1]; 9 | for(int i = nums.length-2; i >= 0; i--) { 10 | output[i] *= outputAfter; 11 | outputAfter = outputAfter * nums[i]; 12 | } 13 | return output; 14 | } 15 | } -------------------------------------------------------------------------------- /056-rotate-image/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @return {void} Do not return anything, modify matrix in-place instead. 4 | */ 5 | var rotate = function(matrix) { 6 | var n = matrix.length; 7 | for(var i = 0; i < Math.floor(n/2); i++) { 8 | for(var j = i; j < n-1-i; j++) { 9 | var tmp = matrix[n-1-j][i]; 10 | matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; 11 | matrix[n-1-i][n-1-j] = matrix[j][n-i-1]; 12 | matrix[j][n-1-i] = matrix[i][j]; 13 | matrix[i][j] = tmp; 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /057-container-with-most-water/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxArea(int[] height) { 3 | if(height.length == 0 || height.length == 1) 4 | return 0; 5 | int left = 0, right = height.length-1, res = 0; 6 | while(left < right) { 7 | int area = (right-left) * Math.min(height[left], height[right]); 8 | res = Math.max(res, area); 9 | if(height[left] < height[right]) 10 | left++; 11 | else 12 | right--; 13 | } 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /015-majority-element/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | 6 | //boyer-moore majority algorithm: use o(1) space and o(n) time, source: 7 | //http://gregable.com/2013/10/majority-vote-algorithm-find-majority.html 8 | 9 | var majorityElement = function(nums) { 10 | var candidate = nums[0], count = 0; 11 | nums.forEach(function(item) { 12 | if(count === 0) 13 | candidate = item; 14 | if(candidate === item) 15 | count++; 16 | else 17 | count--; 18 | }) 19 | return candidate; 20 | }; -------------------------------------------------------------------------------- /005-delete-node-in-a-linked-list/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | 10 | public class Solution { 11 | //I always delete the first node except the situation the list only 12 | //has one node 13 | public void deleteNode(ListNode node) { 14 | if(node == null) 15 | return; 16 | if(node.next == null) 17 | return; 18 | node.val = node.next.val; 19 | node.next = node.next.next; 20 | } 21 | } -------------------------------------------------------------------------------- /078-remove-duplicates-from-sorted-array-ii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | if(nums.length < 3) 4 | return nums.length; 5 | int idx = 1; 6 | for(int i = 2; i < nums.length; i++) { 7 | if(nums[i] == nums[idx]) { 8 | if(nums[i] == nums[idx-1]) 9 | continue; 10 | else 11 | nums[++idx] = nums[i]; 12 | } 13 | else 14 | nums[++idx] = nums[i]; 15 | } 16 | return idx+1; 17 | } 18 | } -------------------------------------------------------------------------------- /008-same-tree/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public boolean isSameTree(TreeNode p, TreeNode q) { 12 | if(p == null && q == null) 13 | return true; 14 | if(p == null || q == null) 15 | return false; 16 | if(p.val != q.val) 17 | return false; 18 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 19 | } 20 | } -------------------------------------------------------------------------------- /011-product-of-array-except-self/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var productExceptSelf = function(nums) { 6 | var output = []; 7 | output.push(1); 8 | //output[i] represents the production from a[0] ... a[i-1] 9 | for(var i = 1; i < nums.length; i++) { 10 | output.push(output[i-1] * nums[i-1]); 11 | } 12 | var outputAfter = nums[nums.length-1]; 13 | for(i = nums.length-2; i >= 0; i--) { 14 | output[i] = outputAfter * output[i]; 15 | outputAfter = outputAfter * nums[i]; 16 | } 17 | return output; 18 | }; -------------------------------------------------------------------------------- /006-move-zeroes/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void moveZeroes(int[] nums) { 3 | //let idx represents the number of non-zero values we found. Each time we find new 4 | //non-zero value, just put the value to that position 5 | int idx = 0; 6 | for(int i = 0; i < nums.length; i++) { 7 | if(nums[i] != 0) { 8 | nums[idx] = nums[i]; 9 | idx++; 10 | } 11 | } 12 | //change all the value after idx to zero 13 | for(int i = idx; i < nums.length; i++) 14 | nums[i] = 0; 15 | } 16 | } -------------------------------------------------------------------------------- /009-single-number-iii/Yiqi_Ou.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var singleNumber = function(nums) { 6 | var xor = 0; 7 | nums.forEach(function(item, idx, array) { 8 | xor ^= item; 9 | }); 10 | var mask = 1; 11 | while((xor & mask) === 0) 12 | mask <<= 1; 13 | var res = []; 14 | var num1 = 0, num2 = 0; 15 | nums.forEach(function(item, idx, array) { 16 | if(item & mask) 17 | num1 ^= item; 18 | else 19 | num2 ^= item; 20 | }); 21 | res.push(num1, num2); 22 | return res; 23 | }; -------------------------------------------------------------------------------- /212-maximum-product-subarray/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProduct(int[] nums) { 3 | if(nums.length == 0) 4 | return 0; 5 | int res = nums[0], min = nums[0], max = nums[0]; 6 | for(int i = 1; i < nums.length; i++) { 7 | int tmpMax = max*nums[i], tmpMin = min*nums[i]; 8 | max = Math.max(tmpMax, tmpMin); 9 | max = Math.max(max, nums[i]); 10 | min = Math.min(tmpMax, tmpMin); 11 | min = Math.min(min, nums[i]); 12 | res = Math.max(res, max); 13 | } 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /002-add-digits/glen.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} num 3 | * @return {number} 4 | */ 5 | //模拟111个人头=100+10+1=99+9+3,99和9都能被整除,余3 6 | //考虑为9的倍数如18,27等return num%9 余0 7 | var addDigits = function(num) { 8 | return (num-1)%9+1 9 | }; 10 | 11 | 12 | //好多坑 13 | var addDigits = function(num) { 14 | num = typeof num === 'number' ? num.toString() : num; 15 | var len = num.length, 16 | i; 17 | var rel = 0; 18 | 19 | while(len > 1) { 20 | for (i = 0; i < len; i++) { 21 | rel += parseInt(num[i]); 22 | } 23 | num = rel.toString(); 24 | rel = 0; 25 | len = num.length; 26 | 27 | } 28 | return parseInt(num) 29 | } -------------------------------------------------------------------------------- /212-maximum-product-subarray/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var maxProduct = function(nums) { 6 | if(nums.length === 0) 7 | return 0; 8 | var res = nums[0], min = nums[0], max = nums[0]; 9 | nums.forEach(function(item, idx) { 10 | //ignore the first element 11 | if(idx === 0) 12 | return; 13 | var tmpMax = max; 14 | max = Math.max(Math.max(max * item, min * item), item); 15 | min = Math.min(Math.min(tmpMax * item, min * item), item); 16 | res = Math.max(res, max); 17 | }) 18 | return res; 19 | }; -------------------------------------------------------------------------------- /098-subsets/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> subsets(int[] nums) { 3 | List> res = new ArrayList<>(); 4 | res.add(new ArrayList()); 5 | Arrays.sort(nums); 6 | for(int i = 0; i < nums.length; i++) { 7 | int size = res.size(); 8 | for(int j = 0; j < size; j++) { 9 | List iter = res.get(j); 10 | List set = new ArrayList<>(iter); 11 | set.add(nums[i]); 12 | res.add(set); 13 | } 14 | } 15 | return res; 16 | } 17 | } -------------------------------------------------------------------------------- /008-same-tree/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} p 10 | * @param {TreeNode} q 11 | * @return {boolean} 12 | */ 13 | var isSameTree = function(p, q) { 14 | if(p===null && q===null){ 15 | return true; 16 | } 17 | if(p===null || q===null){ 18 | return false; 19 | } 20 | if(p.val===q.val){ 21 | return isSameTree(p.left,q.left) && isSameTree(p.right,q.right); 22 | }else{ 23 | return false; 24 | } 25 | 26 | }; 27 | -------------------------------------------------------------------------------- /052-power-of-two/yuanweiwu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | bool isPowerOfTwo(int n) { 8 | // int count = 0; 9 | // while (n > 0) { 10 | // count += n & 1; 11 | // n >>= 1; 12 | // } 13 | // return count == 1; 14 | 15 | return n > 0 && bitset<32>(n).count() == 1; 16 | } 17 | }; 18 | 19 | int main() { 20 | Solution sol = Solution(); 21 | cout << sol.isPowerOfTwo(4) << endl; 22 | cout << sol.isPowerOfTwo(3) << endl; 23 | cout << sol.isPowerOfTwo(0) << endl; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # toy-problems 2 | Interview prep 3 | 4 | ### LeetCode链接 5 | - [LeetCode -> Problems -> Algorithms](https://leetcode.com/problemset/algorithms/) 6 | 7 | ### 说明 8 | - 题目顺序根据Acceptance由高到底排列,如001-nim-game,为最简单的题目 9 | - 在自己的LeetCode上提交代码,看看自己有没有Accepted 10 | - Accepted后,可在Discuss中查看网友代码,学习比较 11 | - 一道题可写多种解决方法,并在本地跑一遍测试运行时间,看哪个更优 12 | - 做完一题后Pull request到相应题目的文件夹,大家互相学习 13 | 14 | ### 个人代码命名方式 15 | - 为了解决文件名冲突和日后统计,请按这个格式命名`yourname.js`,如`vickymin.js` 16 | 17 | ### Pull request方法 18 | - fork这个repo,看见右上方的fork按钮了吗?点它 19 | - 去你自己fork完的repo,点"Clone in Desktop",或者你用命令行也可 20 | - 将自己写的代码push到fork完的repo 21 | - 然后在你自己fork完的repo里面点Pull requests -> New pull request 22 | -------------------------------------------------------------------------------- /027-find-the-duplicate-number/Yiqi.java: -------------------------------------------------------------------------------- 1 | //floyd's cycle detecting algorithms: https://leetcode.com/discuss/69766/share-my-solution-o-n-time-o-1-space-12-ms 2 | public class Solution { 3 | public int findDuplicate(int[] nums) { 4 | int slow = 0, fast = 0; 5 | while(true) { 6 | slow = nums[slow]; 7 | fast = nums[nums[fast]]; 8 | if(slow == fast) 9 | break; 10 | } 11 | int find = 0; 12 | while(true) { 13 | slow = nums[slow]; 14 | find = nums[find]; 15 | if(slow == find) 16 | break; 17 | } 18 | return slow; 19 | } 20 | } -------------------------------------------------------------------------------- /053-minimum-path-sum/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minPathSum(int[][] grid) { 3 | if(grid.length == 0 || grid[0].length == 0) 4 | return 0; 5 | int[] res = new int[grid[0].length]; 6 | res[0] = grid[0][0]; 7 | for(int i = 1; i < res.length; i++) 8 | res[i] += res[i-1] + grid[0][i]; 9 | 10 | for(int i = 1; i < grid.length; i++) { 11 | res[0] += grid[i][0]; 12 | for(int j = 1; j < grid[0].length; j++) { 13 | res[j] = Math.min(res[j], res[j-1]) + grid[i][j]; 14 | } 15 | } 16 | return res[grid[0].length-1]; 17 | } 18 | } -------------------------------------------------------------------------------- /001-nim-game/newraina.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {boolean} 4 | */ 5 | var canWinNim_1 = function(n) { 6 | // 方法一:正常求余 7 | return !(n % 4 === 0); 8 | }; 9 | 10 | var canWinNim_2 = function(n) { 11 | // 方法二:位运算求余 12 | return !!(n & 0x3); 13 | }; 14 | 15 | 16 | // 本机测试运行时间 17 | var start, end; 18 | var count = 90000000; 19 | 20 | start = new Date(); 21 | for (var i = 0; i < count; i++) { 22 | canWinNim_1(i); 23 | }; 24 | end = new Date(); 25 | console.log('canWinNim_1: '+ (end - start)); 26 | 27 | start = new Date(); 28 | for (var i = 0; i < count; i++) { 29 | canWinNim_2(i); 30 | }; 31 | end = new Date(); 32 | console.log('canWinNim_2: '+ (end - start)); -------------------------------------------------------------------------------- /007-invert-binary-tree/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {TreeNode} 11 | */ 12 | var invertTree = function(root) { 13 | //判断root是否合理 14 | if(root===null){ 15 | return null; 16 | }else{ 17 | var node=root.left; 18 | root.left=root.right; 19 | root.right=node; 20 | } 21 | //判断左右分支是否存在 22 | if(root.left){ 23 | invertTree(root.left); 24 | } 25 | if(root.right){ 26 | invertTree(root.right); 27 | } 28 | return root; 29 | }; 30 | -------------------------------------------------------------------------------- /012-valid-anagram/byenow.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @param {string} t 4 | * @return {boolean} 5 | */ 6 | var isAnagram = function(s, t) { 7 | var sArr=s.split(""); 8 | var tArr=t.split(""); 9 | //数组长度不等,直接false 10 | if(sArr.length!==tArr.length){ 11 | return false; 12 | } 13 | for(var i=0;i 0) 18 | numOfPath[j] += numOfPath[j-1]; 19 | }); 20 | }); 21 | return numOfPath[obstacleGrid[0].length-1]; 22 | }; -------------------------------------------------------------------------------- /009-single-number-iii/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] singleNumber(int[] nums) { 3 | int[] res = new int[2]; 4 | if(nums == null || nums.length < 2) 5 | return res; 6 | int xor = 0; 7 | for(int i : nums) 8 | xor = xor ^ i; 9 | int mask = 1; 10 | while((xor & mask) == 0) 11 | mask = mask << 1; 12 | int xor1 = 0, xor2 = 0; 13 | for(int i : nums) { 14 | if((mask & i) == 0) { 15 | xor1 = xor1 ^ i; 16 | } 17 | else { 18 | xor2 = xor2 ^ i; 19 | } 20 | } 21 | res[0] = xor1; 22 | res[1] = xor2; 23 | return res; 24 | } 25 | } -------------------------------------------------------------------------------- /016-binary-tree-preorder-traversal/Yiqi_Ou.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[]} 11 | */ 12 | var preorderTraversal = function(root) { 13 | var stack = [], res = []; 14 | if(root === null) 15 | return res; 16 | var cur = root; 17 | while(cur !== null || stack.length) { 18 | if(cur !== null) { 19 | res.push(cur.val); 20 | if(cur.right !== null) 21 | stack.push(cur.right); 22 | cur = cur.left; 23 | } 24 | else { 25 | cur = stack.pop(); 26 | } 27 | } 28 | return res; 29 | }; -------------------------------------------------------------------------------- /104-subsets-ii/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[][]} 4 | */ 5 | var subsetsWithDup = function(nums) { 6 | var res = []; 7 | res.push([]); 8 | nums.sort(function(val1, val2) { 9 | return val1-val2; 10 | }); 11 | //init 12 | if(nums.length === 0) 13 | return res; 14 | res.push([nums[0]]); 15 | var lastAddNum = 1; 16 | for(var i = 1; i < nums.length; i++) { 17 | var begin = nums[i] === nums[i-1] ? res.length-lastAddNum : 0; 18 | var end = res.length; 19 | lastAddNum = 0; 20 | for(var j = begin; j < end; j++) { 21 | var set = [].concat(res[j]); 22 | set.push(nums[i]); 23 | res.push(set); 24 | lastAddNum++; 25 | } 26 | } 27 | return res; 28 | }; -------------------------------------------------------------------------------- /143-combination-sum-ii/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} candidates 3 | * @param {number} target 4 | * @return {number[][]} 5 | */ 6 | var combinationSum = function(candidates, target) { 7 | var res = [], items = []; 8 | candidates.sort(function(val1, val2) { 9 | return val1-val2; 10 | }); 11 | helper(res, items, candidates, 0, target); 12 | return res; 13 | }; 14 | 15 | function helper(res, items, candidates, start, target) { 16 | if(target === 0) { 17 | var tmp = items.concat(); 18 | res.push(tmp); 19 | } 20 | for(var i = start; i < candidates.length; i++) { 21 | if(candidates[i] > target) 22 | return; 23 | items.push(candidates[i]); 24 | helper(res, items, candidates, i, target-candidates[i]); 25 | items.pop(); 26 | } 27 | } -------------------------------------------------------------------------------- /100-combination-sum/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} candidates 3 | * @param {number} target 4 | * @return {number[][]} 5 | */ 6 | var combinationSum = function(candidates, target) { 7 | var res = [], items = []; 8 | candidates.sort(function(val1, val2) { 9 | return val1-val2; 10 | }); 11 | helper(res, items, candidates, 0, target); 12 | return res; 13 | }; 14 | 15 | function helper(res, items, candidates, start, target) { 16 | if(target === 0) { 17 | var tmp = []; 18 | tmp.push.apply(tmp, items); 19 | res.push(tmp); 20 | } 21 | for(var i = start; i < candidates.length; i++) { 22 | if(candidates[i] > target) 23 | return; 24 | items.push(candidates[i]); 25 | helper(res, items, candidates, i, target-candidates[i]); 26 | items.pop(); 27 | } 28 | } -------------------------------------------------------------------------------- /074-plus-one/yuanweiwu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector plusOne(vector& digits) { 8 | for (int i = digits.size() - 1; i >= 0; i--) { 9 | if (digits[i] == 9) 10 | digits[i] = 0; 11 | else { 12 | digits[i]++; 13 | return digits; 14 | } 15 | } 16 | 17 | digits[0] = 1; 18 | digits.push_back(0); 19 | 20 | return digits; 21 | } 22 | }; 23 | 24 | int main() { 25 | vector input; 26 | input.push_back(1); 27 | input.push_back(9); 28 | 29 | Solution sol = Solution(); 30 | vector output = sol.plusOne(input); 31 | 32 | for (int i = 0; i < output.size(); i++) 33 | cout << output[i]; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /110-triangle/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minimumTotal(List> triangle) { 3 | if(triangle.size() == 0 || triangle.get(0).size() == 0) 4 | return 0; 5 | int[] res = new int[triangle.get(triangle.size()-1).size()]; 6 | res[0] = triangle.get(0).get(0); 7 | for(int i = 1; i < triangle.size(); i++) { 8 | List nums = triangle.get(i); 9 | res[nums.size()-1] = nums.get(nums.size()-1) + res[nums.size()-2]; 10 | for(int j = nums.size()-2; j > 0; j--) { 11 | res[j] = Math.min(res[j], res[j-1]) + nums.get(j); 12 | } 13 | res[0] += nums.get(0); 14 | } 15 | int min = res[0]; 16 | for(int i = 0; i < res.length; i++) 17 | min = Math.min(res[i], min); 18 | return min; 19 | } 20 | } -------------------------------------------------------------------------------- /059-combination-sum-iii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> combinationSum3(int k, int n) { 3 | List> res = new ArrayList<>(); 4 | List items = new ArrayList<>(); 5 | helper(res, items, k, 1, n); 6 | return res; 7 | } 8 | 9 | public void helper(List> res, List items, int k, int start, int target) { 10 | if(k == 0 && target == 0) { 11 | res.add(new ArrayList(items)); 12 | return; 13 | } 14 | else if(k == 0) 15 | return; 16 | for(int i = start; i <= 9; i++) { 17 | if(i > target) 18 | return; 19 | items.add(i); 20 | helper(res, items, k-1, i+1, target-i); 21 | items.remove(items.size()-1); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /003-single-number/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | 3 | public class Solution { 4 | //the HasSet version, use o(n) space and o(n) time 5 | public int singleNumber(int[] nums) { 6 | HashSet set = new HashSet<>(); 7 | for(int i = 0; i < nums.length; i++) { 8 | if(set.contains(nums[i])) 9 | set.remove(nums[i]); 10 | else 11 | set.add(nums[i]); 12 | } 13 | int res = -1; 14 | if(!set.isEmpty()) 15 | res = set.iterator().next(); 16 | return res; 17 | } 18 | 19 | //the xor version, xor two numbers will get 0. The left one 20 | //is the unique number 21 | public int singleNumber2(int[] nums) { 22 | int res = 0; 23 | for(int i = 0; i < nums.length; i++) 24 | res ^= nums[i]; 25 | return res; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /016-binary-tree-preorder-traversal/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public List preorderTraversal(TreeNode root) { 12 | Stack stack = new Stack<>(); 13 | List res = new ArrayList<>(); 14 | TreeNode cur = root; 15 | while(cur != null || !stack.isEmpty()) { 16 | if(cur != null) { 17 | res.add(cur.val); 18 | if(cur.right != null) 19 | stack.push(cur.right); 20 | cur = cur.left; 21 | } 22 | else { 23 | cur = stack.pop(); 24 | } 25 | } 26 | return res; 27 | } 28 | } -------------------------------------------------------------------------------- /003-single-number/newraina.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | 6 | // 暴力循环,超时了 7 | var singleNumber_1 = function(nums) { 8 | nums = nums.sort(function(a, b) { 9 | return a - b; 10 | }); 11 | var i, len; 12 | for (i = 0, len = nums.length; i < len - 1; i++) { 13 | if (nums[i] !== nums[i + 1]) { 14 | break; 15 | } else { 16 | i++; 17 | } 18 | } 19 | return nums[i]; 20 | }; 21 | 22 | // 异或 23 | var singleNumber_2 = function(nums) { 24 | return nums.reduce(function(x, y) { 25 | return x ^ y; 26 | }); 27 | }; 28 | 29 | // 超时了 30 | var singleNumber_3 = function (nums) { 31 | var temp = [], site; 32 | nums.forEach(function (v, i, a) { 33 | site = temp.indexOf(v); 34 | if(site < 0) temp.push(v); 35 | else temp.splice(site, 1); 36 | }); 37 | return temp[0]; 38 | }; 39 | -------------------------------------------------------------------------------- /110-triangle/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} triangle 3 | * @return {number} 4 | */ 5 | var minimumTotal = function(triangle) { 6 | if(triangle.length === 0 || triangle[0].length === 0) 7 | return 0; 8 | var res = []; 9 | //init res array so that the length equals to last row of triangle 10 | for(var i = 0; i < triangle[triangle.length-1].length; i++) 11 | res.push(0); 12 | res[0] = triangle[0][0]; 13 | for(i = 1; i < triangle.length; i++) { 14 | var nums = triangle[i]; 15 | res[nums.length-1] = nums[nums.length-1] + res[nums.length-2]; 16 | for(var j = nums.length-2; j >= 1; j--) 17 | res[j] = Math.min(res[j], res[j-1]) + nums[j]; 18 | res[0] += nums[0]; 19 | } 20 | var min = res[0]; 21 | res.forEach(function(item) { 22 | min = Math.min(min, item); 23 | }); 24 | return min; 25 | }; -------------------------------------------------------------------------------- /007-invert-binary-tree/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public TreeNode invertTree(TreeNode root) { 12 | if(root == null) 13 | return root; 14 | Stack treeStack = new Stack(); 15 | treeStack.push(root); 16 | while(!treeStack.isEmpty()) { 17 | TreeNode cur = treeStack.pop(); 18 | TreeNode tmp = cur.left; 19 | cur.left = cur.right; 20 | cur.right = tmp; 21 | if(cur.left != null) 22 | treeStack.push(cur.left); 23 | if(cur.right != null) 24 | treeStack.push(cur.right); 25 | } 26 | return root; 27 | } 28 | } -------------------------------------------------------------------------------- /100-combination-sum/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> combinationSum(int[] candidates, int target) { 3 | List> res = new ArrayList<>(); 4 | List items = new ArrayList<>(); 5 | Arrays.sort(candidates); 6 | helper(res, items, candidates, 0, target); 7 | return res; 8 | } 9 | 10 | public void helper(List> res, List items, int[] candidates, int start, int target) { 11 | //base case 12 | if(target == 0) { 13 | res.add(new ArrayList(items)); 14 | return; 15 | } 16 | for(int i = start; i < candidates.length; i++) { 17 | if(candidates[i] > target) 18 | return; 19 | items.add(candidates[i]); 20 | helper(res, items, candidates, i, target-candidates[i]); 21 | items.remove(items.size()-1); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /063-find-peak-element/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findPeakElement(int[] nums) { 3 | if(nums.length == 0 || nums.length == 1) 4 | return 0; 5 | int left = 0, right = nums.length-1; 6 | while(left + 1 < right) { 7 | int mid = (left + right)/2; 8 | //since the way I write binary search will not touch first or 9 | //last element it is safe to test mid's neighbour element 10 | if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]) 11 | return mid; 12 | //search left part 13 | else if(nums[mid-1] > nums[mid]) 14 | right = mid; 15 | //search right part 16 | else 17 | left = mid; 18 | } 19 | //Since it must contains peak element, it is either in left or right 20 | if(nums[left] > nums[right]) 21 | return left; 22 | return right; 23 | } 24 | } -------------------------------------------------------------------------------- /159-best-time-to-buy-and-sell-stock-iii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | if(prices.length == 0) 4 | return 0; 5 | int[] res = new int[prices.length]; 6 | int minBefore = prices[0]; 7 | //calculate maxProfit if we do one transaction and sell at day i 8 | for(int i = 0; i < res.length; i++) { 9 | res[i] = Math.max(0, prices[i]-minBefore); 10 | minBefore = Math.min(minBefore, prices[i]); 11 | } 12 | //calculate maxProfit if we do two transactions 13 | int maxProfitAfter = 0, maxAfter = prices[prices.length-1], maxProfit = 0; 14 | for(int i = res.length-1; i >= 0; i--) { 15 | maxProfitAfter = Math.max(maxProfitAfter, maxAfter-prices[i]); 16 | maxAfter = Math.max(maxAfter, prices[i]); 17 | maxProfit = Math.max(maxProfit, res[i] + maxProfitAfter); 18 | } 19 | return maxProfit; 20 | } 21 | } -------------------------------------------------------------------------------- /111-unique-paths-ii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int uniquePathsWithObstacles(int[][] obstacleGrid) { 3 | int n = obstacleGrid.length, m = obstacleGrid[0].length; 4 | int[] numOfPath = new int[m]; 5 | //initialize first row 6 | int idx = 0; 7 | for(idx = 0; idx < m; idx++) { 8 | if(obstacleGrid[0][idx] == 1) 9 | break; 10 | } 11 | for(int i = 0; i < idx; i++) { 12 | numOfPath[i] = 1; 13 | } 14 | for(int i = 1; i < n; i++) { 15 | for(int j = 0; j < m; j++) { 16 | //since for case j == 0, we only change numOfPath[0] to 0 when 17 | //obstacleGrid[i][j] == 1 18 | if(obstacleGrid[i][j] == 1) 19 | numOfPath[j] = 0; 20 | else if(j > 0) { 21 | numOfPath[j] += numOfPath[j-1]; 22 | } 23 | } 24 | } 25 | return numOfPath[m-1]; 26 | } 27 | } -------------------------------------------------------------------------------- /083-trapping-rain-water/Yiqi.java: -------------------------------------------------------------------------------- 1 | //source: http://codeganker.blogspot.com/search?q=Trapping+Rain+Water 2 | public class Solution { 3 | public int trap(int[] height) { 4 | if (height.length == 0 || height.length == 1) 5 | return 0; 6 | int left = 0, right = height.length-1; 7 | int maxL = 0, maxR = 0, res = 0; 8 | //we should not stop when left == right 9 | while(left <= right) { 10 | //if the height of max bar on current index's left is lower than 11 | //the height of max bar on current index's right 12 | if(maxL <= maxR) { 13 | res += maxL > height[left] ? (maxL-height[left]) : 0; 14 | maxL = Math.max(maxL, height[left]); 15 | left++; 16 | } else { 17 | res += maxR > height[right] ? (maxR-height[right]) : 0; 18 | maxR = Math.max(maxR, height[right]); 19 | right--; 20 | } 21 | } 22 | return res; 23 | } 24 | } -------------------------------------------------------------------------------- /143-combination-sum-ii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> combinationSum2(int[] candidates, int target) { 3 | List> res = new ArrayList<>(); 4 | List items = new ArrayList<>(); 5 | Arrays.sort(candidates); 6 | helper(res, items, candidates, 0, target); 7 | return res; 8 | } 9 | 10 | public void helper(List> res, List items, int[] candidates, int start, int target) { 11 | if(target == 0) { 12 | res.add(new ArrayList(items)); 13 | return; 14 | } 15 | for(int i = start; i < candidates.length; i++) { 16 | if(i != start && candidates[i] == candidates[i-1]) 17 | continue; 18 | if(candidates[i] > target) 19 | return; 20 | items.add(candidates[i]); 21 | helper(res, items, candidates, i+1, target-candidates[i]); 22 | items.remove(items.size()-1); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /038-find-minimum-in-rotated-sorted-array/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var findMin = function(nums) { 6 | if(nums.length === 0) 7 | return -1; 8 | var left = 0, right = nums.length-1; 9 | while(left <= right) { 10 | var mid = Math.floor((left + right) / 2); 11 | //base case 1: the nums is ascending order 12 | if(nums[left] < nums[right]) 13 | return nums[left]; 14 | //base case 2: one numbers left 15 | else if(left === right) 16 | return nums[left]; 17 | //base case 3: two numbers left, since there is base case 1 18 | //the nums[right] must be less than nums[left] 19 | else if(left === right - 1) 20 | return nums[right]; 21 | //normally binary search: right part 22 | else if(nums[mid] > nums[left]) 23 | left = mid + 1; 24 | //if(nums[mid] < nums[right]): left part 25 | else 26 | right = mid; 27 | } 28 | //do not return anything 29 | }; -------------------------------------------------------------------------------- /040-unique-paths/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} m 3 | * @param {number} n 4 | * @return {number} 5 | */ 6 | 7 | //The problem has an obvious recursive pattern. The number of paths to grid[i][j] 8 | //is equal to grid[i-1][j] + grid[i][j]. So we can easily solve it recursively. 9 | //But in that way, we compute the same results repeatedly which makes me think that 10 | //we can use dynamic programming to reduce time complexity. We can define a two dimension 11 | //array and compute from grid[0][0]. Thus we got time complexity o(n*m) solution. 12 | //I notice the value of grid[i][j] is only based on grid[i-1][j] and grid[i][j-1], current value's 13 | //upper and left element. Thus we can reduce space by using one dimensional array 14 | var uniquePaths = function(m, n) { 15 | var numOfPath = []; 16 | for(var i = 0; i < n; i++) 17 | numOfPath.push(1); 18 | for(i = 1; i < m; i++) { 19 | for(var j = 1; j < n; j++) { 20 | numOfPath[j] += numOfPath[j-1]; 21 | } 22 | } 23 | return numOfPath[n-1]; 24 | }; -------------------------------------------------------------------------------- /180-first-missing-positive/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int firstMissingPositive(int[] nums) { 3 | if(nums.length == 0) 4 | return 1; 5 | for (int i = 0; i < nums.length; i++) { 6 | //use counting sort: move positive numbers to the position where 7 | //index equals to the number. nums[nums[i]] != nums[i] is used to 8 | //avoid infinite loop 9 | if (nums[i] > 0 && nums[i] < nums.length && nums[nums[i]] != nums[i]) { 10 | int tmp = nums[i]; 11 | nums[i] = nums[nums[i]]; 12 | nums[tmp] = tmp; 13 | //do same operation on the number swapped back again in next iteration 14 | i--; 15 | } 16 | } 17 | for (int i = 1; i < nums.length; i++) { 18 | if (nums[i] != i) 19 | return i; 20 | } 21 | //since 1 ... nums.length-1 exists in array, test nums.length exist or not 22 | return nums[0] == nums.length ? nums.length+1 : nums.length; 23 | } 24 | } -------------------------------------------------------------------------------- /055-find-minimum-in-rotated-sorted-array-ii/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var findMin = function(nums) { 6 | var left = 0, right = nums.length - 1; 7 | while(left <= right) { 8 | var mid = Math.floor((left + right)/2); 9 | //base case 1: there is only one number left 10 | if(left === right) 11 | return nums[left]; 12 | //base case 2: the number is ascending order 13 | else if(nums[left] < nums[right]) 14 | return nums[left]; 15 | //base case 3: there are two numbers left and 16 | //nums[right] <= nums[left] 17 | else if(left === right-1) 18 | return nums[right]; 19 | //normal binary search: left part 20 | else if(nums[mid] <= nums[right] && nums[left] > nums[right]) 21 | right = mid; 22 | //normal binary search: right part 23 | else if(nums[mid] >= nums[left] && nums[left] > nums[right]) 24 | left = mid + 1; 25 | //special case: nums[mid] === nums[left] === nums[right] 26 | else 27 | left++; 28 | } 29 | }; -------------------------------------------------------------------------------- /117-search-for-a-range/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] searchRange(int[] nums, int target) { 3 | int left = 0, right = nums.length-1; 4 | int[] res = new int[2]; 5 | res[0] = -1; 6 | res[1] = -1; 7 | //search left edge 8 | while(left < right - 1) { 9 | int mid = (left + right) / 2; 10 | if(nums[mid] >= target) 11 | right = mid; 12 | else 13 | left = mid; 14 | } 15 | if(nums[left] == target) 16 | res[0] = left; 17 | else if(nums[right] == target) 18 | res[0] = right; 19 | left = 0; 20 | right = nums.length-1; 21 | //search right edge 22 | while(left < right - 1) { 23 | int mid = (left + right)/2; 24 | if(nums[mid] <= target) 25 | left = mid; 26 | else 27 | right = mid; 28 | } 29 | if(nums[right] == target) 30 | res[1] = right; 31 | else if(nums[left] == target) 32 | res[1] = left; 33 | return res; 34 | } 35 | } -------------------------------------------------------------------------------- /124-3sum-closest/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int threeSumClosest(int[] nums, int target) { 3 | if(nums.length < 3) 4 | return -1; 5 | int minDiff = Integer.MAX_VALUE; 6 | Arrays.sort(nums); 7 | for(int i = 0; i < nums.length-2; i++) { 8 | if(i != 0 && nums[i] == nums[i-1]) 9 | continue; 10 | int diff = twoSum(nums, i+1, target-nums[i]); 11 | if(Math.abs(diff) < Math.abs(minDiff)) 12 | minDiff = diff; 13 | } 14 | return target-minDiff; 15 | } 16 | 17 | public int twoSum(int[] nums, int start, int target) { 18 | int left = start, right = nums.length-1, minDiff = Integer.MAX_VALUE; 19 | while(left < right) { 20 | int diff = target-nums[left]-nums[right]; 21 | if(Math.abs(diff) < Math.abs(minDiff)) 22 | minDiff = diff; 23 | if(diff == 0) 24 | return 0; 25 | else if(diff > 0) 26 | left++; 27 | else 28 | right--; 29 | } 30 | return minDiff; 31 | } 32 | } -------------------------------------------------------------------------------- /086-longest-consecutive-sequence/Yiqi.java: -------------------------------------------------------------------------------- 1 | //the idea is consider it as a graph problem. Find consecutive sequence is equivalent 2 | //to bfs all nodes in this graph. This algorighm run o(n) time. 3 | //source: http://codeganker.blogspot.com/2014/04/longest-consecutive-sequence-leetcode.html 4 | public class Solution { 5 | public int longestConsecutive(int[] nums) { 6 | HashSet set = new HashSet<>(); 7 | int res = 0; 8 | for (int i : nums) { 9 | set.add(i); 10 | } 11 | for(int i : nums) { 12 | res = Math.max(res, bfs(i, set)); 13 | } 14 | return res; 15 | } 16 | 17 | public int bfs(int start, HashSet set) { 18 | if (!set.contains(start)) 19 | return 0; 20 | int res = 1; 21 | int nei = start + 1; 22 | while (set.contains(nei)) { 23 | set.remove(nei); 24 | nei++; 25 | res++; 26 | } 27 | nei = start-1; 28 | while (set.contains(nei)) { 29 | set.remove(nei); 30 | nei--; 31 | res++; 32 | } 33 | return res; 34 | } 35 | } -------------------------------------------------------------------------------- /001-nim-game/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * I use dynamic programming to solve the problem. let f(x) represents whether the person can win or not 4 | * if there are x stones. So the recursion function is like: 5 | * 6 | * f(x) = false, if f(x-1) && f(x-2) && f(x-3) == true 7 | * f(x) = true, if f(x-1) && f(x-2) && f(x-3) == false 8 | 9 | * f(x) is false if f(x-1), f(x-2), f(x-3) are all true, meaning that no mater you pick 1, 2 10 | * or 3, your opponent will always get a true. 11 | * f(x) is true if one of f(x-1), f(x-2), f(x-3) is false, 12 | * meaning that you can always pick stones and leave a false state to your opponent. 13 | */ 14 | public boolean canWinNim(int n) { 15 | //the program can not pass the large test cases, so do some trick here. 16 | if(n >= 134882061) return n%4 != 0; 17 | boolean state1 = true, state2 = true, state3 = true, res = true; 18 | if(n <= 0) 19 | return false; 20 | if(n <= 3) 21 | return true; 22 | for(int i = 4; i <= n; i++) { 23 | res = (state1 && state2 && state3) ? false : true; 24 | state1 = state2; 25 | state2 = state3; 26 | state3 = res; 27 | } 28 | return state3; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /120-construct-binary-tree-from-inorder-and-postorder-traversal/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {number[]} inorder 10 | * @param {number[]} postorder 11 | * @return {TreeNode} 12 | */ 13 | var buildTree = function(inorder, postorder) { 14 | return helper(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1); 15 | }; 16 | 17 | function helper(inorder, postorder, inBegin, inEnd, posBegin, posEnd) { 18 | if(inBegin > inEnd) 19 | return null; 20 | else if(inBegin == inEnd) 21 | return new TreeNode(inorder[inBegin]); 22 | var root = new TreeNode(postorder[posEnd]); 23 | var inRootPos = inBegin; 24 | while(inRootPos < inEnd && inorder[inRootPos] != postorder[posEnd]) 25 | inRootPos++; 26 | var len = inRootPos-inBegin; 27 | var leftRoot = helper(inorder, postorder, inBegin, inRootPos-1, posBegin, posBegin+len-1); 28 | var rightRoot = helper(inorder, postorder, inRootPos+1, inEnd, posBegin+len, posEnd-1); 29 | root.left = leftRoot; 30 | root.right = rightRoot; 31 | return root; 32 | } -------------------------------------------------------------------------------- /132-construct-binary-tree-from-preorder-and-inorder-traversal/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {number[]} preorder 10 | * @param {number[]} inorder 11 | * @return {TreeNode} 12 | */ 13 | var buildTree = function(preorder, inorder) { 14 | return helper(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1); 15 | }; 16 | 17 | function helper(preorder, inorder, preBegin, preEnd, inBegin, inEnd) { 18 | if(preBegin > preEnd) 19 | return null; 20 | else if(preBegin == preEnd) 21 | return new TreeNode(preorder[preBegin]); 22 | var root = new TreeNode(preorder[preBegin]); 23 | var inRootPos = inBegin; 24 | while(inRootPos < inEnd && inorder[inRootPos] !== preorder[preBegin]) 25 | inRootPos++; 26 | var leftSubLen = inRootPos-inBegin; 27 | var leftRoot = helper(preorder, inorder, preBegin+1, preBegin+leftSubLen, inBegin, inRootPos-1); 28 | var rightRoot = helper(preorder, inorder, preBegin+leftSubLen+1, preEnd, inRootPos+1, inEnd); 29 | root.left = leftRoot; 30 | root.right = rightRoot; 31 | return root; 32 | } -------------------------------------------------------------------------------- /055-find-minimum-in-rotated-sorted-array-ii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findMin(int[] nums) { 3 | if(nums.length == 0) 4 | return -1; 5 | int left = 0, right = nums.length-1; 6 | while(left <= right) { 7 | int mid = (left + right)/2; 8 | //base case 1: there is only one number 9 | if(left == right) 10 | return nums[left]; 11 | //base case 2: array is ascending order 12 | else if(nums[left] < nums[right]) 13 | return nums[left]; 14 | //base case 3: there are only two numbers 15 | //and nums[right] <= nums[left] 16 | else if(left == right - 1) 17 | return nums[right]; 18 | //normal binary search: right part 19 | else if(nums[mid] >= nums[left] && nums[left] > nums[right]) 20 | left = mid+1; 21 | //normal binary search: left part 22 | else if(nums[mid] <= nums[right] && nums[left] > nums[right]) 23 | right = mid; 24 | //special case: nums[mid] == nums[left] == nums[right] 25 | else 26 | left++; 27 | } 28 | return -1; 29 | } 30 | } -------------------------------------------------------------------------------- /004-maximum-depth-of-binary-tree/Yiqi_Ou.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var maxDepth = function(root) { 13 | if(!(root instanceof TreeNode)) 14 | return 0; 15 | var leftRes = maxDepth(root.left); 16 | var rightRes = maxDepth(root.right); 17 | return Math.max(leftRes, rightRes) + 1; 18 | }; 19 | 20 | 21 | //iterative version 22 | var maxDepth = function(root) { 23 | if(!(root instanceof TreeNode)) 24 | return 0; 25 | var queue = []; 26 | queue.push(root); 27 | var curLevelNum = 1, nextLevelNum = 0, res = 0; 28 | while(queue.length) { 29 | var tree = queue.shift(); 30 | curLevelNum--; 31 | if(tree.left) { 32 | queue.push(tree.left); 33 | nextLevelNum++; 34 | } 35 | if(tree.right) { 36 | queue.push(tree.right); 37 | nextLevelNum++; 38 | } 39 | if(curLevelNum === 0) { 40 | curLevelNum = nextLevelNum; 41 | nextLevelNum = 0; 42 | res++; 43 | } 44 | } 45 | return res; 46 | }; -------------------------------------------------------------------------------- /154-next-permutation/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {void} Do not return anything, modify nums in-place instead. 4 | */ 5 | var nextPermutation = function(nums) { 6 | //initial condition 7 | if(nums.length < 2) 8 | return; 9 | //find the first decreasedPos from left side 10 | var decreasedPos = nums.length-2; 11 | while(decreasedPos >= 0 && nums[decreasedPos] >= nums[decreasedPos+1]) 12 | decreasedPos--; 13 | if(decreasedPos == -1) { 14 | reverse(nums, 0, nums.length-1); 15 | return; 16 | } 17 | //find the first number which is a little bit larger than number at 18 | //decreasedPos from right side 19 | var largerPos = nums.length-1; 20 | while(largerPos > decreasedPos+1 && nums[largerPos] <= nums[decreasedPos]) 21 | largerPos--; 22 | swap(nums, decreasedPos, largerPos); 23 | reverse(nums, decreasedPos+1, nums.length-1); 24 | }; 25 | 26 | function reverse(nums, begin, end) { 27 | for(var i = 0; i <= (end-begin) / 2; i++) { 28 | var tmp = nums[begin+i]; 29 | nums[begin+i] = nums[end-i]; 30 | nums[end-i] = tmp; 31 | } 32 | } 33 | 34 | function swap(nums, i, j) { 35 | var tmp = nums[i]; 36 | nums[i] = nums[j]; 37 | nums[j] = tmp; 38 | } -------------------------------------------------------------------------------- /054-spiral-matrix-ii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[][] generateMatrix(int n) { 3 | int i = 0, j = 0, level = 0, count = n*n, num = 1; 4 | int[][] res = new int[n][n]; 5 | char dir = 'r'; 6 | while(count > 0) { 7 | res[i][j] = num++; 8 | if(dir == 'r') { 9 | if(j != n-1-level) 10 | j++; 11 | else { 12 | dir = 'd'; 13 | i++; 14 | } 15 | } 16 | else if(dir == 'd') { 17 | if(i != n-1-level) 18 | i++; 19 | else { 20 | dir = 'l'; 21 | j--; 22 | } 23 | } 24 | else if(dir == 'l') { 25 | if(j != level) 26 | j--; 27 | else { 28 | dir = 'u'; 29 | i--; 30 | } 31 | } 32 | else { 33 | if(i != level + 1) 34 | i--; 35 | else { 36 | level++; 37 | dir = 'r'; 38 | j++; 39 | } 40 | } 41 | count--; 42 | } 43 | return res; 44 | } 45 | } -------------------------------------------------------------------------------- /120-construct-binary-tree-from-inorder-and-postorder-traversal/Yiqi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public TreeNode buildTree(int[] inorder, int[] postorder) { 12 | return helper(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1); 13 | } 14 | 15 | public TreeNode helper(int[] inorder, int[] postorder, int inBegin, int inEnd, int posBegin, int posEnd) { 16 | if(inBegin > inEnd) 17 | return null; 18 | else if(inBegin == inEnd) 19 | return new TreeNode(inorder[inBegin]); 20 | TreeNode root = new TreeNode(postorder[posEnd]); 21 | //find root position in inorder array 22 | int rootPos = inBegin; 23 | while(rootPos < inEnd && inorder[rootPos] != postorder[posEnd]) 24 | rootPos++; 25 | int len = rootPos-inBegin; 26 | TreeNode leftRoot = helper(inorder, postorder, inBegin, rootPos-1, posBegin, posBegin+len-1); 27 | TreeNode rightRoot = helper(inorder, postorder, rootPos+1, inEnd, posBegin+len, posEnd-1); 28 | root.left = leftRoot; 29 | root.right = rightRoot; 30 | return root; 31 | } 32 | } -------------------------------------------------------------------------------- /061-search-a-2d-matrix/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int left = 0, right = matrix.length-1; 4 | while(left < right-1) { 5 | int mid = (left + right)/2; 6 | if(matrix[mid][0] == target) 7 | return true; 8 | else if(matrix[mid][0] < target) 9 | left = mid; 10 | else 11 | right = mid; 12 | } 13 | //determine which row will be used to do second binary search 14 | //do not forget situation that left == right 15 | int idx = -1; 16 | if(matrix[left][0] > target) 17 | return false; 18 | else if(matrix[left][0] <= target && matrix[right][0] > target) 19 | idx = left; 20 | else if(matrix[right][0] <= target) 21 | idx = right; 22 | int i = 0, j = matrix[idx].length-1; 23 | while(i < j - 1) { 24 | int mid = (i+j)/2; 25 | if(matrix[idx][mid] == target) 26 | return true; 27 | else if(matrix[idx][mid] < target) 28 | i = mid; 29 | else 30 | j = mid; 31 | } 32 | if(matrix[idx][i] == target || matrix[idx][j] == target) 33 | return true; 34 | return false; 35 | } 36 | } -------------------------------------------------------------------------------- /132-construct-binary-tree-from-preorder-and-inorder-traversal/Yiqi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public TreeNode buildTree(int[] preorder, int[] inorder) { 12 | return helper(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1); 13 | } 14 | 15 | public TreeNode helper(int[] preorder, int[] inorder, int preBegin, int preEnd, int inBegin, int inEnd) { 16 | if(preBegin > preEnd) 17 | return null; 18 | else if(preBegin == preEnd) 19 | return new TreeNode(preorder[preBegin]); 20 | TreeNode root = new TreeNode(preorder[preBegin]); 21 | //find root position 22 | int inRootPos = inBegin; 23 | while(inRootPos < inEnd && inorder[inRootPos] != preorder[preBegin]) 24 | inRootPos++; 25 | int leftSubTreeNum = inRootPos-inBegin; 26 | TreeNode leftRoot = helper(preorder, inorder, preBegin+1, preBegin+leftSubTreeNum, inBegin, inRootPos-1); 27 | TreeNode rightRoot = helper(preorder, inorder, preBegin+leftSubTreeNum+1, preEnd, inRootPos+1, inEnd); 28 | root.left = leftRoot; 29 | root.right = rightRoot; 30 | return root; 31 | } 32 | } -------------------------------------------------------------------------------- /015-majority-element/Yiqi.java: -------------------------------------------------------------------------------- 1 | //hashmap solution: use o(n) space and o(n) time 2 | public class Solution { 3 | public int majorityElement(int[] num) { 4 | HashMap countMap = new HashMap(); 5 | for(int i = 0; i < num.length; i++) { 6 | int val = num[i]; 7 | if(countMap.containsKey(val)) 8 | countMap.put(val, countMap.get(val) + 1); 9 | else 10 | countMap.put(val, 1); 11 | } 12 | int res = 0; 13 | for(Integer keyIter: countMap.keySet()) { 14 | if(countMap.get(keyIter) > num.length/2) 15 | res = keyIter; 16 | } 17 | return res; 18 | } 19 | } 20 | 21 | //boyer-moore majority algorithm: use o(1) space and o(n) time, source: 22 | //http://gregable.com/2013/10/majority-vote-algorithm-find-majority.html 23 | public class Solution { 24 | public int majorityElement(int[] nums) { 25 | if(nums.length == 0) 26 | return 0; 27 | int candidate = nums[0], count = 0; 28 | for(int i = 0; i < nums.length; i++) { 29 | if(count == 0) { 30 | candidate = nums[i]; 31 | } 32 | if(candidate == nums[i]) 33 | count++; 34 | else 35 | count--; 36 | } 37 | return candidate; 38 | } 39 | } -------------------------------------------------------------------------------- /061-search-a-2d-matrix/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @param {number} target 4 | * @return {boolean} 5 | */ 6 | var searchMatrix = function(matrix, target) { 7 | var row1 = 0, row2 = matrix.length-1; 8 | //binary search to find which array to do second binary search 9 | while(row1 < row2 - 1) { 10 | var mid = Math.floor((row1 + row2) / 2); 11 | if(matrix[mid][0] === target) 12 | return true; 13 | else if(matrix[mid][0] > target) 14 | row2 = mid; 15 | else 16 | row1 = mid; 17 | } 18 | //determine which array to do binary search, do not forget the 19 | //situatio where row1 == row2 20 | var row = -1; 21 | if(target < matrix[row1][0]) 22 | return false; 23 | else if(matrix[row1][0] <= target && matrix[row2][0] > target) 24 | row = row1; 25 | else if(matrix[row2][0] <= target) 26 | row = row2; 27 | var col1 = 0, col2 = matrix[0].length-1; 28 | while(col1 < col2 - 1) { 29 | mid = Math.floor((col1 + col2)/2); 30 | if(matrix[row][mid] === target) 31 | return true; 32 | else if(matrix[row][mid] > target) 33 | col2 = mid; 34 | else 35 | col1 = mid; 36 | } 37 | if(matrix[row][col1] === target || matrix[row][col2] === target) 38 | return true; 39 | return false; 40 | }; -------------------------------------------------------------------------------- /234-3sum/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[][]} 4 | */ 5 | var threeSum = function(nums) { 6 | var res = []; 7 | if(nums.length < 3) 8 | return res; 9 | nums.sort(function(val1, val2) { 10 | return val1-val2; 11 | }); 12 | var items = []; 13 | for(var i = 0; i < nums.length-2; i++) { 14 | if(i !== 0 && nums[i] === nums[i-1]) 15 | continue; 16 | items.push(nums[i]); 17 | twoSum(res, items, nums, i+1, 0-nums[i]); 18 | items.pop(); 19 | } 20 | return res; 21 | }; 22 | 23 | function twoSum(res, items, nums, start, target) { 24 | var left = start, right = nums.length-1; 25 | while(left < right) { 26 | if(nums[left] + nums[right] === target) { 27 | //backtracking: add 4 nums into res and store the 'items' for next search 28 | items.push(nums[left]); 29 | items.push(nums[right]); 30 | var tmpItems = []; 31 | tmpItems.push.apply(tmpItems, items); 32 | res.push(tmpItems); 33 | items.pop(); 34 | items.pop(); 35 | left++; 36 | //remove duplicate 37 | while(left < right && nums[left] === nums[left-1]) 38 | left++; 39 | } 40 | else if(nums[left] + nums[right] < target) 41 | left++; 42 | else 43 | right--; 44 | } 45 | } -------------------------------------------------------------------------------- /179-largest-rectangle-in-histogram/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int largestRectangleArea(int[] heights) { 3 | Stack stack = new Stack<>(); 4 | int res = 0; 5 | for (int i = 0; i < heights.length; i++) { 6 | //we calculate area by height * width. the heights = heights[idx], 7 | //width = right edge - left edge. so if heights[i] < height[idx], i is 8 | //reactangle's right edge, otherwise push i into stack and find its right edge 9 | //later. Since we keep heights in stack ascending order, left edge will allways 10 | //be value that is under peek of the stack. If it is empty, left edge is 0 11 | while(!stack.isEmpty() && heights[stack.peek()] > heights[i]) { 12 | int idx = stack.pop(); 13 | int area = stack.isEmpty() ? i * heights[idx] : (i-stack.peek()-1) * heights[idx]; 14 | res = Math.max(area, res); 15 | } 16 | stack.push(i); 17 | } 18 | //calculate the rectangle area whose right edge is heights.length-1 19 | while(!stack.isEmpty()) { 20 | int idx = stack.pop(); 21 | int area = stack.isEmpty() ? heights.length * heights[idx] : 22 | (heights.length-1-stack.peek()) * heights[idx]; 23 | res = Math.max(res, area); 24 | } 25 | return res; 26 | } 27 | } -------------------------------------------------------------------------------- /205-word-search/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean exist(char[][] board, String word) { 3 | if(board.length == 0 || board[0].length == 0) 4 | return false; 5 | int m = board.length, n = board[0].length; 6 | boolean[][] visited = new boolean[m][n]; 7 | for(int i = 0; i < m; i++) { 8 | for(int j = 0; j < n; j++) { 9 | if(helper(board, visited, word, i, j, 0)) 10 | return true; 11 | } 12 | } 13 | return false; 14 | } 15 | 16 | public boolean helper(char[][] board, boolean[][] visited, String word, int x, int y, int idx) { 17 | if(idx == word.length()) 18 | return true; 19 | int m = board.length, n = board[0].length; 20 | //x, y should be inside board, not visited before and board[x][y] should match char in word 21 | if(x < 0 || x >= m || y < 0 || y >= n || visited[x][y] || board[x][y] != word.charAt(idx)) 22 | return false; 23 | visited[x][y] = true; 24 | boolean res = helper(board, visited, word, x+1, y, idx+1) || 25 | helper(board, visited, word, x-1, y, idx+1) || 26 | helper(board, visited, word, x, y-1, idx+1) || 27 | helper(board, visited, word, x, y+1, idx+1); 28 | //backtracking: restore the visited state, so that other paths can use char here 29 | visited[x][y] = false; 30 | return res; 31 | } 32 | } -------------------------------------------------------------------------------- /062-game-of-life/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //use two bits to represents board's current state and future state 3 | public void gameOfLife(int[][] board) { 4 | int m = board.length, n = board[0].length; 5 | for(int i = 0; i < m; i++) { 6 | for(int j = 0; j < n; j++) { 7 | int numOfLive = calRound(board, i, j); 8 | //if there are 3 neighbours, then set 2nd bit 9 | if(numOfLive == 3) 10 | board[i][j] = board[i][j] | 2; 11 | //if there are 2 neighbours, then set it as "11" 12 | else if(numOfLive == 2 && board[i][j] == 1) 13 | board[i][j] = 3; 14 | } 15 | } 16 | //right shift to get future state 17 | for(int i = 0; i < m; i++) { 18 | for(int j = 0; j < n; j++) { 19 | board[i][j] = board[i][j] >> 1; 20 | } 21 | } 22 | } 23 | 24 | public int calRound(int[][] board, int x, int y) { 25 | int res = 0; 26 | for(int i = x-1; i <= x+1; i++) { 27 | for(int j = y-1; j <= y+1; j++) { 28 | //i, j must be inside the board and do not calculate point at 29 | //board[x][y] 30 | if(i >= 0 && i < board.length && j >= 0 && j < board[0].length 31 | && (i != x || j != y)) { 32 | res += (board[i][j] & 1); 33 | } 34 | } 35 | } 36 | return res; 37 | } 38 | } -------------------------------------------------------------------------------- /163-jump-game-ii/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int jump(int[] nums) { 3 | if(nums.length <= 1) 4 | return 0; 5 | //curMaxIndex: respresents the max index we can reach within next step 6 | //which means its initial value is max index we can reach in first step 7 | //lastMaxIndex: represents the max index we can reach within current step 8 | //which means its initial value is 0 9 | int res = 0, lastMaxIndex = 0, curMaxIndex = nums[0]; 10 | for(int i = 1; i < nums.length; i++) { 11 | //when we reach lastMaxIndex, the max index we can go within current step 12 | //we add 1 step to res and update lastMaxIndex with curMaxIndex 13 | if(i > lastMaxIndex) { 14 | res++; 15 | lastMaxIndex = curMaxIndex; 16 | } 17 | curMaxIndex = Math.max(i+nums[i], curMaxIndex); 18 | } 19 | return res; 20 | } 21 | } 22 | 23 | 24 | //这种解法更好,因为还能判断是否能走到最后的element 25 | //source:http://codeganker.blogspot.com/2014/03/jump-game-ii-leetcode.html 26 | public int jump(int[] A) { 27 | if(A==null || A.length==0) 28 | return 0; 29 | int lastReach = 0; 30 | int reach = 0; 31 | int step = 0; 32 | for(int i=0;i<=reach&&ilastReach) 35 | { 36 | step++; 37 | lastReach = reach; 38 | } 39 | reach = Math.max(reach,A[i]+i); 40 | } 41 | if(reach> subsetsWithDup(int[] nums) { 3 | List> res = new ArrayList<>(); 4 | res.add(new ArrayList<>()); 5 | Arrays.sort(nums); 6 | if(nums.length == 0) 7 | return res; 8 | //initialization: add first number set and add set into result. Use lastAddSetNum to 9 | //represents how many sets has been added into res in last iteration 10 | List set = new ArrayList<>(); 11 | set.add(nums[0]); 12 | res.add(set); 13 | int lastAddSetNum = 1; 14 | //the idea to generate subsets[i] is that if nums[i] != nums[i-1], we can extract each 15 | //set in subset[i-1] and add nums[i] into the set and add the set back to result. 16 | //if nums[i] == nums[i-1], we do not extract every set in subset[i-1] to generate subset[i]. 17 | //since in that way there must be duplicatition. The only sets we should use in subset[i-1] 18 | //to generate subset[i] is the lastest set which contains element nums[i-1] 19 | for(int i = 1; i < nums.length; i++) { 20 | int begin = nums[i] == nums[i-1] ? res.size()-lastAddSetNum : 0; 21 | int end = res.size(); 22 | lastAddSetNum = 0; 23 | for(int j = begin; j < end; j++) { 24 | set = new ArrayList<>(res.get(j)); 25 | set.add(nums[i]); 26 | res.add(set); 27 | lastAddSetNum++; 28 | } 29 | } 30 | return res; 31 | } 32 | } -------------------------------------------------------------------------------- /002-add-digits/newraina.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} num 3 | * @return {number} 4 | */ 5 | 6 | // 递归写法 7 | var addDigits_1 = function(num) { 8 | num = typeof num === 'number' ? num.toString() : num; 9 | if (num.length < 2) { 10 | return parseInt(num); 11 | } else { 12 | var sum = 0; 13 | for (var i = 0; i < num.length; i++) { 14 | sum += parseInt(num[i]); 15 | } 16 | return addDigits_1(sum); 17 | } 18 | }; 19 | 20 | // 循环写法 21 | var addDigits_2 = function(num) { 22 | num = typeof num === 'number' ? num.toString() : num; 23 | var len = num.length; 24 | var sum = 0; 25 | 26 | while (len > 1) { 27 | for (var i = 0; i < len; i++) { 28 | sum += parseInt(num[i]); 29 | } 30 | num = sum.toString(); 31 | sum = 0; 32 | len = num.length; 33 | } 34 | 35 | return parseInt(num); 36 | } 37 | 38 | // 最有效的写法 39 | var addDigits_3 = function(num) { 40 | return num % 9 === 0 && num !== 0 ? 9 : num % 9; 41 | } 42 | 43 | /*-----------本机测试运行时间-----------*/ 44 | 45 | function testRunningTime(funcName) { 46 | var count = 2000000; 47 | var start, end; 48 | start = new Date(); 49 | for (var i = 0; i < count; i++) { 50 | funcName(i); 51 | }; 52 | end = new Date(); 53 | return (end - start); 54 | } 55 | 56 | console.log('addDigits_1: ' + testRunningTime(addDigits_1) + 'ms'); // 885ms 57 | console.log('addDigits_2: ' + testRunningTime(addDigits_2) + 'ms'); // 875ms 58 | console.log('addDigits_3: ' + testRunningTime(addDigits_3) + 'ms'); // 15ms 59 | -------------------------------------------------------------------------------- /167-majority-element-ii/Yiqi.java: -------------------------------------------------------------------------------- 1 | //source: https://leetcode.com/discuss/43248/boyer-moore-majority-vote-algorithm-and-my-elaboration 2 | public class Solution { 3 | public List majorityElement(int[] nums) { 4 | List res = new ArrayList<>(); 5 | //initial condition must be satisfied 6 | if(nums.length == 0) 7 | return res; 8 | //initial condition, note candidate1 and candidate2 must be different numbers 9 | //they do not have to be numbers in nums array, as long as you set count1 and 10 | //count2 = 0 11 | int candidate1 = 0, candidate2 = 1, count1 = 0, count2 = 0; 12 | for(int iter: nums) { 13 | if(iter == candidate1) 14 | count1++; 15 | else if(iter == candidate2) 16 | count2++; 17 | else if(count1 == 0) { 18 | candidate1 = iter; 19 | count1 = 1; 20 | } 21 | else if(count2 == 0) { 22 | candidate2 = iter; 23 | count2 = 1; 24 | } 25 | else { 26 | count1--; count2--; 27 | } 28 | } 29 | count1 = 0; 30 | count2 = 0; 31 | for(int iter: nums) { 32 | if(iter == candidate1) 33 | count1++; 34 | else if(iter == candidate2) 35 | count2++; 36 | } 37 | if(count1 > nums.length/3) 38 | res.add(candidate1); 39 | if(count2 > nums.length/3) 40 | res.add(candidate2); 41 | return res; 42 | } 43 | } -------------------------------------------------------------------------------- /007-invert-binary-tree/newraina.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {TreeNode} 11 | */ 12 | var invertTree = function(root) { 13 | if (root) { 14 | var temp = root.left; 15 | root.left = root.right; 16 | root.right = temp; 17 | 18 | if (root.left) invertTree(root.left); 19 | if (root.right) invertTree(root.right); 20 | } 21 | 22 | return root; 23 | }; 24 | 25 | // 层次遍历 26 | var invertTree = function(root) { 27 | 28 | if (!root) return root; 29 | 30 | var nodeQue = [], 31 | count = 0, 32 | len, tempQue, tempNode; 33 | 34 | nodeQue.push(root); 35 | 36 | while (nodeQue.length) { 37 | len = nodeQue.length; 38 | tempQue = []; 39 | count++; 40 | 41 | for (var i = 0; i < len; i++) { 42 | // 遍历当前层叶子的所有下一层叶子,存入tempQue 43 | tempNode = nodeQue[i]; 44 | if (tempNode) { 45 | var temp = tempNode.left; 46 | tempNode.left = tempNode.right; 47 | tempNode.right = temp; 48 | 49 | if (tempNode.left) { 50 | tempQue.push(tempNode.left); 51 | } 52 | if (tempNode.right) { 53 | tempQue.push(tempNode.right); 54 | } 55 | } 56 | } 57 | // 使nodeQue始终只保存一层的叶子 58 | nodeQue = tempQue; 59 | } 60 | 61 | return root; 62 | } 63 | -------------------------------------------------------------------------------- /234-3sum/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> threeSum(int[] nums) { 3 | List> res = new ArrayList<>(); 4 | if(nums.length < 3) 5 | return res; 6 | Arrays.sort(nums); 7 | List items = new ArrayList<>(); 8 | for(int i = 0; i < nums.length - 2; i++) { 9 | //remove duplicate 10 | if(i != 0 && nums[i] == nums[i-1]) 11 | continue; 12 | //backtracking: begin search 13 | items.add(nums[i]); 14 | twoSum(res, items, nums, i+1, 0-nums[i]); 15 | items.remove(0); 16 | } 17 | return res; 18 | } 19 | 20 | public void twoSum(List> res, List items, int[] nums, int start, int target) { 21 | int left = start, right = nums.length-1; 22 | while(left < right) { 23 | if(nums[left] + nums[right] == target) { 24 | //backtracking: add 4 numbers into result and restore items to search the next 25 | items.add(nums[left]); 26 | items.add(nums[right]); 27 | res.add(new ArrayList(items)); 28 | items.remove(2); 29 | items.remove(1); 30 | //remove duplicate 31 | left++; 32 | while(left < right && nums[left] == nums[left-1]) 33 | left++; 34 | } 35 | else if(nums[left] + nums[right] < target) 36 | left++; 37 | else 38 | right--; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /154-next-permutation/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void nextPermutation(int[] nums) { 3 | if(nums.length == 0 || nums.length == 1) 4 | return; 5 | //iterate from right, find the first number that begins to 6 | //decrease 7 | int decreasedPos = nums.length-2; 8 | while(decreasedPos >= 0 && 9 | nums[decreasedPos] >= nums[decreasedPos+1]) { 10 | decreasedPos--; 11 | } 12 | //if the nums are in desceding order 13 | if(decreasedPos == -1) { 14 | reverse(nums, 0, nums.length-1); 15 | return; 16 | } 17 | //reorder the number from decreasedPos to nums.length-1, to generate 18 | //next permutation 19 | int largerFirstNumPos = nums.length-1; 20 | //search from find the position where the number is just a little bit larger than 21 | //number at decreasedPos. 22 | while(largerFirstNumPos > decreasedPos+1 && nums[largerFirstNumPos] <= nums[decreasedPos]) { 23 | largerFirstNumPos--; 24 | } 25 | swap(nums, largerFirstNumPos, decreasedPos); 26 | reverse(nums, decreasedPos+1, nums.length-1); 27 | } 28 | 29 | public void reverse(int[] nums, int begin, int end) { 30 | for(int i = 0; i <= (end-begin)/2; i++) { 31 | int tmp = nums[begin+i]; 32 | nums[begin+i] = nums[end-i]; 33 | nums[end-i] = tmp; 34 | } 35 | } 36 | 37 | public void swap(int[] nums, int i, int j) { 38 | int tmp = nums[i]; 39 | nums[i] = nums[j]; 40 | nums[j] =tmp; 41 | } 42 | } -------------------------------------------------------------------------------- /186-insert-interval/Yiqi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for an interval. 3 | * public class Interval { 4 | * int start; 5 | * int end; 6 | * Interval() { start = 0; end = 0; } 7 | * Interval(int s, int e) { start = s; end = e; } 8 | * } 9 | */ 10 | public class Solution { 11 | public List insert(List intervals, Interval newInterval) { 12 | List res = new ArrayList<>(); 13 | int pos = 0; 14 | //find the position where we insert new interval 15 | while (pos < intervals.size() && intervals.get(pos).end < newInterval.start) { 16 | res.add(intervals.get(pos)); 17 | pos++; 18 | } 19 | //the last position to insert 20 | if (pos == intervals.size()) { 21 | res.add(newInterval); 22 | return res; 23 | } 24 | //insert new Interval 25 | res.add(newInterval); 26 | //detect if there are overlap area, if there is merge them, otherwise add to res 27 | for (int i = pos; i < intervals.size(); i++) { 28 | Interval cur = intervals.get(i); 29 | //the precondition here is that cur.end >= newInterval.start, so the condition 30 | //that they have overlap is cur.start <= newInterval.end 31 | if (cur.start <= newInterval.end) { 32 | //merge two interval 33 | newInterval.start = Math.min(newInterval.start, cur.start); 34 | newInterval.end = Math.max(newInterval.end, cur.end); 35 | } else { 36 | res.add(cur); 37 | } 38 | } 39 | return res; 40 | } 41 | } -------------------------------------------------------------------------------- /187-4sum/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[][]} 5 | */ 6 | var fourSum = function(nums, target) { 7 | var res = [], items = []; 8 | if(nums.length < 4) 9 | return res; 10 | //javascript use lexical sort by default 11 | nums.sort(function(val1, val2) { 12 | return val1-val2; 13 | }); 14 | for(var i = 0; i < nums.length-3; i++) { 15 | if(i !== 0 && nums[i] === nums[i-1]) 16 | continue; 17 | items.push(nums[i]); 18 | threeSum(res, nums, items, i+1, target-nums[i]); 19 | items.pop(); 20 | } 21 | return res; 22 | }; 23 | 24 | function threeSum(res, nums, items, start, target) { 25 | for(var j = start; j < nums.length-2; j++) { 26 | if(j !== start && nums[j] === nums[j-1]) 27 | continue; 28 | items.push(nums[j]) 29 | twoSum(res, nums, items, j+1, target-nums[j]); 30 | items.pop(); 31 | } 32 | } 33 | 34 | function twoSum(res, nums, items, start, target) { 35 | var left = start, right = nums.length-1, tmpItem = []; 36 | while(left < right) { 37 | if(nums[left] + nums[right] === target) { 38 | items.push(nums[left]); 39 | items.push(nums[right]); 40 | //clean tmpItem and push all elements in items into tmpItem 41 | tmpItem = []; 42 | tmpItem.push.apply(tmpItem, items); 43 | res.push(tmpItem); 44 | items.pop(); 45 | items.pop(); 46 | left++; 47 | while(left < right && nums[left] === nums[left-1]) 48 | left++; 49 | } 50 | else if(nums[left] + nums[right] > target) 51 | right--; 52 | else 53 | left++; 54 | } 55 | } -------------------------------------------------------------------------------- /051-sort-colors/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //counting sort 3 | public void sortColors(int[] nums) { 4 | int[] count = new int[3]; 5 | for(int i: nums) { 6 | count[i]++; 7 | } 8 | int oldCount = 0, total = 0; 9 | for(int i = 0; i < count.length; i++) { 10 | oldCount = count[i]; 11 | count[i] = total; 12 | total += oldCount; 13 | } 14 | int[] aux = new int[nums.length]; 15 | for(int i : nums) { 16 | aux[count[i]++] = i; 17 | } 18 | for(int i = 0; i < nums.length; i++) { 19 | nums[i] = aux[i]; 20 | } 21 | } 22 | } 23 | 24 | //follow up: use o(1) space and single pass 25 | //the idea is that use three pointers: left, p, right. all nums before left are 0. 26 | //all nums after right are 2. use p to scan through array, if we find 0, swap with left 27 | //pointer. If we find 2, swap with right pointer 28 | public class Solution { 29 | public void sortColors(int[] nums) { 30 | if(nums.length == 0) 31 | return; 32 | int p = 0, l = 0, r = nums.length-1; 33 | //note if p == r, we should not stop, since it is possible the value swapped back is 34 | //0, we need another iteration to swap it with left pointer 35 | while(p <= r) { 36 | if(nums[p] == 0 && p >= l) { 37 | int tmp = nums[l]; 38 | nums[l] = nums[p]; 39 | nums[p] = tmp; 40 | l++; 41 | if(nums[p] != 1) 42 | continue; 43 | } else if(nums[p] == 2) { 44 | int tmp = nums[r]; 45 | nums[r] = nums[p]; 46 | nums[p] = tmp; 47 | r--; 48 | if(nums[p] != 1) 49 | continue; 50 | } 51 | p++; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /187-4sum/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> fourSum(int[] nums, int target) { 3 | List> res = new ArrayList<>(); 4 | if(nums.length < 4) 5 | return res; 6 | ArrayList items = new ArrayList(); 7 | Arrays.sort(nums); 8 | for(int i = 0; i < nums.length-3; i++) { 9 | //remove duplicate 10 | if(i != 0 && nums[i] == nums[i-1]) 11 | continue; 12 | items.add(nums[i]); 13 | threeSum(res, nums, items, i+1, target-nums[i]); 14 | items.remove(0); 15 | } 16 | return res; 17 | } 18 | 19 | public void threeSum(List> res, int[] nums, ArrayList items, int start, int target) { 20 | for(int i = start; i < nums.length-2; i++) { 21 | //remove duplicate 22 | if(i != start && nums[i] == nums[i-1]) 23 | continue; 24 | items.add(nums[i]); 25 | twoSum(res, nums, items, i+1, target-nums[i]); 26 | items.remove(1); 27 | } 28 | } 29 | 30 | public void twoSum(List> res, int[] nums, ArrayList items, int start, int target) { 31 | int left = start, right = nums.length-1; 32 | while(left < right) { 33 | if(nums[left] + nums[right] == target) { 34 | items.add(nums[left]); 35 | items.add(nums[right]); 36 | res.add(new ArrayList(items)); 37 | items.remove(3); 38 | items.remove(2); 39 | left++; 40 | while(left < right && nums[left] == nums[left-1]) 41 | left++; 42 | } 43 | else if(nums[left] + nums[right] > target) 44 | right--; 45 | else 46 | left++; 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /158-minimum-size-subarray-sum/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} s 3 | * @param {number[]} nums 4 | * @return {number} 5 | */ 6 | var minSubArrayLen = function(s, nums) { 7 | if(nums.length === 0) 8 | return 0; 9 | //test adding all numbers is larger than s 10 | var tmpSum = nums.reduce(function(pre, cur, idx, array) { 11 | return pre + cur; 12 | }); 13 | if(tmpSum < s) 14 | return 0; 15 | //generate sum array so that sum[i] = nums[0]+..+nums[i-1] 16 | var sum = []; 17 | sum.push(0); 18 | nums.forEach(function(item, idx, array) { 19 | sum.push(sum[sum.length-1] + item); 20 | }); 21 | var res = nums.length; 22 | for(var i = 0; i < nums.length; i++) { 23 | var right = binarySearch(sum, i, i, nums.length-1, s); 24 | if(right == nums.length) 25 | break; 26 | res = Math.min(res, right-i+1); 27 | } 28 | return res; 29 | }; 30 | 31 | /** 32 | * @param {number} s 33 | * @param {number[]} nums 34 | * @return {number} 35 | */ 36 | var minSubArrayLen = function(s, nums) { 37 | if(nums.length === 0) 38 | return 0; 39 | var tmpSum = nums.reduce(function(pre, cur, idx, array) { 40 | return pre+cur; 41 | }); 42 | if(tmpSum < s) 43 | return 0; 44 | tmpSum = 0; 45 | var left = 0, right = 0, res = nums.length; 46 | while(right < nums.length) { 47 | while(tmpSum < s && right < nums.length) { 48 | tmpSum += nums[right++]; 49 | } 50 | while(tmpSum >= s) { 51 | res = Math.min(right-left, res); 52 | tmpSum -= nums[left++]; 53 | } 54 | } 55 | return res; 56 | }; 57 | 58 | function binarySearch(sum, i, left, right, s) { 59 | while(left <= right) { 60 | var mid = Math.floor((left + right)/2); 61 | var tmpSum = sum[mid+1] - sum[i]; 62 | if(tmpSum == s) { 63 | left = mid; 64 | break; 65 | } 66 | else if(tmpSum > s) { 67 | right = mid-1; 68 | } 69 | else { 70 | left = mid+1; 71 | } 72 | } 73 | return left; 74 | } -------------------------------------------------------------------------------- /067-set-matrix-zeroes/Yiqi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @return {void} Do not return anything, modify matrix in-place instead. 4 | */ 5 | 6 | //use o(m+n) space 7 | var setZeroes = function(matrix) { 8 | //use rows and cols to track which rows and cols should be change to zero 9 | var rows = {}, cols = {}; 10 | matrix.forEach(function(row, rIndex, m) { 11 | row.forEach(function(item, cIndex, r) { 12 | if(item === 0) { 13 | rows[rIndex] = true; 14 | cols[cIndex] = true; 15 | } 16 | }); 17 | }); 18 | matrix.forEach(function(row, rIndex, m) { 19 | row.forEach(function(item, cIndex, r) { 20 | if(rows.hasOwnProperty(rIndex) || cols.hasOwnProperty(cIndex)) 21 | m[rIndex][cIndex] = 0; 22 | }) 23 | }); 24 | }; 25 | 26 | //use o(1) space 27 | /** 28 | * @param {number[][]} matrix 29 | * @return {void} Do not return anything, modify matrix in-place instead. 30 | */ 31 | var setZeroes = function(matrix) { 32 | if(matrix.length === 0 || matrix[0].length === 0) 33 | return; 34 | var changeRow0To0 = false, changeCol0To0 = false; 35 | //should we change first row to zeros 36 | matrix[0].forEach(function(item, idx, array) { 37 | if(item === 0) 38 | changeRow0To0 = true; 39 | }); 40 | //should we change first column to zeros 41 | matrix.forEach(function(row, rIndex, m) { 42 | if(row[0] === 0) 43 | changeCol0To0 = true; 44 | }); 45 | //use first row and first col to indicate whether to change corresponding 46 | //row and col to zero 47 | for(var i = 1; i < matrix.length; i++) { 48 | for(var j = 1; j < matrix[0].length; j++) { 49 | if(matrix[i][j] === 0) { 50 | matrix[i][0] = 0; 51 | matrix[0][j] = 0; 52 | } 53 | } 54 | } 55 | for(i = 1; i < matrix.length; i++) { 56 | for(j = 1; j < matrix[0].length; j++) { 57 | if(matrix[i][0] === 0 || matrix[0][j] === 0) 58 | matrix[i][j] = 0; 59 | } 60 | } 61 | if(changeRow0To0) { 62 | matrix[0].forEach(function(item, col, r) { 63 | r[col] = 0; 64 | }) 65 | } 66 | if(changeCol0To0) { 67 | matrix.forEach(function(row, rIndex, m) { 68 | row[0] = 0; 69 | }) 70 | } 71 | }; -------------------------------------------------------------------------------- /067-set-matrix-zeroes/Yiqi.java: -------------------------------------------------------------------------------- 1 | //use o(m+n) space 2 | public class Solution { 3 | public void setZeroes(int[][] matrix) { 4 | HashSet rows = new HashSet<>(), cols = new HashSet<>(); 5 | for(int i = 0; i < matrix.length; i++) { 6 | for(int j = 0; j < matrix[0].length; j++) { 7 | if(matrix[i][j] == 0) { 8 | rows.add(i); 9 | cols.add(j); 10 | } 11 | } 12 | } 13 | for(int i = 0; i < matrix.length; i++) { 14 | for(int j = 0; j < matrix[0].length; j++) { 15 | if(rows.contains(i) || cols.contains(j)) 16 | matrix[i][j] = 0; 17 | } 18 | } 19 | } 20 | } 21 | 22 | //use constant space 23 | public class Solution { 24 | public void setZeroes(int[][] matrix) { 25 | if(matrix.length == 0 || matrix[0].length == 0) 26 | return; 27 | boolean changeRow0To0 = false, changeCol0To0 = false; 28 | //detect should we change first col to zero 29 | for(int i = 0; i < matrix.length; i++) { 30 | if(matrix[i][0] == 0) { 31 | changeCol0To0 = true; 32 | break; 33 | } 34 | } 35 | //detect should we change first row to zero 36 | for(int j = 0; j < matrix[0].length; j++) { 37 | if(matrix[0][j] == 0) { 38 | changeRow0To0 = true; 39 | break; 40 | } 41 | } 42 | //keep note of whether should we change the corresponding row and col to zero 43 | for(int i = 1; i < matrix.length; i++) { 44 | for(int j = 1; j < matrix[0].length; j++) { 45 | if(matrix[i][j] == 0) { 46 | matrix[i][0] = 0; 47 | matrix[0][j] = 0; 48 | } 49 | } 50 | } 51 | //make change 52 | for(int i = 1; i < matrix.length; i++) { 53 | for(int j = 1; j < matrix[0].length; j++) { 54 | if(matrix[i][0] == 0 || matrix[0][j] == 0) 55 | matrix[i][j] = 0; 56 | } 57 | } 58 | //change first row and first col 59 | if(changeRow0To0) { 60 | for(int j = 0; j < matrix[0].length; j++) 61 | matrix[0][j] = 0; 62 | } 63 | if(changeCol0To0) { 64 | for(int i = 0; i < matrix.length; i++) 65 | matrix[i][0] = 0; 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /158-minimum-size-subarray-sum/Yiqi.java: -------------------------------------------------------------------------------- 1 | //我参考了这个blog: http://www.cnblogs.com/grandyang/p/4501934.html 2 | public class Solution { 3 | 4 | public int minSubArrayLen(int s, int[] nums) { 5 | if(nums.length == 0) 6 | return 0; 7 | //test adding all numbers is larger than s 8 | int sum = 0; 9 | for(int i : nums) 10 | sum += i; 11 | if(sum < s) 12 | return 0; 13 | int left = 0, right = 0, tmpSum = 0, res = nums.length; 14 | while(right < nums.length) { 15 | while(tmpSum < s && right < nums.length) { 16 | tmpSum += nums[right++]; 17 | } 18 | while(tmpSum >= s) { 19 | res = Math.min(res, right-left); 20 | tmpSum -= nums[left++]; 21 | } 22 | } 23 | return res; 24 | } 25 | 26 | public int minSubArrayLen(int s, int[] nums) { 27 | if(nums.length == 0) 28 | return 0; 29 | int[] sum = new int[nums.length+1]; 30 | for(int i = 1; i <= nums.length; i++) 31 | sum[i] = sum[i-1] + nums[i-1]; 32 | //adding all numbers is still less than s 33 | if(sum[nums.length] < s) 34 | return 0; 35 | //init res 36 | int res = nums.length; 37 | //binary search to get range of [i, right] whose sum is 38 | //larger than s 39 | for(int i = 0; i < nums.length; i++) { 40 | int right = binarySearch(sum, i, i, nums.length-1, s); 41 | if(right == nums.length) 42 | break; 43 | res = Math.min(res, right-i+1); 44 | } 45 | return res; 46 | } 47 | 48 | public int binarySearch(int[] sum, int i, int left, int right, int s) { 49 | while(left <= right) { 50 | int mid = (left + right) / 2; 51 | //we want to calculate pos that a[i] + a[i+1] + .. + a[pos] >= s 52 | //a[i] + ... + a[pos] = sum[pos+1] - sum[i] 53 | int tmpSum = sum[mid+1] - sum[i]; 54 | if(tmpSum == s) { 55 | left = mid; 56 | break; 57 | } 58 | else if(tmpSum > s) 59 | right = mid-1; 60 | else 61 | left = mid+1; 62 | } 63 | //the reason to return left is that after binary search, if we find sum == s, 64 | //left would be the right ridge. If we do not find, left would be the right ridge 65 | //that sum just a little bit larger than s. if left == nums.length, it means we do 66 | //not find any sum > s 67 | return left; 68 | } 69 | } -------------------------------------------------------------------------------- /206-spiral-matrix/Yiqi.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List spiralOrder(int[][] matrix) { 3 | List res = new ArrayList<>(); 4 | if(matrix.length == 0 || matrix[0].length == 0) 5 | return res; 6 | int m = matrix.length, n = matrix[0].length; 7 | int min = Math.min(m, n); 8 | for(int l = 0; l < min / 2; l++) { 9 | //go right direction 10 | for(int j = l; j < n-l; j++) 11 | res.add(matrix[l][j]); 12 | //go down direction 13 | for(int i = l+1; i < m-l; i++) 14 | res.add(matrix[i][n-1-l]); 15 | //go left direction 16 | for(int j = n-2-l; j >= l; j--) 17 | res.add(matrix[m-1-l][j]); 18 | //go up direction 19 | for(int i = m-2-l; i >= l+1; i--) 20 | res.add(matrix[i][l]); 21 | } 22 | //add remaining row or col 23 | if(min % 2 == 1) { 24 | //if one row remains 25 | if(min == m) { 26 | for(int j = min/2; j < n-min/2; j++) 27 | res.add(matrix[min/2][j]); 28 | } 29 | //if one col remains 30 | else { 31 | for(int i = min/2; i < m-min/2; i++) 32 | res.add(matrix[i][min/2]); 33 | } 34 | } 35 | return res; 36 | } 37 | } 38 | 39 | 40 | //another solution which I think it is simpler 41 | public class Solution { 42 | public List spiralOrder(int[][] matrix) { 43 | List res = new ArrayList<>(); 44 | if(matrix.length == 0 || matrix[0].length == 0) 45 | return res; 46 | int i = 0, j = 0, layer = 0, m = matrix.length, 47 | n = matrix[0].length, count = m * n; 48 | char dir = 'r'; 49 | while(count > 0) { 50 | res.add(matrix[i][j]); 51 | count--; 52 | if(dir == 'r') { 53 | if(j != n - 1 - layer) 54 | j++; 55 | else { 56 | i++; 57 | dir = 'd'; 58 | } 59 | } 60 | else if(dir == 'd') { 61 | if(i != m - 1 -layer) 62 | i++; 63 | else { 64 | j--; 65 | dir = 'l'; 66 | } 67 | } 68 | else if(dir == 'l') { 69 | if(j != layer) 70 | j--; 71 | else { 72 | i--; 73 | dir = 'u'; 74 | } 75 | } 76 | else { 77 | if(i != layer+1) 78 | i--; 79 | else { 80 | j++; 81 | layer++; 82 | dir = 'r'; 83 | } 84 | } 85 | } 86 | return res; 87 | } 88 | } -------------------------------------------------------------------------------- /004-maximum-depth-of-binary-tree/newraina.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | 13 | // 一:递归 深度优先 前序遍历 14 | // Runtime: 184 ms 15 | var maxDepth_1 = function(root) { 16 | if (!root) return 0; 17 | return Math.max(maxDepth_1(root.left), maxDepth_1(root.right)) + 1; 18 | }; 19 | 20 | // 二:循环 分层遍历,记录层数 21 | // Runtime: 170 ms 22 | var maxDepth_2 = function(root) { 23 | 24 | if (!root) return 0; 25 | 26 | var nodeQue = [], 27 | count = 0, 28 | len, tempQue, tempNode; 29 | 30 | nodeQue.push(root); 31 | 32 | while (nodeQue.length) { 33 | len = nodeQue.length; 34 | tempQue = []; 35 | count++; 36 | 37 | for (var i = 0; i < len; i++) { 38 | // 遍历当前层叶子的所有下一层叶子,存入tempQue 39 | tempNode = nodeQue[i]; 40 | if (tempNode) { 41 | if (tempNode.left) { 42 | tempQue.push(tempNode.left); 43 | } 44 | if (tempNode.right) { 45 | tempQue.push(tempNode.right); 46 | } 47 | } 48 | } 49 | // 使nodeQue始终只保存一层的叶子 50 | nodeQue = tempQue; 51 | } 52 | 53 | return count; 54 | } 55 | 56 | // 三:循环 分层遍历,记录层数 57 | // 第二种实现,不借助tempQue 58 | // Runtime: 284 ms 可能是因为shift操作比较耗时 59 | var maxDepth_3 = function(root) { 60 | 61 | if (!root) return 0; 62 | 63 | var nodeQue = [], 64 | count = 0, 65 | len, tempNode; 66 | 67 | nodeQue.push(root); 68 | 69 | while (nodeQue.length) { 70 | len = nodeQue.length; 71 | count++; 72 | 73 | for (var i = 0; i < len; i++) { 74 | // len保证nodeQue中只有上一层的叶子会被shift取出 75 | // 遍历当前层叶子的所有下一层叶子 76 | tempNode = nodeQue.shift(); 77 | if (tempNode) { 78 | if (tempNode.left) { 79 | nodeQue.push(tempNode.left); 80 | } 81 | if (tempNode.right) { 82 | nodeQue.push(tempNode.right); 83 | } 84 | } 85 | } 86 | } 87 | 88 | return count; 89 | } 90 | 91 | // 四:循环 后序遍历 92 | // Runtime: 192 ms 93 | var maxDepth_4 = function(root) { 94 | var s = [], 95 | tag = [], 96 | p = root, 97 | deep = 0; 98 | 99 | while (p || s.length !== 0) { 100 | while (p) { 101 | s.push(p); 102 | tag.push(0); 103 | p = p.left; 104 | } 105 | if (tag[tag.length - 1] === 1) { 106 | deep = deep > s.length ? deep : s.length; 107 | s.pop(); 108 | tag.pop(); 109 | p = null; 110 | } else { 111 | p = s[s.length - 1].right; 112 | tag[tag.length - 1] = 1; 113 | } 114 | } 115 | 116 | return deep; 117 | } 118 | -------------------------------------------------------------------------------- /004-maximum-depth-of-binary-tree/Yiqi_Ou.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | //recursion version 12 | public int maxDepth(TreeNode root) { 13 | if(root == null) 14 | return 0; 15 | int leftRes = maxDepth(root.left); 16 | int rightRes = maxDepth(root.right); 17 | return Math.max(leftRes, rightRes) + 1; 18 | } 19 | 20 | //the iterative version1: the idea is to do a layer order traversal. 21 | //It uses two queues: treeQueue and layerQueue. layerQueue keeps track 22 | //of the layer of the corresponding treeNode. 23 | public int maxDepth(TreeNode root) { 24 | LinkedList treeQueue = new LinkedList<>(); 25 | LinkedList layerQueue = new LinkedList<>(); 26 | if(root == null) return 0; 27 | treeQueue.offer(root); 28 | layerQueue.offer(1); 29 | int res = 1; 30 | while(!treeQueue.isEmpty()) { 31 | int layer = layerQueue.poll(); 32 | TreeNode cur = treeQueue.poll(); 33 | res = Math.max(res, layer); 34 | if(cur.left != null) { 35 | treeQueue.offer(cur.left); 36 | layerQueue.offer(layer+1); 37 | } 38 | if(cur.right != null) { 39 | treeQueue.offer(cur.right); 40 | layerQueue.offer(layer+1); 41 | } 42 | } 43 | return res; 44 | } 45 | 46 | //iterative version2: the iterative version1 use o(2n) space. 47 | //o(n) for tree queue and o(n) for layer queue. We can actually 48 | //save o(n) layer queue space by defining a variable called curLayerNum, nextLayerNum 49 | //curLayerNum is the number of tree Nodes in this layer and nextLayerNum is the number 50 | //of treeNodes in next layer. Each time we visit a node, curLayerNum--. If it has left child 51 | // nextLayerNum++ and if it has right child, nextLayerNum++. In this way, when curLayerNum 52 | //equals to 0, we know we are going to traverse the next layer. So set curLayerNum = nextLayerNum 53 | //, nextLayerNum = 0. 54 | public int maxDepth(TreeNode root) { 55 | LinkedList queue = new LinkedList<>(); 56 | if(root == null) return 0; 57 | queue.offer(root); 58 | int curLayerNum = 1; 59 | int nextLayerNum = 0; 60 | int res = 0; 61 | while(!queue.isEmpty()) { 62 | TreeNode cur = queue.poll(); 63 | curLayerNum--; 64 | if(cur.left != null) { 65 | queue.offer(cur.left); 66 | nextLayerNum++; 67 | } 68 | if(cur.right != null) { 69 | queue.offer(cur.right); 70 | nextLayerNum++; 71 | } 72 | if(curLayerNum == 0) { 73 | curLayerNum = nextLayerNum; 74 | nextLayerNum = 0; 75 | res++; 76 | } 77 | } 78 | return res; 79 | } 80 | } --------------------------------------------------------------------------------