├── .gitignore ├── .DS_Store ├── Two-Points ├── 26 │ └── solution1.js ├── 141 │ ├── solution2.js │ └── solution1.js ├── 167 │ └── solution1.js ├── 234 │ └── solution1.js ├── 283 │ ├── solution2.js │ └── solution1.js ├── 344 │ └── solution1.js ├── 349 │ └── solution1.js ├── 424 │ └── solution1.js ├── 532 │ └── solution1.js ├── 713 │ └── solution1.js ├── 925 │ └── solution1.js ├── 930 │ ├── solution2.js │ └── solution1.js ├── 15.png ├── 16.png ├── 18.png ├── 28.png ├── 881.png ├── kmp.png ├── partial.png ├── 27-two-points.png ├── 125-two-points.png ├── 141-two-points.png ├── 167-two-points.png ├── 344-two-points.png ├── 845-brute-force.png ├── 923-combination.png ├── 923-two-points.png ├── kmp-first-match.png ├── 3-sliding-window.png ├── 567-sliding-window.png ├── 713-sliding-window.png ├── 845-sliding-window.png ├── 904-sliding-window.png ├── kmp-partial-table.png └── kmp-second-match.png ├── Binary-Search ├── 69 │ └── solution1.js ├── 153 │ └── solution1.js ├── 162 │ └── solution2.js ├── 287 │ └── solution1.js ├── 367 │ └── solution1.js ├── 378 │ └── solution1.js ├── 441 │ └── solution1.js ├── 852 │ ├── solution1.js │ └── solution2.js ├── 34.png ├── 367.png ├── 378.png ├── 475.png ├── 658.png ├── 744.png ├── 852.png ├── 875.png ├── 287-hashmap.png ├── 209-two-points.png ├── 153-binary-search.png ├── 209-binary-search.png ├── 287-binary-search.png ├── 33-binary-search.png ├── 81-binary-search.png ├── 209-two-points-example.jpeg ├── Binary_search_into_array.png └── index.js ├── Binary-Tree ├── 27 │ └── solution1.js ├── 28 │ └── solution1.js ├── 32 │ ├── solution2.js │ └── solution1.js ├── 44 │ └── solution1.js ├── 45 │ ├── solution1.js │ └── solution2.js ├── 49 │ └── solution1.js ├── 54 │ ├── solution1.js │ └── solution2.js ├── 55 │ └── solution1.js ├── 94 │ └── solution1.js ├── 96 │ └── solution1.js ├── 98 │ ├── solution2.js │ └── solution1.js ├── 100 │ └── solution1.js ├── 101 │ └── solution1.js ├── 102 │ ├── solution2.js │ └── solution1.js ├── 104 │ └── solution1.js ├── 105 │ └── solution1.js ├── 106 │ └── solution1.js ├── 107 │ └── solution2.js ├── 108 │ └── solution1.js ├── 110 │ └── solution1.js ├── 111 │ └── solution1.js ├── 112 │ └── solution1.js ├── 113 │ ├── solution1.js │ └── solution2.js ├── 114 │ └── solution1.js ├── 116 │ └── solution1.js ├── 129 │ └── solution1.js ├── 144 │ └── solution1.js ├── 145 │ ├── solution1.js │ └── solution2.js ├── 199 │ └── solution1.js ├── 222 │ └── solution1.js ├── 226 │ └── solution1.js ├── 230 │ └── solution1.js ├── 235 │ └── solution1.js ├── 236 │ └── solution1.js ├── 337 │ └── solution1.js ├── 404 │ └── solution1.js ├── 437 │ └── solution1.js ├── 508 │ └── solution1.js ├── 515 │ └── solution1.js ├── 530 │ └── solution1.js ├── 538 │ └── index.js ├── 543 │ └── solution1.js ├── 559 │ └── solution1.js ├── 563 │ └── solution1.js ├── 606 │ └── solution1.js ├── 617 │ └── solution1.js ├── 652 │ └── solution1.js ├── 653 │ └── solution1.js ├── 654 │ └── solution1.js ├── 662 │ └── solution1.js ├── 669 │ └── solution1.js ├── 700 │ └── solution1.js ├── 701 │ └── solution1.js ├── 783 │ └── solution1.js ├── 814 │ └── solution1.js ├── 865 │ └── solution1.js ├── 889 │ └── solution1.js ├── 938 │ └── solution1.js ├── 951 │ └── solution1.js ├── 958 │ └── solution1.js ├── 965 │ └── solution2.js ├── 971 │ └── solution1.js ├── 998 │ └── solution1.js ├── 1008 │ ├── solution1.js │ └── solution2.js ├── 1022 │ └── solution1.js ├── 1145 │ └── solution2.js ├── 100.png ├── 1008.png ├── 104.png ├── 105.png ├── 106.png ├── 144.png ├── 563.png ├── 783.png ├── 889.png ├── 938.png ├── 102-bfs.png ├── 102-dfs.png ├── 145-dfs.png ├── 987-1.png ├── 987-2.png ├── 145-dfs-2.png ├── binary-tree.png ├── 107-recursion.png ├── 1145-solution1.png ├── 1145-solution2.png ├── 107-queue-stack.png ├── 55-1 │ └── solution1.js ├── 0404 │ └── solution1.js └── 0402 │ └── solution1.js ├── Recursion ├── 761 │ └── solution1.js ├── 779 │ ├── solution2.js │ ├── solution3.js │ └── solution1.js ├── 779-solution1.png └── 779-solution2.png ├── array ├── 1 │ ├── index.js │ └── solution2.js ├── 46 │ └── solution1.js ├── 53 │ └── index.js ├── 58 │ └── solution1.js ├── 78 │ └── solution2.js ├── 88 │ ├── index.js │ └── solution3.js ├── 104 │ └── solution1.js ├── 118 │ └── solution1.js ├── 119 │ └── solution1.js ├── 121 │ ├── solution2.js │ └── solution1.js ├── 189 │ └── solution1.js ├── 217 │ └── solution1.js ├── 219 │ ├── solution1.js │ └── solution2.js ├── 278 │ ├── solution1.js │ └── solution2.js ├── 283 │ └── solution1.js ├── 349 │ └── solution1.js ├── 350 │ └── solution1.js ├── 377 │ └── solution1.js ├── 414 │ └── solution1.js ├── 448 │ └── solution1.js ├── 485 │ └── solution1.js ├── 494 │ └── solution1.js ├── 495 │ └── solution1.js ├── 509 │ └── solution1.js ├── 518 │ ├── solution1.js │ └── solution2.js ├── 523 │ └── solution1.js ├── 540 │ ├── solutIon.js │ └── solution2.js ├── 561 │ └── solution1.js ├── 565 │ └── solution1.js ├── 581 │ ├── solution1.js │ └── solution2.js ├── 628 │ └── solution1.js ├── 643 │ └── solution1.js ├── 717 │ └── solution1.js ├── 724 │ └── solution1.js ├── 766 │ └── solution1.js ├── 832 │ └── solution1.js ├── 867 │ └── solution1.js ├── 888 │ └── solution1.js ├── 896 │ └── solution1.js ├── 905 │ └── solution1.js ├── 977 │ └── solution1.js ├── 1010 │ ├── solution2.js │ └── solution1.js ├── 1018 │ └── solution1.js ├── 1049 │ └── solution1.js └── 1051 │ └── solution1.js ├── dynamic-programming ├── 53 │ └── solution1.js ├── 70 │ └── soluttion1.js ├── 121 │ └── solution2.js ├── 122 │ └── solution1.js ├── 152 │ └── solution2.js ├── 198 │ └── solution1.js ├── 263 │ └── solution1.js ├── 279 │ ├── solution2.js │ └── solution1.js ├── 343 │ ├── solution2.js │ └── solution1.js ├── 357 │ └── solution1.js ├── 377 │ ├── solution2.js │ └── solution1.js ├── 392 │ └── solution2.js ├── 413 │ ├── solution2.js │ └── solution1.js ├── 486 │ └── solution1.js ├── 494 │ └── solution1.js ├── 523 │ ├── solution.js │ └── readme.md ├── 650 │ └── leetcode650.png ├── 714 │ └── solution1.js ├── 746 │ └── solution1.js └── 790 │ └── solution1.js ├── string ├── 3 │ └── solution1.js ├── 9 │ └── solution1.js ├── 20 │ └── solution1.js ├── 242 │ └── solution1.js ├── 387 │ ├── solution2.js │ └── solution1.js ├── 392 │ ├── solution1.js │ └── solution2.js ├── 516 │ └── solution1.js ├── 647 │ └── solution1.js ├── 709 │ └── solution1.js ├── 771 │ └── solution1.js └── 1108 │ └── solution1.js ├── one-question-per-day ├── 20200305 │ └── solution1.js ├── 20200320 │ └── solution1.js ├── 20200330 │ └── solution1.js ├── 20200805 │ └── solution1.js ├── 20200807 │ └── solution1.js ├── 20200817 │ ├── solution2.js │ └── solution1.js ├── 20200819 │ └── solution1.js ├── 20200821 │ └── solution1.js ├── 20200823 │ └── solution1.js ├── 20200824 │ └── solution1.js ├── 20200828 │ └── solution1.js ├── 20200830 │ └── solution1.js ├── 20200831 │ └── solution1.js ├── 20200901 │ ├── solution1.js │ └── solution2.js ├── 20200904 │ └── solution1.js ├── 20200906 │ ├── solution2.js │ └── solution1.js ├── 20200907 │ └── solution1.js ├── 20200908 │ └── solution1.js ├── 20200909 │ └── solution1.js ├── 20200912 │ └── solution1.js ├── 20200921 │ └── solution1.js ├── 20200928 │ └── solution1.js ├── 20200929 │ └── solution1.js ├── 20201012 │ └── solution1.js ├── 20201013 │ ├── solution1.js │ └── solution2.js ├── 20201014 │ └── solution1.js ├── 20201015 │ └── solution1.js ├── 20201016 │ ├── solution1.js │ └── solution2.js ├── 20201018 │ └── solution1.js ├── 20201021 │ └── solution1.js ├── 20201112 │ ├── solution1.js │ └── solution2.js ├── 20201123 │ └── solution1.js ├── 20210603 │ └── index.js ├── 20210614 │ └── solution1.js ├── 20210615 │ └── solution1.js ├── 20210623 │ ├── solution2.js │ └── solution1.js ├── 20210822 │ └── solution1.js ├── 20210823 │ └── solution1.js ├── 20210825 │ └── solution1.js ├── 20210826 │ └── solution1.js ├── 20210828 │ └── solution.js ├── 20211224 │ └── solution1.js ├── 20211226 │ ├── solution2.js │ └── solution1.js ├── 20211228 │ ├── solution1.js │ └── solution2.js ├── 20211229 │ └── solution1.js ├── 20211231 │ ├── solution1.js │ └── solution2.js ├── 20220101 │ └── solution1.js ├── 20220103 │ └── solution2.js ├── 20220105 │ └── solution1.js ├── 20220106 │ └── solution1.js ├── 20220107 │ └── solution1.js ├── 20220108 │ └── solution1.js ├── 20220109 │ └── solution1.js ├── 20220113 │ ├── solution2.js │ └── solution1.js └── 20220115 │ └── solution1.js ├── Queue └── 621 │ └── solution1.js ├── Heap ├── 40 │ └── solution1.js ├── 215 │ └── solution1.js ├── 973 │ └── solution1.js └── 1714 │ └── solution1.js ├── Linked-List ├── 19 │ ├── solution1.js │ └── solution2.js ├── 21 │ ├── solution2.js │ └── solutIon1.js ├── 24 │ ├── solution1.js │ └── solution2.js ├── 27 │ └── solution1.js ├── 83 │ └── solution1.js ├── 92 │ └── solution2.js ├── 141 │ ├── solution1.js │ └── solution2.js ├── 148 │ └── solution1.js ├── 160 │ ├── solution2.js │ └── index.js ├── 203 │ ├── index.js │ └── solution1.js ├── 206 │ ├── solution2.js │ └── solution1.js ├── 237 │ └── solution1.js ├── 328 │ └── solution1.js ├── 817 │ └── solution1.js ├── 876 │ └── solution1.js ├── 1290 │ └── solution.js ├── 1721 │ └── solution2.js └── 2095 │ └── solution1.js ├── Rank ├── 173 │ └── 1332 │ │ └── solution.js ├── 174 │ └── 1337 │ │ └── solution1.js ├── 177 │ └── 5169 │ │ └── solution1.js ├── 178 │ └── 5344 │ │ └── solution1.js ├── 179 │ ├── 1374 │ │ └── solution1.js │ └── 1375 │ │ ├── solution1.js │ │ └── solution2.js ├── 181 │ ├── 1392 │ │ └── solution1.js │ ├── 5178 │ │ └── solution1.js │ └── 5364 │ │ └── solution1.js ├── 182 │ └── 5369 │ │ └── solution1.js ├── 183 │ └── 183-1 │ │ └── solution1.js ├── 184 │ ├── 1409 │ │ └── solution1.js │ └── 1411 │ │ └── solution1.js ├── 186 │ └── solution1.js ├── 200 │ └── solution1.js ├── 201 │ ├── solution1.js │ ├── solution3.js │ └── solution2.js └── biweekly │ ├── 20 │ ├── 5323 │ │ └── solution2.js │ └── 5326 │ │ └── solution1.js │ ├── 22 │ ├── 5348 │ │ └── solution1.js │ └── 5350 │ │ └── solution1.js │ ├── 23 │ └── 23-4 │ │ └── solution1.js │ ├── 24 │ └── 1 │ │ └── solution1.js │ └── 25 │ └── solution1.js ├── Small-Skill └── 287 │ └── solution1.js ├── Stack ├── 496 │ └── solution1.js ├── 739 │ └── solution1.js ├── 1021 │ └── solution1.js └── 1047 │ └── solution1.js ├── Other └── 153 │ └── solution1.js ├── math ├── 204 │ ├── solution2.js │ └── solution1.js └── 263 │ ├── solution1.js │ └── readme.md └── Greedy └── 944 └── solution2.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/.DS_Store -------------------------------------------------------------------------------- /Two-Points/15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/15.png -------------------------------------------------------------------------------- /Two-Points/16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/16.png -------------------------------------------------------------------------------- /Two-Points/18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/18.png -------------------------------------------------------------------------------- /Two-Points/28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/28.png -------------------------------------------------------------------------------- /Two-Points/881.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/881.png -------------------------------------------------------------------------------- /Two-Points/kmp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/kmp.png -------------------------------------------------------------------------------- /Binary-Search/34.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/34.png -------------------------------------------------------------------------------- /Binary-Tree/100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/100.png -------------------------------------------------------------------------------- /Binary-Tree/1008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/1008.png -------------------------------------------------------------------------------- /Binary-Tree/104.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/104.png -------------------------------------------------------------------------------- /Binary-Tree/105.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/105.png -------------------------------------------------------------------------------- /Binary-Tree/106.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/106.png -------------------------------------------------------------------------------- /Binary-Tree/144.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/144.png -------------------------------------------------------------------------------- /Binary-Tree/563.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/563.png -------------------------------------------------------------------------------- /Binary-Tree/783.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/783.png -------------------------------------------------------------------------------- /Binary-Tree/889.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/889.png -------------------------------------------------------------------------------- /Binary-Tree/938.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/938.png -------------------------------------------------------------------------------- /Binary-Search/367.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/367.png -------------------------------------------------------------------------------- /Binary-Search/378.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/378.png -------------------------------------------------------------------------------- /Binary-Search/475.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/475.png -------------------------------------------------------------------------------- /Binary-Search/658.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/658.png -------------------------------------------------------------------------------- /Binary-Search/744.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/744.png -------------------------------------------------------------------------------- /Binary-Search/852.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/852.png -------------------------------------------------------------------------------- /Binary-Search/875.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/875.png -------------------------------------------------------------------------------- /Binary-Tree/102-bfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/102-bfs.png -------------------------------------------------------------------------------- /Binary-Tree/102-dfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/102-dfs.png -------------------------------------------------------------------------------- /Binary-Tree/145-dfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/145-dfs.png -------------------------------------------------------------------------------- /Binary-Tree/987-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/987-1.png -------------------------------------------------------------------------------- /Binary-Tree/987-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/987-2.png -------------------------------------------------------------------------------- /Two-Points/partial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/partial.png -------------------------------------------------------------------------------- /Binary-Tree/145-dfs-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/145-dfs-2.png -------------------------------------------------------------------------------- /Binary-Tree/binary-tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/binary-tree.png -------------------------------------------------------------------------------- /Recursion/779-solution1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Recursion/779-solution1.png -------------------------------------------------------------------------------- /Recursion/779-solution2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Recursion/779-solution2.png -------------------------------------------------------------------------------- /Two-Points/27-two-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/27-two-points.png -------------------------------------------------------------------------------- /array/58/solution1.js: -------------------------------------------------------------------------------- 1 | const lengthOfLastWord = s => s.split(' ').filter(item => item !== '').pop().length; -------------------------------------------------------------------------------- /Binary-Search/287-hashmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/287-hashmap.png -------------------------------------------------------------------------------- /Binary-Tree/107-recursion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/107-recursion.png -------------------------------------------------------------------------------- /Binary-Tree/1145-solution1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/1145-solution1.png -------------------------------------------------------------------------------- /Binary-Tree/1145-solution2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/1145-solution2.png -------------------------------------------------------------------------------- /Two-Points/125-two-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/125-two-points.png -------------------------------------------------------------------------------- /Two-Points/141-two-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/141-two-points.png -------------------------------------------------------------------------------- /Two-Points/167-two-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/167-two-points.png -------------------------------------------------------------------------------- /Two-Points/344-two-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/344-two-points.png -------------------------------------------------------------------------------- /Two-Points/845-brute-force.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/845-brute-force.png -------------------------------------------------------------------------------- /Two-Points/923-combination.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/923-combination.png -------------------------------------------------------------------------------- /Two-Points/923-two-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/923-two-points.png -------------------------------------------------------------------------------- /Two-Points/kmp-first-match.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/kmp-first-match.png -------------------------------------------------------------------------------- /Binary-Search/209-two-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/209-two-points.png -------------------------------------------------------------------------------- /Binary-Tree/107-queue-stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Tree/107-queue-stack.png -------------------------------------------------------------------------------- /Two-Points/3-sliding-window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/3-sliding-window.png -------------------------------------------------------------------------------- /Two-Points/567-sliding-window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/567-sliding-window.png -------------------------------------------------------------------------------- /Two-Points/713-sliding-window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/713-sliding-window.png -------------------------------------------------------------------------------- /Two-Points/845-sliding-window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/845-sliding-window.png -------------------------------------------------------------------------------- /Two-Points/904-sliding-window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/904-sliding-window.png -------------------------------------------------------------------------------- /Two-Points/kmp-partial-table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/kmp-partial-table.png -------------------------------------------------------------------------------- /Two-Points/kmp-second-match.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Two-Points/kmp-second-match.png -------------------------------------------------------------------------------- /Binary-Search/153-binary-search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/153-binary-search.png -------------------------------------------------------------------------------- /Binary-Search/209-binary-search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/209-binary-search.png -------------------------------------------------------------------------------- /Binary-Search/287-binary-search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/287-binary-search.png -------------------------------------------------------------------------------- /Binary-Search/33-binary-search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/33-binary-search.png -------------------------------------------------------------------------------- /Binary-Search/81-binary-search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/81-binary-search.png -------------------------------------------------------------------------------- /dynamic-programming/650/leetcode650.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/dynamic-programming/650/leetcode650.png -------------------------------------------------------------------------------- /Binary-Search/209-two-points-example.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/209-two-points-example.jpeg -------------------------------------------------------------------------------- /Binary-Search/Binary_search_into_array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descire/LeetCode/HEAD/Binary-Search/Binary_search_into_array.png -------------------------------------------------------------------------------- /string/647/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Manacher 3 | * 4 | * 时间复杂度:O(n) 5 | * 空间复杂度:O(n) 6 | */ 7 | const countSubstrings = s => { 8 | 9 | } -------------------------------------------------------------------------------- /one-question-per-day/20200830/solution1.js: -------------------------------------------------------------------------------- 1 | const reverseWords = function(s) { 2 | return s.split(' ').map(item => item.split('').reverse().join('')).join(' '); 3 | }; -------------------------------------------------------------------------------- /one-question-per-day/20210623/solution2.js: -------------------------------------------------------------------------------- 1 | const hammingWeight = (n) => { 2 | let ans = 0; 3 | while (n) { 4 | n &= n - 1; 5 | ans++; 6 | } 7 | 8 | return ans; 9 | } -------------------------------------------------------------------------------- /Queue/621/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/task-scheduler/ 3 | * 4 | * 621. 任务调度器 5 | * 6 | * Medium 7 | */ 8 | const leastInterval = (tasks, n) => { 9 | } -------------------------------------------------------------------------------- /one-question-per-day/20211231/solution1.js: -------------------------------------------------------------------------------- 1 | // 考数学???? 2 | var checkPerfectNumber = function(num) { 3 | return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336 4 | }; -------------------------------------------------------------------------------- /one-question-per-day/20200823/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(logn) 3 | */ 4 | const rangeBitwiseAnd = function(m, n) { 5 | while(m < n) { 6 | n = n & (n - 1); 7 | } 8 | return n; 9 | }; -------------------------------------------------------------------------------- /one-question-per-day/20201016/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 排序 3 | * 时间复杂度 O(nlogn) 4 | * 空间复杂度 O(n) 5 | */ 6 | const sortedSquares = function(A) { 7 | const ans = A.map(item => item * item); 8 | 9 | return ans.sort((a, b) => a - b); 10 | }; -------------------------------------------------------------------------------- /Recursion/761/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/special-binary-string/ 3 | * 4 | * 761. 特殊的二进制序列 5 | * 6 | * Hard 7 | * 8 | * 使得字符最大,也就是尽量将“1”放在左边,“0”放在右边。 9 | */ 10 | const makeLargestSpecial = S => { 11 | 12 | } -------------------------------------------------------------------------------- /one-question-per-day/20210623/solution1.js: -------------------------------------------------------------------------------- 1 | const hammingWeight = (n) => { 2 | let ans = 0; 3 | n = n.toString(2); 4 | for (let i = 0; i < n.length; i++) { 5 | if (n[i] === '1') { 6 | ans++; 7 | } 8 | } 9 | return ans; 10 | } -------------------------------------------------------------------------------- /Binary-Tree/104/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 4 | * 空间复杂度:O(height) 5 | * 6 | */ 7 | const maxDepth = root => { 8 | if (!root) { 9 | return 0; 10 | } 11 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 12 | } -------------------------------------------------------------------------------- /string/709/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 709. 转换成小写字母 3 | * 4 | * https://leetcode-cn.com/problems/to-lower-case/ 5 | * 6 | * Easy 7 | * 8 | * 64ms 95.27% 9 | * 33.5mb 79.35% 10 | */ 11 | const toLowerCase = (str) => { 12 | return str.toLowerCase(); 13 | } -------------------------------------------------------------------------------- /array/977/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/squares-of-a-sorted-array/ 3 | * 4 | * 977. Squares of a Sorted Array 5 | * 6 | * Easy 7 | */ 8 | const sortedSquares = A => { 9 | return A.map(item => item * item).sort((x, y) => x - y) 10 | } -------------------------------------------------------------------------------- /Binary-Tree/27/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | */ 4 | const mirrorTree = function(root) { 5 | if (!root) { 6 | return null; 7 | } 8 | const temp = root.left; 9 | root.left = mirrorTree(root.right); 10 | root.right = mirrorTree(temp); 11 | return root; 12 | }; -------------------------------------------------------------------------------- /array/540/solutIon.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const singleNonDuplicate = function(nums) { 6 | let current = 0; 7 | 8 | while (nums[current] === nums[current + 1]) { 9 | current += 2; 10 | } 11 | 12 | return nums[current]; 13 | }; -------------------------------------------------------------------------------- /one-question-per-day/20220103/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 调用方法 3 | */ 4 | const dayOfTheWeek = function(day, month, year) { 5 | const week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 6 | return week[new Date(year, month - 1, day).getDay()]; 7 | }; -------------------------------------------------------------------------------- /Heap/1714/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/smallest-k-lcci/ 3 | * 4 | * 最小K个数 5 | * 6 | * Medium 7 | * 8 | * 164ms 100.00% 9 | * 41.6mb 100.00% 10 | */ 11 | const smallestK = (arr, k) => { 12 | arr.sort((a, b) => a - b); 13 | return arr.slice(0, k); 14 | } -------------------------------------------------------------------------------- /array/518/solution1.js: -------------------------------------------------------------------------------- 1 | const change = (amount, coins) => { 2 | const dp = new Array(amount + 1).fill(0); 3 | dp[0] = 1; 4 | 5 | for (const coin of coins) { 6 | for (let i = coin; i <= amount; i++) { 7 | dp[i] += dp[i - coin]; 8 | } 9 | } 10 | return dp[amount]; 11 | } -------------------------------------------------------------------------------- /string/1108/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/defanging-an-ip-address/ 3 | * 4 | * 1108. IP 地址无效化 5 | * 6 | * Easy 7 | * 8 | * 72ms 81.93% 9 | * 33.7mb 100.00% 10 | */ 11 | const defangIPaddr = address => { 12 | return address.replace(/\./g, '[.]'); 13 | } -------------------------------------------------------------------------------- /Linked-List/203/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 4 | * 时间复杂度:O(n) 5 | * 空间复杂度:O(n) 6 | */ 7 | const removeElements = function(head, val) { 8 | if (!head) { 9 | return null; 10 | } 11 | 12 | head.next = removeElements(head.next, val); 13 | return head.val === val ? head.next : head; 14 | }; -------------------------------------------------------------------------------- /string/387/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(1) 4 | */ 5 | const firstUniqChar = function(s) { 6 | for (let i = 0; i < s.length; i++) { 7 | if (s.lastIndexOf(s[i]) === s.indexOf(s[i])) { 8 | return i; 9 | } 10 | } 11 | 12 | return -1; 13 | }; -------------------------------------------------------------------------------- /Heap/40/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/ 3 | * 4 | * 40. 最小的k个数 5 | * 6 | * Easy 7 | * 8 | * 132ms 75.00% 9 | * 39.5mb 100.00% 10 | */ 11 | const getLeastNumbers = (arr, k) => { 12 | arr.sort((a, b) => a - b); 13 | return arr.slice(0, k); 14 | } -------------------------------------------------------------------------------- /one-question-per-day/20201013/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归解法 3 | */ 4 | const swapPairs = function(head) { 5 | if (head === null || head.next === null) { 6 | return head; 7 | } 8 | const next = head.next; 9 | head.next = swapPairs(next.next); 10 | next.next = head; 11 | return next; 12 | }; -------------------------------------------------------------------------------- /Binary-Tree/114/solution1.js: -------------------------------------------------------------------------------- 1 | const flatten = root => { 2 | let cur = null 3 | order(root) 4 | function order (root) { 5 | if (!root) { 6 | return null 7 | } 8 | order(root.right) 9 | order(root.left) 10 | root.right = cur 11 | root.left = null 12 | cur = root 13 | } 14 | } -------------------------------------------------------------------------------- /one-question-per-day/20211228/solution1.js: -------------------------------------------------------------------------------- 1 | var twoSum = function(nums, target) { 2 | for (let i = 0; i < nums.length; i++) { 3 | for (let j = i + 1; j < nums.length; j++) { 4 | if (nums[i] + nums[j] === target) { 5 | return [i, j]; 6 | } 7 | } 8 | } 9 | }; -------------------------------------------------------------------------------- /Linked-List/237/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 3 | * 4 | * 237. 删除链表中的节点 5 | * 6 | * Easy 7 | * 8 | * 84ms 91.84% 9 | * 35.5mb 66.57% 10 | */ 11 | const deleteNode = node => { 12 | node.val = node.next.val; 13 | node.next = node.next.next; 14 | } -------------------------------------------------------------------------------- /one-question-per-day/20220108/solution1.js: -------------------------------------------------------------------------------- 1 | const grayCode = function(n) { 2 | const ans = [0]; 3 | for (let i = 1; i <=n; i++) { 4 | const m = ans.length; 5 | for (let j = m - 1; j >= 0; j--) { 6 | ans.push(ans[j] | (1 << (i - 1))) 7 | } 8 | } 9 | return ans; 10 | }; -------------------------------------------------------------------------------- /one-question-per-day/20200807/solution1.js: -------------------------------------------------------------------------------- 1 | var isSameTree = function(p, q) { 2 | if (!p && !q) { 3 | return true; 4 | } 5 | if (!p || !q) { 6 | return false; 7 | } 8 | if (p.val !== q.val) { 9 | return false; 10 | } 11 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 12 | }; -------------------------------------------------------------------------------- /Binary-Tree/222/solution1.js: -------------------------------------------------------------------------------- 1 | const countNodes = root => { 2 | let ans = 0 3 | if (!root) { 4 | return ans 5 | } 6 | inOrder(root) 7 | return ans 8 | function inOrder (root) { 9 | if (!root) { 10 | return 11 | } 12 | inOrder(root.left) 13 | ans++ 14 | inOrder(root.right) 15 | } 16 | } -------------------------------------------------------------------------------- /Binary-Tree/226/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const invertTree = (root) => { 6 | if (!root) { 7 | return null; 8 | } 9 | 10 | [root.left, root.right] = [root.right, root.left]; 11 | 12 | invertTree(root.left); 13 | 14 | invertTree(root.right); 15 | 16 | return root; 17 | } -------------------------------------------------------------------------------- /Heap/215/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/kth-largest-element-in-an-array/ 3 | * 4 | * 215. 数组中的第K个最大元素 5 | * 6 | * Medium 7 | * 8 | * 88ms 61.86% 9 | * 36.5mb 11.30% 10 | * 11 | */ 12 | const findKthLargest = (nums, k) => { 13 | nums.sort((a, b) => b - a); 14 | return nums[k - 1]; 15 | } -------------------------------------------------------------------------------- /Linked-List/141/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const hasCycle = head => { 6 | const record = new Set(); 7 | 8 | while (head) { 9 | if (record.has(head)) { 10 | return true; 11 | } 12 | record.add(head); 13 | head = head.next; 14 | } 15 | 16 | return false; 17 | } -------------------------------------------------------------------------------- /one-question-per-day/20200320/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/ 3 | * 4 | * 最小的k个数 5 | * 6 | * Easy 7 | * 8 | * 184ms 27.33% 9 | * 39.5mb 100.00% 10 | */ 11 | const getLeastNumbers = (arr, k) => { 12 | arr.sort((a, b) => a - b); 13 | return arr.slice(0, k); 14 | } -------------------------------------------------------------------------------- /one-question-per-day/20220115/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 模拟 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(1) 5 | * @param {*} n 6 | * @returns 7 | */ 8 | const totalMoney = function(n) { 9 | let ans = 0; 10 | for (let i = 0; i < n; i++) { 11 | ans += Math.floor(i / 7) + (i % 7 + 1); 12 | } 13 | 14 | return ans; 15 | }; -------------------------------------------------------------------------------- /Linked-List/206/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 4 | * 时间复杂度:O(n) 5 | * 空间复杂度:O(n) 6 | * 7 | */ 8 | const reverseList = head => { 9 | if (!head || !head.next) { 10 | return head; 11 | } 12 | 13 | const newHead = reverseList(head.next); 14 | head.next.next = head; 15 | head.next = null; 16 | return newHead; 17 | } -------------------------------------------------------------------------------- /Linked-List/24/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const swapPairs = head => { 6 | if (head === null || head.next === null) { 7 | return head; 8 | } 9 | const newHead = head.next; 10 | head.next = swapPairs(newHead.next); 11 | newHead.next = head; 12 | return newHead; 13 | } -------------------------------------------------------------------------------- /array/189/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/rotate-array/ 3 | * 4 | * 189. Rotate Array 5 | * 6 | * Easy 7 | * 8 | * 92ms 54.25% 9 | * 35.4mb 38.76% 10 | */ 11 | const rotate = (nums, k) => { 12 | while (k--) { 13 | const item = nums.pop() 14 | nums.unshift(item) 15 | } 16 | return nums 17 | } -------------------------------------------------------------------------------- /one-question-per-day/20210828/solution.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var runningSum = function(nums) { 6 | const ans = []; 7 | let current = 0; 8 | for (let i = 0; i < nums.length; i++) { 9 | current += nums[i]; 10 | ans.push(current); 11 | } 12 | 13 | return ans; 14 | }; -------------------------------------------------------------------------------- /Binary-Tree/199/solution1.js: -------------------------------------------------------------------------------- 1 | const rightSideView = root => { 2 | const ans = [] 3 | levelOrder(root, 0) 4 | return ans 5 | function levelOrder (root, level) { 6 | if (!root) { 7 | return 8 | } 9 | ans[level] = root.val 10 | levelOrder(root.left, level + 1) 11 | levelOrder(root.right, level + 1) 12 | } 13 | } -------------------------------------------------------------------------------- /array/278/solution1.js: -------------------------------------------------------------------------------- 1 | const solution = function(isBadVersion) { 2 | /** 3 | * @param {integer} n Total versions 4 | * @return {integer} The first bad version 5 | */ 6 | return function(n) { 7 | for (let i = 0; i <= n; i++) { 8 | if (isBadVersion(i)) { 9 | return i; 10 | } 11 | } 12 | }; 13 | }; -------------------------------------------------------------------------------- /Rank/biweekly/25/solution1.js: -------------------------------------------------------------------------------- 1 | const kidsWithCandies = (candies, extraCandies) => { 2 | const max = Math.max(...candies); 3 | let ans = []; 4 | for (let i = 0; i < candies.length; i++) { 5 | if (candies[i] + extraCandies >= max) { 6 | ans[i] = true; 7 | } else { 8 | ans[i] = false; 9 | } 10 | } 11 | return ans; 12 | } -------------------------------------------------------------------------------- /array/561/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/array-partition-i/ 3 | * 4 | * 561. Array Partition I 5 | * 6 | * Easy 7 | */ 8 | const arrayPairSum = nums => { 9 | nums.sort((a, b) => a - b) 10 | let ans = 0 11 | for (let i = 0, max = nums.length; i < max; i += 2) { 12 | ans += nums[i] 13 | } 14 | return ans 15 | } -------------------------------------------------------------------------------- /Binary-Tree/129/solution1.js: -------------------------------------------------------------------------------- 1 | const sumNumbers = root => { 2 | return order(root, 0) 3 | function order (root, sum) { 4 | if (!root) { 5 | return 0 6 | } 7 | sum = root.val + sum * 10 8 | if (!root.left && !root.right) { 9 | return sum 10 | } 11 | return order(root.left, sum) + order(root.right, sum) 12 | } 13 | } -------------------------------------------------------------------------------- /Binary-Tree/236/solution1.js: -------------------------------------------------------------------------------- 1 | const lowestCommonAncestor = (root, p, q) => { 2 | if (!root || root == p || root == q) { 3 | return root 4 | } 5 | const left = lowestCommonAncestor(root.left, p, q) 6 | const right = lowestCommonAncestor(root.right, p, q) 7 | 8 | if (left && right) { 9 | return root 10 | } 11 | 12 | return left ? left : right 13 | } -------------------------------------------------------------------------------- /dynamic-programming/70/soluttion1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/climbing-stairs/ 4 | * 5 | * 70. Climbing Stairs 6 | * 7 | * Easy 8 | */ 9 | const climbStairs = n => { 10 | const dp = [] 11 | 12 | dp[1] = 1 13 | dp[2] = 2 14 | for (let i = 3; i <= n; i++) { 15 | dp[i] = dp[i - 1] + dp[i - 2] 16 | } 17 | return dp[n] 18 | } -------------------------------------------------------------------------------- /Linked-List/206/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 迭代 3 | * 4 | * 时间复杂度:O(n) 5 | * 6 | * 空间复杂度:O(1) 7 | * 8 | */ 9 | const reverseList = head => { 10 | let prev = null; 11 | let current = head; 12 | while (current) { 13 | const next = current.next; 14 | current.next = prev; 15 | prev = current; 16 | current = next; 17 | } 18 | 19 | return prev; 20 | } -------------------------------------------------------------------------------- /array/88/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 直接合并两个数组,然后排序处理 3 | * 4 | * 时间复杂度 O((m + n)log(m+n)) 5 | * 空间复杂度 O(m + n) 6 | * @param {*} nums1 7 | * @param {*} m 8 | * @param {*} nums2 9 | * @param {*} n 10 | * @returns 11 | */ 12 | var merge = function(nums1, m, nums2, n) { 13 | nums1.splice(m, nums1.length - m, ...nums2); 14 | return nums1.sort((a, b) => a - b); 15 | }; -------------------------------------------------------------------------------- /Binary-Tree/700/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const searchBST = (root, val) => { 6 | if (!root) { 7 | return null; 8 | } 9 | 10 | if (root.val === val) { 11 | return root; 12 | } 13 | 14 | if (root.val > val) { 15 | return searchBST(root.left, val); 16 | } else { 17 | return searchBST(root.right, val); 18 | } 19 | } -------------------------------------------------------------------------------- /Linked-List/160/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 双指针 3 | */ 4 | const getIntersectionNode = function(headA, headB) { 5 | if (headA === null || headB === null) { 6 | return null; 7 | } 8 | 9 | let a = headA; 10 | let b = headB; 11 | 12 | while (a !== b) { 13 | a = a === null ? headB : a.next; 14 | b = b === null ? headA : b.next; 15 | } 16 | 17 | return a; 18 | } -------------------------------------------------------------------------------- /array/1/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 暴力解法 3 | * 时间复杂度 O(n^2) 4 | * 空间复杂度 O(1); 5 | * @param {*} nums 6 | * @param {*} target 7 | */ 8 | const twoSum = (nums, target) => { 9 | for (let i = 0; i < nums.length; i++) { 10 | for (let j = i + 1; j < nums.length; j++) { 11 | if (nums[i] + nums[j] === target) { 12 | return [i, j]; 13 | } 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Two-Points/344/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/reverse-string/ 3 | * 4 | * 344. Reverse String 5 | * 6 | * Easy 7 | * 93.72% 8 | * 26.52% 9 | */ 10 | const reverseString = s => { 11 | let start = 0 12 | let end = s.length - 1 13 | while (start < end) { 14 | [s[start], s[end]] = [s[end], s[start]] 15 | start++ 16 | end-- 17 | } 18 | } -------------------------------------------------------------------------------- /one-question-per-day/20200921/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(n) 4 | */ 5 | const convertBST = function(root) { 6 | let sum = 0; 7 | dfs(root); 8 | return root; 9 | function dfs(root) { 10 | if (!root) { 11 | return; 12 | } 13 | dfs(root.right); 14 | root.val += sum; 15 | sum = root.val; 16 | dfs(root.left); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /one-question-per-day/20201015/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | */ 4 | const connect = root => { 5 | if (!root) { 6 | return null; 7 | } 8 | if (root.left) { 9 | root.left.next = root.right; 10 | } 11 | if (root.right && root.next) { 12 | root.right.next = root.next.left; 13 | } 14 | 15 | connect(root.left); 16 | connect(root.right); 17 | return root; 18 | } -------------------------------------------------------------------------------- /Binary-Search/852/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/peak-index-in-a-mountain-array/ 3 | * 4 | * 852. Peak Index in a Mountain Array 5 | * 6 | * Easy 7 | * 8 | * 普通解法 9 | */ 10 | const peakIndexInMountainArray = A => { 11 | const max = A.length 12 | for (let i = 0; i < max;i++) { 13 | if (A[i + 1] < A[i]) { 14 | return i 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Binary-Tree/958/solution1.js: -------------------------------------------------------------------------------- 1 | const isCompleteTree = root => { 2 | const stack = [root] 3 | 4 | while (stack[0] != null) { 5 | root = stack.shift() 6 | stack.push(root.left) 7 | stack.push(root.right) 8 | } 9 | 10 | for (let i = 1; i < stack.length; i++) { 11 | if (stack[i] != null) { 12 | return false 13 | } 14 | } 15 | return true 16 | } -------------------------------------------------------------------------------- /array/121/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(1) 4 | * 5 | * 超出时间限制 6 | */ 7 | const maxProfit = function(prices) { 8 | let ans = 0; 9 | for (let i = 0; i < prices.length - 1; i++) { 10 | for (let j = i + 1; j < prices.length; j++) { 11 | ans = Math.max(ans, prices[j] - prices[i]); 12 | } 13 | } 14 | 15 | return ans; 16 | }; -------------------------------------------------------------------------------- /one-question-per-day/20200929/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | */ 6 | const postorderTraversal = function(root) { 7 | const ans = []; 8 | const dfs = (root) => { 9 | if (!root) { 10 | return; 11 | } 12 | dfs(root.left); 13 | dfs(root.right); 14 | ans.push(root.val); 15 | } 16 | 17 | dfs(root); 18 | return ans; 19 | }; -------------------------------------------------------------------------------- /one-question-per-day/20210615/solution1.js: -------------------------------------------------------------------------------- 1 | const peakIndexInMountainArray = arr => { 2 | let start = 0; 3 | let end = arr.length - 1; 4 | 5 | while (start < end) { 6 | const mid = Math.floor(start + (end - start + 1) / 2); 7 | if (arr[mid] > arr[mid - 1]) { 8 | start = mid; 9 | } else { 10 | end = mid - 1; 11 | } 12 | } 13 | 14 | return start; 15 | } -------------------------------------------------------------------------------- /one-question-per-day/20211226/solution2.js: -------------------------------------------------------------------------------- 1 | const findOcurrences = function(text, first, second) { 2 | const ans = []; 3 | const textList = text.split(' '); 4 | for (let i = 0; i <= textList.length - 3; i++) { 5 | if (textList[i] === first && textList[i + 1] === second) { 6 | ans.push(textList[i + 2]); 7 | } 8 | } 9 | 10 | return ans; 11 | }; -------------------------------------------------------------------------------- /Binary-Tree/94/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const help = (root, ans) => { 6 | if (!root) { 7 | return; 8 | } 9 | // 左 10 | help(root.left, ans); 11 | // 中 12 | ans.push(root.val); 13 | // 右 14 | help(root.right, ans); 15 | } 16 | 17 | const inorderTraversal = root => { 18 | const ans = []; 19 | help(root, ans); 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Binary-Tree/144/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const help = (root, ans) => { 6 | if (!root) { 7 | return; 8 | } 9 | 10 | ans.push(root.val); 11 | // 遍历左子树 12 | help(root.left, ans); 13 | // 遍历右子树 14 | help(root.right, ans); 15 | } 16 | const preorderTraversal = root => { 17 | const ans = []; 18 | help(root, ans); 19 | return ans; 20 | } 21 | -------------------------------------------------------------------------------- /Binary-Tree/145/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const postorderTraversal = root => { 6 | const ans = []; 7 | help(root, ans); 8 | return ans 9 | } 10 | 11 | function help(root, ans) { 12 | if (!root) { 13 | return; 14 | } 15 | 16 | // 左 17 | help(root.left, ans); 18 | // 右 19 | help(root.right, ans); 20 | // 中 21 | ans.push(root.val); 22 | } -------------------------------------------------------------------------------- /Binary-Tree/98/solution2.js: -------------------------------------------------------------------------------- 1 | const isValidBST = root => { 2 | 3 | return helper(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER) 4 | 5 | function helper (root, start, end) { 6 | if (!root) { 7 | return true 8 | } 9 | const rv = root.val 10 | const x = rv > start && rv < end 11 | return x && helper(root.left, start, rv) && helper(root.right, rv, end) 12 | } 13 | } -------------------------------------------------------------------------------- /Binary-Tree/701/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const insertIntoBST = (root, val) => { 6 | if (!root) { 7 | return new TreeNode(val); 8 | } 9 | 10 | if (root.val > val) { 11 | root.left = insertIntoBST(root.left, val); 12 | } 13 | 14 | if (root.val < val) { 15 | root.right = insertIntoBST(root.right, val); 16 | } 17 | 18 | return root; 19 | } -------------------------------------------------------------------------------- /Binary-Search/index.js: -------------------------------------------------------------------------------- 1 | function binarySearch(arr, target) { 2 | let start = 0 3 | let end = arr.length - 1 4 | 5 | while (start < end) { 6 | const mid = Math.floor(start + (end - start) / 2) 7 | if (arr[mid] < target) { 8 | start = mid + 1 9 | } else { 10 | end = mid 11 | } 12 | } 13 | 14 | return start 15 | } 16 | 17 | console.log(binarySearch([0, 1, 2, 3, 4, 5], 5)) -------------------------------------------------------------------------------- /one-question-per-day/20200330/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/ 3 | * 4 | * 面试题62. 圆圈中最后剩下的数字 5 | * 6 | * Easy 7 | * 8 | * 64ms 96.77% 9 | * 35mb 100.00% 10 | */ 11 | const lastRemaining = (n, m) => { 12 | let ans = 0; 13 | for (let i = 2; i != n + 1; i++) { 14 | ans = (m + ans) % i; 15 | } 16 | return ans; 17 | } -------------------------------------------------------------------------------- /Binary-Tree/112/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(height) 4 | */ 5 | const hasPathSum = (root, sum) => { 6 | if (!root) { 7 | return false; 8 | } 9 | if (!root.left && !root.right) { 10 | return root.val === sum; 11 | } 12 | 13 | const reset = sum - root.val; 14 | const x = hasPathSum(root.left, reset); 15 | const y = hasPathSum(root.right, reset); 16 | return x || y; 17 | } -------------------------------------------------------------------------------- /one-question-per-day/20201112/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(n) 4 | */ 5 | const sortArrayByParityII = function(A) { 6 | const ans = []; 7 | 8 | let x = 0; 9 | let y = 1; 10 | for (let item of A) { 11 | if (item % 2 === 0) { 12 | ans[x] = item; 13 | x += 2; 14 | } else { 15 | ans[y] = item; 16 | y += 2; 17 | } 18 | } 19 | 20 | return ans; 21 | }; -------------------------------------------------------------------------------- /array/118/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(numRows^2) 3 | * 空间复杂度:O(1) 4 | */ 5 | var generate = function(numRows) { 6 | const ans = []; 7 | 8 | for (let i = 0; i < numRows; i++) { 9 | const row = new Array(i + 1).fill(1); 10 | for (let j = 1; j < row.length - 1; j++) { 11 | row[j] = ans[i - 1][j - 1] + ans[i - 1][j]; 12 | } 13 | ans.push(row); 14 | } 15 | return ans; 16 | }; -------------------------------------------------------------------------------- /array/121/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const maxProfit = function(prices) { 6 | let ans = 0; 7 | let selectPrice = prices[0]; 8 | for (let i = 1; i < prices.length; i++) { 9 | ans = Math.max(ans, prices[i] - selectPrice); 10 | if (selectPrice > prices[i]) { 11 | selectPrice = prices[i]; 12 | } 13 | } 14 | 15 | return ans; 16 | }; -------------------------------------------------------------------------------- /one-question-per-day/20200824/solution1.js: -------------------------------------------------------------------------------- 1 | const repeatedSubstringPattern = function(s) { 2 | let len = s.length; 3 | const halfLength = Math.floor(len / 2); 4 | for (let i = 0; i < halfLength; i++) { 5 | const repeatCount = Math.floor(len / (i + 1)); 6 | const sub = s.substr(0, i + 1); 7 | if (sub.repeat(repeatCount) === s) { 8 | return true; 9 | } 10 | } 11 | 12 | return false; 13 | }; -------------------------------------------------------------------------------- /one-question-per-day/20220107/solution1.js: -------------------------------------------------------------------------------- 1 | const maxDepth = function(s) { 2 | const stack = []; 3 | let ans = 0; 4 | for (let i = 0; i < s.length; i++) { 5 | if (s[i] === '(') { 6 | stack.push(s[i]); 7 | ans = Math.max(ans, stack.length); 8 | } else if (s[i] === ')' && stack.length) { 9 | stack.pop(); 10 | } 11 | } 12 | return ans; 13 | }; -------------------------------------------------------------------------------- /array/104/solution1.js: -------------------------------------------------------------------------------- 1 | const combinationSum4 = function(nums, target) { 2 | const dp = Array(target + 1).fill(0); 3 | 4 | dp[0] = 1; 5 | 6 | let ans = 0; 7 | 8 | for (let sum = 1; sum <= target; sum++) { 9 | for (const num of nums) { 10 | if (num <= sum) { 11 | dp[sum] += dp[sum - num]; 12 | } 13 | } 14 | } 15 | 16 | return dp[target]; 17 | }; -------------------------------------------------------------------------------- /array/53/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(1) 4 | * 5 | * 单独一段 还是连续一段 6 | * 7 | * max = Number.max(连续的一段,新的起点) 8 | * @param {*} nums 9 | */ 10 | const maxSubArray = (nums) => { 11 | let ans = nums[0] 12 | let pre = nums[0]; 13 | 14 | for (let i = 1; i < nums.length; i++) { 15 | pre = Math.max(pre + nums[i], nums[i]); 16 | ans = Math.max(ans, pre); 17 | } 18 | 19 | return ans; 20 | } -------------------------------------------------------------------------------- /Binary-Tree/55-1/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const maxDepth = function(root) { 6 | let ans = 0; 7 | 8 | const help = (root) => { 9 | if (!root) { 10 | return 0; 11 | } 12 | 13 | const maxHeight = Math.max(help(root.left), help(root.right)) + 1; 14 | ans = Math.max(maxHeight, ans); 15 | return maxHeight; 16 | } 17 | 18 | help(root); 19 | return ans; 20 | }; -------------------------------------------------------------------------------- /Binary-Tree/0404/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 4 | * 二叉树高度 5 | */ 6 | const isBalanced = function(root) { 7 | if (!root) { 8 | return true; 9 | } 10 | return (isBalanced(root.left) && isBalanced(root.right) && Math.abs(help(root.left) - help(root.right)) <= 1); 11 | }; 12 | 13 | function help(root) { 14 | if (!root) { 15 | return 0; 16 | } 17 | return Math.max(help(root.left), help(root.right)) + 1; 18 | } -------------------------------------------------------------------------------- /Linked-List/876/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/middle-of-the-linked-list/ 3 | * 4 | * 876. 链表的中间结点 5 | * 6 | * Easy 7 | * 8 | * 60ms 92.99% 9 | * 33.6mb 63.49% 10 | * 11 | * 快慢指针 12 | */ 13 | const middleNode = head => { 14 | let fast = head; 15 | let slow = head; 16 | while(fast && fast.next) { 17 | slow = slow.next; 18 | fast = fast.next.next; 19 | } 20 | return slow; 21 | } -------------------------------------------------------------------------------- /one-question-per-day/20211228/solution2.js: -------------------------------------------------------------------------------- 1 | var twoSum = function(nums, target) { 2 | const record = new Map(); 3 | 4 | for (let i = 0; i < nums.length; i++) { 5 | if (!record.has(nums[i])) { 6 | record.set(nums[i], i); 7 | } 8 | 9 | const j = record.get(target - nums[i]); 10 | 11 | if (j !== undefined && i !== j) { 12 | return [j, i]; 13 | } 14 | } 15 | }; -------------------------------------------------------------------------------- /Rank/179/1374/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/generate-a-string-with-characters-that-have-odd-counts/ 3 | * 4 | * 1374. 生成每种字符都是奇数个的字符串 5 | * 6 | * Easy 7 | * 8 | * 56ms 100.00% 9 | * 34.7mb 100.00% 10 | */ 11 | 12 | const generateTheString = n => { 13 | if (n % 2 === 0) { 14 | return Array(n - 1).fill('a').join('') + 'b'; 15 | } else { 16 | return Array(n).fill('a').join(''); 17 | } 18 | } -------------------------------------------------------------------------------- /array/905/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/sort-array-by-parity/ 3 | * 4 | * 905. Sort Array By Parity 5 | * 6 | * Easy 7 | */ 8 | const sortArrayByParity = A => { 9 | const ans = [] 10 | for (let i = 0, max = A.length; i < max; i++) { 11 | const item = A[i] 12 | if (item % 2 === 0) { 13 | ans.unshift(item) 14 | } else { 15 | ans.push(item) 16 | } 17 | } 18 | return ans 19 | } -------------------------------------------------------------------------------- /Binary-Tree/54/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 利用 BST 的特性 3 | * 4 | * 时间复杂度 O(n) 5 | * 空间复杂度 O(n) 6 | */ 7 | const kthLargest = function(root, k) { 8 | const ans = []; 9 | help(root, ans); 10 | return ans[ans.length - k]; 11 | }; 12 | 13 | function help(root, record) { 14 | if (!root) { 15 | return; 16 | } 17 | root.left && help(root.left, record); 18 | record.push(root.val); 19 | root.right && help(root.right, record); 20 | } -------------------------------------------------------------------------------- /Rank/181/5364/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/create-target-array-in-the-given-order/submissions/ 3 | * 4 | * 5364. 按既定顺序创建目标数组 5 | * 6 | * Easy 7 | * 8 | * 72ms 100.00% 9 | * 34mb 100.00% 10 | */ 11 | const createTargetArray = (nums, index) => { 12 | const target = []; 13 | for (let i = 0, max = nums.length; i < max; i++) { 14 | target.splice(index[i], 0, nums[i]); 15 | } 16 | return target; 17 | } -------------------------------------------------------------------------------- /array/509/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/fibonacci-number/ 3 | * 4 | * 5 | * 509. Fibonacci Number 6 | * 7 | * Easy 8 | */ 9 | const fib = N => { 10 | let x = 0 11 | let y = 1 12 | if (N === 0) { 13 | return x 14 | } 15 | if (N === 1) { 16 | return y 17 | } 18 | 19 | for (let i = 2; i <= N; i++) { 20 | const result = x + y 21 | x = y 22 | y = result 23 | } 24 | return y 25 | } -------------------------------------------------------------------------------- /array/78/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/subsets/ 3 | * 4 | * 78. Subsets 5 | * 6 | * Medium 7 | * 8 | * 56ms 94.68% 9 | * 35.2mb 41.78% 10 | */ 11 | const subsets = nums => { 12 | const ans = [[]] 13 | for (let i = 0, max = nums.length; i < max; i++) { 14 | const len = ans.length 15 | for (let j = 0; j < len; j++) { 16 | ans.push([...ans[j], nums[i]]) 17 | } 18 | } 19 | return ans 20 | } -------------------------------------------------------------------------------- /one-question-per-day/20220113/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * 时间复杂度 O(nlogn) 4 | * 空间复杂度 O(n) 5 | * @param {*} nums 6 | * @returns 7 | */ 8 | const dominantIndex = function(nums) { 9 | let sortNums = [...nums].sort((a,b) => a - b) 10 | // 唯一一个 11 | let res = nums.indexOf(sortNums[nums.length - 1]) 12 | if(sortNums[nums.length-2] * 2 > sortNums[nums.length - 1]){ 13 | res = -1 14 | } 15 | return res 16 | }; -------------------------------------------------------------------------------- /Binary-Tree/230/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | * 5 | */ 6 | const kthSmallest = (root, k) => { 7 | let ans = 0; 8 | let count = 0; 9 | 10 | const dfs = (root) => { 11 | if (!root) { 12 | return; 13 | } 14 | dfs(root.left); 15 | if (++count === k) { 16 | ans = root.val; 17 | return; 18 | } 19 | dfs(root.right); 20 | } 21 | 22 | dfs(root); 23 | 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Binary-Tree/235/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const lowestCommonAncestor = (root, p, q) => { 6 | if (!root) { 7 | return null; 8 | } 9 | 10 | if (p.val < root.val && q.val < root.val) { 11 | return lowestCommonAncestor(root.left, p, q); 12 | } else if (p.val > root.val && q.val > root.val) { 13 | return lowestCommonAncestor(root.right, p, q); 14 | } else { 15 | return root; 16 | } 17 | } -------------------------------------------------------------------------------- /Linked-List/203/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 迭代 3 | * 4 | * 时间复杂度:O(n) 5 | * 空间复杂度:O(n) 6 | */ 7 | const removeElements = (head, val) => { 8 | const dummyHead = new ListNode(-1, head); 9 | let current = dummyHead; 10 | 11 | while (current.next) { 12 | if (current.next.val === val) { 13 | current.next = current.next.next; 14 | } else { 15 | current = current.next; 16 | } 17 | } 18 | 19 | return dummyHead.next; 20 | } -------------------------------------------------------------------------------- /dynamic-programming/413/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 空间复杂度优化 3 | */ 4 | const numberOfArithmeticSlices = A => { 5 | const max = A.length 6 | 7 | if (max <= 2) { 8 | return 0 9 | } 10 | 11 | let pre = 0 12 | let sum = 0 13 | 14 | for (let i = 2; i < max; i++) { 15 | if (A[i] - A[i - 1] === A[i - 1] - A[i - 2]) { 16 | pre += 1 17 | } else { 18 | pre = 0 19 | } 20 | sum += pre 21 | } 22 | return sum 23 | } -------------------------------------------------------------------------------- /Binary-Tree/814/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/binary-tree-pruning/ 3 | * 4 | * 814. 二叉树剪枝 5 | * 6 | * Medium 7 | * 8 | * 72ms 89.66% 9 | * 34mb 23.08% 10 | */ 11 | const pruneTree = root => { 12 | if (!root) { 13 | return null 14 | } 15 | root.left = pruneTree(root.left) 16 | root.right = pruneTree(root.right) 17 | return (root.val === 1 || root.left !== null || root.right !== null) ? root : null 18 | } -------------------------------------------------------------------------------- /Linked-List/160/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 哈希表 3 | * 时间复杂度 O(n+m) 4 | * 空间复杂度 O(n) 5 | */ 6 | const getIntersectionNode = function(headA, headB) { 7 | const record = new Set(); 8 | 9 | while (headA !== null) { 10 | record.add(headA); 11 | headA = headA.next; 12 | } 13 | 14 | while (headB !== null) { 15 | if (record.has(headB)) { 16 | return headB; 17 | } 18 | headB = headB.next; 19 | } 20 | 21 | return null; 22 | }; -------------------------------------------------------------------------------- /one-question-per-day/20211226/solution1.js: -------------------------------------------------------------------------------- 1 | const findOcurrences = function(text, first, second) { 2 | const ans = []; 3 | const textList = text.split(' '); 4 | let start = 0; 5 | let max = textList.length - 3; 6 | while (start <= max) { 7 | if (textList[start] === first && textList[start + 1] === second) { 8 | ans.push(textList[start + 2]); 9 | } 10 | start++; 11 | } 12 | 13 | return ans; 14 | }; -------------------------------------------------------------------------------- /Linked-List/141/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const hasCycle = head => { 6 | 7 | if (!head || !head.next) { 8 | return false; 9 | } 10 | 11 | let slow = head; 12 | let fast = head.next; 13 | 14 | while (slow !== fast) { 15 | if (!fast || !fast.next) { 16 | // 到达终点 17 | return false; 18 | } 19 | 20 | slow = slow.next; 21 | fast = fast.next.next; 22 | } 23 | 24 | return true; 25 | } -------------------------------------------------------------------------------- /Linked-List/2095/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const deleteMiddle = function(head) { 6 | if (!head.next) { 7 | return null; 8 | } 9 | let fast = head; 10 | let slow = head; 11 | let pre = null; 12 | while (fast && fast.next) { 13 | fast = fast.next.next; 14 | pre = slow; 15 | slow = slow.next; 16 | } 17 | 18 | pre.next = slow.next; 19 | 20 | return head; 21 | }; -------------------------------------------------------------------------------- /array/377/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(target*n) 3 | * 空间复杂度:O(target) 4 | */ 5 | const combinationSum4 = function(nums, target) { 6 | const dp = Array(target + 1).fill(0); 7 | 8 | dp[0] = 1; 9 | 10 | for (let sum = 1; sum <= target; sum++) { 11 | for (const num of nums) { 12 | if (num <= sum) { 13 | dp[sum] += dp[sum - num]; 14 | } 15 | } 16 | } 17 | 18 | return dp[target]; 19 | } -------------------------------------------------------------------------------- /array/832/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/flipping-an-image/ 3 | * 4 | * 832. Flipping an Image 5 | * 6 | * Easy 7 | */ 8 | const flipAndInvertImage = A => { 9 | const ans = [] 10 | for (let i = 0, max = A.length; i < max; i++) { 11 | const item = A[i] 12 | ans[i] = [] 13 | for (let j = 0, subMax = item.length; j < subMax; j++) { 14 | ans[i].unshift(item[j] == 1 ? 0 : 1) 15 | } 16 | } 17 | return ans 18 | } -------------------------------------------------------------------------------- /one-question-per-day/20201013/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 迭代解决 3 | */ 4 | const swapPairs = function(head) { 5 | const pre = new ListNode(0); 6 | pre.next = head; 7 | let temp = pre; 8 | 9 | while (temp.next !== null && temp.next.next !== null) { 10 | const x = temp.next; 11 | const y = temp.next.next; 12 | 13 | temp.next = y; 14 | x.next = y.next; 15 | y.next = x; 16 | 17 | temp = x; 18 | } 19 | 20 | return pre.next; 21 | }; -------------------------------------------------------------------------------- /one-question-per-day/20210614/solution1.js: -------------------------------------------------------------------------------- 1 | const guessNumber = function(n) { 2 | let start = 0; 3 | let end = n; 4 | while (start < end) { 5 | const mid = Math.floor(start + (end - start) / 2); 6 | const item = guess(mid); 7 | if (item === 1) { 8 | start = mid + 1; 9 | } else if (item === -1) { 10 | end = mid; 11 | } else { 12 | return mid; 13 | } 14 | } 15 | 16 | return start; 17 | }; -------------------------------------------------------------------------------- /Binary-Tree/998/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/maximum-binary-tree-ii/ 3 | * 4 | * 998. 最大二叉树 II 5 | * 6 | * Medium 7 | * 8 | * 80ms 92.31% 9 | * 34.7mb 28.57% 10 | */ 11 | const insertIntoMaxTree = (root, val) => { 12 | if (root && root.val > val) { 13 | root.right = insertIntoMaxTree(root.right, val); 14 | return root; 15 | } 16 | const _root = new TreeNode(val); 17 | _root.left = root; 18 | return _root; 19 | } -------------------------------------------------------------------------------- /array/219/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/contains-duplicate-ii/ 3 | * 4 | * 219. Contains Duplicate II 5 | * 6 | * Easy 7 | */ 8 | const containsNearbyDuplicate = (nums, k) => { 9 | const len = nums.length 10 | 11 | for (let i = 0; i < len - 1; i++) { 12 | for (let j = i + 1; j <= i + k && j < len; j++) { 13 | if (nums[i] === nums[j]) { 14 | return true 15 | } 16 | } 17 | } 18 | 19 | return false 20 | } -------------------------------------------------------------------------------- /dynamic-programming/343/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/integer-break/ 3 | * 4 | * 343. Integer Break 5 | * 6 | * Medium 7 | * 8 | */ 9 | const integerBreak = n => { 10 | const map = [0, 0, 1, 2, 4] 11 | 12 | if (n === 2) { 13 | return 1 14 | } 15 | if (n === 3) { 16 | return 2 17 | } 18 | let ans = 1 19 | while (n > 4) { 20 | n -= 3 21 | ans *= 3 22 | } 23 | 24 | return ans * Math.max(map[n], n) 25 | } -------------------------------------------------------------------------------- /string/392/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/is-subsequence/ 3 | * 4 | * 392. Is Subsequence 5 | * 6 | * Medium 7 | * 8 | * 80ms 42.86% 9 | * 38.6mb 84.94% 10 | * 11 | * O(n) 12 | */ 13 | const isSubsequence = (s, t) => { 14 | 15 | const stack = s.split('') 16 | 17 | for (let i = 0, max = t.length; i < max; i++) { 18 | if (stack[0] === t[i]) { 19 | stack.shift() 20 | } 21 | } 22 | return stack.length === 0 23 | } -------------------------------------------------------------------------------- /string/9/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const isPalindrome = function(x) { 6 | if (x < 0) { 7 | return false; 8 | } 9 | const str = String(x); 10 | let start = 0; 11 | let end = str.length - 1; 12 | 13 | while (start < end) { 14 | if (str[start] !== str[end]) { 15 | return false; 16 | } 17 | start++; 18 | end--; 19 | } 20 | 21 | return true; 22 | }; -------------------------------------------------------------------------------- /Binary-Tree/102/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const levelOrder = root => { 6 | const ans = []; 7 | dfs(root, 0, ans); 8 | return ans; 9 | } 10 | 11 | function dfs(root, level, ans) { 12 | if (!root) { 13 | return; 14 | } 15 | 16 | if (!ans[level]) { 17 | ans[level] = []; 18 | } 19 | 20 | ans[level].push(root.val); 21 | dfs(root.left, level + 1, ans); 22 | dfs(root.right, level + 1, ans); 23 | return; 24 | } -------------------------------------------------------------------------------- /Binary-Tree/96/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/unique-binary-search-trees/ 3 | * 4 | * 96. 不同的二叉搜索树 5 | * 6 | * Medium 7 | * 8 | * 64ms 96.00% 9 | * 33.7mb 40.00% 10 | */ 11 | const numTrees = n => { 12 | const dp = new Array(n + 1).fill(0) 13 | dp[0] = 1 14 | dp[1] = 1 15 | for (let i = 2; i <= n; i++) { 16 | for (let j = 0; j < i; j++) { 17 | dp[i] += dp[j] * dp[i - j - 1] 18 | } 19 | } 20 | return dp[n] 21 | } -------------------------------------------------------------------------------- /Heap/973/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/k-closest-points-to-origin/ 3 | * 4 | * 973. 最接近原点的 K 个点 5 | * 6 | * Medium 7 | * 8 | * 排序解决 O(nlogn) 9 | */ 10 | const kClosest = (points, K) => { 11 | points.sort((a, b) => { 12 | const [x1, y1] = a; 13 | const [x2, y2] = b; 14 | const aDis = x1 ** 2 + y1 ** 2; 15 | const bDis = x2 ** 2 + y2 ** 2; 16 | return aDis - bDis; 17 | }) 18 | 19 | return points.slice(0, K); 20 | } -------------------------------------------------------------------------------- /array/540/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(logn) 3 | * 空间复杂度:O(1) 4 | */ 5 | const singleNonDuplicate = function(nums) { 6 | let start = 0; 7 | let end = nums.length - 1; 8 | 9 | while (start < end) { 10 | const mid = Math.floor((end - start) / 2) + start; 11 | if (nums[mid] == nums[mid ^ 1]) { 12 | start = mid + 1; 13 | } else { 14 | end = mid; 15 | } 16 | } 17 | 18 | return nums[start]; 19 | }; -------------------------------------------------------------------------------- /dynamic-programming/357/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/count-numbers-with-unique-digits/ 4 | * 5 | * 6 | * 357. Count Numbers with Unique Digits 7 | * 8 | * Medium 9 | */ 10 | const countNumbersWithUniqueDigits = n => { 11 | if (n == 0) { 12 | return 1 13 | } 14 | let ans = 10, base = 9 15 | for (let i = 2; i <= Math.min(10, n); i++) { 16 | base = base * (9 - i + 2) 17 | ans += base 18 | } 19 | return ans 20 | } -------------------------------------------------------------------------------- /one-question-per-day/20201123/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(nlogn) 3 | * 空间复杂度 O(nlogn) 4 | */ 5 | const findMinArrowShots = function(points) { 6 | if (points.length === 0) { 7 | return 0; 8 | } 9 | 10 | points.sort((a, b) => a[1] - b[1]); 11 | 12 | let endPos = points[0][1]; 13 | let ans = 1; 14 | for (let [start, end] of points) { 15 | if (start > endPos) { 16 | endPos = end; 17 | ans++; 18 | } 19 | } 20 | return ans; 21 | }; -------------------------------------------------------------------------------- /Small-Skill/287/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/find-the-duplicate-number/ 3 | * 4 | * 287. Find the Duplicate Number 5 | * 6 | * Medium 7 | * 8 | * 56ms 93.47% 9 | * 36.4mb 37.72% 10 | */ 11 | const findDuplicate = nums => { 12 | const s = new Set() 13 | const max = nums.length 14 | for (let i = 0; i < max; i++) { 15 | const item = nums[i] 16 | if (s.has(item)) { 17 | return item 18 | } 19 | s.add(item) 20 | } 21 | } -------------------------------------------------------------------------------- /array/217/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 哈希表 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | * 6 | * 因为本题只需要记录是否重复,所以直接使用 JavaScript 中的 Set 数据结构记录是否重复即可。 7 | * @param {*} nums 8 | * @returns 9 | */ 10 | const containsDuplicate = function(nums) { 11 | const record = new Set(); 12 | for (let i = 0; i < nums.length; i++) { 13 | if (!record.has(nums[i])) { 14 | record.add(nums[i]); 15 | } else { 16 | return true; 17 | } 18 | } 19 | 20 | return false; 21 | }; -------------------------------------------------------------------------------- /one-question-per-day/20211231/solution2.js: -------------------------------------------------------------------------------- 1 | var checkPerfectNumber = function(num) { 2 | if (num === 1) { 3 | return false; 4 | } 5 | 6 | let sum = 1; 7 | let i = 2; 8 | const sqrt = Math.sqrt(num); 9 | for (;i < sqrt; i++) { 10 | if (num % i === 0) { 11 | sum += i; 12 | sum += num / i; 13 | } 14 | } 15 | 16 | if (i * i === num) { 17 | sum += i; 18 | } 19 | return sum === num; 20 | }; -------------------------------------------------------------------------------- /Binary-Tree/45/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const findBottomLeftValue = (root) => { 6 | let ans = []; 7 | dfs(root, 0, ans); 8 | return ans.pop(); 9 | } 10 | 11 | function dfs(root, level, ans) { 12 | if (!root) { 13 | return; 14 | } 15 | 16 | if (ans[level] === undefined) { 17 | ans[level] = root.val; 18 | } 19 | 20 | dfs(root.left, level + 1, ans); 21 | dfs(root.right, level + 1, ans); 22 | } -------------------------------------------------------------------------------- /Binary-Tree/98/solution1.js: -------------------------------------------------------------------------------- 1 | const isValidBST = root => { 2 | const ans = [] 3 | inOrder(root) 4 | for (let i = 0, max = ans.length; i < max - 1; i++) { 5 | const pre = ans[i] 6 | const next = ans[i + 1] 7 | if (next <= pre) { 8 | return false 9 | } 10 | } 11 | return true 12 | function inOrder (root) { 13 | if (!root) { 14 | return 15 | } 16 | inOrder(root.left) 17 | ans.push(root.val) 18 | inOrder(root.right) 19 | } 20 | } -------------------------------------------------------------------------------- /Two-Points/349/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/intersection-of-two-arrays/ 3 | * 4 | * 349. Intersection of Two Arrays 5 | * 6 | * Easy 7 | * 8 | * 97.18% 9 | * 13.61% 10 | * 11 | * 非双指针题目 12 | * 采用 集合处理 13 | */ 14 | const intersection = (nums1, nums2) => { 15 | const s1 = new Set(nums1) 16 | const s2 = new Set() 17 | for (let num of nums2) { 18 | if (s1.has(num)) { 19 | s2.add(num) 20 | } 21 | } 22 | return [...s2] 23 | } -------------------------------------------------------------------------------- /one-question-per-day/20200904/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 深度优先遍历 3 | */ 4 | const binaryTreePaths = function(root) { 5 | const ans = []; 6 | dfs(root, []); 7 | return ans; 8 | function dfs(root, paths) { 9 | if (!root) { 10 | return 11 | } 12 | const newPath = paths.concat(root.val); 13 | if (!root.left && !root.right) { 14 | ans.push(newPath.join('->')); 15 | } 16 | dfs(root.left, newPath); 17 | dfs(root.right, newPath); 18 | } 19 | }; -------------------------------------------------------------------------------- /Binary-Search/287/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/find-the-duplicate-number/ 3 | * 4 | * 287. 寻找重复数 5 | * 6 | * Medium 7 | * 8 | * 76ms 97.60% 9 | * 36.6mb 21.36% 10 | * 11 | * HashMap 12 | */ 13 | const findDuplicate = nums => { 14 | const record = new Set() 15 | for (let i = 0, max = nums.length; i < max; i++) { 16 | const num = nums[i] 17 | if (record.has(num)) { 18 | return num 19 | } 20 | record.add(num) 21 | } 22 | } -------------------------------------------------------------------------------- /Binary-Tree/0402/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 构造 BST 4 | */ 5 | const sortedArrayToBST = function(nums) { 6 | return help(nums); 7 | }; 8 | 9 | function help(arr) { 10 | if (arr.length === 0) { 11 | return null; 12 | } 13 | const midIndex = Math.floor(arr.length / 2); 14 | const middle = arr[midIndex]; 15 | const root = new TreeNode(middle); 16 | root.left = help(arr.slice(0, midIndex)); 17 | root.right = help(arr.slice(midIndex + 1)); 18 | return root; 19 | } -------------------------------------------------------------------------------- /Binary-Tree/559/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | */ 4 | const maxDepth = function(root) { 5 | let ans = 0; 6 | 7 | const help = root => { 8 | if (!root) { 9 | return 0; 10 | } 11 | let max = 0; 12 | for (let i = 0; i < root.children.length; i++) { 13 | const item = root.children[i]; 14 | max = Math.max(max, help(item)); 15 | } 16 | ans = Math.max(ans, max + 1); 17 | return max + 1; 18 | } 19 | 20 | help(root); 21 | return ans; 22 | }; -------------------------------------------------------------------------------- /one-question-per-day/20200821/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * DFS 4 | */ 5 | const minDepth = function(root) { 6 | if (!root) { 7 | return 0; 8 | } 9 | if (root.left === null && root.right === null) { 10 | return 1; 11 | } 12 | 13 | let min = Number.MAX_SAFE_INTEGER; 14 | if (root.left) { 15 | min = Math.min(min, minDepth(root.left)); 16 | } 17 | if (root.right) { 18 | min = Math.min(min, minDepth(root.right)); 19 | } 20 | 21 | return min + 1; 22 | }; -------------------------------------------------------------------------------- /Binary-Tree/100/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/same-tree/ 3 | * 4 | * 100. 相同的树 5 | * 6 | * Easy 7 | * 8 | * 递归的特性 9 | * 10 | * 80ms 73.52% 11 | * 33.8% 19.32% 12 | */ 13 | const 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 | 21 | return (p.val === q.val) && (isSameTree(p.left, q.left)) && (isSameTree(p.right, q.right)) 22 | } -------------------------------------------------------------------------------- /Binary-Tree/538/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 538. 把二叉搜索树转换为累加树 3 | * 4 | * https://leetcode-cn.com/problems/convert-bst-to-greater-tree/ 5 | * 6 | * Easy 7 | * 8 | * 132ms 84.93% 9 | * 40.2mb 41.38% 10 | */ 11 | const convertBST = root => { 12 | let sum = 0 13 | dfs(root, 0) 14 | return root 15 | 16 | function dfs(root) { 17 | if (!root) { 18 | return 19 | } 20 | dfs(root.right) 21 | root.val += sum 22 | sum = root.val 23 | dfs(root.left) 24 | } 25 | } -------------------------------------------------------------------------------- /array/414/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/third-maximum-number/ 3 | * 4 | * 414. Third Maximum Number 5 | * 6 | * Easy 7 | * 8 | * 68ms 60.91% 9 | * 37.9mb 17.03% 10 | */ 11 | const thirdMax = nums => { 12 | const s = new Set(nums) 13 | const array = [...s] 14 | array.sort((a, b) => a - b) 15 | 16 | const thirdMax = array[array.length - 3] 17 | if (thirdMax === undefined) { 18 | return array[array.length - 1] 19 | } 20 | return thirdMax 21 | } -------------------------------------------------------------------------------- /dynamic-programming/746/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/min-cost-climbing-stairs/ 3 | * 4 | * 746. Min Cost Climbing Stairs 5 | * 6 | * Easy 7 | */ 8 | const minCostClimbingStairs = cost => { 9 | const max = cost.length 10 | if (max < 2) { 11 | return 0 12 | } 13 | 14 | const dp = [0, cost[0]] 15 | 16 | for (let i = 1; i < max; i++) { 17 | dp[i + 1] = Math.min(dp[i], dp[i - 1]) + cost[i] 18 | } 19 | return Math.min(dp[max], dp[max - 1]) 20 | } -------------------------------------------------------------------------------- /Binary-Tree/108/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(logn) 4 | * 5 | */ 6 | const help = (nums, start, end) => { 7 | if (start > end) { 8 | return null; 9 | } 10 | const mid = Math.floor((start + end + 1) / 2); 11 | const root = new TreeNode(nums[mid]); 12 | root.left = help(nums, start, mid - 1); 13 | root.right = help(nums, mid + 1, end); 14 | return root; 15 | } 16 | const sortedArrayToBST = function (nums) { 17 | return help(nums, 0, nums.length - 1); 18 | } -------------------------------------------------------------------------------- /Rank/181/1392/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/longest-happy-prefix/ 3 | * 4 | * 1392. 最长快乐前缀 5 | * 6 | * Hard 7 | */ 8 | const longestPrefix = s => { 9 | let ans = 0; 10 | for (let i = 0, max = s.length; i < max; i++) { 11 | const prefix = s.slice(0, i); 12 | const suffix = s.slice(-i); 13 | if (prefix === suffix) { 14 | ans = i; 15 | } 16 | } 17 | 18 | if (ans === 0) { 19 | return ''; 20 | } 21 | return s.slice(0, ans); 22 | } -------------------------------------------------------------------------------- /array/349/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/intersection-of-two-arrays/ 3 | * 4 | * 349. Intersection of Two Arrays 5 | * 6 | * Easy 7 | * 8 | * 48ms 99.49% 9 | * 34.8MB 52.93% 10 | */ 11 | const intersection = (nums1, nums2) => { 12 | const s1 = new Set(nums1) 13 | const s2 = new Set(nums2) 14 | const result = [] 15 | for (let item of s2.values()) { 16 | if (s1.has(item)) { 17 | result.push(item) 18 | } 19 | } 20 | 21 | return result 22 | } -------------------------------------------------------------------------------- /array/494/solution1.js: -------------------------------------------------------------------------------- 1 | // 回溯 2 | const findTargetSumWays = (nums, target) => { 3 | let ans = 0; 4 | 5 | const backtrack = (nums, target, index, sum) => { 6 | if (index === nums.length) { 7 | if (sum === target) { 8 | ans++; 9 | } 10 | } else { 11 | backtrack(nums, target, index + 1, sum + nums[index]); 12 | backtrack(nums, target, index + 1, sum - nums[index]); 13 | } 14 | } 15 | 16 | backtrack(nums, target, 0, 0); 17 | 18 | return ans; 19 | } -------------------------------------------------------------------------------- /one-question-per-day/20201018/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 双指针 3 | */ 4 | const removeNthFromEnd = (head, n) => { 5 | const dummyHead = new ListNode(null); 6 | dummyHead.next = head; 7 | 8 | let first = dummyHead; 9 | let second = dummyHead; 10 | for (let i = 0; i < n + 1; i++) { 11 | first = first.next; 12 | } 13 | 14 | while(first) { 15 | first = first.next; 16 | second = second.next; 17 | } 18 | 19 | second.next = second.next.next; 20 | return dummyHead.next; 21 | } -------------------------------------------------------------------------------- /one-question-per-day/20220113/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * hash + 排序 3 | * 4 | * 时间复杂度 O(nlogn) 5 | * 空间复杂度 O(n) 6 | * @param {*} nums 7 | * @returns 8 | */ 9 | const dominantIndex = function(nums) { 10 | if (nums.length < 2) { 11 | return 0 12 | } 13 | const record = new Map(nums.map((item, index) => [item, index])); 14 | 15 | nums.sort((a, b) => b - a); 16 | if (nums[0] >= 2 * nums[1]) { 17 | return record.get(nums[0]); 18 | } 19 | return -1; 20 | }; -------------------------------------------------------------------------------- /Binary-Tree/865/solution1.js: -------------------------------------------------------------------------------- 1 | const subtreeWithAllDeepest = root => { 2 | return help(root)[1] 3 | function help (root) { 4 | if (!root) { 5 | return [-1, null] 6 | } 7 | const l = help(root.left) 8 | const r = help(root.right) 9 | 10 | const lHeight = l[0] 11 | const rHeight = r[0] 12 | 13 | const h = Math.max(rHeight, lHeight) + 1 14 | 15 | const v = lHeight === rHeight ? root : (lHeight > rHeight ? l[1] : r[1]) 16 | 17 | return [h, v] 18 | } 19 | } -------------------------------------------------------------------------------- /Linked-List/21/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 时间复杂度 O(m + n) 4 | * 空间复杂度 O(m + n) 5 | */ 6 | const mergeTwoLists = function(list1, list2) { 7 | if (!list1) { 8 | return list2; 9 | } else if (!list2) { 10 | return list1; 11 | } else if (list1.val <= list2.val) { 12 | list1.next = mergeTwoLists(list1.next, list2); 13 | return list1; 14 | } else { 15 | list2.next = mergeTwoLists(list1, list2.next); 16 | return list2; 17 | } 18 | }; -------------------------------------------------------------------------------- /Two-Points/424/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/longest-repeating-character-replacement/ 3 | * 4 | * 424. 替换后的最长重复字符 5 | * 6 | * 相似的题目 1004 7 | * 8 | * 滑动窗口算法 9 | * 10 | * 如何确定左滑和右滑的条件 11 | * 12 | * Medium 13 | */ 14 | const characterReplacement = (s, k) => { 15 | const countMap = new Map() 16 | let ans = 0 17 | const max = s.length 18 | let start = 0 19 | let end = 0 20 | while (end < s && start <= end) { 21 | const curItem = s[end] 22 | } 23 | } -------------------------------------------------------------------------------- /array/283/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/move-zeroes/ 3 | * 4 | * 283. Move Zeroes 5 | * 6 | * Easy 7 | */ 8 | const moveZeroes = nums => { 9 | const max = nums.length 10 | 11 | let start = 0 12 | let end = max - 1 13 | 14 | while (start < end) { 15 | const item = nums[start] 16 | if (item === 0) { 17 | nums.splice(start, 1) 18 | nums.push(0) 19 | end-- 20 | continue 21 | } 22 | start++ 23 | } 24 | 25 | return nums 26 | } -------------------------------------------------------------------------------- /array/628/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-product-of-three-numbers/ 3 | * 4 | * 628. Maximum Product of Three Numbers 5 | * 6 | * Easy 7 | * 8 | * 104ms 59.86% 9 | * 38.3mb 33.96% 10 | */ 11 | const maximumProduct = nums => { 12 | 13 | nums.sort((a, b) => a - b) 14 | 15 | const max = nums.length 16 | 17 | let x = nums[max - 1] * nums[max - 2] * nums[max - 3] 18 | 19 | let y = nums[0] * nums[1] * nums[max - 1] 20 | 21 | return Math.max(x, y) 22 | } -------------------------------------------------------------------------------- /one-question-per-day/20200817/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 (自顶向下) 3 | * 时间复杂度 O(n^2) 4 | * 空间复杂度 O(n) 5 | * 6 | */ 7 | const isBalanced = function(root) { 8 | if (!root) { 9 | return true; 10 | } 11 | return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right); 12 | } 13 | 14 | // 求解二叉树的高度 15 | function height(root) { 16 | if (!root) { 17 | return 0; 18 | } 19 | return Math.max(height(root.left), height(root.right)) + 1; 20 | } -------------------------------------------------------------------------------- /one-question-per-day/20201014/solution1.js: -------------------------------------------------------------------------------- 1 | const commonChars = function(A) { 2 | const n = A.length; 3 | const obj = {}; 4 | let res = []; 5 | for(let i = 0;i<26;i++){ 6 | obj[String.fromCharCode(97+i)] = new Array(n).fill(0); 7 | } 8 | for(let i = 0;i < n;i++){ 9 | for(let char of A[i]){ 10 | obj[char][i]++; 11 | } 12 | } 13 | for(let char in obj){ 14 | let count = Math.min(...obj[char]); 15 | while(count--) res.push(char); 16 | } 17 | return res; 18 | }; -------------------------------------------------------------------------------- /Binary-Tree/1022/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | */ 6 | const sumRootToLeaf = root => { 7 | let sum = 0; 8 | const dfs = (root, path) => { 9 | if (!root) { 10 | return; 11 | } 12 | path += String(root.val); 13 | if (!root.left && !root.right) { 14 | sum += Number.parseInt(path, 2); 15 | return; 16 | } 17 | 18 | dfs(root.left, path); 19 | dfs(root.right, path); 20 | } 21 | dfs(root, ''); 22 | return sum; 23 | } 24 | -------------------------------------------------------------------------------- /Binary-Tree/107/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const levelOrderBottom = root => { 6 | const ans = []; 7 | dfs(root, 0, ans); 8 | ans.reverse(); 9 | return ans; 10 | } 11 | 12 | function dfs(root, level, ans) { 13 | if (!root) { 14 | return; 15 | } 16 | 17 | if (!ans[level]) { 18 | ans[level] = []; 19 | } 20 | 21 | ans[level].push(root.val); 22 | dfs(root.left, level + 1, ans); 23 | dfs(root.right, level + 1, ans); 24 | return; 25 | } 26 | -------------------------------------------------------------------------------- /array/1/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * 空间换时间,二维转化为一维 4 | * 时间复杂度 O(n) 5 | * 空间复杂度 O(n) 6 | * @param {*} nums 7 | * @param {*} target 8 | */ 9 | const twoSum = (nums, target) => { 10 | const record = new Map(); 11 | 12 | for (let i = 0; i < nums.length; i++) { 13 | const item = target - nums[i] 14 | const j = record.get(item); 15 | record.set(nums[i], i); 16 | if (j !== undefined && j !== i) { 17 | return [j, i]; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /array/1051/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/height-checker/ 3 | * 4 | * Easy 5 | * 6 | * 56ms 88.59% 7 | * 8 | * 35MB 100% 9 | * 10 | */ 11 | const heightChecker = heights => { 12 | const sortHeights = heights.map(item => item).sort((a, b) => a - b) 13 | let ans = 0 14 | for (let i = 0, max = heights.length; i < max; i++) { 15 | const x = heights[i] 16 | const y = sortHeights[i] 17 | if (x !== y) { 18 | ans++ 19 | } 20 | } 21 | return ans 22 | } -------------------------------------------------------------------------------- /one-question-per-day/20200901/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 4 | * 时间复杂度 O(2^n) 5 | * 空间复杂度 O(n) 6 | */ 7 | const PredictTheWinner = function(nums) { 8 | return total(nums, 0, nums.length - 1) >= 0; 9 | }; 10 | 11 | function total(nums, start, end) { 12 | if (start === end) { 13 | return nums[start] 14 | } 15 | const scoreStart = nums[start] - total(nums, start + 1, end); 16 | const scoreEnd = nums[end] - total(nums, start, end - 1); 17 | return Math.max(scoreStart, scoreEnd); 18 | } -------------------------------------------------------------------------------- /Binary-Tree/106/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(n) 4 | * 5 | */ 6 | const buildTree = (inorder, postorder) => { 7 | if (inorder.length === 0) { 8 | return null; 9 | } 10 | 11 | const el = postorder.pop(); 12 | const root = new TreeNode(el); 13 | const index = inorder.indexOf(el); 14 | 15 | root.left = buildTree(inorder.slice(0, index), postorder.slice(0, index)); 16 | root.right = buildTree(inorder.slice(index + 1), postorder.slice(index)); 17 | 18 | return root; 19 | } -------------------------------------------------------------------------------- /array/278/solution2.js: -------------------------------------------------------------------------------- 1 | const solution = function(isBadVersion) { 2 | /** 3 | * @param {integer} n Total versions 4 | * @return {integer} The first bad version 5 | */ 6 | return function(n) { 7 | let start = 1; 8 | let end = n; 9 | 10 | while (start < end) { 11 | const mid = Math.floor(start + (end - start) / 2); 12 | if (isBadVersion(mid)) { 13 | end = mid; 14 | } else { 15 | start = mid + 1; 16 | } 17 | } 18 | return start; 19 | }; 20 | }; -------------------------------------------------------------------------------- /Rank/179/1375/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/bulb-switcher-iii/ 3 | * 4 | * 1375. 灯泡开关 III 5 | * 6 | * Medium 7 | * 8 | * 64ms 100.00% 9 | * 41mb 100.00% 10 | * 11 | * 重点如何保证当前是 前 n 盏灯全亮 12 | */ 13 | 14 | const numTimesAllBlue = light => { 15 | let ans = 0; 16 | let currentSum = 0; 17 | for (let i = 0; i < light.length; i++) { 18 | currentSum += light[i]; 19 | if (currentSum === (i + 1)*(i + 2) / 2) { 20 | ans++; 21 | } 22 | } 23 | return ans; 24 | } -------------------------------------------------------------------------------- /Binary-Tree/54/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 利用 BST 的特性 3 | * 提前返回 4 | * 时间复杂度 O(n) 5 | * 空间复杂度 O(n) 6 | */ 7 | const kthLargest = function(root, k) { 8 | let ans = null; 9 | const help = root => { 10 | if (!root) { 11 | return; 12 | } 13 | if (k <= 0) { 14 | return; 15 | } 16 | help(root.right); 17 | if (k === 1) { 18 | ans = root.val; 19 | k--; 20 | return; 21 | } 22 | k--; 23 | help(root.left); 24 | } 25 | 26 | help(root); 27 | return ans; 28 | }; -------------------------------------------------------------------------------- /Binary-Tree/652/solution1.js: -------------------------------------------------------------------------------- 1 | const findDuplicateSubtrees = root => { 2 | const ans = [] 3 | if (!root) { 4 | return ans 5 | } 6 | 7 | // 记录所有的路径 8 | const m = {} 9 | help(root) 10 | 11 | return ans 12 | 13 | function help (root) { 14 | if (!root) { 15 | return 16 | } 17 | 18 | const path = `${root.val},${help(root.left)},${help(root.right)}` 19 | 20 | m[path] = m[path] || 0 21 | if (m[path]++ === 1) { 22 | ans.push(root) 23 | } 24 | return path 25 | } 26 | } -------------------------------------------------------------------------------- /Binary-Tree/951/solution1.js: -------------------------------------------------------------------------------- 1 | const flipEquiv = (root1, root2) => { 2 | if (!root1 && !root2) { 3 | return true 4 | } 5 | if (!root1 || !root2) { 6 | return false 7 | } 8 | 9 | if (root1.val === root2.val) { 10 | const x = flipEquiv(root1.left, root2.left) 11 | const y = flipEquiv(root1.right, root2.right) 12 | const z = flipEquiv(root1.left, root2.right) 13 | const k = flipEquiv(root1.right, root2.left) 14 | return (x && y || z && k) 15 | } else { 16 | return false 17 | } 18 | } -------------------------------------------------------------------------------- /Two-Points/283/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/move-zeroes/ 3 | * 4 | * 283. Move Zeroes 5 | * 6 | * Easy 7 | * 8 | * 60ms 92.57% 9 | * 35.9mb 25.26% 10 | */ 11 | const moveZeroes = nums => { 12 | const max = nums.length 13 | let index = 0 14 | for (let i = 0; i < max; i++) { 15 | if (nums[i] !== 0) { 16 | nums[index] = nums[i] 17 | index++ 18 | } 19 | } 20 | 21 | for (let i = index; i < max; i++) { 22 | nums[i] = 0 23 | } 24 | 25 | return nums 26 | } -------------------------------------------------------------------------------- /array/581/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(nlogn) 3 | * 空间复杂度:O(logn) 4 | */ 5 | const findUnsortedSubarray = function(nums) { 6 | const sortNums = [...nums].sort((a, b) => a - b ); 7 | 8 | let start = 0; 9 | let end = nums.length - 1; 10 | 11 | while (start < nums.length - 1 && sortNums[start] === nums[start]) { 12 | start++; 13 | } 14 | 15 | while (end >= 0 && sortNums[end] === nums[end]) { 16 | end--; 17 | } 18 | 19 | return end > start ? end - start + 1 : 0; 20 | }; -------------------------------------------------------------------------------- /array/867/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/transpose-matrix/ 4 | * 5 | * 6 | * 867. Transpose Matrix 7 | * 8 | * Easy 9 | */ 10 | const transpose = A => { 11 | let ans = [] 12 | const rows = A.length 13 | if (!rows) { 14 | return ans 15 | } 16 | 17 | const cols = A[0].length 18 | for (let i = 0; i < cols; i++) { 19 | ans[i] || (ans[i] = []) 20 | for (let j = 0; j < rows; j++) { 21 | ans[i].push(A[j][i]) 22 | } 23 | } 24 | 25 | return ans 26 | } -------------------------------------------------------------------------------- /dynamic-programming/263/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/ugly-number/ 3 | * 4 | * 263. 丑数 5 | * 6 | * Easy 7 | * 8 | * 72ms 94.92% 9 | * 35.6mb 42.86% 10 | */ 11 | const isUgly = num => { 12 | // 特殊情况 13 | if (num <= 0) { 14 | return false; 15 | } 16 | 17 | while (num % 2 === 0) { 18 | num /= 2; 19 | } 20 | 21 | while (num % 3 === 0) { 22 | num /= 3; 23 | } 24 | 25 | while (num % 5 === 0) { 26 | num /= 5; 27 | } 28 | 29 | return num === 1; 30 | } -------------------------------------------------------------------------------- /one-question-per-day/20201112/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(1) 4 | * 5 | * 双指针 6 | */ 7 | const sortArrayByParityII = A => { 8 | let i = 0; 9 | let j = 1; 10 | 11 | for (let i = 0; i < A.length; i += 2) { 12 | if (A[i] % 2 !== 0) { 13 | while (A[j] % 2 !== 0) { 14 | j += 2; 15 | } 16 | swap(A, i, j); 17 | } 18 | } 19 | return A; 20 | } 21 | 22 | function swap(arr, i , j) { 23 | const temp = arr[i]; 24 | arr[i] = arr[j]; 25 | arr[j] = temp; 26 | } -------------------------------------------------------------------------------- /Binary-Tree/111/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ 3 | * 4 | * 111. 二叉树的最小深度 5 | * 6 | * Easy 7 | * 8 | * 84ms 96.55% 9 | * 37.5mb 22.22% 10 | */ 11 | const minDepth = function (root) { 12 | if (!root) { 13 | return 0 14 | } 15 | 16 | if (!root.left) { 17 | return minDepth(root.right) + 1 18 | } 19 | if (!root.right) { 20 | return minDepth(root.left) + 1 21 | } 22 | 23 | return Math.min(minDepth(root.left), minDepth(root.right)) + 1 24 | } -------------------------------------------------------------------------------- /Binary-Tree/653/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | 6 | const findTarget = (root, k) => { 7 | let ans = false; 8 | const record = new Set(); 9 | const help = (root) => { 10 | if (!root) { 11 | return; 12 | } 13 | 14 | help(root.left); 15 | const reset = k - root.val; 16 | if (record.has(reset)) { 17 | ans = true; 18 | return; 19 | } 20 | record.add(root.val); 21 | help(root.right); 22 | } 23 | 24 | help(root); 25 | return ans; 26 | } -------------------------------------------------------------------------------- /Stack/1047/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/ 3 | * 4 | * 1047. 删除字符串中的所有相邻重复项 5 | * 6 | * easy 7 | * 88ms 73.21% 8 | * 42mb 100% 9 | */ 10 | const removeDuplicates = S => { 11 | const stack = []; 12 | for (let i = 0, max = S.length; i < max; i++) { 13 | const str = S[i]; 14 | if (stack[stack.length - 1] === str) { 15 | stack.pop(); 16 | } else { 17 | stack.push(str); 18 | } 19 | } 20 | 21 | return stack.join(''); 22 | } -------------------------------------------------------------------------------- /Two-Points/283/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/move-zeroes/ 3 | * 4 | * 283. Move Zeroes 5 | * 6 | * Easy 7 | * 8 | * 数组 部分操作符太慢 9 | * 10 | * 60ms 92.57% 11 | * 36mb 21.13% 12 | */ 13 | const moveZeroes = nums => { 14 | let start = 0 15 | let end = nums.length - 1 16 | 17 | while (start < end) { 18 | if (nums[start] === 0) { 19 | nums.splice(start, 1) 20 | nums.push(0) 21 | end-- 22 | continue 23 | } 24 | start++ 25 | } 26 | return nums 27 | } -------------------------------------------------------------------------------- /dynamic-programming/486/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * 4 | * https://leetcode.com/problems/predict-the-winner/ 5 | * 6 | * 7 | * 486. Predict the Winner 8 | * 9 | * Medium 10 | */ 11 | const PredictTheWinner = nums => { 12 | const max = nums.length 13 | 14 | return help(0, max - 1) >= 0 15 | 16 | function help (start, end) { 17 | if (start === end) { 18 | return nums[start] 19 | } 20 | 21 | return Math.max(nums[start] - help(start + 1, end), nums[end] - help(start, end - 1)) 22 | } 23 | } -------------------------------------------------------------------------------- /one-question-per-day/20200828/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(N) 3 | * 空间复杂度 O(1) 4 | */ 5 | const deltaX = { 6 | "R": 1, 7 | "L": -1, 8 | "U": 0, 9 | "D": 0, 10 | } 11 | 12 | const deltaY = { 13 | "R": 0, 14 | "L": 0, 15 | "U": -1, 16 | "D": 1, 17 | } 18 | 19 | const judgeCircle = function(moves) { 20 | let x = 0; 21 | let y = 0; 22 | 23 | for (let i = 0; i < moves.length; i++) { 24 | x += deltaX[moves[i]]; 25 | y += deltaY[moves[i]]; 26 | } 27 | 28 | return (x === 0 && y === 0); 29 | }; -------------------------------------------------------------------------------- /Binary-Tree/965/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 965. 单值二叉树 3 | * 4 | * https://leetcode-cn.com/problems/univalued-binary-tree/ 5 | * 6 | * Easy 7 | * 8 | * 72ms 94.69% 9 | * 34.3mb 6.98% 10 | */ 11 | const isUnivalTree = root => { 12 | if (!root) { 13 | return true 14 | } 15 | 16 | if (root.left && root.left.val !== root.val) { 17 | return false 18 | } 19 | if (root.right && root.right.val !== root.val) { 20 | return false 21 | } 22 | 23 | return isUnivalTree(root.left) && isUnivalTree(root.right) 24 | } -------------------------------------------------------------------------------- /Other/153/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 3 | * 4 | * 153. Find Minimum in Rotated Sorted Array 5 | * 6 | * Medium 7 | * 8 | * 52ms 94.11% 9 | * 33.8mb 71.58% 10 | */ 11 | const findMin = nums => { 12 | 13 | let preItem = nums[0] 14 | let index = 1 15 | const max = nums.length 16 | while (index < max) { 17 | const item = nums[index] 18 | if (item < preItem) { 19 | return item 20 | } 21 | index++ 22 | } 23 | return preItem 24 | } -------------------------------------------------------------------------------- /dynamic-programming/279/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/perfect-squares/ 3 | * 4 | * 279. Perfect Squares 5 | * 6 | * Medium 7 | */ 8 | const numSquares = n => { 9 | const dp = new Array(n + 1).fill(Number.MAX_SAFE_INTEGER) 10 | 11 | for (let i = 1; i * i <= n; i++) { 12 | dp[i * i] = 1 13 | } 14 | 15 | for (let i = 1; i < n; i++) { 16 | for (let j = 1; i + j * j <= n; j++) { 17 | dp[i + j * j] = Math.min(dp[i] + 1, dp[i + j * j]) 18 | } 19 | } 20 | 21 | return dp[n] 22 | } -------------------------------------------------------------------------------- /one-question-per-day/20201012/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | */ 4 | const getMinimumDifference = root => { 5 | let ans = Number.MAX_SAFE_INTEGER; 6 | let preValue = null; 7 | 8 | const help = root => { 9 | if (!root) { 10 | return; 11 | } 12 | 13 | help(root.left); 14 | // 注意判断 15 | if (preValue !== null) { 16 | ans = Math.min(ans, Math.abs(preValue - root.val)); 17 | } 18 | preValue = root.val; 19 | help(root.right); 20 | } 21 | 22 | help(root); 23 | return ans; 24 | } -------------------------------------------------------------------------------- /one-question-per-day/20201021/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 双指针 3 | * 时间复杂度 O(n + m) 4 | * 空间复杂度 O(1) 5 | */ 6 | const isLongPressedName = function(name, typed) { 7 | let x = 0; 8 | let y = 0; 9 | const len1 = name.length; 10 | const len2 = typed.length; 11 | while (x < len1 || y < len2) { 12 | if (name[x] === typed[y]) { 13 | x++; 14 | y++; 15 | } else if (y > 0 && typed[y] === typed[y - 1]) { 16 | y++; 17 | } else { 18 | return false; 19 | } 20 | } 21 | 22 | return true; 23 | }; -------------------------------------------------------------------------------- /one-question-per-day/20220106/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度O(n) 3 | * 空间复杂度O(n) 4 | * @param {*} path 5 | * @returns 6 | */ 7 | const simplifyPath = function(path) { 8 | const paths = path.split('/'); 9 | const stack = []; 10 | for (const name of paths) { 11 | if (name === '..' && stack.length) { 12 | stack.pop(); 13 | } else if (name.length && name !== '.' && name !== '..') { 14 | stack.push(name); 15 | } 16 | } 17 | 18 | return '/' + stack.join('/'); 19 | }; -------------------------------------------------------------------------------- /string/771/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/jewels-and-stones/ 3 | * 4 | * 5 | * 771. 宝石与石头 6 | * 7 | * Easy 8 | * 9 | * 72ms 96.19% 10 | * 33.8mb 67.26% 11 | * 12 | */ 13 | const numJewelsInStones = (J, S) => { 14 | const record = new Set(); 15 | for (let i = 0; i < J.length; i++) { 16 | record.add(J[i]); 17 | } 18 | 19 | let count = 0; 20 | for (let i = 0; i < S.length; i++) { 21 | if (record.has(S[i])) { 22 | count++; 23 | } 24 | } 25 | return count; 26 | } -------------------------------------------------------------------------------- /Binary-Tree/49/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const sumNumbers = root => { 6 | let ans = 0; 7 | 8 | const dfs = (root, path) => { 9 | if (!root) { 10 | return ''; 11 | } 12 | 13 | path += root.val; 14 | 15 | if (!root.left && !root.right) { 16 | ans += Number.parseInt(path); 17 | } 18 | 19 | dfs(root.left, path); 20 | dfs(root.right, path); 21 | } 22 | 23 | dfs(root, ''); 24 | 25 | return ans; 26 | } -------------------------------------------------------------------------------- /Linked-List/1290/solution.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/convert-binary-number-in-a-linked-list-to-integer/ 3 | * 4 | * Easy 5 | * 6 | * 48ms 99.31% 7 | * 33.7mb 100.00% 8 | * 9 | * 基本操作 遍历链表 10 | */ 11 | const getDecimalValue = head => { 12 | if (!head) { 13 | return 0; 14 | } 15 | 16 | let currentHead = head; 17 | let ans = ''; 18 | 19 | while (currentHead) { 20 | ans += currentHead.val; 21 | currentHead = currentHead.next; 22 | } 23 | 24 | return Number.parseInt(ans, 2); 25 | } -------------------------------------------------------------------------------- /Linked-List/817/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/linked-list-components/ 3 | * 4 | * 817. 链表组件 5 | * 6 | * Medium 7 | * 8 | * 88ms 79.31% 9 | * 39mb 100.00% 10 | */ 11 | const numComponents = (head, G) => { 12 | if (!head) { 13 | return 0; 14 | } 15 | 16 | const record = new Set(G); 17 | let ans = 0; 18 | while(head) { 19 | if (record.has(head.val) && (!head.next || !record.has(head.next.val))) { 20 | ++ans; 21 | } 22 | head = head.next; 23 | } 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Rank/201/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 考察数据结构 栈 3 | * 4 | * 时间复杂度 O(n) 5 | * 空间复杂度 O(n) 6 | */ 7 | const makeGood = function(s) { 8 | const stack = []; 9 | let startIndex = 0; 10 | while (startIndex < s.length) { 11 | const currentItem = s[startIndex]; 12 | if (stack.length && Math.abs(currentItem.charCodeAt(0) - stack[stack.length - 1].charCodeAt(0)) === 32) { 13 | stack.pop(); 14 | } else { 15 | stack.push(currentItem); 16 | } 17 | startIndex++; 18 | } 19 | 20 | return stack.join(''); 21 | }; -------------------------------------------------------------------------------- /Binary-Tree/530/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 4 | * 时间复杂度 O(n) 5 | * 空间复杂度 O(n) 6 | */ 7 | const getMinimumDifference = root => { 8 | let ans = Number.MAX_SAFE_INTEGER; 9 | let preValue = null; 10 | 11 | const help = root => { 12 | if (!root) { 13 | return 0; 14 | } 15 | help(root.left); 16 | if (preValue !== null) { 17 | ans = Math.min(ans, Math.abs(preValue - root.val)); 18 | } 19 | preValue = root.val; 20 | help(root.right); 21 | } 22 | 23 | help(root); 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Recursion/779/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/k-th-symbol-in-grammar/ 3 | * 4 | * 779. 第K个语法符号 5 | * 6 | * Medium 7 | * 8 | * 76ms 64.15% 9 | * 33.8mb 20.00% 10 | * 11 | * 规律 12 | * 13 | * 0 1 14 | * / \ / \ 15 | * 0 1 1 0 16 | */ 17 | const kthGrammar = (N, K) => { 18 | if (N === 1) { 19 | return 0; 20 | } 21 | if (K % 2 === 0) { 22 | return kthGrammar(N - 1, K / 2) ? 0 : 1; 23 | } else { 24 | return kthGrammar(N - 1, (K + 1) / 2) ? 1 : 0; 25 | } 26 | } -------------------------------------------------------------------------------- /math/263/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/ugly-number/ 3 | * 4 | * 263. Ugly Number 5 | * 6 | * Medium 7 | */ 8 | const isUgly = num => { 9 | if (num < 1) { 10 | return false 11 | } 12 | while (num !== 1) { 13 | if (num % 2 === 0) { 14 | num /= 2 15 | continue 16 | } 17 | if (num % 3 === 0) { 18 | num /= 3 19 | continue 20 | } 21 | if (num % 5 === 0) { 22 | num /= 5 23 | continue 24 | } 25 | return false 26 | } 27 | 28 | return true 29 | } -------------------------------------------------------------------------------- /one-question-per-day/20210822/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} ghosts 3 | * @param {number[]} target 4 | * @return {boolean} 5 | */ 6 | var escapeGhosts = function(ghosts, target) { 7 | const distance = minDistance([0, 0], target); 8 | for (const ghost of ghosts) { 9 | if (minDistance(ghost, target) <= distance) { 10 | return false; 11 | } 12 | } 13 | 14 | return true; 15 | 16 | function minDistance(start, end) { 17 | return Math.abs(start[0] - end[0]) + Math.abs(start[1] - end[1]); 18 | } 19 | }; -------------------------------------------------------------------------------- /Binary-Tree/28/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | */ 4 | const isSymmetric = function(root) { 5 | const help = (left, right) => { 6 | if (!left && !right) { 7 | return true; 8 | } 9 | 10 | if (!left || !right) { 11 | return false; 12 | } 13 | 14 | if (left.val !== right.val) { 15 | return false; 16 | } 17 | 18 | return help(left.left, right.right) && help(left.right, right.left); 19 | } 20 | 21 | if (!root) { 22 | return true; 23 | } 24 | 25 | return help(root.left, root.right); 26 | }; -------------------------------------------------------------------------------- /Rank/173/1332/solution.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/remove-palindromic-subsequences/ 3 | * 4 | * 1332. 删除回文子序列 5 | * 6 | * Easy 7 | * 8 | * 56ms 84.21% 9 | * 34.2mb 100.00% 10 | */ 11 | const removePalindromeSub = s => { 12 | if (!s) { 13 | return 0; 14 | } 15 | 16 | let start = 0; 17 | let end = s.length - 1; 18 | 19 | while (start < end) { 20 | if (s[start] !== s[end]) { 21 | return 2; 22 | } else { 23 | start++; 24 | end--; 25 | } 26 | } 27 | 28 | return 1; 29 | } -------------------------------------------------------------------------------- /Rank/186/solution1.js: -------------------------------------------------------------------------------- 1 | const destCity = paths => { 2 | const record = new Set(); 3 | for (let i = 0; i < paths.length; i++) { 4 | record.add(paths[i][0]); 5 | } 6 | 7 | for (let i = 0; i < paths.length; i++) { 8 | if (!record.has(paths[i][1])) { 9 | return paths[i][1]; 10 | } 11 | } 12 | } 13 | 14 | console.log(destCity([["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]) === 'Sao Paulo'); 15 | console.log(destCity([["B","C"],["D","B"],["C","A"]]) === 'A'); 16 | console.log(destCity([["A","Z"]]) === 'Z') -------------------------------------------------------------------------------- /Rank/201/solution3.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 前缀和 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | */ 6 | var maxNonOverlapping = function(nums, target) { 7 | let record = new Set(); 8 | record.add(0); 9 | let ans = 0; 10 | let currentSum = 0; 11 | for (let i = 0; i < nums.length; i++) { 12 | currentSum += nums[i]; 13 | if (record.has(currentSum - target)) { 14 | ans++; 15 | record.clear(); 16 | record.add(0); 17 | currentSum = 0; 18 | } else { 19 | record.add(currentSum); 20 | } 21 | } 22 | return ans; 23 | }; -------------------------------------------------------------------------------- /Binary-Tree/113/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const pathSum = (root, sum) => { 6 | const ans = []; 7 | dfs(root, sum, [], ans); 8 | 9 | return ans; 10 | } 11 | 12 | function dfs(root, sum, path, ans) { 13 | if (!root) { 14 | return; 15 | } 16 | 17 | const _sum = sum - root.val; 18 | if (!root.left && !root.right && _sum === 0) { 19 | ans.push([...path, root.val]) 20 | } 21 | 22 | dfs(root.left, _sum, [...path, root.val], ans); 23 | dfs(root.right, _sum, [...path, root.val], ans); 24 | } -------------------------------------------------------------------------------- /Rank/biweekly/23/23-4/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/reducing-dishes/ 3 | * 4 | * 做菜顺序 5 | * 6 | * Hard 7 | * 8 | * 64ms 100.00% 9 | * 34.6mb 100.00% 10 | */ 11 | const maxSatisfaction = satisfaction => { 12 | satisfaction.sort((a, b) => b - a); 13 | 14 | let ans = 0; 15 | let sum = 0; 16 | let current = 0; 17 | for (let i = 0, max = satisfaction.length; i < max; i++) { 18 | current += satisfaction[i]; 19 | sum += current; 20 | ans = Math.max(ans, sum); 21 | } 22 | 23 | return ans; 24 | } -------------------------------------------------------------------------------- /Two-Points/141/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/linked-list-cycle/ 3 | * 4 | * 5 | * 141. 环形链表 6 | * 7 | * Easy 8 | * 9 | * 快慢指针 10 | * 11 | * 12 | * 98.59% 13 | * 69.73% 14 | * 15 | */ 16 | const hasCycle = head => { 17 | let first = head 18 | let slow = head 19 | 20 | while (first) { 21 | if (!first.next) { 22 | return false 23 | } 24 | first = first.next.next 25 | slow = slow.next 26 | if (first === slow) { 27 | return true 28 | } 29 | } 30 | return false 31 | } -------------------------------------------------------------------------------- /dynamic-programming/343/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/integer-break/ 3 | * 4 | * 343. Integer Break 5 | * 6 | * Medium 7 | * 8 | */ 9 | const integerBreak = n => { 10 | const dp = new Array(n + 1).fill(1) 11 | 12 | for (let i = 1; i <= n; i++) { 13 | for (let j = 1; j <= i + 1; j++) { 14 | if (i + j <= n) { 15 | const x = Math.max(dp[i], i) 16 | const y = Math.max(dp[j], j) 17 | 18 | dp[i + j] = Math.max(x * y, dp[i + j]) 19 | } 20 | } 21 | } 22 | return dp[n] 23 | } -------------------------------------------------------------------------------- /dynamic-programming/392/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/is-subsequence/ 4 | * 5 | * 392. Is Subsequence 6 | * 7 | * Medium 8 | */ 9 | const isSubsequence = (s, t) => { 10 | const max = s.length 11 | return isSub(0, 0) 12 | function isSub (sIndex, startIndex) { 13 | if (sIndex === max) { 14 | return true 15 | } 16 | 17 | const index = t.indexOf(s[sIndex], startIndex) 18 | if (index < 0) { 19 | return false 20 | } 21 | 22 | return isSub(sIndex + 1, index + 1) 23 | } 24 | } -------------------------------------------------------------------------------- /string/3/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const lengthOfLongestSubstring = function(s) { 6 | let start = 0; 7 | let ans = 0; 8 | const record = new Map(); 9 | 10 | for (let end = 0; end < s.length; end++) { 11 | const char = s[end]; 12 | if (record.has(char) && record.get(char) >= start) { 13 | start = record.get(char) + 1; 14 | } 15 | 16 | record.set(char, end); 17 | 18 | ans = Math.max(ans, end - start + 1); 19 | } 20 | 21 | return ans; 22 | }; -------------------------------------------------------------------------------- /Linked-List/24/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const swapPairs = head => { 6 | const dummyHead = new ListNode(null); 7 | dummyHead.next = head; 8 | 9 | let temp = dummyHead; 10 | 11 | while (temp.next !== null && temp.next.next !== null) { 12 | const node1 = temp.next; 13 | const node2 = temp.next.next; 14 | 15 | temp.next = node2; 16 | node1.next = node2.next; 17 | node2.next = node1; 18 | temp = node1; 19 | } 20 | 21 | return dummyHead.next; 22 | } -------------------------------------------------------------------------------- /Rank/179/1375/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/bulb-switcher-iii/ 3 | * 4 | * 1375. 灯泡开关 III 5 | * 6 | * Medium 7 | * 8 | * 64ms 100.00% 9 | * 41.1mb 100.00% 10 | * 11 | * 重点如何保证当前是 前 n 盏灯全亮 12 | */ 13 | 14 | const numTimesAllBlue = light => { 15 | let ans = 0; 16 | let currentMaxIndex = 0; 17 | for (let i = 0, max = light.length; i < max; i++) { 18 | currentMaxIndex = Math.max(currentMaxIndex, light[i]) 19 | if (currentMaxIndex === i + 1) { 20 | ans++; 21 | } 22 | } 23 | return ans; 24 | } -------------------------------------------------------------------------------- /array/1018/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/binary-prefix-divisible-by-5/ 3 | * 4 | * 1018. Binary Prefix Divisible By 5 5 | * 6 | * Easy 7 | * 8 | * 68ms 97.28% 9 | * 37.7mb 97.84% 10 | * 11 | */ 12 | const prefixesDivBy5 = A => { 13 | 14 | let preSum = 0 15 | for (let i = 0, max = A.length; i < max; i++) { 16 | // 数组比较大 数字可能会越界!!! 17 | preSum = (preSum * 2 + A[i]) % 5 18 | 19 | if (preSum === 0) { 20 | A[i] = true 21 | continue 22 | } 23 | A[i] = false 24 | } 25 | return A 26 | } -------------------------------------------------------------------------------- /dynamic-programming/377/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/combination-sum-iv/ 4 | * 5 | * 377. Combination Sum IV 6 | * 7 | * Medium 8 | * 9 | */ 10 | const combinationSum4 = (nums, target) => { 11 | const max = nums.length 12 | 13 | const dp = new Array(target + 1).fill(0) 14 | 15 | dp[0] = 1 16 | 17 | for (let i = 1; i <= target; i++) { 18 | for (let j = 0; j < max; j++) { 19 | if (nums[j] <= i) { 20 | dp[i] += dp[i - nums[j]] 21 | } 22 | } 23 | } 24 | return dp[target] 25 | } -------------------------------------------------------------------------------- /Binary-Tree/32/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | */ 6 | const levelOrder = function(root) { 7 | const ans = []; 8 | if (!root) { 9 | return ans; 10 | } 11 | 12 | help(ans, root); 13 | return ans; 14 | }; 15 | 16 | function help(ans, root, height = 0) { 17 | if (!root) { 18 | return; 19 | } 20 | if (!ans[height]) { 21 | ans[height] = []; 22 | } 23 | 24 | ans[height].push(root.val); 25 | root.left && help(ans, root.left, height + 1); 26 | root.right && help(ans, root.right, height + 1); 27 | } -------------------------------------------------------------------------------- /Binary-Tree/437/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(n) 4 | */ 5 | const pathSum = (root, sum) => { 6 | if (!root) { 7 | return 0; 8 | } 9 | 10 | return rootSum(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); 11 | } 12 | 13 | function rootSum (root, sum) { 14 | if (!root) { 15 | return 0; 16 | } 17 | 18 | let ans = 0; 19 | if (root.val === sum) { 20 | ans++; 21 | } 22 | 23 | ans += rootSum(root.left, sum - root.val); 24 | ans += rootSum(root.right, sum - root.val); 25 | 26 | return ans; 27 | } -------------------------------------------------------------------------------- /Binary-Tree/55/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 3 | */ 4 | const isBalanced = function(root) { 5 | if (!root) { 6 | return true; 7 | } 8 | const leftTreeHeight = help(root.left); 9 | const rightTreeHeight = help(root.right); 10 | 11 | return isBalanced(root.left) && isBalanced(root.right) && (Math.abs(leftTreeHeight - rightTreeHeight) <= 1); 12 | }; 13 | 14 | function help(root) { 15 | if (!root) { 16 | return 0; 17 | } 18 | const lh = help(root.left); 19 | const rh = help(root.right); 20 | 21 | return Math.max(lh + 1, rh + 1); 22 | } -------------------------------------------------------------------------------- /Rank/177/5169/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/number-of-days-between-two-dates/ 3 | * 4 | * 5169. 日期之间隔几天 5 | * 6 | * Easy 7 | * 8 | * 80ms 100.00% 9 | * 34mb 100.00% 10 | */ 11 | const ONE_DAY = 24 * 60 * 60 * 1000; 12 | 13 | const daysBetweenDates = (date1, date2) => { 14 | const [y1, m1, d1] = date1.split('-'); 15 | const [y2, m2, d2] = date2.split('-'); 16 | const time1 = new Date(y1, m1 - 1, d1, 0, 0, 0); 17 | const time2 = new Date(y2, m2 - 1, d2, 0, 0, 0); 18 | return Math.abs(time2 - time1) / ONE_DAY; 19 | } -------------------------------------------------------------------------------- /array/717/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/1-bit-and-2-bit-characters/ 3 | * 4 | * 717. 1-bit and 2-bit Characters 5 | * 6 | * Easy 7 | * 8 | * 60ms 82.77% 9 | * 10 | * 34.4MB 51.98% 11 | */ 12 | const isOneBitCharacter = bits => { 13 | const max = bits.length 14 | 15 | for (let i = 0; i < max;) { 16 | const bit = bits[i] 17 | if (bit === 1) { 18 | i += 2 19 | continue 20 | } 21 | if (bit === 0 && i === max - 1) { 22 | return true 23 | } 24 | i++ 25 | } 26 | return false 27 | } -------------------------------------------------------------------------------- /one-question-per-day/20200831/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 深度优先遍历 3 | * 时间复杂度 O(n + m) 4 | * 空间复杂度 O(n) 5 | */ 6 | const canVisitAllRooms = function(rooms) { 7 | const record = new Set(); 8 | const roomLength = rooms.length; 9 | dfs(0); 10 | 11 | return roomLength === record.size; 12 | function dfs(startIndex) { 13 | if (record.has(startIndex)) { 14 | return; 15 | } 16 | record.add(startIndex); 17 | const room = rooms[startIndex]; 18 | for (let i = 0; i < room.length; i++) { 19 | dfs(room[i]); 20 | } 21 | } 22 | }; -------------------------------------------------------------------------------- /string/387/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 暴力解法超时 3 | * 4 | * 哈希 5 | * 6 | * 时间复杂度:O(n) 7 | * 空间复杂度:O(1) 8 | */ 9 | const firstUniqChar = function(s) { 10 | const record = new Map(); 11 | for (let i = 0; i < s.length; i++) { 12 | if (!record.has(s[i])) { 13 | record.set(s[i], 0); 14 | } 15 | record.set(s[i], record.get(s[i]) + 1); 16 | } 17 | 18 | for (let i = 0; i < s.length; i++) { 19 | if (record.get(s[i]) === 1) { 20 | return i; 21 | } 22 | } 23 | 24 | return -1; 25 | }; -------------------------------------------------------------------------------- /Binary-Tree/101/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const isSymmetric = function (root) { 6 | if (!root) { 7 | return true; 8 | } 9 | 10 | return _isSymmetric(root.left, root.right); 11 | } 12 | 13 | function _isSymmetric (left, right) { 14 | if (left === null && right === null) { 15 | return true; 16 | } 17 | 18 | if (left === null || right === null) { 19 | return false; 20 | } 21 | 22 | return (left.val === right.val && _isSymmetric(left.left, right.right) && _isSymmetric(left.right, right.left)); 23 | } -------------------------------------------------------------------------------- /Binary-Tree/116/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/ 3 | * 4 | * 5 | * 116. 填充每个节点的下一个右侧节点指针 6 | * 7 | * medium 8 | * 9 | * 104ms 98.95% 10 | * 39.3mb 79.49% 11 | */ 12 | const connect = root => { 13 | if (!root) { 14 | return null; 15 | } 16 | if (root.left) { 17 | root.left.next = root.right; 18 | } 19 | if (root.right && root.next) { 20 | root.right.next = root.next.left; 21 | } 22 | 23 | connect(root.left); 24 | connect(root.right); 25 | return root; 26 | } -------------------------------------------------------------------------------- /Binary-Tree/45/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const findBottomLeftValue = (root) => { 6 | let ans = 0; 7 | let currentLevel = -1; 8 | 9 | const dfs = (root, level) => { 10 | if (!root) { 11 | return; 12 | } 13 | 14 | if (level > currentLevel) { 15 | ans = root.val; 16 | currentLevel = level; 17 | } 18 | 19 | dfs(root.left, level + 1); 20 | dfs(root.right, level + 1); 21 | } 22 | 23 | dfs(root, 0); 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Rank/174/1337/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/the-k-weakest-rows-in-a-matrix/ 3 | * 4 | * 1337. 方阵中战斗力最弱的 K 行 5 | * 6 | * Easy 7 | * 8 | * 64ms 93.75% 9 | * 35.2mb 100.00 10 | * 11 | * 考点 12 | * 13 | * - 数组基本操作 14 | * - 多条件排序 15 | */ 16 | const kWeakestRows = (mat, k) => mat.map((subArray, index) => [index, subArray.reduce((a, b) => a + b, 0)]) 17 | .sort((a, b) => { 18 | if (a[1] === b[1]) { 19 | return a[0] - b[0]; 20 | } 21 | return a[1] - b[1]; 22 | }) 23 | .slice(0, k) 24 | .map(item => item[0]) -------------------------------------------------------------------------------- /Rank/200/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n^3) 3 | * 4 | * 空间复杂度 O(1) 5 | * 6 | * 124ms 37.9mb 7 | */ 8 | const countGoodTriplets = function(arr, a, b, c) { 9 | const max = arr.length; 10 | let ans = 0; 11 | 12 | for (let i = 0; i < max - 2; i++) { 13 | for (let j = i + 1; j < max - 1; j++) { 14 | for (let k = j + 1; k < max; k++) { 15 | if (Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] -arr[k]) <= c) { 16 | ans++; 17 | } 18 | } 19 | } 20 | } 21 | return ans; 22 | }; -------------------------------------------------------------------------------- /dynamic-programming/53/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-subarray/ 3 | * 4 | * 53. Maximum Subarray 5 | * 6 | * Easy 7 | */ 8 | const maxSubArray = nums => { 9 | const max = nums.length 10 | if (!max) { 11 | return 0 12 | } 13 | 14 | const dp = [nums[0]] 15 | if (max === 1) { 16 | return dp[0] 17 | } 18 | let ans = dp[0] 19 | 20 | for (let i = 1; i < max; i++) { 21 | const num = nums[i] 22 | dp[i] = Math.max(dp[i - 1] + num, num) 23 | ans = Math.max(ans, dp[i]) 24 | } 25 | return ans 26 | } -------------------------------------------------------------------------------- /math/204/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/count-primes/ 3 | * 4 | * 204. Count Primes 5 | * 6 | * Medium 7 | * 8 | * 筛子法 9 | */ 10 | const countPrimes = n => { 11 | if (n < 3) { 12 | return 0 13 | } 14 | const dp = new Array(n).fill(1) 15 | 16 | dp[0] = 0 17 | dp[1] = 0 18 | 19 | for (let i = 2; i <= Math.sqrt(n); i++) { 20 | if (!dp[i]) { 21 | continue 22 | } 23 | for (let j = i * i; j < n; j += i) { 24 | dp[j] = 0 25 | } 26 | } 27 | 28 | return dp.reduce((a, b) => a + b, 0) 29 | } 30 | -------------------------------------------------------------------------------- /Binary-Search/367/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/valid-perfect-square/ 3 | * 4 | * 367. Valid Perfect Square 5 | * 6 | * Easy 7 | * 8 | * 40ms 99.67% 9 | * 33.8mb 79.73% 10 | */ 11 | const isPerfectSquare = num => { 12 | let start = 1 13 | let end = num 14 | 15 | while (start < end) { 16 | const mid = Math.floor(start + (end - start) / 2) 17 | const middle = mid * mid 18 | if (num > middle) { 19 | start = mid + 1 20 | } else { 21 | end = mid 22 | } 23 | } 24 | 25 | return start ** 2 === num 26 | } -------------------------------------------------------------------------------- /Binary-Tree/44/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const largestValues = (root) => { 6 | const ans = []; 7 | dfs(root, 0, ans); 8 | return ans; 9 | } 10 | 11 | function dfs(root, level, ans) { 12 | if (!root) { 13 | return; 14 | } 15 | 16 | if (ans[level] === undefined) { 17 | ans[level] = Number.MIN_SAFE_INTEGER; 18 | } 19 | 20 | ans[level] = Math.max(ans[level], root.val); 21 | 22 | dfs(root.left, level + 1, ans); 23 | dfs(root.right, level + 1, ans); 24 | 25 | return; 26 | } -------------------------------------------------------------------------------- /Binary-Tree/669/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * 669. 修剪二叉搜索树 4 | * 5 | * https://leetcode-cn.com/problems/trim-a-binary-search-tree/ 6 | * 7 | * Easy 8 | * 9 | * 92ms 98.25% 10 | * 39.5mb 11.76% 11 | */ 12 | 13 | const trimBST = (root, L, R) => { 14 | if (!root) { 15 | return null 16 | } 17 | if (root.val < L) { 18 | return trimBST(root.right, L, R) 19 | } 20 | if (root.val > R) { 21 | return trimBST(root.left, L, R) 22 | } 23 | root.left = trimBST(root.left, L, R) 24 | root.right = trimBST(root.right, L, R) 25 | return root 26 | } -------------------------------------------------------------------------------- /array/1010/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/ 3 | * 4 | * 1010. Pairs of Songs With Total Durations Divisible by 60 5 | * 6 | * Easy 7 | * 8 | * 72ms 76.61% 9 | * 38.6mb 14.48% 10 | */ 11 | const numPairsDivisibleBy60 = time => { 12 | 13 | const cache = new Map() 14 | let ans = 0 15 | for (let t of time) { 16 | ans += cache.get((60 - t % 60) % 60) || 0 17 | 18 | const temp = cache.get(t % 60) || 0 19 | cache.set(t % 60, temp + 1) 20 | } 21 | 22 | return ans 23 | } -------------------------------------------------------------------------------- /dynamic-programming/523/solution.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/continuous-subarray-sum/ 3 | * 4 | * 523. Continuous Subarray Sum 5 | * 6 | * Medium 7 | */ 8 | const checkSubarraySum = (nums, k) => { 9 | 10 | for (let i = 0, max = nums.length; i < max; i++) { 11 | let sum = nums[i] 12 | for (let j = i + 1; j < max; j++) { 13 | sum += nums[j] 14 | if (sum === k) { 15 | return true 16 | } 17 | if (k !== 0 && sum % k === 0) { 18 | return true 19 | } 20 | } 21 | } 22 | 23 | return false 24 | } -------------------------------------------------------------------------------- /one-question-per-day/20201016/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 双指针 3 | * 4 | * 时间复杂度 O(n) 5 | * 空间复杂度 O(n) 6 | */ 7 | const sortedSquares = function(A) { 8 | const ans = []; 9 | let position = A.length - 1; 10 | let start = 0; 11 | let end = A.length - 1; 12 | while (start <= end) { 13 | const x = A[start] * A[start]; 14 | const y = A[end] * A[end]; 15 | if (x >= y) { 16 | ans[position] = x; 17 | start++; 18 | } else { 19 | ans[position] = y; 20 | end--; 21 | } 22 | position--; 23 | } 24 | 25 | return ans; 26 | }; -------------------------------------------------------------------------------- /one-question-per-day/20210826/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} people 3 | * @param {number} limit 4 | * @return {number} 5 | */ 6 | var numRescueBoats = function(people, limit) { 7 | // 贪心 8 | // nlog(n) 9 | people.sort((a, b) => a - b); 10 | 11 | let ans = 0; 12 | let start = 0; 13 | let end = people.length - 1; 14 | // O(n) 15 | while (start <= end) { 16 | if (people[start] + people[end] <= limit) { 17 | start++; 18 | end--; 19 | } else { 20 | end--; 21 | } 22 | ans++; 23 | } 24 | 25 | return ans; 26 | }; -------------------------------------------------------------------------------- /Binary-Search/69/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/sqrtx/ 3 | * 4 | * 69. Sqrt(x) 5 | * 6 | * Easy 7 | * 8 | * 68ms 96.10% 9 | * 35.7mb 47.66% 10 | */ 11 | const mySqrt = x => { 12 | let start = 1 13 | let end = x 14 | 15 | while (start < end) { 16 | const mid = Math.floor(start + (end - start) / 2) 17 | const middle = mid * mid 18 | if (x > middle) { 19 | start = mid + 1 20 | } else { 21 | end = mid 22 | } 23 | } 24 | if (start ** 2 === x) { 25 | return start 26 | } 27 | return start - 1 28 | } -------------------------------------------------------------------------------- /Two-Points/234/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/palindrome-linked-list/ 3 | * 4 | * 234. 回文链表 5 | * 6 | * Easy 7 | * 8 | * 96.51% 9 | * 37.67% 10 | * 11 | */ 12 | const isPalindrome = head => { 13 | const arr = [] 14 | 15 | while (head) { 16 | arr.push(head.val) 17 | head = head.next 18 | } 19 | 20 | let start = 0 21 | let end = arr.length - 1 22 | 23 | while (start < end) { 24 | if (arr[start] !== arr[end]) { 25 | return false 26 | } 27 | start++ 28 | end-- 29 | } 30 | 31 | return true 32 | } -------------------------------------------------------------------------------- /array/1010/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/ 3 | * 4 | * 1010. Pairs of Songs With Total Durations Divisible by 60 5 | * 6 | * Easy 7 | * 8 | * 5468ms 30.07% 9 | * 38.1mb 23.34% 10 | */ 11 | const numPairsDivisibleBy60 = time => { 12 | const max = time.length 13 | let ans = 0 14 | for (let i = 0; i < max - 1; i++) { 15 | for (let j = i + 1; j < max; j++) { 16 | if ((time[i] + time[j]) % 60 === 0) { 17 | ans++ 18 | } 19 | } 20 | } 21 | 22 | return ans 23 | } -------------------------------------------------------------------------------- /one-question-per-day/20200817/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归 (自底向上) 3 | * 时间复杂度 O(n); 4 | * 空间复杂度 O(1); 5 | */ 6 | const isBalanced = root => { 7 | const ans = help(root); 8 | return ans[1]; 9 | } 10 | 11 | function help(root) { 12 | if (!root) { 13 | return [0, true]; 14 | } 15 | 16 | const [leftHeight, isLeftValid] = help(root.left); 17 | const [rightHeight, isRightValid] = help(root.right); 18 | const isValid = Math.abs(leftHeight - rightHeight) <= 1; 19 | return [Math.max(leftHeight, rightHeight) + 1, isValid && isLeftValid && isRightValid]; 20 | } -------------------------------------------------------------------------------- /Binary-Tree/543/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/diameter-of-binary-tree/ 3 | * 4 | * 543. 二叉树的直径 5 | * 6 | * Easy 7 | * 8 | * 92ms 93.98% 9 | * 37mb 75.00% 10 | * 11 | */ 12 | const diameterOfBinaryTree = root => { 13 | let sum = 0 14 | postOrder(root) 15 | return sum 16 | function postOrder (root) { 17 | if (!root) { 18 | return 0 19 | } 20 | const left = postOrder(root.left) 21 | const right = postOrder(root.right) 22 | sum = Math.max(sum, left + right) 23 | return Math.max(right, left) + 1 24 | } 25 | } -------------------------------------------------------------------------------- /dynamic-programming/121/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 3 | * 4 | * 121. Best Time to Buy and Sell Stock 5 | * 6 | */ 7 | const maxProfit = prices => { 8 | const max = prices.length 9 | if (max < 2) { 10 | return 0 11 | } 12 | 13 | const sell = [0] 14 | const hold = [-prices[0]] 15 | 16 | for (let i = 1; i < max; i++) { 17 | const price = prices[i] 18 | sell[i] = Math.max(sell[i - 1], hold[i - 1] + price) 19 | hold[i] = Math.max(hold[i - 1], -price) 20 | } 21 | return sell[max - 1] 22 | } -------------------------------------------------------------------------------- /dynamic-programming/279/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/perfect-squares/ 3 | * 4 | * 279. Perfect Squares 5 | * 6 | * Medium 7 | */ 8 | const numSquares = n => { 9 | const dp = new Array(n + 1).fill(Number.MAX_SAFE_INTEGER) 10 | dp[1] = 1 11 | for (let i = 2; i <= n; i++) { 12 | const s = Math.floor(Math.sqrt(i)) 13 | for (let j = i - 1; j >= Math.floor(i / 2); j--) { 14 | if (Math.pow(s, 2) === i) { 15 | dp[i] = 1 16 | } 17 | dp[i] = Math.min(dp[j] + dp[i - j], dp[i]) 18 | } 19 | } 20 | return dp[n] 21 | } -------------------------------------------------------------------------------- /Binary-Tree/515/solution1.js: -------------------------------------------------------------------------------- 1 | const largestValues = root => { 2 | const ans = [] 3 | if (!root) { 4 | return ans 5 | } 6 | 7 | const q = [root] 8 | while (q.length) { 9 | const len = q.length 10 | let max = Number.MIN_SAFE_INTEGER 11 | 12 | for (let i = 0; i < len; i++) { 13 | const item = q.pop() 14 | if (item) { 15 | max = Math.max(item.val, max) 16 | 17 | item.left && q.unshift(item.left) 18 | 19 | item.right && q.unshift(item.right) 20 | } 21 | } 22 | 23 | ans.push(max) 24 | } 25 | return ans 26 | } -------------------------------------------------------------------------------- /one-question-per-day/20210825/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} graph 3 | * @return {number[][]} 4 | */ 5 | var allPathsSourceTarget = function(graph) { 6 | const ans = []; 7 | const stack = [0]; 8 | function dfs(graph, index, size) { 9 | if (index === size) { 10 | ans.push([...stack]); 11 | return; 12 | } 13 | for (const item of graph[index]) { 14 | stack.push(item); 15 | dfs(graph, item, size); 16 | // 重置 17 | stack.pop(); 18 | } 19 | } 20 | 21 | dfs(graph, 0, graph.length - 1); 22 | 23 | return ans; 24 | }; -------------------------------------------------------------------------------- /Binary-Tree/404/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/sum-of-left-leaves/ 3 | * 4 | * 404. 左叶子之和 5 | * 6 | * Easy 7 | * 8 | * 80ms 90.00% 9 | * 34.6mb 36.11% 10 | */ 11 | const sumOfLeftLeaves = root => { 12 | 13 | let sum = 0 14 | inOrder(root) 15 | return sum 16 | 17 | function inOrder(root) { 18 | if (!root) { 19 | return 20 | } 21 | 22 | inOrder(root.left) 23 | if (root.left && !root.left.left && !root.left.right) { 24 | // 左叶子节点 25 | sum += root.left.val 26 | } 27 | inOrder(root.right) 28 | } 29 | } -------------------------------------------------------------------------------- /Binary-Tree/563/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode-cn.com/problems/binary-tree-tilt/ 4 | * 5 | * 563. 二叉树的坡度 6 | * 7 | * Easy 8 | * 9 | * 108ms 73.33% 10 | * 38.3mb 26.67% 11 | */ 12 | const findTilt = root => { 13 | let sum = 0 14 | postOrder(root) 15 | return sum 16 | 17 | function postOrder (root) { 18 | if (!root) { 19 | return 0 20 | } 21 | const leftSum = postOrder(root.left) 22 | const rightSum = postOrder(root.right) 23 | 24 | sum += Math.abs(leftSum - rightSum) 25 | return root.val + leftSum + rightSum 26 | } 27 | } -------------------------------------------------------------------------------- /Binary-Tree/654/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/maximum-binary-tree/ 3 | * 4 | * 654. 最大二叉树 5 | * 6 | * Medium 7 | * 8 | * 140ms 85.71% 9 | * 41mb 82.14% 10 | * 11 | */ 12 | const constructMaximumBinaryTree = nums => { 13 | if (nums.length === 0) { 14 | return null 15 | } 16 | const max = Math.max(...nums) 17 | const index = nums.indexOf(max) 18 | const root = new TreeNode(max) 19 | root.left = constructMaximumBinaryTree(nums.slice(0, index)) 20 | root.right = constructMaximumBinaryTree(nums.slice(index + 1)) 21 | return root 22 | } -------------------------------------------------------------------------------- /Linked-List/19/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const removeNthFromEnd = (head, n) => { 6 | const dummyHead = new ListNode(-1); 7 | dummyHead.next = head; 8 | let currentHead = dummyHead.next; 9 | let len = 0; 10 | while (currentHead) { 11 | len++; 12 | currentHead = currentHead.next; 13 | } 14 | 15 | currentHead = dummyHead; 16 | let index = len - n; 17 | while (index) { 18 | index--; 19 | currentHead = currentHead.next; 20 | } 21 | 22 | currentHead.next = currentHead.next.next; 23 | 24 | return dummyHead.next; 25 | } -------------------------------------------------------------------------------- /Rank/biweekly/20/5326/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/count-all-valid-pickup-and-delivery-options/submissions/ 3 | * 4 | * 5326. 有效的快递序列数目 5 | * 6 | * Hard 7 | * 8 | * 56ms 100.00% 9 | * 34.3mb 100.00% 10 | */ 11 | const MAX_NUMBER = (10 ** 9 + 7); 12 | const countOrders = n => { 13 | let ans = 1; 14 | if (!n) { 15 | return 0; 16 | } 17 | if (n === 1) { 18 | return ans; 19 | } 20 | for (let i = 2; i <= n; i++) { 21 | const k = i * 2 - 1; 22 | ans = ans * (k + k * (k - 1) / 2) % MAX_NUMBER; 23 | } 24 | 25 | return ans; 26 | } -------------------------------------------------------------------------------- /array/485/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/max-consecutive-ones/ 3 | * 4 | * 485. Max Consecutive Ones 5 | * 6 | * Easy 7 | */ 8 | const findMaxConsecutiveOnes = nums => { 9 | const max = nums.length 10 | if (!max) { 11 | return 0 12 | } 13 | let ans = 0 14 | let current = 0 15 | for (let i = 0; i < max; i++) { 16 | const num = nums[i] 17 | if (num === 1) { 18 | current += 1 19 | } else { 20 | ans = Math.max(ans, current) 21 | current = 0 22 | } 23 | } 24 | ans = Math.max(ans, current) 25 | return ans 26 | } -------------------------------------------------------------------------------- /array/766/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/toeplitz-matrix/ 3 | * 4 | * 766. Toeplitz Matrix 5 | * 6 | * Easy 7 | */ 8 | const isToeplitzMatrix = matrix => { 9 | const row = matrix.length 10 | if (!row) { 11 | return true 12 | } 13 | const col = matrix[0].length 14 | for (let i = 0; i < row - 1; i++) { 15 | for (let j = 0; j < col - 1; j++) { 16 | const item = matrix[i][j] 17 | const sameItem = matrix[i + 1][j + 1] 18 | if (item !== sameItem) { 19 | return false 20 | } 21 | } 22 | } 23 | return true 24 | } -------------------------------------------------------------------------------- /one-question-per-day/20200901/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 动态规划 3 | * 4 | * 时间复杂度 O(n^2) 5 | * 空间复杂度 O(n^2) 6 | */ 7 | const PredictTheWinner = function(nums) { 8 | const len = nums.length; 9 | 10 | const dp = Array.from({ length: len }, () => Array(len).fill(0)); 11 | 12 | for (let i = 0; i < len; i++) { 13 | dp[i][i] = nums[i]; 14 | } 15 | 16 | for (let i = len - 2; i >= 0; i--) { 17 | for (let j = i + 1; j < len; j++) { 18 | dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 19 | } 20 | } 21 | 22 | return dp[0][len - 1] >= 0; 23 | }; -------------------------------------------------------------------------------- /Rank/biweekly/24/1/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/minimum-value-to-get-positive-step-by-step-sum/ 3 | * 4 | * 5372. 逐步求和得到正数的最小值 5 | * 6 | * Easy 7 | * 8 | * 时间复杂度 O(n) 9 | * 空间复杂度 O(1) 10 | */ 11 | const minStartValue = nums => { 12 | let ans = 0; 13 | let currentSum = 0; 14 | for (let i = 0, max = nums.length; i < max; i++) { 15 | currentSum += nums[i]; 16 | if (currentSum < 1) { 17 | ans += Math.abs(currentSum) + 1; 18 | currentSum = 1; 19 | } 20 | } 21 | if (ans === 0) { 22 | return 1; 23 | } 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Stack/739/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/daily-temperatures/ 3 | * 4 | * 739. 每日温度 5 | * 6 | * Medium 7 | * 8 | * 140ms 100.00% 9 | * 42.6mb 44.44% 10 | */ 11 | const dailyTemperatures = T => { 12 | const max = T.length; 13 | const ans = new Array(max).fill(0); 14 | 15 | const stack = []; 16 | 17 | for (let i = 0, max = T.length; i < max; i++) { 18 | while(stack.length && T[i] > T[stack[stack.length - 1]]) { 19 | let index = stack.pop(); 20 | ans[index] = i - index; 21 | } 22 | stack.push(i); 23 | } 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Two-Points/141/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/linked-list-cycle/ 3 | * 4 | */ 5 | 6 | /** 7 | * Definition for singly-linked list. 8 | * function ListNode(val) { 9 | * this.val = val; 10 | * this.next = null; 11 | * } 12 | */ 13 | 14 | /** 15 | * @param {ListNode} head 16 | * @return {boolean} 17 | */ 18 | var hasCycle = function(head) { 19 | const visited = new Set() 20 | 21 | while (head) { 22 | if (visited.has(head)) { 23 | return true 24 | } 25 | visited.add(head) 26 | head = head.next 27 | } 28 | return false 29 | } -------------------------------------------------------------------------------- /math/263/readme.md: -------------------------------------------------------------------------------- 1 | # [263. Ugly Number](https://leetcode.com/problems/ugly-number/) 2 | 3 | ### 一、解题思路 4 | 5 |   简单的取模运算的应用。 6 | 7 | ### 二、代码实现 8 | 9 | ```JavaScript 10 | const isUgly = num => { 11 | if (num < 1) { 12 | return false 13 | } 14 | while (num !== 1) { 15 | if (num % 2 === 0) { 16 | num /= 2 17 | continue 18 | } 19 | if (num % 3 === 0) { 20 | num /= 3 21 | continue 22 | } 23 | if (num % 5 === 0) { 24 | num /= 5 25 | continue 26 | } 27 | return false 28 | } 29 | 30 | return true 31 | } 32 | ``` -------------------------------------------------------------------------------- /Binary-Tree/606/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/construct-string-from-binary-tree/ 3 | * 4 | * 606. 根据二叉树创建字符串 5 | * 6 | * Easy 7 | * 8 | * 112ms 74.47% 9 | * 37.5mb 100.00% 10 | * 11 | */ 12 | const tree2str = t => { 13 | if (!t) { 14 | return '' 15 | } 16 | if (!t.left && !t.right) { 17 | return `${t.val}` 18 | } 19 | if (!t.left) { 20 | return `${t.val}()(${tree2str(t.right)})` 21 | } 22 | if (!t.right) { 23 | return `${t.val}(${tree2str(t.left)})` 24 | } 25 | return `${t.val}(${tree2str(t.left)})(${tree2str(t.right)})` 26 | } -------------------------------------------------------------------------------- /Linked-List/148/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/sort-list/ 3 | * 4 | * 148. 排序链表 5 | * 6 | * Medium 7 | * 8 | * 124ms 85.23% 9 | * 40.8mb 48.53% 10 | */ 11 | const sortList = head => { 12 | const result = []; 13 | let sortHead = head; 14 | while(sortHead) { 15 | result.push(sortHead.val); 16 | sortHead = sortHead.next; 17 | } 18 | result.sort((a, b) => a - b); 19 | 20 | let temp = head; 21 | for (let i = 0, max = result.length; i < max; i++) { 22 | temp.val = result[i]; 23 | temp = temp.next; 24 | } 25 | return head; 26 | } 27 | -------------------------------------------------------------------------------- /Linked-List/27/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const isPalindrome = function(head) { 6 | const list = []; 7 | while (head) { 8 | list.push(head.val); 9 | head = head.next; 10 | } 11 | 12 | return isPalindromeList(list); 13 | }; 14 | 15 | function isPalindromeList (list) { 16 | let start = 0; 17 | let end = list.length - 1; 18 | while (start < end) { 19 | if (list[start] !== list[end]) { 20 | return false; 21 | } 22 | start++; 23 | end--; 24 | } 25 | 26 | return true; 27 | } -------------------------------------------------------------------------------- /Rank/178/5344/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 3 | * 4 | * 5344. 有多少小于当前数字的数字 5 | * 6 | * Easy 7 | * 8 | * 96ms 100.00% 9 | * 35.6mb 100.00% 10 | */ 11 | const smallerNumbersThanCurrent = nums => { 12 | const ans = []; 13 | 14 | for (let i = 0; i < nums.length; i++) { 15 | const item = nums[i]; 16 | let count = 0; 17 | for (let j = 0; j < nums.length; j++) { 18 | if (nums[j] < item) { 19 | count++; 20 | } 21 | } 22 | ans.push(count); 23 | } 24 | return ans; 25 | } -------------------------------------------------------------------------------- /array/523/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(n) 4 | */ 5 | const checkSubarraySum = (nums, k) => { 6 | const length = nums.length; 7 | if (length < 2) { 8 | return false; 9 | } 10 | 11 | const preSum = Array(length + 1).fill(0); 12 | 13 | for (let i = 1; i <= length; i++) { 14 | preSum[i] = preSum[i - 1] + nums[i - 1]; 15 | } 16 | 17 | for (let i = 0; i < length + 1; i++) { 18 | for (let j = i + 2; j < length + 1; j++) { 19 | if ((preSum[j] - preSum[i]) % k === 0) { 20 | return true; 21 | } 22 | } 23 | } 24 | return false; 25 | } -------------------------------------------------------------------------------- /Binary-Search/378/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 3 | * 4 | * 378. Kth Smallest Element in a Sorted Matrix 5 | * 6 | * Medium 7 | * 8 | * 96ms 63.21% 9 | * 40.6mb 28.43% 10 | */ 11 | const kthSmallest = (matrix, k) => { 12 | const rows = matrix.length 13 | const cols = matrix[0].length 14 | 15 | const a = [] 16 | for (let row = 0; row < rows; row++) { 17 | for (let col = 0; col < cols; col++) { 18 | a.push(matrix[row][col]) 19 | } 20 | } 21 | 22 | a.sort((a, b) => a - b) 23 | 24 | return a[k - 1] 25 | } -------------------------------------------------------------------------------- /Binary-Tree/32/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 队列 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | */ 6 | const levelOrder = function(root) { 7 | const ans = []; 8 | if (!root) { 9 | return ans; 10 | } 11 | 12 | const queue = [root]; 13 | while (queue.length) { 14 | const size = queue.length; 15 | let temp = []; 16 | for (let i = 0; i < size; i++) { 17 | const item = queue.shift(); // 这个地方可以再优化 18 | temp.push(item.val); 19 | item.left && queue.push(item.left); 20 | item.right && queue.push(item.right); 21 | } 22 | ans.push(temp); 23 | } 24 | return ans; 25 | }; -------------------------------------------------------------------------------- /Linked-List/1721/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const swapNodes = function(head, k) { 6 | let currentHead1 = head; 7 | let currentHead2 = head; 8 | 9 | while (--k) { 10 | currentHead1 = currentHead1.next; 11 | } 12 | 13 | const x = currentHead1; 14 | 15 | while (currentHead1.next) { 16 | currentHead1 = currentHead1.next; 17 | currentHead2 = currentHead2.next; 18 | } 19 | 20 | const y = currentHead2 21 | 22 | const temp = x.val; 23 | x.val = y.val; 24 | y.val = temp; 25 | 26 | return head; 27 | }; -------------------------------------------------------------------------------- /Linked-List/21/solutIon1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(1) 4 | */ 5 | const mergeTwoLists = function(list1, list2) { 6 | const head = new ListNode(-1); 7 | let current = head; 8 | 9 | while (list1 && list2) { 10 | if (list1.val <= list2.val) { 11 | current.next = list1; 12 | list1 = list1.next; 13 | } else { 14 | current.next = list2; 15 | list2 = list2.next; 16 | } 17 | 18 | current = current.next; 19 | } 20 | 21 | current.next = list1 ? list1 : list2; 22 | 23 | return head.next; 24 | }; -------------------------------------------------------------------------------- /one-question-per-day/20200819/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * 时间复杂度 O(n^2) 4 | */ 5 | const countSubstrings = function(s) { 6 | let ans = 0; 7 | 8 | for (let i = 0; i < s.length; i++) { 9 | for (let j = i; j < s.length; j++) { 10 | if (isValid(s, i, j)) { 11 | ans++; 12 | } 13 | } 14 | } 15 | return ans; 16 | }; 17 | 18 | function isValid(str, startIndex, endIndex) { 19 | while (startIndex <= endIndex) { 20 | if (str[startIndex] !== str[endIndex]) { 21 | return false; 22 | } 23 | startIndex++; 24 | endIndex--; 25 | } 26 | return true; 27 | } -------------------------------------------------------------------------------- /one-question-per-day/20200907/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * sort hashmap 3 | * 4 | * 时间复杂度 O(nlogn) 5 | * 空间复杂度 O(n) 6 | */ 7 | const topKFrequent = (nums, k) => { 8 | const record = new Map(); 9 | for (let i = 0; i < nums.length; i++) { 10 | if (!record.has(nums[i])) { 11 | record.set(nums[i], 0); 12 | } 13 | record.set(nums[i], record.get(nums[i]) + 1); 14 | } 15 | 16 | const arr = Array.from(record.entries()); 17 | arr.sort((a, b) => b[1] - a[1]); 18 | 19 | const ans = []; 20 | for (let i = 0; i < k; i++) { 21 | ans.push(arr[i][0]); 22 | } 23 | return ans; 24 | } -------------------------------------------------------------------------------- /one-question-per-day/20200912/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 本质上遍历二叉树 3 | * 4 | * BFS 5 | * 6 | * 时间复杂度 O(n) 7 | * 空间复杂度 O(n) 8 | */ 9 | const averageOfLevels = function(root) { 10 | const queue = [root]; 11 | const ans = []; 12 | 13 | while (queue.length) { 14 | const size = queue.length; 15 | let sum = 0; 16 | for (let i = 0; i < size; i++) { 17 | const item = queue.shift(); 18 | sum += item.val; 19 | item.left && queue.push(item.left); 20 | item.right && queue.push(item.right); 21 | } 22 | ans.push(sum / size); 23 | } 24 | 25 | return ans; 26 | }; -------------------------------------------------------------------------------- /Binary-Tree/337/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/house-robber-iii/ 3 | * 4 | * 337. House Robber III 5 | * 6 | * [Medium] 7 | * 8 | * 简单的递归处理 9 | */ 10 | 11 | const rob = root => { 12 | if (!root) { 13 | return 0 14 | } 15 | 16 | const rv = root.val 17 | 18 | const ll = root.left ? rob(root.left.left) : 0 19 | const lr = root.left ? rob(root.left.right) : 0 20 | const rl = root.right ? rob(root.right.left) : 0 21 | const rr = root.right ? rob(root.right.right) : 0 22 | 23 | return Math.max(rv + ll + lr + rl + rr, rob(root.left) + rob(root.right)) 24 | } 25 | -------------------------------------------------------------------------------- /Greedy/944/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/delete-columns-to-make-sorted/ 3 | * 4 | * 944. 删列造序 5 | * 6 | * Easy 7 | * 8 | * Greedy 9 | * 10 | * 100ms 96.97% 11 | * 39.5mb 25.00% 12 | * 13 | * 每一列尽最大的可能比较更多元素 14 | */ 15 | const minDeletionSize = A => { 16 | const rows = A.length 17 | const cols = A[0].length 18 | let ans = 0 19 | for (let col = 0; col < cols; col++) { 20 | for (let row = 0; row < rows - 1; row++) { 21 | if (A[row][col] > A[row + 1][col]) { 22 | ans++ 23 | break 24 | } 25 | } 26 | } 27 | return ans 28 | } -------------------------------------------------------------------------------- /Linked-List/83/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/ 3 | * 4 | * 83. 删除排序链表中的重复元素 5 | * 6 | * Easy 7 | * 8 | * 80ms 61.04% 9 | * 36.4mb 10.74% 10 | */ 11 | const deleteDuplicates = head => { 12 | if (!head || !head.next) { 13 | return head; 14 | } 15 | let currentHead = head; 16 | while (currentHead && currentHead.next) { 17 | if (currentHead.val === currentHead.next.val) { 18 | currentHead.next = currentHead.next.next; 19 | } else { 20 | currentHead = currentHead.next; 21 | } 22 | } 23 | return head; 24 | } -------------------------------------------------------------------------------- /Rank/181/5178/solution1.js: -------------------------------------------------------------------------------- 1 | const sumFourDivisors = nums => { 2 | let ans = 0; 3 | for (let i = 0; i < nums.length; i++) { 4 | const temp = help(nums[i]); 5 | if (temp.size === 4) { 6 | ans += Array.from(temp.values()).reduce((a, b) => a + b, 0); 7 | } 8 | } 9 | return ans; 10 | } 11 | 12 | function help(num) { 13 | const max = Math.floor(Math.sqrt(num)); 14 | const ans = new Set(); 15 | let min = 1; 16 | while (min <= max) { 17 | if ((num / min) % 1 === 0) { 18 | ans.add(min); 19 | ans.add(num / min); 20 | } 21 | min++; 22 | } 23 | return ans; 24 | } -------------------------------------------------------------------------------- /Rank/184/1411/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/number-of-ways-to-paint-n-x-3-grid/submissions/ 3 | * 4 | * 给 N x 3 网格图涂色的方案数 5 | * 6 | * Hard 7 | * 8 | * 68ms 9 | * 35.5mb 10 | * 11 | * 时间复杂度 O(n) 12 | * 空间复杂度 O(1) 13 | */ 14 | const MOD = 1e9 + 7 15 | const numOfWays = n => { 16 | if (n < 1) { 17 | return 0; 18 | } 19 | let x = 6; 20 | let y = 6; 21 | 22 | for (let i = 0; i < n - 1; i++) { 23 | const x1 = (x * 3 + y * 2) % MOD; 24 | const y1 = (x * 2 + y * 2) % MOD; 25 | x = x1; 26 | y = y1; 27 | } 28 | 29 | return (x + y) % MOD; 30 | } -------------------------------------------------------------------------------- /Stack/1021/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/remove-outermost-parentheses/ 3 | * 4 | * 1021. 删除最外层的括号 5 | * 6 | * Easy 7 | * 8 | * 60ms 97.18% 9 | * 36mb 61.54% 10 | */ 11 | const removeOuterParentheses = S => { 12 | let ans = ''; 13 | let count = 0; 14 | for (let i = 0, max = S.length; i < max; i++) { 15 | const str = S[i]; 16 | if (str === '(') { 17 | count++; 18 | } else { 19 | count--; 20 | } 21 | if (!((count === 1 && str === '(') || (count === 0 && str === ')'))) { 22 | ans += str; 23 | } 24 | } 25 | return ans; 26 | } -------------------------------------------------------------------------------- /Two-Points/930/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/binary-subarrays-with-sum/ 3 | * 4 | * 930. 和相同的二元子数组 5 | * 6 | * Medium 7 | * 8 | * 前缀和 9 | * 10 | * 96ms 100.00% 11 | * 42.2mb 25.00% 12 | * 13 | */ 14 | const numSubarraysWithSum = (A, S) => { 15 | const max = A.length 16 | let ans = 0 17 | let record = new Map() 18 | record.set(0, 1) 19 | let sum = 0 20 | for (let item of A) { 21 | sum += item 22 | if (sum >= S) { 23 | ans += record.get(sum - S) || 0 24 | } 25 | record.set(sum, (record.get(sum) || 0) + 1) 26 | } 27 | return ans 28 | } -------------------------------------------------------------------------------- /array/46/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n*n!) 3 | * 空间复杂度:O(n) 4 | */ 5 | const permute = function(nums) { 6 | const ans = []; 7 | const path = []; 8 | 9 | const backtrack = (nums) => { 10 | if (nums.length === 0) { 11 | ans.push([...path]); 12 | return; 13 | } 14 | 15 | for (let i = 0; i < nums.length; i++) { 16 | path.push(nums[i]); 17 | 18 | backtrack([...nums.slice(0, i), ...nums.slice(i + 1)]); 19 | 20 | path.pop(); 21 | } 22 | } 23 | 24 | backtrack(nums); 25 | 26 | return ans; 27 | }; -------------------------------------------------------------------------------- /one-question-per-day/20220105/solution1.js: -------------------------------------------------------------------------------- 1 | 2 | const replaceStr = (pre, next) => { 3 | const str = 'abcdefghijklmnopqrstuvwxyz'; 4 | for (let i = 0; i < str.length; i++) { 5 | if (str[i] !== pre && str[i] !== next) { 6 | return str[i]; 7 | } 8 | } 9 | } 10 | const modifyString = function(s) { 11 | const ans = s.split(''); 12 | 13 | for (let i = 0; i < ans.length; i++) { 14 | if (ans[i] === '?') { 15 | // 进行替换操作 16 | ans[i] = replaceStr(ans[i - 1], ans[i + 1]); 17 | } 18 | } 19 | 20 | return ans.join(''); 21 | }; -------------------------------------------------------------------------------- /array/888/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/fair-candy-swap/ 3 | * 4 | * 888. Fair Candy Swap 5 | * 6 | * Easy 7 | */ 8 | const fairCandySwap = (A, B) => { 9 | const sum1 = A.reduce((x, y) => x + y, 0) 10 | const sum2 = B.reduce((x, y) => x + y, 0) 11 | 12 | if ((sum1 - sum2) % 2 !== 0) { 13 | return [] 14 | } 15 | const diff = (sum1 - sum2) / 2 16 | const cache = new Set(B) 17 | for (let i = 0; i < A.length; i++) { 18 | const x = A[i] 19 | const y = x - diff 20 | if (cache.has(y)) { 21 | return [x, y] 22 | } 23 | } 24 | 25 | return [] 26 | } -------------------------------------------------------------------------------- /one-question-per-day/20200906/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * dfs 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | */ 6 | const levelOrderBottom = function(root) { 7 | if (!root) { 8 | return []; 9 | } 10 | const ans = []; 11 | dfs(root, 0); 12 | return ans.reverse(); 13 | function dfs(root, depth) { 14 | if (!root) { 15 | return; 16 | } 17 | if (!ans[depth]) { 18 | ans[depth] = []; 19 | } 20 | ans[depth].push(root.val); 21 | if (root.left) { 22 | dfs(root.left, depth + 1); 23 | } 24 | if (root.right) { 25 | dfs(root.right, depth + 1); 26 | } 27 | } 28 | }; -------------------------------------------------------------------------------- /Binary-Search/153/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/ 3 | * 4 | * 153. 寻找旋转排序数组中的最小值 5 | * 6 | * Medium 7 | * 8 | * 一类题型 33 81 9 | * 10 | * 这道题目相对比较简单 11 | * 12 | * 68ms 99.04% 13 | * 33.6mb 63.89% 14 | * 15 | */ 16 | const findMin = nums => { 17 | let start = 0 18 | let end = nums.length - 1 19 | 20 | while (start < end) { 21 | const mid = Math.floor(start + (end - start) / 2) 22 | if (nums[mid] > nums[end]) { 23 | start = mid + 1 24 | } else { 25 | end = mid 26 | } 27 | } 28 | return nums[start] 29 | } -------------------------------------------------------------------------------- /Binary-Tree/105/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(n) 4 | */ 5 | const buildTree = (preorder, inorder) => { 6 | // 递归中止条件 7 | if (preorder.length === 0) { 8 | return null; 9 | } 10 | 11 | const rootElement = preorder[0]; 12 | 13 | const root = new TreeNode(rootElement); 14 | 15 | const rootElementIndex = inorder.indexOf(rootElement); 16 | 17 | root.left = buildTree(preorder.slice(1, rootElementIndex + 1), inorder.slice(0, rootElementIndex)); 18 | 19 | root.right = buildTree(preorder.slice(rootElementIndex + 1), inorder.slice(rootElementIndex + 1)); 20 | 21 | return root; 22 | } -------------------------------------------------------------------------------- /Binary-Tree/783/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/ 3 | * 4 | * 783. 二叉搜索树结点最小距离 5 | * 6 | * Easy 7 | * 8 | * 80ms 80.22% 9 | * 34.6mb 45.00% 10 | */ 11 | const minDiffInBST = root => { 12 | let ans = Number.MAX_SAFE_INTEGER 13 | let pre = null 14 | inOrder(root) 15 | return ans 16 | 17 | function inOrder(root) { 18 | if (!root) { 19 | return 20 | } 21 | inOrder(root.left) 22 | if (pre) { 23 | ans = Math.min(ans, Math.abs(pre - root.val)) 24 | } 25 | pre = root.val 26 | inOrder(root.right) 27 | } 28 | } -------------------------------------------------------------------------------- /array/219/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/contains-duplicate-ii/ 3 | * 4 | * 219. Contains Duplicate II 5 | * 6 | * Easy 7 | * 8 | * 52ms 100% 9 | * 40.1mb 67.12% 10 | */ 11 | const containsNearbyDuplicate = (nums, k) => { 12 | const len = nums.length 13 | const m = new Map() 14 | 15 | for (let i = 0; i < len; i++) { 16 | const num = nums[i] 17 | if (!m.has(num)) { 18 | m.set(num, i) 19 | continue 20 | } 21 | 22 | const j = m.get(num) 23 | if (i - j <= k) { 24 | return true 25 | } 26 | m.set(num, i) 27 | } 28 | return false 29 | } -------------------------------------------------------------------------------- /dynamic-programming/790/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/domino-and-tromino-tiling/ 3 | * 4 | * 790. Domino and Tromino Tiling 5 | * 6 | * Medium 7 | */ 8 | const kMod = 10 ** 9 + 7 9 | const numTilings = N => { 10 | const dp = [] 11 | dp[0] = [1, 0, 0] 12 | dp[1] = [1, 0, 0] 13 | 14 | for (let i = 2; i <= N; i++) { 15 | dp[i] = [] 16 | 17 | dp[i][0] = (dp[i - 1][0] + dp[i - 2][0] + dp[i - 1][1] + dp[i - 1][2]) % kMod 18 | dp[i][1] = (dp[i - 2][0] + dp[i - 1][2]) % kMod 19 | dp[i][2] = (dp[i - 2][0] + dp[i - 1][1]) % kMod 20 | } 21 | 22 | return dp[N][0] 23 | } -------------------------------------------------------------------------------- /Binary-Search/162/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/find-peak-element/ 3 | * 4 | * 162. Find Peak Element 5 | * 6 | * Medium 7 | * 8 | * 52ms 90.60% 9 | * 33.9mb 55.70% 10 | * 11 | */ 12 | const findPeakElement = nums => { 13 | const max = nums.length 14 | let first = 0 15 | let last = max - 1 16 | 17 | while (first < last) { 18 | const mid = Math.floor(first + (last - first) / 2) 19 | if (nums[mid] < nums[mid + 1]) { 20 | first = mid + 1 21 | } else { 22 | last = mid 23 | } 24 | } 25 | return first 26 | } -------------------------------------------------------------------------------- /Binary-Search/852/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/peak-index-in-a-mountain-array/ 3 | * 4 | * 852. Peak Index in a Mountain Array 5 | * 6 | * Easy 7 | * 8 | * 48ms 98.70% 9 | * 34.3mb 72.89% 10 | */ 11 | const peakIndexInMountainArray = A => { 12 | return binarySearch(A, 0, A.length - 1) 13 | } 14 | 15 | function binarySearch(array, start, end) { 16 | while (start < end) { 17 | const mid = Math.floor(start + (end - start) / 2) 18 | if (array[mid + 1] > array[mid]) { 19 | start = mid + 1 20 | } else { 21 | end = mid 22 | } 23 | } 24 | 25 | return start 26 | } -------------------------------------------------------------------------------- /Rank/183/183-1/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/minimum-subsequence-in-non-increasing-order/submissions/ 3 | * 4 | * 非递增顺序的最小子序列 5 | * 6 | * Easy 7 | * 8 | * 80ms 100% 9 | * 35.5mb 100.00% 10 | */ 11 | const minSubsequence = nums => { 12 | nums.sort((a, b) => b - a); 13 | 14 | const sum = nums.reduce((a, b) => a + b, 0); 15 | 16 | let ans = []; 17 | let subSum = 0; 18 | for (let i = 0, max = nums.length; i < max; i++) { 19 | subSum += nums[i]; 20 | ans.push(nums[i]); 21 | if (subSum > sum - subSum) { 22 | return ans; 23 | } 24 | } 25 | return ans; 26 | } -------------------------------------------------------------------------------- /array/448/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 448. Find All Numbers Disappeared in an Array 3 | * 4 | * https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 5 | * 6 | * Easy 7 | */ 8 | const findDisappearedNumbers = nums => { 9 | const max = nums.length 10 | if (!max) { 11 | return [] 12 | } 13 | 14 | const temp = [] 15 | 16 | for (let i = 0; i < max; i++) { 17 | const num = nums[i] 18 | temp[num - 1] = num 19 | } 20 | 21 | const ans = [] 22 | for (let i = 0; i < max; i++) { 23 | if (temp[i] == null) { 24 | ans.push(i + 1) 25 | } 26 | } 27 | 28 | return ans 29 | } -------------------------------------------------------------------------------- /array/724/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/find-pivot-index/ 3 | * 4 | * 724. Find Pivot Index 5 | * 6 | * Easy 7 | * 8 | * 92ms 43.93ms 9 | * 40.2mb 17.78% 10 | */ 11 | const pivotIndex = nums => { 12 | const max = nums.length 13 | const preSum = [] 14 | const sufSum = [] 15 | for (let i = 0; i < max; i++) { 16 | preSum[i] = (preSum[i - 1] || 0) + nums[i] 17 | sufSum[max - i - 1] = (sufSum[max - i] || 0) + nums[max - i - 1] 18 | } 19 | 20 | for (let i = 0; i < max; i++) { 21 | if (preSum[i] === sufSum[i]) { 22 | return i 23 | } 24 | } 25 | return -1 26 | } -------------------------------------------------------------------------------- /string/392/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/is-subsequence/ 3 | * 4 | * 392. Is Subsequence 5 | * 6 | * Medium 7 | * 8 | * 60ms 96.43% 9 | * 10 | * 37.9MB 89.16% 11 | * 12 | * O(n) 13 | */ 14 | const isSubsequence = (s, t) => { 15 | 16 | let fromIndex = 0 17 | 18 | for (let i = 0, max = s.length; i < max; i++) { 19 | const item = s[i] 20 | const index = t.indexOf(item, fromIndex) 21 | if (index < 0) { 22 | return false 23 | } else { 24 | fromIndex = index + 1 25 | } 26 | } 27 | return true 28 | } -------------------------------------------------------------------------------- /Binary-Tree/1008/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(n) 4 | */ 5 | const bstFromPreorder = preorder => { 6 | // 终止条件 7 | if (!preorder.length) { 8 | return null 9 | } 10 | const root = new TreeNode(preorder[0]) 11 | 12 | // 寻找左右子树的分割点 13 | let index = 0 14 | const max = preorder.length 15 | for (let i = 0; i < max; i++) { 16 | if (preorder[0] >= preorder[i]) { 17 | index = i 18 | continue 19 | } 20 | break 21 | } 22 | root.left = bstFromPreorder(preorder.slice(1, index + 1)) 23 | root.right = bstFromPreorder(preorder.slice(index + 1)) 24 | return root 25 | } -------------------------------------------------------------------------------- /Binary-Tree/145/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ 3 | * 4 | * 145. 二叉树的后序遍历 5 | * 6 | * Hard 7 | * 8 | * 迭代的方式处理 9 | * 10 | * 栈先进后出 11 | * 12 | * 76ms 84.00% 13 | * 33.8mb 18.03% 14 | */ 15 | const postorderTraversal = root => { 16 | const ans = [] 17 | if (!root) { 18 | return ans 19 | } 20 | const stack = [root] 21 | while (stack.length) { 22 | const item = stack.pop() 23 | if (item) { 24 | ans.push(item.val) 25 | stack.push(item.left) 26 | stack.push(item.right) 27 | } 28 | } 29 | return ans.reverse() 30 | } -------------------------------------------------------------------------------- /Binary-Tree/508/solution1.js: -------------------------------------------------------------------------------- 1 | const findFrequentTreeSum = root => { 2 | const sumList = {} 3 | 4 | order(root) 5 | 6 | const max = Math.max.apply(this, Object.values(sumList)) 7 | 8 | const ans = [] 9 | for (let key in sumList) { 10 | if (sumList[key] === max) { 11 | ans.push(key) 12 | } 13 | } 14 | 15 | return ans 16 | 17 | function order (root) { 18 | if (!root) { 19 | return 0 20 | } 21 | 22 | const curSum = root.val + order(root.left) + order(root.right) 23 | 24 | sumList[curSum] || (sumList[curSum] = 0) 25 | sumList[curSum]++ 26 | return curSum 27 | } 28 | } -------------------------------------------------------------------------------- /Stack/496/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/next-greater-element-i/ 3 | * 4 | * 496. 下一个更大元素 I 5 | * 6 | * Easy 7 | * 8 | * 72ms 77.24% 9 | * 34.9mb 79.10% 10 | * 11 | * O(n*m) 12 | */ 13 | const nextGreaterElement = (num1, num2) => { 14 | const ans = []; 15 | for (let i = 0, max = num1.length; i < max; i++) { 16 | const num = num1[i]; 17 | ans[i] = -1; 18 | let index = num2.length - 1; 19 | while (index && num2[index] !== num) { 20 | if (num2[index] > num) { 21 | ans[i] = num2[index]; 22 | } 23 | index--; 24 | } 25 | } 26 | return ans; 27 | } -------------------------------------------------------------------------------- /Binary-Tree/102/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const levelOrder = root => { 6 | if (!root) { 7 | return []; 8 | } 9 | 10 | const ans = []; 11 | let queue = [root]; 12 | 13 | while (queue.length) { 14 | const len = queue.length 15 | const item = []; 16 | for (let i = 0; i < len; i++) { 17 | const currentRoot = queue.shift(); 18 | item.push(currentRoot.val); 19 | 20 | currentRoot.left && queue.push(currentRoot.left); 21 | currentRoot.right && queue.push(currentRoot.right); 22 | } 23 | 24 | ans.push(item); 25 | } 26 | 27 | return ans; 28 | } -------------------------------------------------------------------------------- /Recursion/779/solution3.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/k-th-symbol-in-grammar/ 3 | * 4 | * 779. 第K个语法符号 5 | * 6 | * Medium 7 | * 8 | * heap out of memory 9 | * 10 | */ 11 | const kthGrammar = (N, K) => { 12 | let str = '0'; 13 | if (N > 1) { 14 | for (let i = 1; i < N; i++) { 15 | let newStr = '' 16 | for (let i = 0; i < str.length; i++) { 17 | const s = str[i] 18 | if (s === '0') { 19 | newStr += '01'; 20 | } else { 21 | newStr += '10'; 22 | } 23 | } 24 | str = newStr; 25 | } 26 | } 27 | return str[K - 1]; 28 | } 29 | -------------------------------------------------------------------------------- /Two-Points/532/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/k-diff-pairs-in-an-array/ 3 | * 4 | * 532. 数组中的K-diff数对 5 | * 6 | * Easy 7 | */ 8 | const findPairs = (nums, k) => { 9 | const record = new Set() 10 | for (let i = 0, max = nums.length; i < max - 1; i++) { 11 | for (let j = i + 1; j < max; j++) { 12 | const x = nums[i] 13 | const y = nums[j] 14 | if (Math.abs(x - y) === k) { 15 | if (x > y) { 16 | record.add(`${x}-${y}`) 17 | } else { 18 | record.add(`${y}-${x}`) 19 | } 20 | } 21 | } 22 | } 23 | 24 | return record.size 25 | } -------------------------------------------------------------------------------- /Two-Points/713/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/subarray-product-less-than-k/ 3 | * 4 | * 713. 乘积小于K的子数组 5 | * 6 | * Medium 7 | * 8 | * 滑动窗口 9 | * 10 | * 116ms 100.00% 11 | * 41.9mb 62.50% 12 | */ 13 | const numSubarrayProductLessThanK = (nums, k) => { 14 | if (k <= 1) { 15 | return 0 16 | } 17 | let ans = 0 18 | let p = 1 19 | let start = 0 20 | let end = 0 21 | const max = nums.length 22 | for (;end < max; end++) { 23 | p *= nums[end] 24 | while (p >= k) { 25 | p /= nums[start++] 26 | } 27 | ans += end - start + 1 28 | } 29 | 30 | return ans 31 | } -------------------------------------------------------------------------------- /array/119/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/pascals-triangle-ii/ 3 | * 4 | * 119. Pascal's Triangle II 5 | * 6 | * Easy 7 | * 8 | * 44ms 99.54% 9 | * 33.8mb 86.90% 10 | */ 11 | const getRow = index => { 12 | let ans = [1] 13 | 14 | if (index === 0) { 15 | return ans 16 | } 17 | ans.push(1) 18 | 19 | if (index === 1) { 20 | return ans 21 | } 22 | 23 | for (let i = 2; i <= index; i++) { 24 | const temp = [1] 25 | for (let j = 1; j < i; j++) { 26 | temp[j] = ans[j - 1] + ans[j] 27 | } 28 | temp.push(1) 29 | ans = temp 30 | } 31 | 32 | return ans 33 | } -------------------------------------------------------------------------------- /dynamic-programming/377/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * https://leetcode.com/problems/combination-sum-iv/ 4 | * 5 | * 377. Combination Sum IV 6 | * 7 | * Medium 8 | * 9 | * TLES 10 | * 11 | * 时间复杂度 O(len^(target / min)) 12 | */ 13 | const combinationSum4 = (nums, target) => { 14 | let ans = 0 15 | dfs(nums, 0) 16 | 17 | return ans 18 | 19 | function dfs (nums, sum) { 20 | if (sum === target) { 21 | ans++ 22 | return 23 | } 24 | if (sum > target) { 25 | return 26 | } 27 | 28 | for (let i = 0; i < nums.length; i++) { 29 | dfs(nums, sum + nums[i]) 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /one-question-per-day/20200909/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 回溯算法 3 | */ 4 | const combinationSum = function(candidates, target) { 5 | const ans = []; 6 | dfs([], target, 0); 7 | return ans; 8 | function dfs(current, sum, index) { 9 | if (index === candidates.length) { 10 | return; 11 | } 12 | if (0 === sum) { 13 | ans.push([...current]); 14 | return; 15 | } 16 | 17 | dfs(current, sum, index + 1); 18 | if (sum - candidates[index] >= 0) { 19 | current.push(candidates[index]); 20 | dfs(current, sum - candidates[index], index); 21 | current.pop(); 22 | } 23 | } 24 | }; -------------------------------------------------------------------------------- /Linked-List/92/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const reverseBetween = (head, m, n) => { 6 | const dummyHead = new ListNode(null); 7 | dummyHead.next = head; 8 | 9 | let preHead = dummyHead; 10 | for (let i = 0; i < m - 1; i++) { 11 | preHead = preHead.next; 12 | } 13 | 14 | let current = preHead.next; 15 | let next = null; 16 | 17 | for (let i = 0; i < n - m; i++) { 18 | next = current.next; 19 | current.next = next.next; 20 | next.next = preHead.next; 21 | preHead.next = next; 22 | } 23 | 24 | return dummyHead.next; 25 | } -------------------------------------------------------------------------------- /Rank/182/5369/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/count-number-of-teams/ 3 | * 4 | * 5369. 统计作战单位数 5 | * 6 | * Medium 7 | */ 8 | const numTeams = rating => { 9 | let ans = 0; 10 | const max = rating.length; 11 | for (let i = 0; i < max; i++) { 12 | for (let j = i + 1; j < max; j++) { 13 | for (let k = j + 1; k < max; k++) { 14 | if (rating[i] < rating[j] && rating[j] < rating[k]) { 15 | ans++; 16 | } 17 | 18 | if (rating[i] > rating[j] && rating[j] > rating[k]) { 19 | ans++; 20 | } 21 | } 22 | } 23 | } 24 | 25 | return ans; 26 | } -------------------------------------------------------------------------------- /array/643/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-average-subarray-i/ 3 | * 4 | * 643. Maximum Average Subarray I 5 | * 6 | * Easy 7 | * 8 | * 76ms 100% 9 | * 42.6mb 59.56% 10 | */ 11 | const findMaxAverage = (nums, k) => { 12 | 13 | const max = nums.length 14 | 15 | let sum = 0 16 | for (let i = 0;i < k; i++) { 17 | sum += nums[i] 18 | } 19 | 20 | let curSum = sum 21 | 22 | 23 | for (let i = k; i < max; i++) { 24 | const pre = nums[i - k] 25 | const cur = nums[i] 26 | curSum = curSum - pre + cur 27 | sum = Math.max(sum, curSum) 28 | } 29 | 30 | return sum / k 31 | } -------------------------------------------------------------------------------- /array/88/solution3.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 双指针 去掉一次数组遍历 3 | * 时间复杂度 O(m + n) 4 | * 空间复杂度 O(1) 5 | */ 6 | const merge = function(nums1, m, nums2, n) { 7 | let p1 = m - 1; 8 | let p2 = n - 1; 9 | let tail = m + n - 1; 10 | let current; 11 | while (p1 >= 0 || p2 >= 0) { 12 | if (p1 === -1) { 13 | current = nums2[p2--]; 14 | } else if (p2 === -1) { 15 | current = nums1[p1--]; 16 | } else if (nums1[p1] > nums2[p2]) { 17 | current = nums1[p1--]; 18 | } else { 19 | current = nums2[p2--]; 20 | } 21 | nums1[tail--] = current; 22 | } 23 | } -------------------------------------------------------------------------------- /dynamic-programming/198/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/house-robber/ 3 | * 4 | * 198. House Robber 5 | * 6 | * dp[i] 表示第i个房屋可以抢劫的最大金额 7 | * 8 | * dp[0] = 0 9 | * dp[1] = nums[0] 10 | * 11 | * dp[i + 1] = Math.max(dp[i], dp[i - 1] + nums[i]) 12 | */ 13 | 14 | const rob = nums => { 15 | const max = nums.length 16 | if (!max) { 17 | return 0 18 | } 19 | 20 | const dp = new Array(max + 1).fill(0) 21 | 22 | dp[1] = nums[0] 23 | 24 | for (let i = 1; i < max; i++) { 25 | const money = nums[i] 26 | dp[i + 1] = Math.max(dp[i], dp[i - 1] + money) 27 | } 28 | return dp[max] 29 | } -------------------------------------------------------------------------------- /string/20/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const isValid = function(s) { 6 | const stack = []; 7 | for (let i = 0; i < s.length; i++) { 8 | const item = s[i]; 9 | if (item === ')' && stack[stack.length - 1] === '(') { 10 | stack.pop(); 11 | } else if (item === '}' && stack[stack.length - 1] === '{') { 12 | stack.pop(); 13 | } else if (item === ']' && stack[stack.length - 1] === '[') { 14 | stack.pop(); 15 | } else { 16 | stack.push(item); 17 | } 18 | } 19 | 20 | return stack.length === 0; 21 | }; -------------------------------------------------------------------------------- /Binary-Tree/617/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/merge-two-binary-trees/ 3 | * 4 | * 617. 合并二叉树 5 | * 6 | * Easy 7 | * 8 | * 120ms 97.63% 9 | * 40.9mb 22.34% 10 | */ 11 | const mergeTrees = (t1, t2) => { 12 | if (!t1 && !t2) { 13 | return null 14 | } 15 | if (!t1) { 16 | return t2 17 | } 18 | if (!t2) { 19 | return t1 20 | } 21 | 22 | const leftTree = mergeTrees(t1.left, t2.left) 23 | const rightTree = mergeTrees(t1.right, t2.right) 24 | 25 | const root = new TreeNode(t1.val + t2.val) 26 | 27 | root.left = leftTree 28 | root.right = rightTree 29 | 30 | return root 31 | } -------------------------------------------------------------------------------- /Rank/biweekly/22/5350/solution1.js: -------------------------------------------------------------------------------- 1 | const getKth = (lo, hi, k) => { 2 | const ans = []; 3 | for (let i = lo; i <= hi; i++) { 4 | const step = computedStep(i); 5 | ans.push([i, step]); 6 | } 7 | 8 | ans.sort((a, b) => { 9 | if (a[1] !== b[1]) { 10 | return a[1] - b[1]; 11 | } 12 | return a[0] - b[0]; 13 | }) 14 | 15 | return ans[k - 1][0]; 16 | } 17 | 18 | function computedStep(number) { 19 | let ans = 0; 20 | while (number !== 1) { 21 | if (number % 2 === 0) { 22 | number /= 2; 23 | } else { 24 | number = number * 3 + 1; 25 | } 26 | ans++; 27 | } 28 | return ans; 29 | } -------------------------------------------------------------------------------- /Recursion/779/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/k-th-symbol-in-grammar/ 3 | * 4 | * 779. 第K个语法符号 5 | * 6 | * Medium 7 | * 8 | * heap out of memory 9 | */ 10 | const kthGrammar = (N, K) => { 11 | const str = getString(N); 12 | return str[K - 1]; 13 | } 14 | 15 | function getString(N) { 16 | if (N === 1) { 17 | return '0'; 18 | } 19 | const str = getString(N - 1); 20 | let newStr = '' 21 | for (let i = 0; i < str.length; i++) { 22 | const s = str[i] 23 | if (s === '0') { 24 | newStr += '01'; 25 | } else { 26 | newStr += '10'; 27 | } 28 | } 29 | return newStr; 30 | } -------------------------------------------------------------------------------- /dynamic-programming/152/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/maximum-product-subarray/ 3 | * 4 | * 152. Maximum Product Subarray 5 | * 6 | * Medium 7 | * 8 | * TLE 9 | */ 10 | const maxProduct = nums => { 11 | const max = nums.length 12 | if (!max) { 13 | return 0 14 | } 15 | let minMul = nums[0] 16 | let maxMul = nums[0] 17 | let ans = nums[0] 18 | for (let i = 1; i < max; i++) { 19 | let a = minMul * nums[i] 20 | let b = maxMul * nums[i] 21 | maxMul = Math.max(a, b, nums[i]) 22 | minMul = Math.min(a, b, nums[i]) 23 | ans = Math.max(ans, maxMul) 24 | } 25 | return ans 26 | } -------------------------------------------------------------------------------- /dynamic-programming/494/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/target-sum/ 3 | * 4 | * 494. Target Sum 5 | * 6 | * Medium 7 | */ 8 | const findTargetSumWays = (nums, S) => { 9 | let ans = 0 10 | let max = nums.length - 1 11 | dfs(0, S) 12 | return ans 13 | function dfs (startIndex, S) { 14 | if (startIndex === max) { 15 | if (S - nums[startIndex] === 0) { 16 | ans++ 17 | } 18 | if (S + nums[startIndex] === 0) { 19 | ans++ 20 | } 21 | return 22 | } 23 | dfs(startIndex + 1, S - nums[startIndex]) 24 | dfs(startIndex + 1, S + nums[startIndex]) 25 | } 26 | } -------------------------------------------------------------------------------- /one-question-per-day/20220101/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n*m) 3 | * 空间复杂度 O(n*m) 4 | * @param {*} original 5 | * @param {*} m s 6 | * @param {*} n 7 | * @returns 8 | */ 9 | const construct2DArray = function(original, m, n) { 10 | const maxLength = original.length; 11 | if (m * n !== maxLength) { 12 | return []; 13 | } 14 | 15 | const ans = []; 16 | for (let i = 0; i < maxLength; i++) { 17 | const index = Math.floor(i / n); 18 | if (!ans[index]) { 19 | ans[index] = []; 20 | } 21 | 22 | ans[index].push(original[i]); 23 | } 24 | 25 | return ans; 26 | }; -------------------------------------------------------------------------------- /Binary-Tree/113/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | */ 5 | const pathSum = (root, sum) => { 6 | const ans = []; 7 | const path = []; 8 | const dfs = (root, sum) => { 9 | if (!root) { 10 | return; 11 | } 12 | 13 | path.push(root.val); 14 | 15 | const _sum = sum - root.val; 16 | if (!root.left && !root.right && _sum === 0) { 17 | ans.push([...path]); 18 | } 19 | 20 | dfs(root.left, _sum); 21 | dfs(root.right, _sum); 22 | path.pop(); 23 | } 24 | 25 | dfs(root, sum); 26 | 27 | return ans; 28 | } -------------------------------------------------------------------------------- /Binary-Tree/889/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(n) 4 | * 5 | */ 6 | const constructFromPrePost = (pre, post) => { 7 | if (pre.length === 0) { 8 | return null 9 | } 10 | // 拿出根节点 11 | const rootValue = pre.shift() 12 | post.pop() 13 | 14 | const root = new TreeNode(rootValue) 15 | 16 | if (pre.length > 0) { 17 | // 左子树的根节点 18 | const l = pre[0] 19 | const lIndex = post.indexOf(l) 20 | 21 | root.left = constructFromPrePost(pre.slice(0, lIndex + 1), post.slice(0, lIndex + 1)) 22 | root.right = constructFromPrePost(pre.slice(lIndex + 1), post.slice(lIndex + 1)) 23 | } 24 | return root 25 | } -------------------------------------------------------------------------------- /dynamic-programming/523/readme.md: -------------------------------------------------------------------------------- 1 | # [523. Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) 2 | 3 | ### 一、解题思路 4 | 5 |   这道题目比较简单,依次验证以每个元素为起点的子数组是否满足条件即可: 6 | 7 | ### 二、代码实现 8 | 9 | ```JavaScript 10 | const checkSubarraySum = (nums, k) => { 11 | 12 | for (let i = 0, max = nums.length; i < max; i++) { 13 | let sum = nums[i] 14 | for (let j = i + 1; j < max; j++) { 15 | sum += nums[j] 16 | if (sum === k) { 17 | return true 18 | } 19 | if (k !== 0 && sum % k === 0) { 20 | return true 21 | } 22 | } 23 | } 24 | 25 | return false 26 | } 27 | ``` -------------------------------------------------------------------------------- /math/204/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/count-primes/ 3 | * 4 | * 204. Count Primes 5 | * 6 | * Medium 7 | * 8 | * TLE 9 | */ 10 | const countPrimes = n => { 11 | const dp = new Array(n) 12 | dp[0] = 0 13 | dp[1] = 0 14 | for (let i = 2; i <= n; i++) { 15 | dp[i] = dp[i - 1] 16 | if (isPrimes(i - 1)) { 17 | dp[i] += 1 18 | } 19 | } 20 | return dp[n] 21 | } 22 | 23 | function isPrimes (n) { 24 | if (n === 1) { 25 | return false 26 | } 27 | for (let i = 2; i <= Math.sqrt(n); i++) { 28 | if (n % i === 0) { 29 | return false 30 | } 31 | } 32 | return true 33 | } -------------------------------------------------------------------------------- /one-question-per-day/20200906/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * BFS 队列 3 | */ 4 | const levelOrderBottom = function(root) { 5 | if (!root) { 6 | return []; 7 | } 8 | const queue = [root]; 9 | const ans = []; 10 | while (queue.length) { 11 | const current = []; 12 | const count = queue.length; 13 | for (let i = 0; i < count; i++) { 14 | const node = queue.shift(); 15 | current.push(node.val); 16 | if (node.left) { 17 | queue.push(node.left); 18 | } 19 | if (node.right) { 20 | queue.push(node.right); 21 | } 22 | } 23 | ans.unshift(current); 24 | } 25 | return ans; 26 | }; -------------------------------------------------------------------------------- /one-question-per-day/20220109/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(1) 4 | * @param {*} releaseTimes 5 | * @param {*} keysPressed 6 | * @returns 7 | */ 8 | const slowestKey = function(releaseTimes, keysPressed) { 9 | let ans = keysPressed[0]; 10 | let max = releaseTimes[0]; 11 | 12 | for (let i = 1; i < releaseTimes.length; i++) { 13 | const time = releaseTimes[i] - releaseTimes[i - 1]; 14 | const key = keysPressed[i]; 15 | 16 | if (time > max || (time === max && key > ans)) { 17 | ans = key; 18 | max = time; 19 | } 20 | } 21 | 22 | return ans; 23 | }; -------------------------------------------------------------------------------- /Binary-Tree/110/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/balanced-binary-tree/ 3 | * 4 | * 110. 平衡二叉树 5 | * 6 | * Easy 7 | * 8 | * 96ms 95.09% 9 | * 37.9mb 15.84% 10 | */ 11 | const isBalanced = root => { 12 | if (!root) { 13 | return true 14 | } 15 | 16 | const leftHeight = getHeight(root.left) 17 | const rightHeight = getHeight(root.right) 18 | 19 | return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right) 20 | } 21 | 22 | function getHeight(root) { 23 | if (!root) { 24 | return 0 25 | } 26 | return Math.max(getHeight(root.left), getHeight(root.right)) + 1 27 | } -------------------------------------------------------------------------------- /Binary-Tree/971/solution1.js: -------------------------------------------------------------------------------- 1 | const flipMatchVoyage = (root, voyage) => { 2 | let ans = [] 3 | let position = 0 4 | 5 | preOrder(root, voyage) 6 | 7 | return ans 8 | 9 | function preOrder (root, voyage) { 10 | if (!root) { 11 | return 12 | } 13 | if (root.val !== voyage[position++]) { 14 | ans = [-1] 15 | return 16 | } 17 | if (root.left && root.left.val !== voyage[position]) { 18 | ans.push(root.val) 19 | preOrder(root.right, voyage) 20 | preOrder(root.left, voyage) 21 | } else { 22 | preOrder(root.left, voyage) 23 | preOrder(root.right, voyage) 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /dynamic-programming/122/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 3 | * 4 | * 122. Best Time to Buy and Sell Stock II 5 | * 6 | * Easy 7 | * 8 | */ 9 | const maxProfit = prices => { 10 | const max = prices.length 11 | if (max < 2) { 12 | return 0 13 | } 14 | 15 | const sell = [0] // 当前获取的最大收益 16 | const hold = [-prices[0]] // 当前持有的最大收益 17 | 18 | for (let i = 1; i < max; i++) { 19 | const price = prices[i] 20 | sell[i] = Math.max(sell[i - 1], hold[i - 1] + price) 21 | hold[i] = Math.max(hold[i - 1], sell[i - 1] - price) 22 | } 23 | 24 | return sell[max - 1] 25 | } -------------------------------------------------------------------------------- /one-question-per-day/20210823/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number} 4 | */ 5 | var getMaximumGenerated = function(n) { 6 | let ans = Number.MIN_SAFE_INTEGER; 7 | const list = []; 8 | for (let i = 0; i <= n; i++) { 9 | let item; 10 | if (i === 0) { 11 | item = 0; 12 | } else if (i === 1) { 13 | item = 1; 14 | } else if (i % 2 === 0){ 15 | item = list[Math.floor(i / 2)]; 16 | } else { 17 | item = list[Math.floor((i - 1) / 2)] + list[Math.floor((i - 1) / 2 + 1)]; 18 | } 19 | 20 | ans = Math.max(ans, item); 21 | list[i] = item; 22 | } 23 | 24 | return ans; 25 | }; -------------------------------------------------------------------------------- /string/242/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 哈希表 3 | * 时间复杂度:O(max(n, m)) 4 | * 空间复杂度:O(n) 5 | */ 6 | const isAnagram = function(s, t) { 7 | const record = new Map(); 8 | for (let i = 0; i < s.length; i++) { 9 | record.set(s[i], (record.get(s[i]) || 0) + 1); 10 | } 11 | 12 | for (let i = 0; i < t.length; i++) { 13 | const item = t[i]; 14 | if (!record.has(item)) { 15 | return false; 16 | } 17 | 18 | record.set(item, record.get(item) - 1); 19 | if (record.get(item) === 0) { 20 | record.delete(item); 21 | } 22 | } 23 | 24 | return record.size === 0; 25 | }; -------------------------------------------------------------------------------- /Binary-Search/441/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/arranging-coins/ 3 | * 4 | * 441. Arranging Coins 5 | * 6 | * Easy 7 | * 8 | * (k + 1) * k / 2 9 | * 10 | * 60ms 100.00% 11 | * 36mb 90.18% 12 | */ 13 | 14 | const arrangeCoins = n => { 15 | let start = 0 16 | let end = n 17 | while (start < end) { 18 | const mid = Math.floor(start + (end - start) / 2) 19 | const target = (mid + 1) * mid / 2 20 | if (n > target) { 21 | start = mid + 1 22 | } else { 23 | end = mid 24 | } 25 | } 26 | 27 | if ((start + 1) * start / 2 > n) { 28 | return start - 1 29 | } 30 | return start 31 | } -------------------------------------------------------------------------------- /Linked-List/328/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 328. 奇偶链表 3 | * 4 | * https://leetcode-cn.com/problems/odd-even-linked-list/ 5 | * 6 | * Medium 7 | * 8 | * odd 1 2 3 4 5 6 7 9 | * even 2 3 4 5 6 7 10 | * 11 | * 80ms 75.16% 12 | * 36.1mb 65.31% 13 | * 14 | */ 15 | const oddEvenList = head => { 16 | if (!head || !head.next) { 17 | return head; 18 | } 19 | 20 | let odd = head; 21 | let even = head.next; 22 | let temp = even; 23 | 24 | while (even && even.next) { 25 | odd.next = even.next; 26 | odd = odd.next; 27 | even.next = odd.next; 28 | even = even.next; 29 | } 30 | 31 | odd.next = temp; 32 | return head; 33 | } -------------------------------------------------------------------------------- /Rank/biweekly/20/5323/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/sort-integers-by-the-number-of-1-bits/ 3 | * 4 | * 5 | * 5323. 根据数字二进制下 1 的数目排序 6 | * 7 | * Easy 8 | * 9 | * 76ms 100.00% 10 | * 36.1mb 100.00% 11 | * 12 | */ 13 | 14 | const sortByBits = arr => { 15 | arr.sort((a, b) => { 16 | const x = getBitCount(a); 17 | const y = getBitCount(b); 18 | if (x - y == 0) { 19 | return a - b; 20 | } 21 | return x - y 22 | }) 23 | return arr; 24 | } 25 | 26 | function getBitCount(num) { 27 | let ans = 0; 28 | while (num) { 29 | ans++; 30 | num &= (num - 1); 31 | } 32 | return ans; 33 | } -------------------------------------------------------------------------------- /Rank/biweekly/22/5348/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/find-the-distance-value-between-two-arrays/ 3 | * 4 | * 5348. 两个数组间的距离值 5 | * 6 | * Easy 7 | * 8 | * 76ms 100.00% 9 | * 34.8mb 100.00% 10 | */ 11 | const findTheDistanceValue = (arr1, arr2, d) => { 12 | let ans = 0; 13 | for (let i = 0, max = arr1.length; i < max; i++) { 14 | const item = arr1[i]; 15 | let isValid = true; 16 | for (let j = 0, submax = arr2.length; j < submax; j++) { 17 | if (Math.abs(item - arr2[j]) <= d) { 18 | isValid = false; 19 | } 20 | } 21 | 22 | isValid && ans++; 23 | } 24 | 25 | return ans; 26 | } -------------------------------------------------------------------------------- /Two-Points/925/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/long-pressed-name/ 3 | * 4 | * 925. 长按键入 5 | * 6 | * Easy 7 | * 8 | * 100% 9 | * 42.24% 10 | */ 11 | const isLongPressedName = (name, typed) => { 12 | const max = name.length 13 | 14 | let index1 = 0 15 | let index2 = 0 16 | 17 | while (index1 < max) { 18 | const originStr = name[index1] 19 | const str = typed[index2] 20 | if (originStr === str) { 21 | index1++ 22 | index2++ 23 | continue 24 | } 25 | 26 | if (str !== typed[index2 - 1]) { 27 | return false 28 | } 29 | index2++ 30 | } 31 | 32 | return true 33 | } -------------------------------------------------------------------------------- /array/1049/solution1.js: -------------------------------------------------------------------------------- 1 | const lastStoneWeightII = stones => { 2 | const len = stones.length; 3 | let sum = 0; 4 | for (let i = 0; i < len; i++) { 5 | sum += stones[i]; 6 | } 7 | const t = Math.floor(sum / 2); 8 | const dp = new Array(len + 1).fill(0).map(() => new Array(t + 1).fill(0)); 9 | 10 | for (let i = 1; i <= len; i++) { 11 | const stone = stones[i - 1]; 12 | for (let j = 0; j <= t; j++) { 13 | dp[i][j] = dp[i - 1][j]; 14 | if (j >= stone) { 15 | dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - stone] + stone); 16 | } 17 | } 18 | } 19 | 20 | return Math.abs(sum - dp[len][t] - dp[len][t]); 21 | } -------------------------------------------------------------------------------- /one-question-per-day/20200805/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(n) 4 | */ 5 | const rob = function(root) { 6 | 7 | const { selected, notSelected } = dfs(root); 8 | 9 | return Math.max(selected, notSelected); 10 | 11 | function dfs(root) { 12 | if (!root) { 13 | return { selected: 0, notSelected: 0 }; 14 | } 15 | 16 | const { selected: LS, notSelected: LN } = dfs(root.left); 17 | const { selected: RS, notSelected: RN } = dfs(root.right); 18 | 19 | const selected = root.val + LN + RN; 20 | const notSelected = Math.max(LS, LN) + Math.max(RS, RN); 21 | return { selected, notSelected }; 22 | } 23 | }; -------------------------------------------------------------------------------- /one-question-per-day/20200908/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 递归枚举 3 | * 4 | * 时间复杂度 O(2^n) 5 | * 空间复杂度 O(nk) 6 | */ 7 | const combine = function(n, k) { 8 | const ans = []; 9 | if (k > n || !n) { 10 | return []; 11 | } 12 | let temp = []; 13 | 14 | dfs(1); 15 | return ans; 16 | function dfs(current) { 17 | if (temp.length === k) { 18 | ans.push([...temp]); 19 | return; 20 | } 21 | 22 | if (current === n + 1) { 23 | return; 24 | } 25 | 26 | // 选择当前数字 27 | temp.push(current); 28 | dfs(current + 1); 29 | // 状态回滚 30 | temp.pop(); 31 | // 不选择当前数字 32 | dfs(current + 1); 33 | } 34 | }; -------------------------------------------------------------------------------- /one-question-per-day/20210603/index.js: -------------------------------------------------------------------------------- 1 | // 前缀和 + 哈希表 2 | const findMaxLength = (nums) => { 3 | let maxLength = 0; 4 | let currentSum = 0; 5 | const record = new Map(); 6 | record.set(currentSum, -1); 7 | 8 | for (let i = 0; i < nums.length; i++) { 9 | const num = nums[i]; 10 | 11 | if (num === 0) { 12 | currentSum--; 13 | } else { 14 | currentSum++; 15 | } 16 | 17 | if (record.has(currentSum)) { 18 | const preIndex = record.get(currentSum); 19 | maxLength = Math.max(maxLength, i - preIndex); 20 | } else { 21 | record.set(currentSum, i); 22 | } 23 | } 24 | 25 | return maxLength; 26 | } -------------------------------------------------------------------------------- /Binary-Tree/1008/solution2.js: -------------------------------------------------------------------------------- 1 | 2 | const bstFromPreorder = (preorder) => { 3 | const inorder = preorder.map(item => item).sort((a, b) => a - b); 4 | 5 | return buildTree(preorder, inorder); 6 | } 7 | 8 | const buildTree = (preorder, inorder) => { 9 | if (preorder.length === 0) { 10 | return null; 11 | } 12 | 13 | const rootValue = preorder.shift(); 14 | 15 | const root = new TreeNode(rootValue); 16 | 17 | const index = inorder.indexOf(rootValue); 18 | 19 | root.left = buildTree(preorder.slice(0, index), inorder.slice(0, index)); 20 | root.right = buildTree(preorder.slice(index), inorder.slice(index + 1)); 21 | 22 | return root; 23 | } -------------------------------------------------------------------------------- /Rank/184/1409/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/queries-on-a-permutation-with-key/ 3 | * 4 | * 1409. 查询带键的排列 5 | * 6 | * Medium 7 | * 8 | * 76ms 9 | * 41mb 10 | * 11 | * 时间复杂度 O(n^2) 12 | * 空间复杂度 O(n) 13 | */ 14 | const processQueries = (queries, m) => { 15 | const ans = []; 16 | const list = Array.from({ length: m }, (item, index) => index + 1); 17 | 18 | for (let i = 0, max = queries.length; i < max; i++) { 19 | const item = queries[i]; 20 | const index = list.findIndex(el => el === item); 21 | ans.push(index); 22 | list.splice(index, 1); 23 | list.unshift(item); 24 | } 25 | return ans; 26 | } -------------------------------------------------------------------------------- /Rank/201/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度 O(n) 3 | * 空间复杂度 O(1) 4 | */ 5 | const findKthBit = function(n, k) { 6 | let current = n; 7 | let reverseCount = 0; 8 | if (n === 1) { 9 | return '0' 10 | } 11 | while (current > 1) { 12 | const total = 2 ** current - 1; 13 | const mid = Math.ceil(total / 2); 14 | if (k === mid) { 15 | if (reverseCount % 2 === 0) { 16 | return '1'; 17 | } 18 | return '0'; 19 | } 20 | if (k > mid) { 21 | reverseCount++; 22 | k = mid * 2 - k; 23 | } 24 | current--; 25 | } 26 | if (reverseCount % 2 === 0) { 27 | return '0'; 28 | } 29 | return '1'; 30 | }; -------------------------------------------------------------------------------- /Two-Points/26/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 3 | * 4 | * 26. 删除排序数组中的重复项 5 | * 6 | * Easy 7 | * 8 | * 96.77% 9 | * 67.72% 10 | */ 11 | const removeDuplicates = nums => { 12 | const max = nums.length 13 | if (max === 1) { 14 | return nums 15 | } 16 | let slow = 1 17 | for (let fast = 1; fast < max; fast++) { 18 | if (nums[fast] !== nums[slow - 1]) { 19 | nums[slow] = nums[fast] 20 | slow++ 21 | continue 22 | } 23 | } 24 | let count = max - slow 25 | while (count > 0) { 26 | nums.pop() 27 | count-- 28 | } 29 | return nums.length 30 | } -------------------------------------------------------------------------------- /Two-Points/930/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/binary-subarrays-with-sum/ 3 | * 4 | * 930. 和相同的二元子数组 5 | * 6 | * Medium 7 | * 8 | * 朴素解法 9 | * 10 | * 2040ms 38.46% 11 | * 37.2mb 50.00% 12 | */ 13 | const numSubarraysWithSum = (A, S) => { 14 | let ans = 0 15 | const max = A.length 16 | for (let i = 0; i < max; i++) { 17 | let sum = A[i] 18 | if (sum === S) { 19 | ans++ 20 | } 21 | for (let j = i + 1; j < max; j++) { 22 | sum += A[j] 23 | if (sum === S) { 24 | ans++ 25 | } 26 | if (sum > S) { 27 | break 28 | } 29 | } 30 | } 31 | return ans 32 | } -------------------------------------------------------------------------------- /dynamic-programming/714/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 3 | * 4 | * 714. Best Time to Buy and Sell Stock with Transaction Fee 5 | * 6 | * Medium 7 | */ 8 | const maxProfit = (prices, fee) => { 9 | const max = prices.length 10 | if (max < 2) { 11 | return 0 12 | } 13 | 14 | const sell = [0] 15 | const hold = [-prices[0]] 16 | 17 | for (let i = 1; i < max; i++) { 18 | const price = prices[i] 19 | sell[i] = Math.max(sell[i - 1], hold[i - 1] + price - fee) 20 | hold[i] = Math.max(hold[i - 1], sell[i - 1] - price) 21 | } 22 | return sell[max - 1] 23 | } -------------------------------------------------------------------------------- /one-question-per-day/20200305/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/distribute-candies-to-people/ 3 | * 4 | * 1103. 分糖果 II 5 | * 6 | * Easy 7 | * 8 | * 56ms 95.04% 9 | * 34.7mb 26.23% 10 | */ 11 | const distributeCandies = (candies, num_people) => { 12 | let ans = Array(num_people).fill(0); 13 | 14 | let count = 0; 15 | while(candies) { 16 | // 确定下标 17 | const startIndex = count % num_people; 18 | 19 | // 得出能够分配多少个 20 | const allocationCount = Math.min(count + 1, candies); 21 | ans[startIndex] += allocationCount; 22 | candies -= allocationCount; 23 | count++; 24 | } 25 | 26 | return ans; 27 | } -------------------------------------------------------------------------------- /one-question-per-day/20200928/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 队列 3 | * 时间复杂度 O(n) 4 | * 空间复杂度 O(n) 5 | */ 6 | const connect = function(root) { 7 | const queue = [root]; 8 | while(queue.length) { 9 | const size = queue.length; 10 | 11 | for (let i = 0; i < size; i++) { 12 | const item = queue.shift(); 13 | if (item) { 14 | // 末尾的特殊情况 15 | if (i === size - 1) { 16 | item.next = null; 17 | } else { 18 | item.next = queue[0]; 19 | } 20 | item.left && queue.push(item.left); 21 | item.right && queue.push(item.right); 22 | } 23 | } 24 | } 25 | 26 | return root; 27 | }; -------------------------------------------------------------------------------- /one-question-per-day/20211229/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 直接枚举 3 | * 空间复杂度 O(1) 4 | * 时间复杂度 O(n^4) 5 | * @param {*} nums 6 | */ 7 | const countQuadruplets = function(nums) { 8 | const max = nums.length; 9 | let ans = 0; 10 | 11 | for (let i = 0; i < max; i++) { 12 | for (let j = i + 1; j < max; j++) { 13 | for (let k = j + 1; k < max; k++) { 14 | for (let q = k + 1; q < max; q++) { 15 | if (nums[i] + nums[j] + nums[k] === nums[q]) { 16 | ans++; 17 | } 18 | } 19 | } 20 | } 21 | } 22 | 23 | return ans; 24 | }; -------------------------------------------------------------------------------- /Binary-Tree/662/solution1.js: -------------------------------------------------------------------------------- 1 | const widthOfBinaryTree = root => { 2 | if (!root) { 3 | return 0 4 | } 5 | const MAX = Number.MAX_SAFE_INTEGER 6 | const q = [ [root, 1] ] 7 | let ans = 1 8 | while (q.length) { 9 | ans = Math.max(q[q.length - 1][1] - q[0][1] + 1, ans) 10 | const max = q.length 11 | for (let i = 0; i < max; i++) { 12 | const x = q.shift() 13 | const [item, index] = x 14 | if (item.left) { 15 | q.push([item.left, (2 * index) % MAX]) 16 | } 17 | if (item.right) { 18 | q.push([item.right, (2 * index + 1) % MAX]) 19 | } 20 | } 21 | } 22 | return ans 23 | } -------------------------------------------------------------------------------- /array/565/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/array-nesting/ 3 | * 4 | * 565. Array Nesting 5 | * 6 | * Medium 7 | * 8 | * 76ms 94.27% 9 | * 40.1mb 40.37% 10 | */ 11 | const arrayNesting = nums => { 12 | const len = nums.length 13 | const visited = new Set() 14 | let ans = Number.MIN_SAFE_INTEGER 15 | for (let i = 0; i < len; i++) { 16 | if (visited.has(i)) { 17 | continue 18 | } 19 | let item = i 20 | let count = 0 21 | while (!visited.has(item)) { 22 | visited.add(item) 23 | count++ 24 | item = nums[item] 25 | } 26 | ans = Math.max(count, ans) 27 | } 28 | 29 | return ans 30 | } -------------------------------------------------------------------------------- /one-question-per-day/20211224/solution1.js: -------------------------------------------------------------------------------- 1 | var eatenApples = function (apples, days) { 2 | let res = 0, minDay = days[0] - 1; 3 | const freshArr = new Array(days.length).fill(0); 4 | for (let i = 0; i < freshArr.length; i++) { 5 | if (i < days.length && days[i] > 0) { 6 | freshArr[i + days[i] - 1] = (freshArr[i + days[i] - 1] || 0) + apples[i]; 7 | minDay = Math.min(minDay, i + days[i] - 1); 8 | } 9 | minDay = Math.max(minDay, i); 10 | while (minDay < freshArr.length && !freshArr[minDay]) minDay++; 11 | if (freshArr[minDay]) res++, freshArr[minDay]--; 12 | } 13 | return res; 14 | }; 15 | -------------------------------------------------------------------------------- /Binary-Tree/1145/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/binary-tree-coloring-game/ 3 | * 4 | * 1145. 二叉树着色游戏 5 | * 6 | * Medium 7 | * 8 | * 68ms 97.14% 9 | * 34.1mb 100.00% 10 | */ 11 | const btreeGameWinningMove = (root, n, x) => { 12 | let max = 0; 13 | 14 | dfs(root); 15 | 16 | return max > n / 2; 17 | 18 | function dfs(root) { 19 | if (!root) { 20 | return 0; 21 | } 22 | const lCount = dfs(root.left); 23 | const rCount = dfs(root.right); 24 | 25 | if (root.val === x) { 26 | max = Math.max(lCount, rCount, n - lCount - rCount - 1); 27 | } 28 | 29 | return lCount + rCount + 1; 30 | } 31 | } -------------------------------------------------------------------------------- /Binary-Tree/938/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/range-sum-of-bst/ 3 | * 4 | * 938. 二叉搜索树的范围和 5 | * 6 | * Easy 7 | * 8 | * 272ms 96.57% 9 | * 66.3mb 92.31% 10 | */ 11 | const rangeSumBST = (root, L, R) => { 12 | if (!root) { 13 | return 0 14 | } 15 | 16 | let ans = 0 17 | 18 | const val = root.val 19 | 20 | if (val >= L && val <= R) { 21 | ans += val 22 | ans += rangeSumBST(root.left, L, R) 23 | ans += rangeSumBST(root.right, L, R) 24 | } else if (val < L) { 25 | ans += rangeSumBST(root.right, L, R) 26 | } else if (val > R) { 27 | ans += rangeSumBST(root.left, L, R) 28 | } 29 | return ans 30 | } -------------------------------------------------------------------------------- /Linked-List/19/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ 3 | * 4 | * 19. 删除链表的倒数第N个节点 5 | * 6 | * Medium 7 | * 8 | * 双指针解法 9 | * 10 | * 56ms 98.71% 11 | * 34.5mb 5.18% 12 | */ 13 | const removeNthFromEnd = (head, n) => { 14 | const dummyHead = new ListNode(null); 15 | dummyHead.next = head; 16 | 17 | let first = dummyHead; 18 | let second = dummyHead; 19 | for (let i = 0; i < n + 1; i++) { 20 | first = first.next; 21 | } 22 | 23 | while(first) { 24 | first = first.next; 25 | second = second.next; 26 | } 27 | 28 | second.next = second.next.next; 29 | return dummyHead.next; 30 | } -------------------------------------------------------------------------------- /array/350/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 哈希表 3 | * 4 | * 时间复杂度 O(m + n) 5 | * 空间复杂度 O(min(m + n)) 6 | * 7 | */ 8 | const intersect = (nums1, nums2) => { 9 | const record = new Map(); 10 | for (let i = 0; i < nums1.length; i++) { 11 | const item = nums1[i]; 12 | if (!record.has(item)) { 13 | record.set(item, 0); 14 | } 15 | record.set(item, record.get(item) + 1); 16 | } 17 | 18 | const ans = []; 19 | 20 | for (let i = 0; i < nums2.length; i++) { 21 | const item = nums2[i]; 22 | if (record.get(item) > 0) { 23 | record.set(item, record.get(item) - 1); 24 | ans.push(item); 25 | } 26 | } 27 | 28 | return ans; 29 | } -------------------------------------------------------------------------------- /array/518/solution2.js: -------------------------------------------------------------------------------- 1 | const change = (amount, coins) => { 2 | const record = new Map(); 3 | 4 | const backtrack = (amount, index) => { 5 | if (amount === 0) { 6 | return 1; 7 | } 8 | 9 | const cacheKey = `${amount}-${index}`; 10 | if (record.has(cacheKey)) { 11 | return record.get(cacheKey); 12 | } 13 | 14 | let count = 0; 15 | for (let i = index; i < coins.length; i++) { 16 | const coin = coins[i]; 17 | if (amount >= coin) { 18 | count += backtrack(amount - coin, i); 19 | } 20 | } 21 | record.set(cacheKey, count); 22 | return count; 23 | } 24 | 25 | return backtrack(amount, 0); 26 | } -------------------------------------------------------------------------------- /string/516/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n^2) 3 | * 空间复杂度:O(n^2) 4 | */ 5 | const longestPalindromeSubseq = function(s) { 6 | const max = s.length 7 | const dp = Array(max).fill(0).map(() => Array(max).fill(0)) 8 | for (let i = max - 1; i >= 0; i--) { 9 | dp[i][i] = 1; 10 | const c1 = s[i]; 11 | for (let j = i + 1; j < max; j++) { 12 | const c2 = s[j]; 13 | if (c1 === c2) { 14 | dp[i][j] = dp[i + 1][j - 1] + 2; 15 | } else { 16 | dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); 17 | } 18 | } 19 | } 20 | 21 | return dp[0][max - 1]; 22 | }; -------------------------------------------------------------------------------- /Two-Points/167/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/ 3 | * 4 | * 167. 两数之和 II - 输入有序数组 5 | * 6 | * Easy 7 | * 8 | * 91.86% 9 | * 57.10% 10 | */ 11 | const twoSum = (numbers, target) => { 12 | const max = numbers.length 13 | let start = 0 14 | let end = max - 1 15 | 16 | while (start < end) { 17 | const sum = numbers[start] + numbers[end] 18 | 19 | if (sum === target) { 20 | return [start + 1, end + 1] 21 | } 22 | 23 | if (sum > target) { 24 | end-- 25 | continue 26 | } 27 | 28 | if (sum < target) { 29 | start++ 30 | continue 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /array/495/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/teemo-attacking/ 3 | * 4 | * 495. Teemo Attacking 5 | * 6 | * Medium 7 | * 8 | * 60ms 93.14% 9 | * 37.9mb 98.41% 10 | */ 11 | const findPoisonedDuration = (timeSeries, duration) => { 12 | const max = timeSeries.length 13 | if (max === 0) { 14 | return 0 15 | } 16 | let pre = timeSeries[0] 17 | let ans = 0 18 | for (let i = 1; i < max;i++) { 19 | const time = timeSeries[i] 20 | const diff = time - pre 21 | if (diff > duration) { 22 | ans += duration 23 | } else { 24 | ans += diff 25 | } 26 | pre = time 27 | } 28 | 29 | return ans + duration 30 | } -------------------------------------------------------------------------------- /array/581/solution2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 时间复杂度:O(n) 3 | * 空间复杂度:O(1) 4 | */ 5 | const findUnsortedSubarray = function(nums) { 6 | const maxIndex = nums.length - 1; 7 | let start = 0; 8 | let end = -1; 9 | let max = nums[0]; 10 | let min = nums[nums.length - 1]; 11 | 12 | for (let i = 0; i < nums.length; i++) { 13 | if (nums[i] < max) { 14 | end = i; 15 | } else { 16 | max = nums[i]; 17 | } 18 | 19 | if (nums[maxIndex - i] > min) { 20 | start = maxIndex - i; 21 | } else { 22 | min = nums[maxIndex - i]; 23 | } 24 | } 25 | 26 | return end - start + 1; 27 | }; -------------------------------------------------------------------------------- /array/896/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://leetcode.com/problems/monotonic-array/ 3 | * 4 | * 896. Monotonic Array 5 | * 6 | * Easy 7 | */ 8 | const isMonotonic = A => { 9 | const max = A.length 10 | if (max === 2) { 11 | return true 12 | } 13 | 14 | let flag = null 15 | 16 | for (let i = 0; i < max - 1; i++) { 17 | const cur = A[i] 18 | const next = A[i + 1] 19 | if (cur === next) { 20 | continue 21 | } 22 | 23 | const diff = cur > next 24 | if (flag === null) { 25 | flag = diff 26 | continue 27 | } 28 | 29 | if (flag !== diff) { 30 | return false 31 | } 32 | 33 | } 34 | return true 35 | } -------------------------------------------------------------------------------- /dynamic-programming/413/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * DP 3 | * 4 | * dp[i] 表示1以A[i]结尾的等差数列的个数 5 | * 6 | * dp[0] = 0 7 | * dp[1] = 0 8 | * 9 | * | dp[i - 1] + 1 A[i] - A[i - 1] === A[i - 1] - A[i - 2] 10 | * dp[i] = | 11 | * | 0 12 | */ 13 | 14 | const numberOfArithmeticSlices = A => { 15 | const max = A.length 16 | 17 | if (max <= 2) { 18 | return 0 19 | } 20 | 21 | const dp = [0, 0] 22 | let sum = 0 23 | 24 | for (let i = 2; i < max; i++) { 25 | if (A[i] - A[i - 1] === A[i - 1] - A[i - 2]) { 26 | dp[i] = dp[i - 1] + 1 27 | } else { 28 | dp[i] = 0 29 | } 30 | sum += dp[i] 31 | } 32 | return sum 33 | } --------------------------------------------------------------------------------