├── .browserslistrc ├── .eslintignore ├── .eslintrc.js ├── .github └── workflows │ └── deploy.yml ├── .gitignore ├── .prettierrc ├── .vcmrc ├── CNAME ├── README.md ├── babel.config.js ├── code ├── ch01 │ ├── 1.3.1.sum.cpp │ ├── 1.3.1.sum.java │ ├── 1.3.1.sum.js │ ├── 1.3.1.sum.py │ ├── 1.3.2.two-sum.cpp │ ├── 1.3.2.two-sum.java │ ├── 1.3.2.two-sum.js │ ├── 1.3.2.two-sum.py │ ├── 1.3.3. dailyTemperatures.cpp │ ├── 1.3.3. dailyTemperatures.js │ ├── 1.3.3. dailyTemperatures.py │ ├── 1.3.3.dailyTemperatures.java │ ├── 1.3.4.numRescueBoats.cpp │ ├── 1.3.4.numRescueBoats.java │ ├── 1.3.4.numRescueBoats.js │ ├── 1.3.4.numRescueBoats.py │ ├── 1.3.5.fibonacci.cpp │ ├── 1.3.5.fibonacci.java │ ├── 1.3.5.fibonacci.js │ ├── 1.3.5.fibonacci.py │ ├── 1.3.6.permute.cpp │ ├── 1.3.6.permute.java │ ├── 1.3.6.permute.js │ └── 1.3.6.permute.py ├── ch02 │ ├── 2.1.1.twoSum.cpp │ ├── 2.1.1.twoSum.java │ ├── 2.1.1.twoSum.js │ ├── 2.1.1.twoSum.py │ ├── 2.1.2.twoSum.cpp │ ├── 2.1.2.twoSum.java │ ├── 2.1.2.twoSum.js │ ├── 2.1.2.twoSum.py │ ├── 2.10.1.numPrimeArrangements.cpp │ ├── 2.10.1.numPrimeArrangements.java │ ├── 2.10.1.numPrimeArrangements.py │ ├── 2.2.1.threeSum.cpp │ ├── 2.2.1.threeSum.java │ ├── 2.2.1.threeSum.js │ ├── 2.2.1.threeSum.py │ ├── 2.3.1.fourSum.cpp │ ├── 2.3.1.fourSum.java │ ├── 2.3.1.fourSum.js │ ├── 2.3.1.fourSum.py │ ├── 2.3.2.fourSum.cpp │ ├── 2.3.2.fourSum.java │ ├── 2.3.2.fourSum.js │ ├── 2.3.2.fourSum.py │ ├── 2.4.1.fourSumCount.cpp │ ├── 2.4.1.fourSumCount.java │ ├── 2.4.1.fourSumCount.py │ ├── 2.5.1.threeSumCloset.cpp │ ├── 2.5.1.threeSumCloset.java │ ├── 2.5.1.threeSumCloset.py │ ├── 2.6.1.maxSubArray.cpp │ ├── 2.6.1.maxSubArray.java │ ├── 2.6.1.maxSubArray.py │ ├── 2.6.2.maxSubArray.cpp │ ├── 2.6.2.maxSubArray.java │ ├── 2.6.2.maxSubArray.py │ ├── 2.6.3.maxSubArray.cpp │ ├── 2.6.3.maxSubArray.java │ ├── 2.6.3.maxSubArray.py │ ├── 2.6.4.maxSubArray.cpp │ ├── 2.6.4.maxSubArray.java │ ├── 2.6.4.maxSubArray.py │ ├── 2.7.1.largestNumber.cpp │ ├── 2.7.1.largestNumber.java │ ├── 2.7.1.largestNumber.py │ ├── 2.7.2.largestNumber.cpp │ ├── 2.7.2.largestNumber.java │ ├── 2.7.2.largestNumber.py │ ├── 2.8.1.fractionToDecimal.cpp │ ├── 2.8.1.fractionToDecimal.java │ ├── 2.8.1.fractionToDecimal.py │ ├── 2.9.1.largestDivisibleSubset.cpp │ ├── 2.9.1.largestDivisibleSubset.java │ ├── 2.9.1.largestDivisibleSubset.py │ ├── 2.9.2.largetDivisibleSubset.cpp │ ├── 2.9.2.largetDivisibleSubset.java │ └── 2.9.2.largetDivisibleSubset.py ├── ch03 │ ├── 3.1.1.isPalindrome.cpp │ ├── 3.1.1.isPalindrome.java │ ├── 3.1.1.isPalindrome.js │ ├── 3.1.1.isPalindrome.py │ ├── 3.1.2.validPalindrome.cpp │ ├── 3.1.2.validPalindrome.java │ ├── 3.1.2.validPalindrome.js │ ├── 3.1.2.validPalindrome.py │ ├── 3.2.1.reverseList.cpp │ ├── 3.2.1.reverseList.java │ ├── 3.2.1.reverseList.py │ ├── 3.2.2.isPalindrome.cpp │ ├── 3.2.2.isPalindrome.java │ ├── 3.2.2.isPalindrome.py │ ├── 3.3.1.isPalindrome.cpp │ ├── 3.3.1.isPalindrome.java │ ├── 3.3.1.isPalindrome.py │ ├── 3.4.1.longestPalindrome.cpp │ ├── 3.4.1.longestPalindrome.java │ ├── 3.4.1.longestPalindrome.py │ ├── 3.5.1.longestPalindromeSubseq.cpp │ ├── 3.5.1.longestPalindromeSubseq.java │ ├── 3.5.1.longestPalindromeSubseq.py │ ├── 3.5.2.longestPalindromeSubseq.cpp │ ├── 3.5.2.longestPalindromeSubseq.java │ ├── 3.5.2.longestPalindromeSubseq.py │ ├── 3.6.1.superpalindromesInRange.cpp │ ├── 3.6.1.superpalindromesInRange.java │ ├── 3.6.1.superpalindromesInRange.py │ ├── 3.6.2.superpalindromesInRange.cpp │ ├── 3.6.2.superpalindromesInRange.java │ └── 3.6.2.superpalindromesInRange.py ├── ch04 │ ├── 4.1.1.CountAndSay.cpp │ ├── 4.1.1.CountAndSay.java │ ├── 4.1.1.CountAndSay.py │ ├── 4.1.2.CountAndSay.cpp │ ├── 4.1.2.CountAndSay.java │ ├── 4.1.2.CountAndSay.py │ ├── 4.2.1.24game.cpp │ ├── 4.2.1.24game.java │ ├── 4.2.1.24game.py │ ├── 4.2.2.24game.cpp │ ├── 4.2.2.24game.java │ ├── 4.2.2.24game.py │ ├── 4.3.1.SudokuSolver.cpp │ ├── 4.3.1.SudokuSolver.java │ ├── 4.3.1.SudokuSolver.py │ ├── 4.3.2.SudokuSolver.cpp │ ├── 4.3.2.SudokuSolver.java │ ├── 4.3.2.SudokuSolver.py │ ├── 4.4.1.GameOfLife.cpp │ ├── 4.4.1.GameOfLife.java │ └── 4.4.1.GameOfLife.py ├── ch05 │ ├── 5.1.1-path-sum.cpp │ ├── 5.1.1-path-sum.java │ ├── 5.1.1-path-sum.py │ ├── 5.1.2-path-sum.cpp │ ├── 5.1.2-path-sum.java │ ├── 5.1.2-path-sum.py │ ├── 5.1.3-path-sum-II.cpp │ ├── 5.1.3-path-sum-II.java │ ├── 5.1.3-path-sum-II.py │ ├── 5.1.4-path-sum-II.cpp │ ├── 5.1.4-path-sum-II.java │ ├── 5.1.4-path-sum-II.py │ ├── 5.1.5-binary-tree-maximum-path-sum.cpp │ ├── 5.1.5-binary-tree-maximum-path-sum.java │ ├── 5.1.5-binary-tree-maximum-path-sum.py │ ├── 5.2.1-number-of-islands.cpp │ ├── 5.2.1-number-of-islands.java │ ├── 5.2.1-number-of-islands.py │ ├── 5.2.2-number-of-islands.cpp │ ├── 5.2.2-number-of-islands.java │ ├── 5.2.2-number-of-islands.py │ ├── 5.2.3-number-of-islands.cpp │ ├── 5.2.3-number-of-islands.java │ ├── 5.2.3-number-of-islands.py │ ├── 5.2.4-number-of-islands.cpp │ ├── 5.2.4-number-of-islands.java │ ├── 5.2.4-number-of-islands.py │ ├── 5.2.5-number-of-islands-II.cpp │ ├── 5.2.5-number-of-islands-II.java │ └── 5.2.5-number-of-islands-II.py ├── ch06 │ ├── 6.1.1.binary-search.cpp │ ├── 6.1.1.binary-search.java │ ├── 6.1.1.binary-search.py │ ├── 6.2.1.find-minimum-in-rotated-sorted-array.cpp │ ├── 6.2.1.find-minimum-in-rotated-sorted-array.java │ ├── 6.2.1.find-minimum-in-rotated-sorted-array.py │ ├── 6.3.1.koko-eating-bananas.cpp │ ├── 6.3.1.koko-eating-bananas.java │ ├── 6.3.1.koko-eating-bananas.py │ ├── 6.4.1.sqrtx.cpp │ ├── 6.4.1.sqrtx.java │ ├── 6.4.1.sqrtx.py │ ├── 6.4.2.sqrtx.cpp │ ├── 6.4.2.sqrtx.java │ ├── 6.4.2.sqrtx.py │ ├── 6.4.3.sqrtx.cpp │ ├── 6.4.3.sqrtx.java │ ├── 6.4.3.sqrtx.py │ ├── 6.5.1.find-peak-element.cpp │ ├── 6.5.1.find-peak-element.java │ ├── 6.5.1.find-peak-element.py │ ├── 6.6.1.split-array-largest-sum.cpp │ ├── 6.6.1.split-array-largest-sum.java │ └── 6.6.1.split-array-largest-sum.py ├── ch07 │ ├── 7.1.1_haming_weight.cpp │ ├── 7.1.1_haming_weight.java │ ├── 7.1.1_haming_weight.py │ ├── 7.1.2_haming_weight.cpp │ ├── 7.1.2_haming_weight.java │ ├── 7.1.2_haming_weight.py │ ├── 7.2.1_get_sum.cpp │ ├── 7.2.1_get_sum.java │ ├── 7.2.1_get_sum.py │ ├── 7.3.1_integer_replacement.cpp │ ├── 7.3.1_integer_replacement.java │ ├── 7.3.1_integer_replacement.py │ ├── 7.3.2_integer_replacement.cpp │ ├── 7.3.2_integer_replacement.java │ ├── 7.3.2_integer_replacement.py │ ├── 7.3.3_integer_replacement.cpp │ ├── 7.3.3_integer_replacement.java │ ├── 7.3.3_integer_replacement.py │ ├── 7.4.1_single_number.cpp │ ├── 7.4.1_single_number.java │ ├── 7.4.1_single_number.py │ ├── 7.4.2_single_number.cpp │ ├── 7.4.2_single_number.java │ ├── 7.4.2_single_number.py │ ├── 7.4.3_single_number.java │ ├── 7.4.3_single_number.py │ ├── 7.4.4_single_number.cpp │ ├── 7.4.4_single_number.java │ ├── 7.4.4_single_number.py │ ├── 7.4.5_single_number.java │ └── 7.4.5_single_number.py ├── ch08 │ ├── 8.1.1_155.min-stack.cpp │ ├── 8.1.1_155.min-stack.java │ ├── 8.1.1_155.min-stack.py │ ├── 8.1.2_155.min-stack.cpp │ ├── 8.1.2_155.min-stack.java │ ├── 8.1.2_155.min-stack.py │ ├── 8.1.3_155.min-stack.cpp │ ├── 8.1.3_155.min-stack.java │ ├── 8.1.3_155.min-stack.py │ ├── 8.1.4_155.min-stack.cpp │ ├── 8.1.4_155.min-stack.java │ ├── 8.1.4_155.min-stack.py │ ├── 8.2.1_208.implement-trie-prefix-tree.cpp │ ├── 8.2.1_208.implement-trie-prefix-tree.java │ ├── 8.2.1_208.implement-trie-prefix-tree.py │ ├── 8.3.1_146.lru-cache.cpp │ ├── 8.3.1_146.lru-cache.java │ ├── 8.3.1_146.lru-cache.py │ ├── 8.4.1_460.lfu-cache.cpp │ ├── 8.4.1_460.lfu-cache.java │ ├── 8.4.1_460.lfu-cache.py │ ├── 8.5.1_1206.skip-list.cpp │ ├── 8.5.1_1206.skip-list.java │ └── 8.5.1_1206.skip-list.py ├── ch09 │ ├── 9.1.1.2.two_sum.cpp │ ├── 9.1.1.2.two_sum.java │ ├── 9.1.1.2.two_sum.py │ ├── 9.1.1.two_sum.cpp │ ├── 9.1.1.two_sum.java │ ├── 9.1.1.two_sum.py │ ├── 9.1.2.1.container_with_most_water.cpp │ ├── 9.1.2.1.container_with_most_water.java │ ├── 9.1.2.1.container_with_most_water.py │ ├── 9.1.2.2.container_with_most_water.cpp │ ├── 9.1.2.2.container_with_most_water.java │ ├── 9.1.2.2.container_with_most_water.py │ ├── 9.2.1.1.linked_list_cycle.cpp │ ├── 9.2.1.1.linked_list_cycle.java │ ├── 9.2.1.1.linked_list_cycle.py │ ├── 9.2.1.2.linked_list_cycle.cpp │ ├── 9.2.1.2.linked_list_cycle.java │ ├── 9.2.1.2.linked_list_cycle.py │ ├── 9.2.2.1.long_substr_without_repeat_char.java │ ├── 9.2.2.1.long_substr_without_repeat_char.py │ ├── 9.2.2.2.long_substr_without_repeat_char.cpp │ ├── 9.2.2.2.long_substr_without_repeat_char.java │ ├── 9.2.2.2.long_substr_without_repeat_char.py │ ├── 9.2.2.3.long_substr_without_repeat_char.cpp │ ├── 9.2.2.3.long_substr_without_repeat_char.java │ ├── 9.2.2.3.long_substr_without_repeat_char.py │ ├── 9.2.2.4.long_substr_without_repeat_char.cpp │ ├── 9.2.2.4.long_substr_without_repeat_char.java │ └── 9.2.2.4.long_substr_without_repeat_char.py ├── ch10 │ ├── 10.1.1.ClimbingStairs.cpp │ ├── 10.1.1.ClimbingStairs.java │ ├── 10.1.1.ClimbingStairs.py │ ├── 10.1.2.ClimbingStairs.cpp │ ├── 10.1.2.ClimbingStairs.java │ ├── 10.1.2.ClimbingStairs.py │ ├── 10.2.1-house-rob.cpp │ ├── 10.2.1-house-rob.java │ ├── 10.2.1-house-rob.py │ ├── 10.2.2-house-rob.cpp │ ├── 10.2.2-house-rob.java │ ├── 10.2.2-house-rob.py │ ├── 10.2.3-house-rob.cpp │ ├── 10.2.3-house-rob.java │ ├── 10.2.3-house-rob.py │ ├── 10.2.4-house-rob.cpp │ ├── 10.2.4-house-rob.java │ ├── 10.2.4-house-rob.py │ ├── 10.2.5-house-rob-II.cpp │ ├── 10.2.5-house-rob-II.java │ ├── 10.2.5-house-rob-II.py │ ├── 10.3.1.uniquePaths.cpp │ ├── 10.3.1.uniquePaths.java │ ├── 10.3.1.uniquePaths.py │ ├── 10.3.2.uniquePaths.cpp │ ├── 10.3.2.uniquePaths.java │ ├── 10.3.2.uniquePaths.py │ ├── 10.3.3.uniquePaths.cpp │ ├── 10.3.3.uniquePaths.java │ ├── 10.3.3.uniquePaths.py │ ├── 10.4.1.CoinChange.cpp │ ├── 10.4.1.CoinChange.java │ ├── 10.4.1.CoinChange.py │ ├── 10.4.2.CoinChange2.cpp │ ├── 10.4.2.CoinChange2.java │ └── 10.4.2.CoinChange2.py ├── ch11 │ ├── 11.1.1_maxslidingwindow.cpp │ ├── 11.1.1_maxslidingwindow.java │ ├── 11.1.1_maxslidingwindow.py │ ├── 11.1.2_maxslidingwindow.cpp │ ├── 11.1.2_maxslidingwindow.java │ ├── 11.1.2_maxslidingwindow.py │ ├── 11.2.1_minwindow.java │ ├── 11.2.1_minwindow.py │ ├── 11.2.2_minwindow.cpp │ ├── 11.2.2_minwindow.py │ ├── 11.3.1_characterReplacement.cpp │ ├── 11.3.1_characterReplacement.java │ ├── 11.3.1_characterReplacement.py │ ├── 11.3.2_characterReplacement.cpp │ ├── 11.3.2_characterReplacement.java │ ├── 11.3.2_characterReplacement.py │ ├── 11.4.1_checkinclusion.py │ ├── 11.4.2_checkinclusion.cpp │ ├── 11.4.2_checkinclusion.java │ └── 11.4.2_checkinclusion.py ├── ch12 │ ├── 12.1.1.StoneGame.cpp │ ├── 12.1.1.StoneGame.java │ ├── 12.1.1.StoneGame.py │ ├── 12.1.2.StoneGame.cpp │ ├── 12.1.2.StoneGame.java │ ├── 12.1.2.stoneGame.py │ ├── 12.2.1.PredictTheWinner.cpp │ ├── 12.2.1.PredictTheWinner.java │ ├── 12.2.1.predictTheWinner.py │ ├── 12.2.2.PredictTheWinner.cpp │ ├── 12.2.2.PredictTheWinner.java │ ├── 12.2.2.PredictTheWinner.py │ ├── 12.2.3.PredictTheWinner.cpp │ ├── 12.2.3.PredictTheWinner.java │ ├── 12.2.3.PredictTheWinner.py │ ├── 12.3.1.nimGame.cpp │ ├── 12.3.1.nimGame.java │ ├── 12.3.1.nimGame.py │ ├── 12.3.2.nimGame.cpp │ ├── 12.3.2.nimGame.java │ ├── 12.3.2.nimGame.py │ ├── 12.3.3.nimGame.cpp │ ├── 12.3.3.nimGame.java │ ├── 12.3.3.nimGame.py │ ├── 12.4.1.GetMoneyAmount.cpp │ ├── 12.4.1.GetMoneyAmount.java │ ├── 12.4.1.GetMoneyAmount.py │ ├── 12.4.2.GetMoneyAmount.cpp │ ├── 12.4.2.GetMoneyAmount.java │ └── 12.4.2.GetMoneyAmount.py ├── ch13 │ ├── 13.1.1.Solution.java │ ├── 13.1.1.best_time_to_buy_and_sell_stock.cpp │ ├── 13.1.1.best_time_to_buy_and_sell_stock.py │ ├── 13.1.2.Solution.java │ ├── 13.1.2.best_time_to_buy_and_sell_stock.cpp │ ├── 13.1.2.best_time_to_buy_and_sell_stock.py │ ├── 13.2.1.Solution.java │ ├── 13.2.best_time_to_buy_and_sell_stock_II.cpp │ ├── 13.2.best_time_to_buy_and_sell_stock_II.py │ ├── 13.3.1.Solution.java │ ├── 13.3.1.best_time_to_buy_and_sell_stock_with_fee.cpp │ ├── 13.3.1.best_time_to_buy_and_sell_stock_with_fee.py │ ├── 13.3.2.Solution.java │ ├── 13.3.2.best_time_to_buy_and_sell_stock_with_fee.cpp │ ├── 13.3.2.best_time_to_buy_and_sell_stock_with_fee.py │ ├── 13.4.1.Solution.java │ ├── 13.4.1.best_time_to_buy_and_sell_stock_with_cooldown.cpp │ ├── 13.4.1.best_time_to_buy_and_sell_stock_with_cooldown.py │ ├── 13.4.2.Solution.java │ ├── 13.4.2.best_time_to_buy_and_sell_stock_with_cooldown.cpp │ ├── 13.4.2.best_time_to_buy_and_sell_stock_with_cooldown.py │ ├── 13.5.1.Solution.java │ ├── 13.5.1.best_time_to_buy_and_sell_stock_IV.cpp │ ├── 13.5.1.best_time_to_buy_and_sell_stock_IV.py │ ├── 13.5.2.Solution.java │ ├── 13.5.2.best_time_to_buy_and_sell_stock_IV.cpp │ ├── 13.5.2.best_time_to_buy_and_sell_stock_IV.py │ ├── 13.6.1.Solution.cpp │ ├── 13.6.1.Solution.java │ └── 13.6.1.Solution.py ├── ch14 │ ├── 14.1.1_21.merge-two-sorted-lists.cpp │ ├── 14.1.1_21.merge-two-sorted-lists.java │ ├── 14.1.1_21.merge-two-sorted-lists.py │ ├── 14.1.2_21.merge-two-sorted-lists.cpp │ ├── 14.1.2_21.merge-two-sorted-lists.java │ ├── 14.1.2_21.merge-two-sorted-lists.py │ ├── 14.1.3_23.merge-k-sorted-lists.cpp │ ├── 14.1.3_23.merge-k-sorted-lists.java │ ├── 14.1.3_23.merge-k-sorted-lists.py │ ├── 14.1.4_23.merge-k-sorted-lists.cpp │ ├── 14.1.4_23.merge-k-sorted-lists.java │ ├── 14.1.4_23.merge-k-sorted-lists.py │ ├── 14.1.5_23.merge-k-sorted-lists.cpp │ ├── 14.1.5_23.merge-k-sorted-lists.java │ ├── 14.1.5_23.merge-k-sorted-lists.py │ ├── 14.2.1_215.kth-largest-element-in-an-array.cpp │ ├── 14.2.1_215.kth-largest-element-in-an-array.java │ ├── 14.2.1_215.kth-largest-element-in-an-array.py │ ├── 14.2.2_215.kth-largest-element-in-an-array.cpp │ ├── 14.2.2_215.kth-largest-element-in-an-array.java │ ├── 14.2.2_215.kth-largest-element-in-an-array.py │ ├── 14.2.3_215.kth-largest-element-in-an-array.cpp │ ├── 14.2.3_215.kth-largest-element-in-an-array.java │ ├── 14.2.3_215.kth-largest-element-in-an-array.py │ ├── 14.2.4_215.kth-largest-element-in-an-array.cpp │ ├── 14.2.4_215.kth-largest-element-in-an-array.java │ ├── 14.2.4_215.kth-largest-element-in-an-array.py │ ├── 14.2.5_215.kth-largest-element-in-an-array.cpp │ ├── 14.2.5_215.kth-largest-element-in-an-array.java │ ├── 14.2.5_215.kth-largest-element-in-an-array.py │ ├── 14.3.1_240.search-a-2d-matrix-ii.cpp │ ├── 14.3.1_240.search-a-2d-matrix-ii.java │ ├── 14.3.1_240.search-a-2d-matrix-ii.py │ ├── 14.3.2_240.search-a-2d-matrix-ii.cpp │ ├── 14.3.2_240.search-a-2d-matrix-ii.java │ ├── 14.3.2_240.search-a-2d-matrix-ii.py │ ├── 14.3.3_240.search-a-2d-matrix-ii.cpp │ ├── 14.3.3_240.search-a-2d-matrix-ii.java │ ├── 14.3.3_240.search-a-2d-matrix-ii.py │ ├── 14.3.4_240.search-a-2d-matrix-ii.cpp │ ├── 14.3.4_240.search-a-2d-matrix-ii.java │ ├── 14.3.4_240.search-a-2d-matrix-ii.py │ ├── 14.3.5_240.search-a-2d-matrix-ii.cpp │ ├── 14.3.5_240.search-a-2d-matrix-ii.java │ ├── 14.3.5_240.search-a-2d-matrix-ii.py │ ├── 14.3.6_240.search-a-2d-matrix-ii.cpp │ ├── 14.3.6_240.search-a-2d-matrix-ii.java │ └── 14.3.6_240.search-a-2d-matrix-ii.py ├── ch15 │ ├── 15.1.1.assign-cookies.cpp │ ├── 15.1.1.assign-cookies.java │ ├── 15.1.1.assign-cookies.py │ ├── 15.2.1.jump-game.cpp │ ├── 15.2.1.jump-game.java │ ├── 15.2.1.jump-game.py │ ├── 15.2.2.jump-game.cpp │ ├── 15.2.2.jump-game.java │ ├── 15.2.2.jump-game.py │ ├── 15.2.3.jump-game.cpp │ ├── 15.2.3.jump-game.java │ ├── 15.2.3.jump-game.py │ ├── 15.3.1.task-scheduler.cpp │ ├── 15.3.1.task-scheduler.java │ ├── 15.3.1.task-scheduler.py │ ├── 15.4.1.candy.cpp │ ├── 15.4.1.candy.java │ └── 15.4.1.candy.py ├── ch16 │ ├── 16.1.1.combination-sum.cpp │ ├── 16.1.1.combination-sum.java │ ├── 16.1.1.combination-sum.py │ ├── 16.2.1.combination-sum-ii.cpp │ ├── 16.2.1.combination-sum-ii.java │ ├── 16.2.1.combination-sum-ii.py │ ├── 16.3.1.subsets.cpp │ ├── 16.3.1.subsets.java │ ├── 16.3.1.subsets.py │ ├── 16.4.1.permutations.cpp │ ├── 16.4.1.permutations.java │ ├── 16.4.1.permutations.py │ ├── 16.5.1.sudoku-solver.cpp │ ├── 16.5.1.sudoku-solver.java │ └── 16.5.1.sudoku-solver.py ├── ch17 │ ├── 17.1.1.majorityElement.cpp │ ├── 17.1.1.majorityElement.java │ ├── 17.1.1.majorityElement.py │ ├── 17.2.1.largestRectangleArea.cpp │ ├── 17.2.1.largestRectangleArea.java │ ├── 17.2.1.largestRectangleArea.py │ ├── 17.2.2.largestRectangleArea.cpp │ ├── 17.2.2.largestRectangleArea.java │ ├── 17.2.2.largestRectangleArea.py │ ├── 17.2.3.largestRectangleArea.cpp │ ├── 17.2.3.largestRectangleArea.java │ ├── 17.2.3.largestRectangleArea.py │ ├── 17.2.4.largestRectangleArea.cpp │ ├── 17.2.4.largestRectangleArea.java │ ├── 17.2.4.largestRectangleArea.py │ ├── 17.3.1.dayOfTheWeek.cpp │ ├── 17.3.1.dayOfTheWeek.java │ ├── 17.3.1.dayOfTheWeek.py │ ├── 17.3.2.dayOfTheWeek.cpp │ ├── 17.3.2.dayOfTheWeek.java │ ├── 17.3.2.dayOfTheWeek.py │ ├── 17.4.1.canMeasureWater.cpp │ ├── 17.4.1.canMeasureWater.java │ ├── 17.4.1.canMeasureWater.py │ ├── 17.4.2.canMeasureWater.cpp │ ├── 17.4.2.canMeasureWater.java │ ├── 17.4.2.canMeasureWater.py │ ├── 17.4.3.canMeasureWater.cpp │ ├── 17.4.3.canMeasureWater.java │ ├── 17.4.3.canMeasureWater.py │ ├── 17.4.4.canMeasureWater.cpp │ ├── 17.4.4.canMeasureWater.java │ ├── 17.4.4.canMeasureWater.py │ ├── 17.5.1.poorPigs.cpp │ ├── 17.5.1.poorPigs.java │ ├── 17.5.1.poorPigs.py │ ├── 17.5.2.poorPigs.cpp │ ├── 17.5.2.poorPigs.java │ └── 17.5.2.poorPigs.py ├── ch18 │ ├── 18.1.1.cpp │ ├── 18.1.1.java │ ├── 18.1.1.py │ ├── 18.1.2.cpp │ ├── 18.1.2.java │ ├── 18.1.2.py │ ├── 18.1.3.cpp │ ├── 18.1.3.java │ ├── 18.1.3.py │ ├── 18.3.1.cpp │ ├── 18.3.1.java │ ├── 18.3.1.py │ ├── 18.3.2.cpp │ ├── 18.3.2.java │ ├── 18.3.2.py │ ├── 18.4.1.cpp │ ├── 18.4.1.java │ ├── 18.4.1.py │ ├── 18.6.1.cpp │ ├── 18.6.1.java │ ├── 18.6.1.py │ ├── 18.6.2.cpp │ ├── 18.6.2.java │ ├── 18.6.2.py │ ├── 18.6.3.cpp │ ├── 18.6.3.java │ ├── 18.6.3.py │ ├── 18.6.4.cpp │ ├── 18.6.4.java │ └── 18.6.4.py ├── ch19 │ ├── 19.1.1. 189.rotate.cpp │ ├── 19.1.1. 189.rotate.java │ ├── 19.1.1. 189.rotate.py │ ├── 19.1.2. 189.rotate.cpp │ ├── 19.1.2. 189.rotate.java │ ├── 19.1.2. 189.rotate.py │ ├── 19.1.3. 189.rotate.cpp │ ├── 19.1.3. 189.rotate.java │ ├── 19.1.3. 189.rotate.py │ ├── 19.1.4. 189.rotate.cpp │ ├── 19.1.4. 189.rotate.java │ ├── 19.1.4. 189.rotate.py │ ├── 19.1.5. 189.rotate.cpp │ ├── 19.1.5. 189.rotate.java │ ├── 19.1.5. 189.rotate.py │ ├── 19.2.1. 61.rotateRight.cpp │ ├── 19.2.1. 61.rotateRight.java │ ├── 19.2.1. 61.rotateRight.py │ ├── 19.3.1. 72.minDistance.cpp │ ├── 19.3.1. 72.minDistance.java │ ├── 19.3.1. 72.minDistance.py │ ├── 19.3.2. 72.minDistance.cpp │ ├── 19.3.2. 72.minDistance.java │ ├── 19.3.2. 72.minDistance.py │ ├── 19.3.3. 72.minDistance.cpp │ ├── 19.3.3. 72.minDistance.java │ ├── 19.3.3. 72.minDistance.py │ ├── 19.3.4. 72.minDistance.cpp │ ├── 19.3.4. 72.minDistance.java │ ├── 19.3.4. 72.minDistance.py │ ├── 19.4.1. 215.findKthLargest.cpp │ ├── 19.4.1. 215.findKthLargest.java │ ├── 19.4.1. 215.findKthLargest.py │ ├── 19.4.2. 703.KthLargest.cpp │ ├── 19.4.2. 703.KthLargest.java │ ├── 19.4.2. 703.KthLargest.py │ ├── 19.4.3. 230.kthSmallest.cpp │ ├── 19.4.3. 230.kthSmallest.java │ ├── 19.4.3. 230.kthSmallest.py │ ├── 19.4.4. 378.kthSmallest.cpp │ ├── 19.4.4. 378.kthSmallest.java │ ├── 19.4.4. 378.kthSmallest.py │ ├── 19.4.5. 668.findKthNumber.cpp │ ├── 19.4.5. 668.findKthNumber.java │ └── 19.4.5. 668.findKthNumber.py └── ch20 │ ├── 20.1.1. 1284.minFlips.cpp │ ├── 20.1.1. 1284.minFlips.java │ ├── 20.1.1. 1284.minFlips.py │ ├── 20.1.2. 73.setZeroes.cpp │ ├── 20.1.2. 73.setZeroes.java │ ├── 20.1.2. 73.setZeroes.py │ ├── 20.1.3. 73.setZeroes.cpp │ ├── 20.1.3. 73.setZeroes.java │ ├── 20.1.3. 73.setZeroes.py │ ├── 20.2.1. 1291.sequentialDigits.cpp │ ├── 20.2.1. 1291.sequentialDigits.java │ ├── 20.2.1. 1291.sequentialDigits.py │ ├── 20.2.2. 1291.sequentialDigits.cpp │ ├── 20.2.2. 1291.sequentialDigits.java │ ├── 20.2.2. 1291.sequentialDigits.py │ ├── 20.2.3. 1291.sequentialDigits.cpp │ ├── 20.2.3. 1291.sequentialDigits.java │ ├── 20.2.3. 1291.sequentialDigits.py │ ├── 20.2.4. 127.ladderLength.cpp │ ├── 20.2.4. 127.ladderLength.java │ ├── 20.2.4. 127.ladderLength.py │ ├── 20.2.5. 127.ladderLength.cpp │ ├── 20.2.5. 127.ladderLength.java │ ├── 20.2.5. 127.ladderLength.py │ ├── 20.3.1. 1277.countSquares.cpp │ ├── 20.3.1. 1277.countSquares.java │ ├── 20.3.1. 1277.countSquares.py │ ├── 20.3.2. 1297.maxFreq.cpp │ ├── 20.3.2. 1297.maxFreq.java │ ├── 20.3.2. 1297.maxFreq.py │ ├── 20.3.3. 1297.maxFreq.cpp │ ├── 20.3.3. 1297.maxFreq.java │ ├── 20.3.3. 1297.maxFreq.py │ ├── 20.4.1. 289.gameOfLife.cpp │ ├── 20.4.1. 289.gameOfLife.java │ ├── 20.4.1. 289.gameOfLife.py │ ├── 20.4.2. 289.gameOfLife.cpp │ ├── 20.4.2. 289.gameOfLife.java │ └── 20.4.2. 289.gameOfLife.py ├── docs ├── 404.html └── CNAME ├── external ├── dark-mode.d.ts └── dark-mode.js ├── googlee3ba7abca18bfdec.html ├── jest.config.js ├── package-lock.json ├── package.json ├── patches └── gitalk+1.7.2.patch ├── public ├── favicon.ico ├── googlee3ba7abca18bfdec.html └── index.html ├── scripts ├── deploy.sh ├── generateBookCode.mjs ├── generateLeetCodeContestHistory.mjs └── generateLeetCodeContestHistory.py ├── src ├── App.vue ├── apis │ ├── 91.js │ ├── github.js │ ├── meta.js │ ├── request.js │ └── user.js ├── assets │ ├── bronze_metal.svg │ ├── card.svg │ ├── github.png │ ├── golden_metal.svg │ ├── logo.png │ ├── official_account.png │ ├── official_account_QR_code.jpg │ ├── silver_metal.svg │ ├── wechat.png │ ├── wechat_QR_code.jpg │ └── zhihu.png ├── auth.ts ├── common │ └── less │ │ └── layout.less ├── components │ ├── Card.vue │ ├── Code.vue │ ├── Counter.vue │ ├── LayoutContent │ │ ├── LayoutContent.vue │ │ └── index.ts │ ├── LayoutFooter │ │ ├── LayoutFooter.vue │ │ └── index.ts │ ├── LayoutHeader │ │ ├── LayoutHeader.vue │ │ └── index.ts │ ├── components_use.ts │ └── lazy_use.ts ├── config │ └── index.js ├── enhancement │ ├── darken-mode.less │ ├── darken-mode.ts │ ├── index.ts │ └── nprogress.ts ├── external.d.ts ├── layouts │ ├── BasicLayout.vue │ ├── Layout.vue │ └── index.ts ├── main.ts ├── override.less ├── reset.css ├── router │ ├── index.ts │ └── router.config.ts ├── shims-tsx.d.ts ├── shims-vue.d.ts ├── store │ └── index.ts ├── themes │ ├── atom-one-dark.less │ ├── base.less │ ├── blue.js │ ├── index.js │ ├── menu.svg │ ├── outline-base.less │ ├── purple.js │ ├── vue.js │ └── wechat.js ├── utils │ ├── browser.js │ ├── format.js │ ├── highlight-lines.js │ ├── langHighlight.js │ ├── markdown-it-imageflow.js │ ├── markdown-it-li.js │ ├── markdown-it-linkfoot.js │ ├── markdown-it-multiquote.js │ ├── markdown-it-removepre.js │ ├── markdown-it-span.js │ ├── markdown-it-table-container.js │ ├── package.json │ ├── storage.js │ ├── storage.test.js │ └── style.js └── views │ ├── 91 │ ├── 91-article.vue │ ├── 91.db.js │ ├── 91.vue │ ├── 91algo.vue │ ├── QuestionDecription.vue │ ├── contest.js │ ├── db │ │ ├── baseContest.json │ │ ├── contest.json │ │ ├── my-5.json │ │ ├── my-6.json │ │ ├── solution-5.json │ │ ├── solution-6.json │ │ └── weekly-287.json │ ├── faq.vue │ ├── ranking.vue │ ├── recall.vue │ └── students-talk.js │ ├── Book.js │ ├── Book.vue │ ├── BookCode.vue │ ├── Download.vue │ ├── Everyday.vue │ ├── Home.vue │ ├── LastWords.vue │ ├── Markdown.vue │ ├── ProblemDetail.md │ ├── ProblemDetail.vue │ ├── Solution.md │ ├── Solution.vue │ ├── SolutionCode.vue │ ├── SolutionDetail.md │ ├── SolutionDetail.vue │ ├── SolutionStaticData.js │ ├── Video.mobile.less │ ├── Video.pc.less │ ├── Video.vue │ ├── download-data.js │ ├── github.code-workspace │ ├── mark-down-nice.vue │ └── solutionCode.js ├── tests └── unit │ ├── example.spec.ts │ └── example.spec.ts.rej ├── tsconfig.json ├── vue.config.js └── yarn.lock /.browserslistrc: -------------------------------------------------------------------------------- 1 | > 1% 2 | last 2 versions 3 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | code/**/*.js -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | /js 5 | /css 6 | /img 7 | 404.html 8 | index.html 9 | favicon.ico 10 | 11 | # local env files 12 | .env.local 13 | .env.*.local 14 | 15 | # Log files 16 | npm-debug.log* 17 | yarn-debug.log* 18 | yarn-error.log* 19 | 20 | # Editor directories and files 21 | .idea 22 | .vscode 23 | *.suo 24 | *.ntvs* 25 | *.njsproj 26 | *.sln 27 | *.sw? 28 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "semi": false 4 | } 5 | -------------------------------------------------------------------------------- /.vcmrc: -------------------------------------------------------------------------------- 1 | { 2 | "types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert"], 3 | "scope": { 4 | "required": false, 5 | "allowed": ["*"], 6 | "validate": false, 7 | "multiple": false 8 | }, 9 | "warnOnFail": false, 10 | "maxSubjectLength": 100, 11 | "subjectPattern": ".+", 12 | "subjectPatternErrorMsg": "subject does not match subject pattern!", 13 | "helpMessage": "", 14 | "autoFix": false 15 | } -------------------------------------------------------------------------------- /CNAME: -------------------------------------------------------------------------------- 1 | leetcode-solution.cn -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | const plugins = [ 2 | [ 3 | 'import', 4 | { libraryName: 'ant-design-vue', libraryDirectory: 'es', style: true } 5 | ] 6 | ] 7 | if (process.env.NODE_ENV === 'production') { 8 | plugins.push('transform-remove-console') 9 | } 10 | 11 | module.exports = { 12 | presets: ['@vue/cli-plugin-babel/preset'], 13 | plugins: plugins 14 | } 15 | -------------------------------------------------------------------------------- /code/ch01/1.3.1.sum.cpp: -------------------------------------------------------------------------------- 1 | 2 | int sum(int n) { 3 | int res = 0; 4 | for (int i = 0; i < n; i++) { 5 | res += i; 6 | } 7 | return res; 8 | } 9 | -------------------------------------------------------------------------------- /code/ch01/1.3.1.sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int sum(int n) { 3 | int res = 0; 4 | for (int i = 0; i < n; i++) { 5 | res += i; 6 | } 7 | return res; 8 | } 9 | } -------------------------------------------------------------------------------- /code/ch01/1.3.1.sum.js: -------------------------------------------------------------------------------- 1 | function sum(n) { 2 | let res = 0 3 | for (let i = 0; i < n; i++) { 4 | res += i 5 | } 6 | return res 7 | } 8 | -------------------------------------------------------------------------------- /code/ch01/1.3.1.sum.py: -------------------------------------------------------------------------------- 1 | def sum(n: int) -> int: 2 | res = 0 3 | for i in range(1, n + 1): 4 | res += i 5 | return res 6 | -------------------------------------------------------------------------------- /code/ch01/1.3.2.two-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &nums, int target) { 4 | 5 | unordered_map mapper; 6 | 7 | for (int i = 0; i < nums.size(); i++) { 8 | if (mapper.find(target - nums[i]) != mapper.end()) 9 | return {mapper[target - nums[i]], i}; 10 | 11 | mapper[nums[i]] = i; 12 | } 13 | return {}; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /code/ch01/1.3.2.two-sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | Map mapper = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | if (mapper.containsKey(target - nums[i])) { 6 | return new int[] {mapper.get(target - nums[i]), i}; 7 | } 8 | mapper.put(nums[i], i); 9 | } 10 | return new int[] {}; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/ch01/1.3.2.two-sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | const twoSum = function(nums, target) { 7 | const n = nums.length 8 | const mapper = {} 9 | for (let i = 0; i < n; i++) { 10 | if (mapper[target - nums[i]] !== void 0) 11 | return [mapper[target - nums[i]], i] 12 | mapper[nums[i]] = i 13 | } 14 | return [] 15 | } 16 | -------------------------------------------------------------------------------- /code/ch01/1.3.2.two-sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | n = len(nums) 4 | mapper = {} 5 | for i in range(n): 6 | if target - nums[i] in mapper: 7 | return [mapper[target - nums[i]], i] 8 | else: 9 | mapper[nums[i]] = i 10 | 11 | return [] 12 | -------------------------------------------------------------------------------- /code/ch01/1.3.3. dailyTemperatures.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dailyTemperatures(vector &T) { 4 | stack st; 5 | vector ans(T.size(), 0); 6 | for (int i = 0; i < T.size(); i++) { 7 | while (!st.empty() && T[i] > T[st.top()]) { 8 | int top = st.top(); 9 | st.pop(); 10 | ans[top] = i - top; 11 | } 12 | st.push(i); 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /code/ch01/1.3.3. dailyTemperatures.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} T 3 | * @return {number[]} 4 | */ 5 | const dailyTemperatures = function(T) { 6 | const stack = [] 7 | const ans = [] 8 | 9 | for (let i = 0; i < T.length; i++) { 10 | ans[i] = 0 11 | while (stack.length !== 0 && T[i] > T[stack[stack.length - 1]]) { 12 | const peek = stack.pop() 13 | ans[peek] = i - peek 14 | } 15 | stack.push(i) 16 | } 17 | 18 | return ans 19 | } 20 | -------------------------------------------------------------------------------- /code/ch01/1.3.3. dailyTemperatures.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def dailyTemperatures(self, T: List[int]) -> List[int]: 3 | stack = [] 4 | ans = [0] * len(T) 5 | for i in range(len(T)): 6 | while stack and T[i] > T[stack[-1]]: 7 | peek = stack.pop(-1) 8 | ans[peek] = i - peek 9 | stack.append(i) 10 | return ans 11 | -------------------------------------------------------------------------------- /code/ch01/1.3.3.dailyTemperatures.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] dailyTemperatures(int[] T) { 3 | Stack stack = new Stack(); 4 | int[] ans = new int[T.length]; 5 | for (int i = 0; i < T.length; i++) { 6 | while (!stack.empty() && T[i] > T[stack.peek()]) { 7 | int top = stack.pop(); 8 | ans[top] = i - top; 9 | } 10 | stack.push(i); 11 | } 12 | return ans; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /code/ch01/1.3.4.numRescueBoats.cpp: -------------------------------------------------------------------------------- 1 | 2 | int binarySearch(vector& nums, int target){ 3 | if(nums.size() == 0) 4 | return -1; 5 | 6 | int left = 0, right = nums.size() - 1; 7 | while(left <= right){ 8 | int mid = left + ((right - left) >> 1); 9 | if(nums[mid] == target){ return mid; } 10 | // 搜索区间变为 [mid+1, right] 11 | else if(nums[mid] < target) 12 | left = mid + 1; 13 | // 搜索区间变为 [left, mid - 1] 14 | else 15 | right = mid - 1; 16 | } 17 | return -1; 18 | } 19 | -------------------------------------------------------------------------------- /code/ch01/1.3.4.numRescueBoats.java: -------------------------------------------------------------------------------- 1 | public int binarySearch(int[] nums, int target) { 2 | // 左右都闭合的区间 [l, r] 3 | int left = 0; 4 | int right = nums.length - 1; 5 | 6 | while(left <= right) { 7 | int mid = left + (right - left) / 2; 8 | if(nums[mid] == target) 9 | return mid; 10 | if (nums[mid] < target) 11 | // 搜索区间变为 [mid+1, right] 12 | left = mid + 1; 13 | if (nums[mid] > target) 14 | // 搜索区间变为 [left, mid - 1] 15 | right = mid - 1; 16 | } 17 | return -1; 18 | } -------------------------------------------------------------------------------- /code/ch01/1.3.4.numRescueBoats.js: -------------------------------------------------------------------------------- 1 | function binarySearch(nums, target) { 2 | let left = 0 3 | let right = nums.length - 1 4 | while (left <= right) { 5 | const mid = Math.floor(left + (right - left) / 2) 6 | if (nums[mid] == target) return mid 7 | if (nums[mid] < target) 8 | // 搜索区间变为 [mid+1, right] 9 | left = mid + 1 10 | if (nums[mid] > target) 11 | // 搜索区间变为 [left, mid - 1] 12 | right = mid - 1 13 | } 14 | return -1 15 | } 16 | -------------------------------------------------------------------------------- /code/ch01/1.3.4.numRescueBoats.py: -------------------------------------------------------------------------------- 1 | def binarySearch(nums, target): 2 | # 左右都闭合的区间 [l, r] 3 | l, r = 0, len(nums) - 1 4 | while l <= r: 5 | mid = (left + right) >> 1 6 | if nums[mid] == target: 7 | return mid 8 | # 搜索区间变为 [mid+1, right] 9 | if nums[mid] < target: 10 | l = mid + 1 11 | # 搜索区间变为 [left, mid - 1] 12 | if nums[mid] > target: 13 | r = mid - 1 14 | return -1 -------------------------------------------------------------------------------- /code/ch01/1.3.5.fibonacci.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fibonacci(int n) { 4 | if (n < 2) { 5 | return n 6 | } 7 | return fibonacci(n - 1) + fibonacci(n - 2); 8 | } 9 | }; -------------------------------------------------------------------------------- /code/ch01/1.3.5.fibonacci.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fibonacci(int n) { 3 | if (n < 2) return n; 4 | return fibonacci(n - 1) + fibonacci(n - 2); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /code/ch01/1.3.5.fibonacci.js: -------------------------------------------------------------------------------- 1 | function fibonacci(n) { 2 | if (n < 2) return n 3 | return fibonacci(n - 1) + fibonacci(n - 2) 4 | } 5 | -------------------------------------------------------------------------------- /code/ch01/1.3.5.fibonacci.py: -------------------------------------------------------------------------------- 1 | def fibonacci(n: int) -> int: 2 | if n < 2: 3 | return n 4 | return fibonacci(n - 1) + fibonacci(n - 2) 5 | -------------------------------------------------------------------------------- /code/ch01/1.3.6.permute.js: -------------------------------------------------------------------------------- 1 | function backtrack(list, tempList, nums) { 2 | if (tempList.length === nums.length) return list.push([...tempList]) 3 | for (let i = 0; i < nums.length; i++) { 4 | if (tempList.includes(nums[i])) continue 5 | tempList.push(nums[i]) 6 | backtrack(list, tempList, nums) 7 | tempList.pop() 8 | } 9 | } 10 | /** 11 | * @param {number[]} nums 12 | * @return {number[][]} 13 | */ 14 | const permute = function(nums) { 15 | const list = [] 16 | backtrack(list, [], nums) 17 | return list 18 | } 19 | -------------------------------------------------------------------------------- /code/ch02/2.1.1.twoSum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &nums, int target) { 4 | int n = nums.size(); 5 | sort(nums.begin(), nums.end()); 6 | int l = 0; 7 | int r = n - 1; 8 | while (l < r) { 9 | if (nums[l] + nums[r] < target) { 10 | l += 1; 11 | } else if (nums[l] + nums[r] > target) { 12 | r -= 1; 13 | } else { 14 | return {l, r}; 15 | } 16 | } 17 | return {}; 18 | } 19 | }; -------------------------------------------------------------------------------- /code/ch02/2.1.1.twoSum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | int n = nums.length; 4 | Arrays.sort(nums); 5 | int l = 0; 6 | int r = n - 1; 7 | while (l < r) { 8 | if (nums[l] + nums[r] < target) { 9 | l += 1; 10 | } else if (nums[l] + nums[r] > target) { 11 | r -= 1; 12 | } else { 13 | return new int[] {l, r}; 14 | } 15 | } 16 | return new int[] {}; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/ch02/2.1.1.twoSum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | const twoSum = function(nums, target) { 7 | const n = nums.length 8 | nums.sort((a, b) => a - b) 9 | let l = 0 10 | let r = n - 1 11 | while (l < r) { 12 | if (nums[l] + nums[r] < target) { 13 | l += 1 14 | } else if (nums[l] + nums[r] > target) { 15 | r -= 1 16 | } else { 17 | return [l, r] 18 | } 19 | } 20 | return [] 21 | } 22 | -------------------------------------------------------------------------------- /code/ch02/2.1.1.twoSum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | n = len(nums) 4 | nums.sort() 5 | l = 0 6 | r = n - 1 7 | while l < r: 8 | if nums[l] + nums[r] < target: 9 | l += 1 10 | elif nums[l] + nums[r] > target: 11 | r -= 1 12 | else: 13 | return [l, r] 14 | return [] 15 | -------------------------------------------------------------------------------- /code/ch02/2.1.2.twoSum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &nums, int target) { 4 | 5 | unordered_map mapper; 6 | 7 | for (int i = 0; i < nums.size(); i++) { 8 | if (mapper.find(target - nums[i]) != mapper.end()) 9 | return {mapper[target - nums[i]], i}; 10 | 11 | mapper[nums[i]] = i; 12 | } 13 | return {}; 14 | } 15 | }; -------------------------------------------------------------------------------- /code/ch02/2.1.2.twoSum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | Map mapper = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | if (mapper.containsKey(target - nums[i])) { 6 | return new int[] {mapper.get(target - nums[i]), i}; 7 | } 8 | mapper.put(nums[i], i); 9 | } 10 | return new int[] {}; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/ch02/2.1.2.twoSum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | const twoSum = function(nums, target) { 7 | const n = nums.length 8 | const mapper = {} 9 | for (let i = 0; i < n; i++) { 10 | if (mapper[target - nums[i]] !== void 0) 11 | return [mapper[target - nums[i]], i] 12 | mapper[nums[i]] = i 13 | } 14 | return [] 15 | } 16 | -------------------------------------------------------------------------------- /code/ch02/2.1.2.twoSum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | n = len(nums) 4 | mapper = {} 5 | for i in range(n): 6 | if target - nums[i] in mapper: 7 | # 实际上这里返回的索引顺序是不重要的 8 | # 即返回 [i, mapper[target - nums[i]]] 也是正确的 9 | return [mapper[target - nums[i]], i] 10 | else: 11 | mapper[nums[i]] = i 12 | 13 | return [] 14 | -------------------------------------------------------------------------------- /code/ch02/2.3.1.fourSum.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch02/2.3.1.fourSum.js -------------------------------------------------------------------------------- /code/ch02/2.3.2.fourSum.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch02/2.3.2.fourSum.js -------------------------------------------------------------------------------- /code/ch02/2.4.1.fourSumCount.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fourSumCount( 3 | self, A: List[int], B: List[int], C: List[int], D: List[int] 4 | ) -> int: 5 | mapper = {} 6 | res = 0 7 | for i in A: 8 | for j in B: 9 | mapper[i + j] = mapper.get(i + j, 0) + 1 10 | 11 | for i in C: 12 | for j in D: 13 | res += mapper.get(-1 * (i + j), 0) 14 | return res 15 | -------------------------------------------------------------------------------- /code/ch02/2.6.1.maxSubArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxSubArray(const vector& nums) { 8 | int n = nums.size(); 9 | int maxSum = -0x7fffffff, total = 0; 10 | for (int i = 0; i < n; i++) { 11 | total = 0; 12 | for (int j = i; j < n; j++) { 13 | total += nums[j]; 14 | maxSum = max(maxSum, total); 15 | } 16 | } 17 | return maxSum; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch02/2.6.1.maxSubArray.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int maxSubArray(int[] nums) { 5 | int n = nums.length; 6 | int maxSum = -0x7fffffff, total = 0; 7 | for (int i = 0; i < n; i++) { 8 | total = 0; 9 | for (int j = i; j < n; j++) { 10 | total += nums[j]; 11 | maxSum = Math.max(maxSum, total); 12 | } 13 | } 14 | return maxSum; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /code/ch02/2.6.1.maxSubArray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSubArray(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | maxSum = float("-inf") 5 | total = 0 6 | for i in range(n): 7 | total = 0 8 | for j in range(i, n): 9 | total += nums[j] 10 | maxSum = max(maxSum, total) 11 | 12 | return maxSum 13 | -------------------------------------------------------------------------------- /code/ch02/2.6.3.maxSubArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxSubArray(const vector& nums) { 8 | int n = nums.size(); 9 | int max_sum_ending_curr_idx = nums[0]; 10 | int max_sum = nums[0]; 11 | for (int i = 1; i < n; i++) { 12 | max_sum_ending_curr_idx = max( 13 | max_sum_ending_curr_idx + nums[i], 14 | nums[i] 15 | ); 16 | max_sum = max( 17 | max_sum_ending_curr_idx, 18 | max_sum 19 | ); 20 | } 21 | return max_sum; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /code/ch02/2.6.3.maxSubArray.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int maxSubArray(int[] nums) { 5 | int n = nums.length; 6 | int max_sum_ending_curr_idx = nums[0]; 7 | int max_sum = nums[0]; 8 | for (int i = 1; i < n; i++) { 9 | max_sum_ending_curr_idx = Math.max( 10 | max_sum_ending_curr_idx + nums[i], 11 | nums[i] 12 | ); 13 | max_sum = Math.max( 14 | max_sum_ending_curr_idx, 15 | max_sum 16 | ); 17 | } 18 | return max_sum; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/ch02/2.6.3.maxSubArray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSubArray(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | max_sum_ending_curr_index = max_sum = nums[0] 5 | for i in range(1, n): 6 | max_sum_ending_curr_index = max( 7 | max_sum_ending_curr_index + nums[i], nums[i] 8 | ) 9 | max_sum = max(max_sum_ending_curr_index, max_sum) 10 | 11 | return max_sum 12 | -------------------------------------------------------------------------------- /code/ch02/2.6.4.maxSubArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int maxSubArray(const vector& nums) { 8 | int n = nums.size(); 9 | int maxSum = nums[0], minSum = 0, sum = 0; 10 | for (int i = 0; i < n; i++) { 11 | sum += nums[i]; 12 | maxSum = max(maxSum, sum - minSum); 13 | minSum = min(minSum, sum); 14 | } 15 | return maxSum; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /code/ch02/2.6.4.maxSubArray.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int maxSubArray(int[] nums) { 5 | int n = nums.length; 6 | int maxSum = nums[0], minSum = 0, sum = 0; 7 | for (int i = 0; i < n; i++) { 8 | sum += nums[i]; 9 | maxSum = Math.max(maxSum, sum - minSum); 10 | minSum = Math.min(minSum, sum); 11 | } 12 | return maxSum; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /code/ch02/2.6.4.maxSubArray.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSubArray(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | maxSum = nums[0] 5 | minSum = sum = 0 6 | for i in range(n): 7 | sum += nums[i] 8 | maxSum = max(maxSum, sum - minSum) 9 | minSum = min(minSum, sum) 10 | 11 | return maxSum 12 | -------------------------------------------------------------------------------- /code/ch02/2.7.1.largestNumber.py: -------------------------------------------------------------------------------- 1 | import functools 2 | 3 | 4 | class Solution: 5 | def largestNumber(self, nums: List[int]) -> str: 6 | s = [str(i) for i in nums] 7 | 8 | def comp(a, b): 9 | if (a + b) > (b + a): 10 | return 1 11 | if (a + b) < (b + a): 12 | return -1 13 | return 0 14 | 15 | # 这里加了一次 int 转换过程,用于处理第一个数字为 0 的情况 16 | s.sort(reverse=True, key=functools.cmp_to_key(comp)) 17 | return str(int("".join(s))) 18 | -------------------------------------------------------------------------------- /code/ch02/2.7.2.largestNumber.py: -------------------------------------------------------------------------------- 1 | def largestNumber(self, nums: List[int]) -> str: 2 | return str(int("".join(sorted(map(str, nums), cmp=lambda a, b: cmp(b + a, a + b))))) 3 | -------------------------------------------------------------------------------- /code/ch02/2.9.1.largestDivisibleSubset.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestDivisibleSubset(self, nums: List[int]) -> List[int]: 3 | # base case for simplicity 4 | S = {-1: set()} 5 | nums.sort() 6 | for x in nums: 7 | temp = [] 8 | for d in S: 9 | if x % d == 0: 10 | S[d].add(x) 11 | temp.append(S[d]) 12 | S[d].remove(x) 13 | S[x] = max(temp, key=len) | {x} 14 | return list(max(S.values(), key=len)) 15 | -------------------------------------------------------------------------------- /code/ch02/2.9.2.largetDivisibleSubset.cpp: -------------------------------------------------------------------------------- 1 | // reference 2.9.1.largestDivisibleSubset.cpp 2 | 3 | -------------------------------------------------------------------------------- /code/ch02/2.9.2.largetDivisibleSubset.java: -------------------------------------------------------------------------------- 1 | // reference 2.9.1.largestDivisibleSubset.java 2 | 3 | -------------------------------------------------------------------------------- /code/ch02/2.9.2.largetDivisibleSubset.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestDivisibleSubset(self, nums: List[int]) -> List[int]: 3 | S = {-1: set()} 4 | for x in sorted(nums): 5 | S[x] = max((S[d] for d in S if x % d == 0), key=len) | {x} 6 | return list(max(S.values(), key=len)) 7 | -------------------------------------------------------------------------------- /code/ch03/3.1.1.isPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool isPalindrome(std::string s, int n) { 5 | int l = 0; 6 | int r = n - 1; 7 | while (l < r) { 8 | if (s[l] != s[r]) 9 | return false; 10 | l += 1; 11 | r -= 1; 12 | } 13 | return true; 14 | } 15 | -------------------------------------------------------------------------------- /code/ch03/3.1.1.isPalindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(String s, int n) { 3 | int l = 0; 4 | int r = n - 1; 5 | while (l < r) { 6 | if (s.charAt(l) != s.charAt(r)) return false; 7 | l += 1; 8 | r -= 1; 9 | } 10 | return true; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/ch03/3.1.1.isPalindrome.js: -------------------------------------------------------------------------------- 1 | function isPalindrome(s, n) { 2 | let l = 0 3 | let r = n - 1 4 | while (l < r) { 5 | if (s[l] !== s[r]) return false 6 | l += 1 7 | r -= 1 8 | } 9 | return true 10 | } 11 | -------------------------------------------------------------------------------- /code/ch03/3.1.1.isPalindrome.py: -------------------------------------------------------------------------------- 1 | def isPalindrome(s: str, n: int) -> bool: 2 | l = 0 3 | r = n - 1 4 | while l < r: 5 | if s[l] != s[r]: 6 | return False 7 | l += 1 8 | r -= 1 9 | 10 | return True 11 | -------------------------------------------------------------------------------- /code/ch03/3.2.1.reverseList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Solution { 5 | public: 6 | ListNode *reverseList(ListNode *head) { 7 | if (!head) return NULL; 8 | ListNode *prev = NULL, *cur = head; 9 | while (cur) { 10 | cur->next = prev; 11 | prev = cur; 12 | cur = cur->next; 13 | } 14 | return prev; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /code/ch03/3.2.1.reverseList.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode reverseList(ListNode head) { 3 | if (!head) return null; 4 | ListNode prev = null, cur = head; 5 | while (cur) { 6 | cur.next = prev; 7 | prev = cur; 8 | cur = cur.next; 9 | } 10 | return prev; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/ch03/3.2.1.reverseList.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseList(self, head: ListNode) -> ListNode: 3 | if not head: 4 | return None 5 | prev = None 6 | cur = head 7 | while cur: 8 | cur.next, prev, cur = prev, cur, cur.next 9 | return prev 10 | -------------------------------------------------------------------------------- /code/ch03/3.2.2.isPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Solution { 5 | public: 6 | bool isPalindrome(ListNode *head) { 7 | ListNode *pre = NULL; 8 | ListNode *slow = head, *fast = head; 9 | while (fast && fast->next) { 10 | fast = fast->next->next; 11 | ListNode *next = slow->next; 12 | slow->next = pre; 13 | pre = slow; 14 | slow = next; 15 | } 16 | if (fast) slow = slow->next; 17 | while (slow) { 18 | if (slow->val != pre->val) return false; 19 | pre = pre->next; 20 | slow = slow->next; 21 | } 22 | return true; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /code/ch03/3.2.2.isPalindrome.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPalindrome(ListNode head) { 3 | ListNode pre = null; 4 | ListNode slow = head, fast = head; 5 | while (fast != null && fast.next != null) { 6 | fast = fast.next.next; 7 | ListNode next = slow.next; 8 | slow.next = pre; 9 | pre = slow; 10 | slow = next; 11 | } 12 | if (fast != null) slow = slow.next; 13 | while (slow != null) { 14 | if (slow.val != pre.val) return false; 15 | pre = pre.next; 16 | slow = slow.next; 17 | } 18 | return true; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/ch03/3.3.1.isPalindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if (x < 0) return false; 5 | if (x == 0) return true; 6 | if (x % 10 == 0) return false; 7 | int res = 0, copy = x; 8 | while (x > 0) { 9 | res = res * 10 + (x % 10); 10 | x /= 10; 11 | } 12 | return copy == res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /code/ch03/3.3.1.isPalindrome.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPalindrome(int x) { 3 | if (x < 0) return false; 4 | if (x == 0) return true; 5 | if (x % 10 == 0) return false; 6 | int res = 0, copy = x; 7 | while (x > 0) { 8 | res = res * 10 + (x % 10); 9 | x = x / 10; 10 | } 11 | return copy == res; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /code/ch03/3.3.1.isPalindrome.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | if x < 0: 4 | return False 5 | if x == 0: 6 | return True 7 | if x % 10 == 0: 8 | return False 9 | 10 | res = 0 11 | copy = x 12 | while x > 0: 13 | res = res * 10 + (x % 10) 14 | x = x // 10 15 | 16 | return copy == res 17 | -------------------------------------------------------------------------------- /code/ch03/3.5.1.longestPalindromeSubseq.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int longestPalindromeSubseq(String s) { 5 | int n = s.length(); 6 | int[][] dp = new int[n][n]; 7 | for (int i = 0; i < n; i++) 8 | for (int j = 0; j < n; j++) dp[i][j] = 0; 9 | for (int i = n - 1; i >= 0; i--) 10 | for (int j = i; j < n; j++) { 11 | if (i == j) dp[i][j] = 1; 12 | else if (s.charAt(i) == s.charAt(j)) 13 | dp[i][j] = dp[i + 1][j - 1] + 2; 14 | else 15 | dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); 16 | } 17 | return dp[0][n - 1]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/ch03/3.5.1.longestPalindromeSubseq.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPalindromeSubseq(self, s: str) -> int: 3 | n = len(s) 4 | dp = [[0] * n for i in range(n)] 5 | 6 | for i in reversed(range(n)): 7 | for j in range(i, n): 8 | if i == j: 9 | dp[i][j] = 1 10 | elif s[i] == s[j]: 11 | dp[i][j] = dp[i + 1][j - 1] + 2 12 | else: 13 | dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) 14 | return dp[0][n - 1] 15 | -------------------------------------------------------------------------------- /code/ch03/3.5.2.longestPalindromeSubseq.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int longestPalindromeSubseq(String s) { 5 | int n = s.length(); 6 | int[] pre = new int[n]; 7 | int[] cur = new int[n]; 8 | for (int i = 0; i < n; i++) pre[i] = cur[i] = 0; 9 | for (int i = n - 1; i >= 0; i--) { 10 | for (int j = i; j < n; j++) { 11 | if (i == j) cur[j] = 1; 12 | else if (s.charAt(i) == s.charAt(j)) 13 | cur[j] = pre[j - 1] + 2; 14 | else 15 | cur[j] = Math.max(pre[j], cur[j - 1]); 16 | } 17 | for (int j = 0; j < n; j++) pre[j] = cur[j]; 18 | } 19 | return pre[n - 1]; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code/ch03/3.5.2.longestPalindromeSubseq.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPalindromeSubseq(self, s: str) -> int: 3 | n = len(s) 4 | pre = [0] * n 5 | cur = [0] * n 6 | 7 | for i in reversed(range(n)): 8 | for j in range(i, n): 9 | if i == j: 10 | cur[j] = 1 11 | elif s[i] == s[j]: 12 | cur[j] = pre[j - 1] + 2 13 | else: 14 | cur[j] = max(pre[j], cur[j - 1]) 15 | pre = cur.copy() 16 | return pre[-1] 17 | -------------------------------------------------------------------------------- /code/ch03/3.6.1.superpalindromesInRange.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | class Solution: 5 | def superpalindromesInRange(self, L: str, R: str) -> int: 6 | cnt = 0 7 | 8 | def validPalindrome(s: str) -> bool: 9 | l = 0 10 | r = len(s) - 1 11 | while l < r: 12 | if s[l] != s[r]: 13 | return False 14 | l += 1 15 | r -= 1 16 | return True 17 | 18 | for i in range(math.floor(int(L) ** 0.5), math.ceil(int(R) ** 0.5)): 19 | if validPalindrome(str(i)) and validPalindrome(str(i ** 2)): 20 | cnt += 1 21 | return cnt 22 | -------------------------------------------------------------------------------- /code/ch04/4.1.1.CountAndSay.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countAndSay(self, n: int) -> str: 3 | ans = "1" 4 | for i in range(1, n): 5 | tmp = "" 6 | current_char, char_count = ans[0], 0 7 | for j in range(len(ans)): 8 | if ans[j] != current_char: 9 | tmp += str(char_count) + current_char 10 | current_char, char_count = ans[j], 1 11 | else: 12 | char_count += 1 13 | tmp += str(char_count) + current_char 14 | ans = tmp 15 | return ans 16 | 17 | -------------------------------------------------------------------------------- /code/ch05/5.1.1-path-sum.cpp: -------------------------------------------------------------------------------- 1 | struct TreeNode { 2 | int val; 3 | TreeNode *left; 4 | TreeNode *right; 5 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 6 | }; 7 | class Solution { 8 | public: 9 | bool hasPathSum(TreeNode* root, int sum) { 10 | if(root == NULL) return false; 11 | 12 | if(root->left == NULL && root->right == NULL) { 13 | if(root->val == sum) return true; 14 | 15 | return false; 16 | } 17 | 18 | return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root.val); 19 | } 20 | 21 | }; -------------------------------------------------------------------------------- /code/ch05/5.1.1-path-sum.java: -------------------------------------------------------------------------------- 1 | public class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | 6 | TreeNode(int x) { 7 | val = x; 8 | } 9 | } 10 | 11 | class Solution { 12 | public boolean hasPathSum(TreeNode root, int sum) { 13 | if (root == null) return false; 14 | 15 | sum -= root.val; 16 | 17 | if ((root.left == null) && (root.right == null)) return (sum == 0); 18 | 19 | return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); 20 | } 21 | } -------------------------------------------------------------------------------- /code/ch05/5.1.1-path-sum.py: -------------------------------------------------------------------------------- 1 | class TreeNode: 2 | def __init__(self, x): 3 | self.val = x 4 | self.left = None 5 | self.right = None 6 | 7 | 8 | class Solution: 9 | def hasPathSum(self, root: TreeNode, sum: int) -> bool: 10 | if not root: 11 | return False 12 | 13 | if root.left is None and root.right is None: 14 | if root.val == sum: 15 | return True 16 | return False 17 | 18 | return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum( 19 | root.right, sum - root.val 20 | ) 21 | -------------------------------------------------------------------------------- /code/ch05/5.1.3-path-sum-II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch05/5.1.3-path-sum-II.cpp -------------------------------------------------------------------------------- /code/ch05/5.1.4-path-sum-II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch05/5.1.4-path-sum-II.cpp -------------------------------------------------------------------------------- /code/ch05/5.2.2-number-of-islands.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch05/5.2.2-number-of-islands.cpp -------------------------------------------------------------------------------- /code/ch05/5.2.2-number-of-islands.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch05/5.2.2-number-of-islands.java -------------------------------------------------------------------------------- /code/ch05/5.2.5-number-of-islands-II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch05/5.2.5-number-of-islands-II.cpp -------------------------------------------------------------------------------- /code/ch05/5.2.5-number-of-islands-II.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/code/ch05/5.2.5-number-of-islands-II.java -------------------------------------------------------------------------------- /code/ch06/6.1.1.binary-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int n = nums.size(); 5 | int l = 0, h = n - 1; 6 | while (l <= h) { 7 | int mid = l + (h - l) / 2; 8 | if (nums[mid] == target) { 9 | return mid; 10 | } else if (nums[mid] < target) { 11 | l = mid + 1; 12 | } else { 13 | h = mid - 1; 14 | } 15 | } 16 | return -1; 17 | } 18 | }; -------------------------------------------------------------------------------- /code/ch06/6.1.1.binary-search.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int n = nums.length; 4 | int l = 0, h = n - 1; 5 | while (l <= h) { 6 | int mid = l + (h - l) / 2; 7 | if (nums[mid] == target) { 8 | return mid; 9 | } else if (nums[mid] < target) { 10 | l = mid + 1; 11 | } else { 12 | h = mid - 1; 13 | } 14 | } 15 | return -1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/ch06/6.1.1.binary-search.py: -------------------------------------------------------------------------------- 1 | # 查找 nums 数组中元素值为 target 的下标,如果不存在则返回 -1 2 | def binary_search(nums: [], target: int) -> int: 3 | n = len(nums) 4 | l, h = 0, n - 1 5 | while l <= h: 6 | mid = l + (h - l) // 2 7 | if nums[mid] == target: 8 | return mid 9 | elif nums[mid] < target: 10 | l = mid + 1 11 | else: 12 | h = mid - 1 13 | return -1 14 | -------------------------------------------------------------------------------- /code/ch06/6.2.1.find-minimum-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& nums) { 4 | int l = 0, h = nums.size() - 1; 5 | while (l <= h) { 6 | int mid = l + (h - l) / 2; 7 | if (l == h) { 8 | return nums[l]; 9 | } else if (nums[mid] > nums[h]) { 10 | l = mid + 1; 11 | } else { 12 | h = mid; 13 | } 14 | } 15 | return -1; 16 | } 17 | }; -------------------------------------------------------------------------------- /code/ch06/6.2.1.find-minimum-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMin(int[] nums) { 3 | int l = 0, h = nums.length - 1; 4 | while (l <= h) { 5 | int mid = l + (h - l) / 2; 6 | if (l == h) { 7 | return nums[l]; 8 | } else if (nums[mid] > nums[h]) { 9 | l = mid + 1; 10 | } else { 11 | h = mid; 12 | } 13 | } 14 | return -1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /code/ch06/6.2.1.find-minimum-in-rotated-sorted-array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMin(self, nums: List[int]) -> int: 3 | l, h = 0, len(nums) - 1 4 | while l <= h: 5 | mid = l + (h - l) // 2 6 | if l == h: 7 | return nums[l] 8 | elif nums[mid] > nums[h]: 9 | l = mid + 1 10 | elif nums[mid] < nums[h]: 11 | h = mid 12 | return -1 13 | -------------------------------------------------------------------------------- /code/ch06/6.3.1.koko-eating-bananas.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minEatingSpeed(self, piles: List[int], H: int) -> int: 3 | # 判断速度 k 是否满足条件 4 | def help(k: int) -> boolean: 5 | cnt = 0 6 | for pile in piles: 7 | cnt += (pile - 1) // k + 1 8 | return cnt <= H 9 | 10 | l, h = 1, max(piles) 11 | while l <= h: 12 | mid = l + (h - l) // 2 13 | if l == h: 14 | return l 15 | if help(mid): 16 | h = mid 17 | else: 18 | l = mid + 1 19 | return -1 20 | -------------------------------------------------------------------------------- /code/ch06/6.4.1.sqrtx.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | long l = 0, h = x; 5 | while (l <= h) { 6 | long mid = l + (h - l) / 2; 7 | if (l == h || l + 1 == h) { 8 | break; 9 | } else if (mid * mid > x) { 10 | h = mid - 1; 11 | } else { 12 | l = mid; 13 | } 14 | } 15 | if (h * h <= x) { 16 | return (int)h; 17 | } else { 18 | return (int)l; 19 | } 20 | } 21 | }; -------------------------------------------------------------------------------- /code/ch06/6.4.1.sqrtx.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int mySqrt(int x) { 3 | long l = 0, h = x; 4 | while (l <= h) { 5 | long mid = l + (h - l) / 2; 6 | if (l == h || l + 1 == h) { 7 | break; 8 | } else if (mid * mid > x) { 9 | h = mid - 1; 10 | } else { 11 | l = mid; 12 | } 13 | } 14 | if (h * h <= x) { 15 | return (int) h; 16 | } else { 17 | return (int) l; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/ch06/6.4.1.sqrtx.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mySqrt(self, x: int) -> int: 3 | l, h = 0, x 4 | while l <= h: 5 | mid = l + (h - l) // 2 6 | if l == h or l + 1 == h: 7 | break 8 | elif mid * mid > x: 9 | h = mid - 1 10 | else: 11 | l = mid 12 | if h * h <= x: 13 | return h 14 | else: 15 | return l 16 | -------------------------------------------------------------------------------- /code/ch06/6.4.2.sqrtx.cpp: -------------------------------------------------------------------------------- 1 | int bs(vector& nums, int x) { 2 | int l = 0, h = nums.size() - 1; 3 | while (l <= h) { 4 | int mid = l + (h - l) / 2; 5 | if (l == h) { 6 | break; 7 | } else if (nums[mid] >= x) { 8 | h = mid; 9 | } else { 10 | l = mid + 1; 11 | } 12 | } 13 | return nums[l]; 14 | } -------------------------------------------------------------------------------- /code/ch06/6.4.2.sqrtx.java: -------------------------------------------------------------------------------- 1 | public int bs(int[] nums, int x) { 2 | int l = 0, h = nums.length - 1; 3 | while (l <= h) { 4 | int mid = l + (h - l) / 2; 5 | if (l == h) { 6 | break; 7 | } else if (nums[mid] >= x) { 8 | h = mid; 9 | } else { 10 | l = mid + 1; 11 | } 12 | } 13 | return nums[l]; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /code/ch06/6.4.2.sqrtx.py: -------------------------------------------------------------------------------- 1 | # 查找第一个大于等于 x 的元素 2 | def bs(nums: List[int], x: int) -> int: 3 | l, h = 0, len(nums) - 1 4 | while l <= h: 5 | mid = l + (h - l) // 2 6 | if l == h: 7 | break 8 | elif nums[mid] >= x: 9 | h = mid 10 | else: 11 | l = mid + 1 12 | return nums[l] 13 | -------------------------------------------------------------------------------- /code/ch06/6.4.3.sqrtx.cpp: -------------------------------------------------------------------------------- 1 | int bs(vector& nums, int x) { 2 | int l = 0, h = nums.size() - 1; 3 | while (l <= h) { 4 | int mid = l + (h - l) / 2; 5 | if (l == h || l + 1 == h) { 6 | break; 7 | } else if (nums[mid] <= x) { 8 | l = mid; 9 | } else { 10 | h = mid - 1; 11 | } 12 | } 13 | if (nums[h] <= x) { 14 | return nums[h]; 15 | } else { 16 | return nums[l]; 17 | } 18 | } -------------------------------------------------------------------------------- /code/ch06/6.4.3.sqrtx.java: -------------------------------------------------------------------------------- 1 | public int bs(int[] nums, int x) { 2 | int l = 0, h = nums.length - 1; 3 | while (l <= h) { 4 | int mid = l + (h - l) / 2; 5 | if (l == h || l + 1 == h) { 6 | break; 7 | } else if (nums[mid] <= x) { 8 | l = mid; 9 | } else { 10 | h = mid - 1; 11 | } 12 | } 13 | if (nums[h] <= x) { 14 | return nums[h]; 15 | } else { 16 | return nums[l]; 17 | } 18 | } -------------------------------------------------------------------------------- /code/ch06/6.4.3.sqrtx.py: -------------------------------------------------------------------------------- 1 | # 查找最后一个小于等于 x 的元素 2 | def bs(nums: List[int], x: int) -> int: 3 | l, h = 0, len(nums) - 1 4 | while l <= h: 5 | mid = l + (h - l) // 2 6 | if l == h or l + 1 == h: 7 | break 8 | elif nums[mid] <= x: 9 | l = mid 10 | else: 11 | h = mid - 1 12 | if nums[h] <= x: 13 | return nums[h] 14 | else: 15 | return nums[l] 16 | -------------------------------------------------------------------------------- /code/ch06/6.5.1.find-peak-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPeakElement(vector& nums) { 4 | int n = nums.size(); 5 | int l = 0, h = n - 1; 6 | while (l <= h) { 7 | int mid = l + (h - l) / 2; 8 | if (mid + 1 < n && nums[mid] < nums[mid + 1]) { 9 | l = mid + 1; 10 | } else if (mid - 1 >= 0 && nums[mid] < nums[mid - 1]) { 11 | h = mid - 1; 12 | } else { 13 | return mid; 14 | } 15 | } 16 | return -1; 17 | } 18 | }; -------------------------------------------------------------------------------- /code/ch06/6.5.1.find-peak-element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findPeakElement(int[] nums) { 3 | int n = nums.length; 4 | int l = 0, h = n - 1; 5 | while (l <= h) { 6 | int mid = l + (h - l) / 2; 7 | if (mid + 1 < n && nums[mid] < nums[mid + 1]) { 8 | l = mid + 1; 9 | } else if (mid - 1 >= 0 && nums[mid] < nums[mid - 1]) { 10 | h = mid - 1; 11 | } else { 12 | return mid; 13 | } 14 | } 15 | return -1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/ch06/6.5.1.find-peak-element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPeakElement(self, nums: List[int]) -> int: 3 | n = len(nums) 4 | l, h = 0, n - 1 5 | while l <= h: 6 | mid = l + (h - l) // 2 7 | if mid + 1 < n and nums[mid] < nums[mid + 1]: 8 | l = mid + 1 9 | elif mid - 1 >= 0 and nums[mid] < nums[mid - 1]: 10 | h = mid - 1 11 | else: 12 | return mid 13 | return -1 14 | -------------------------------------------------------------------------------- /code/ch07/7.1.1_haming_weight.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int num = 0; 5 | for (int i = 0; i < 32; i++){ 6 | if (n & (1 << i)) 7 | num++; 8 | } 9 | 10 | return num; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /code/ch07/7.1.1_haming_weight.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight(int n) { 4 | int retVal = 0; 5 | int mask = 1; 6 | for (int i = 0; i < 32; i++) { 7 | if ((n & mask) != 0) { 8 | retVal++; 9 | } 10 | mask <<= 1; 11 | } 12 | return retVal; 13 | } 14 | } -------------------------------------------------------------------------------- /code/ch07/7.1.1_haming_weight.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingWeight(self, n:int)->int: 3 | retval = 0 4 | for i in range(32): 5 | if n & (1 << i): 6 | retval = retval + 1 7 | 8 | return retval 9 | -------------------------------------------------------------------------------- /code/ch07/7.1.2_haming_weight.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int ret = 0; 5 | 6 | while (n) { 7 | ret = ret + 1; 8 | n &= (n - 1); 9 | } 10 | return ret; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /code/ch07/7.1.2_haming_weight.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight2(int n) { 4 | int retVal = 0; 5 | while (n != 0) { 6 | n = n & (n - 1); 7 | retVal++; 8 | } 9 | return retVal; 10 | } 11 | } -------------------------------------------------------------------------------- /code/ch07/7.1.2_haming_weight.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingWeight(self, n:int)->int: 3 | retval = 0 4 | while n: 5 | retval = retval + 1 6 | n &= (n - 1) 7 | 8 | return retval 9 | -------------------------------------------------------------------------------- /code/ch07/7.2.1_get_sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSum(int a, int b) { 4 | int ans = a ^ b; 5 | int c = a & b; 6 | while(c != 0) { 7 | c = (c & 0xffffffff) << 1; 8 | int ans_prim = ans ^ c; 9 | c = ans & c; 10 | ans = ans_prim; 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /code/ch07/7.2.1_get_sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getSum(int a, int b) { 3 | while (b != 0) { 4 | int carry = (a & b) << 1; 5 | int temp = a ^ b; 6 | 7 | a = temp; 8 | b = carry; 9 | } 10 | return a; 11 | } 12 | } -------------------------------------------------------------------------------- /code/ch07/7.2.1_get_sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSum(self, a:int, b:int)->int: 3 | carry = 0 4 | mask = 0xFFFFFFFF 5 | 6 | while b & mask != 0: 7 | carry = (a & b) << 1 8 | a = a ^ b 9 | b = carry 10 | 11 | if b > mask: 12 | return a & mask 13 | else: 14 | return a 15 | -------------------------------------------------------------------------------- /code/ch07/7.3.1_integer_replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | unordered_map visited; 4 | public: 5 | int integerReplacement(int n) { 6 | if (n == 1) return 0; 7 | 8 | if (visited.count(n) == 0) { 9 | if (n & 1 == 1) 10 | visited[n] = 2 + min(integerReplacement(n >> 1), integerReplacement((n >> 1) + 1)); 11 | else 12 | visited[n] = 1 + integerReplacement(n >> 1); 13 | } 14 | 15 | return visited[n]; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /code/ch07/7.3.1_integer_replacement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int integerReplacement(int n) { 3 | if (n == 1) 4 | return 0; 5 | if (n == Integer.MAX_VALUE) 6 | return 32; 7 | if (n % 2 == 0) 8 | return integerReplacement(n / 2) + 1; 9 | else 10 | return Math.min(integerReplacement(n + 1), integerReplacement(n - 1)) + 1; 11 | } 12 | } -------------------------------------------------------------------------------- /code/ch07/7.3.1_integer_replacement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def integerReplacement(self, n:int)->int: 3 | if n == 1: 4 | return 0 5 | elif n % 2 == 0: 6 | return 1 + self.integerReplacement(n // 2) 7 | else: 8 | return min(self.integerReplacement(n + 1), self.integerReplacement(n - 1)) + 1 9 | -------------------------------------------------------------------------------- /code/ch07/7.3.2_integer_replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerReplacement(int n) { 4 | long int tmp_num = n; 5 | int count = 0; 6 | 7 | while (tmp_num > 1) { 8 | if (tmp_num % 2 == 0) 9 | tmp_num >>= 1; 10 | else if (((tmp_num + 1) % 4 == 0) && (tmp_num != 3)) 11 | tmp_num += 1; 12 | else 13 | tmp_num -= 1; 14 | 15 | count += 1; 16 | } 17 | 18 | return count; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /code/ch07/7.3.2_integer_replacement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int integerReplacement(int n) { 3 | if (n == 1) 4 | return 0; 5 | if (n == Integer.MAX_VALUE) 6 | return 32; 7 | int count = 0; 8 | while (n > 3) { 9 | if (n % 2 == 0) 10 | n >>= 1; 11 | else if ((n + 1) % 4 == 0) 12 | n += 1; 13 | else 14 | n -= 1; 15 | count++; 16 | } 17 | 18 | return n == 3 ? count + 2 : count + 1; // 处理 n==3 的特殊情况 19 | } 20 | } -------------------------------------------------------------------------------- /code/ch07/7.3.2_integer_replacement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def integerReplacement(self, n:int)->int: 3 | count = 0 4 | while n > 1: 5 | if n & 1 == 0: 6 | n >>= 1 7 | elif (n + 1) % 4 == 0 and n != 3: 8 | n += 1 9 | else: 10 | n -= 1 11 | count += 1 12 | 13 | return count 14 | -------------------------------------------------------------------------------- /code/ch07/7.3.3_integer_replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerReplacement(int n) { 4 | long int tmp_num = n; 5 | int count = 0; 6 | 7 | while (tmp_num != 1) { 8 | if (tmp_num % 2 == 0) { 9 | tmp_num >>= 1; 10 | } else { 11 | if (((tmp_num & 2) == 0) || (tmp_num == 3)) 12 | tmp_num -= 1; 13 | else 14 | tmp_num += 1; 15 | } 16 | 17 | count += 1; 18 | } 19 | 20 | return count; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /code/ch07/7.3.3_integer_replacement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int integerReplacement(int n) { 3 | if (n == 1) 4 | return 0; 5 | if (n == Integer.MAX_VALUE) 6 | return 32; 7 | int count = 0; 8 | while (n > 3) { 9 | if ((n & 1) == 0) //判断是 0 或 1 10 | n >>= 1; 11 | else if ((n & 2) == 0) // 已经确定最后一位是1,判断是01 还是11 12 | n -= 1; 13 | else 14 | n += 1; 15 | count++; 16 | } 17 | return n == 3 ? count + 2 : count + 1; // 处理 n==3 的特殊情况 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch07/7.3.3_integer_replacement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def integerReplacement(self, n:int)->int: 3 | count = 0 4 | while n != 1: 5 | if n & 1 == 0: 6 | n >>= 1 7 | else: 8 | if (n & 2) == 0 or n == 3: 9 | n += -1 10 | else: 11 | n += 1 12 | count += 1 13 | 14 | return count 15 | -------------------------------------------------------------------------------- /code/ch07/7.4.1_single_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | unordered_map m; 5 | 6 | for (int i = 0; i < nums.size(); i++) { 7 | m[nums[i]]++; 8 | } 9 | 10 | for (auto it = m.begin(); it != m.end(); it++) 11 | if (it->second == 1) 12 | return it->first; 13 | 14 | return 0; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /code/ch07/7.4.1_single_number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | Map map = new HashMap<>(); 4 | for (int num : nums) 5 | map.put(num, map.getOrDefault(num, 0) + 1); 6 | for (int num : map.keySet()) 7 | if (map.get(num) == 1) 8 | return num; 9 | return 0; 10 | } 11 | } -------------------------------------------------------------------------------- /code/ch07/7.4.1_single_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums:List[int])->int: 3 | ret = 0 4 | 5 | for i in range(len(nums)): 6 | ret ^= nums[i] 7 | 8 | return ret 9 | -------------------------------------------------------------------------------- /code/ch07/7.4.2_single_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | int result = 0; 5 | 6 | for (int i = 0; i < nums.size(); i++) { 7 | result ^= nums[i]; 8 | } 9 | 10 | return result; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /code/ch07/7.4.2_single_number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int res = nums[0]; 4 | for (int i = 1; i < nums.length; i++) { 5 | res ^= nums[i]; 6 | } 7 | return res; 8 | } 9 | } -------------------------------------------------------------------------------- /code/ch07/7.4.2_single_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums:List[int])->int: 3 | hash_tab = defaultdict(int) 4 | 5 | for i in nums: 6 | hash_tab[i] += 1 7 | 8 | for i in hash_tab: 9 | if hash_tab[i] == 1: 10 | return i 11 | -------------------------------------------------------------------------------- /code/ch07/7.4.3_single_number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int distinctSum = Arrays.stream(nums).distinct().sum(); 4 | int sum = Arrays.stream(nums).sum(); 5 | return distinctSum * 2 - sum; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /code/ch07/7.4.3_single_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums:List[int])->int: 3 | return 2 * sum(set(nums)) - sum(nums) 4 | -------------------------------------------------------------------------------- /code/ch07/7.4.4_single_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | vector t(32); 5 | 6 | for (int i = 0; i < nums.size(); i++) { 7 | int num = nums[i]; 8 | for (int j = 31; j >= 0; j--) { 9 | t[j] += num & 1; 10 | num >>= 1; 11 | if (!num) break; 12 | } 13 | } 14 | 15 | int res = 0; 16 | for (int j = 31; j >= 0; j--) { 17 | int num = t[j] % 3; 18 | if (num) res += 1 << (31 - j); 19 | } 20 | 21 | return res; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /code/ch07/7.4.4_single_number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int[] counts = new int[32]; 4 | for (int num : nums) { 5 | for (int i = 0; i < 32; i++) { 6 | counts[i] += num & 1; 7 | num >>= 1; 8 | } 9 | } 10 | int res = 0; 11 | for (int i = 0; i < 32; i++) { 12 | res <<= 1; 13 | res |= counts[31 - i] % 3; 14 | } 15 | return res; 16 | } 17 | } -------------------------------------------------------------------------------- /code/ch07/7.4.4_single_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums: List[int]) -> int: 3 | counts = [0] * 32 4 | # 统计 nums 中每个元素的二进制中,对应位上的 1 的个数 5 | for num in nums: 6 | for j in range(32): 7 | counts[j] += num & 1 8 | num >>= 1 9 | res, m = 0, 3 10 | # 对每一位上,统计的 1 的个数取余 11 | for i in range(32): 12 | res <<= 1 13 | res |= counts[31 - i] % m 14 | 15 | return res if counts[31] % m == 0 else ~(res ^ 0xffffffff) 16 | 17 | -------------------------------------------------------------------------------- /code/ch07/7.4.5_single_number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int[] counts = new int[32]; 4 | for (int num : nums) { 5 | for (int i = 0; i < 32; i++) { 6 | counts[i] += num & 1; 7 | num >>= 1; 8 | } 9 | } 10 | int res = 0; 11 | for (int i = 0; i < 32; i++) { 12 | res <<= 1; 13 | res |= counts[31 - i] % 3; 14 | } 15 | return res; 16 | } 17 | } -------------------------------------------------------------------------------- /code/ch07/7.4.5_single_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums: List[int]) -> List[int]: 3 | ret = 0 # 所有数字异或的结果 4 | a = 0 5 | b = 0 6 | for n in nums: 7 | ret ^= n 8 | # 找到第一位不是 0 的 9 | h = 1 10 | while ret & h == 0: 11 | h <<= 1 12 | for n in nums: 13 | # 根据该位是否为 0 将其分为两组 14 | if h & n == 0: 15 | a ^= n 16 | else: 17 | b ^= n 18 | 19 | return [a, b] 20 | -------------------------------------------------------------------------------- /code/ch08/8.1.1_155.min-stack.cpp: -------------------------------------------------------------------------------- 1 | class MinStack { 2 | stack x_stack; 3 | stack min_stack; 4 | public: 5 | MinStack() { 6 | min_stack.push(INT_MAX); 7 | } 8 | 9 | void push(int x) { 10 | x_stack.push(x); 11 | min_stack.push(min(min_stack.top(), x)); 12 | } 13 | 14 | void pop() { 15 | x_stack.pop(); 16 | min_stack.pop(); 17 | } 18 | 19 | int top() { 20 | return x_stack.top(); 21 | } 22 | 23 | int getMin() { 24 | return min_stack.top(); 25 | } 26 | }; -------------------------------------------------------------------------------- /code/ch08/8.1.2_155.min-stack.cpp: -------------------------------------------------------------------------------- 1 | class MinStack { 2 | stack x_stack; 3 | stack min_stack; 4 | public: 5 | MinStack() { 6 | min_stack.push(INT_MAX); 7 | } 8 | 9 | void push(int x) { 10 | x_stack.push(x); 11 | min_stack.push(min(min_stack.top(), x)); 12 | } 13 | 14 | void pop() { 15 | x_stack.pop(); 16 | min_stack.pop(); 17 | } 18 | 19 | int top() { 20 | return x_stack.top(); 21 | } 22 | 23 | int getMin() { 24 | return min_stack.top(); 25 | } 26 | }; -------------------------------------------------------------------------------- /code/ch08/8.1.3_155.min-stack.cpp: -------------------------------------------------------------------------------- 1 | class MinStack { 2 | stack x_stack; 3 | stack min_stack; 4 | public: 5 | MinStack() { 6 | min_stack.push(INT_MAX); 7 | } 8 | 9 | void push(int x) { 10 | x_stack.push(x); 11 | min_stack.push(min(min_stack.top(), x)); 12 | } 13 | 14 | void pop() { 15 | x_stack.pop(); 16 | min_stack.pop(); 17 | } 18 | 19 | int top() { 20 | return x_stack.top(); 21 | } 22 | 23 | int getMin() { 24 | return min_stack.top(); 25 | } 26 | }; -------------------------------------------------------------------------------- /code/ch09/9.1.1.2.two_sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& numbers, int target) { 4 | for (int left = 0, right = numbers.size() - 1; left < right; ){ 5 | if (numbers[left] + numbers[right] == target) 6 | return vector{left + 1, right + 1}; 7 | else if (numbers[left] + numbers[right] < target) 8 | left++; 9 | else 10 | right--; 11 | } 12 | return vector{}; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /code/ch09/9.1.1.2.two_sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | int left = 0, right = nums.length - 1; 4 | while (left < right) { 5 | if (nums[left] + nums[right] == target) 6 | return new int[]{left + 1, right + 1}; 7 | else if (nums[left] + nums[right] < target) 8 | left++; 9 | else 10 | right--; 11 | } 12 | return null; 13 | } 14 | } -------------------------------------------------------------------------------- /code/ch09/9.1.1.2.two_sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, number:List[int], target:int)->List[int]: 3 | left, right = 0, len(numbers) - 1 4 | while left < right: 5 | if (numbers[left] + numbers[right] == target): 6 | return [left + 1, right + 1] 7 | elif (numbers[left] + numbers[right] < target): 8 | left += 1 9 | elif: 10 | right -= 1 11 | 12 | return [] 13 | -------------------------------------------------------------------------------- /code/ch09/9.1.1.two_sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& numbers, int target) { 4 | vector ret; 5 | for (int i = 0; i <= numbers.size() - 1 - 1; i++) { 6 | for (int j = i + 1; j <= numbers.size() - 1; j++) { 7 | if (numbers[i] + numbers[j] == target) { 8 | ret.push_back(i + 1); 9 | ret.push_back(j + 1); 10 | } 11 | } 12 | } 13 | return ret; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /code/ch09/9.1.1.two_sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | for (int i = 0; i < nums.length; i++) { 4 | for (int j = i + 1; j < nums.length; j++) { 5 | if (nums[j] == target - nums[i]) 6 | return new int[]{i+1, j+1}; 7 | } 8 | } 9 | return null; 10 | } 11 | } -------------------------------------------------------------------------------- /code/ch09/9.1.1.two_sum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, numbers:List[int], target:int)->List[int]: 3 | for i in range(0, len(numbers), 1): 4 | for j in range(i + 1, len(numbers), 1): 5 | if numbers[i] + numbers[j] == target: 6 | return [i + 1, j + 1] 7 | 8 | return [] -------------------------------------------------------------------------------- /code/ch09/9.1.2.1.container_with_most_water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int res = 0; 5 | int area = 0; 6 | for (int i = 0; i < height.size() - 1; i++) 7 | for (int j = i + 1; j < height.size(); j++) { 8 | area = (j - i) * min(height[i], height[j]); 9 | if (area > res) 10 | res = area; 11 | } 12 | 13 | return res; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /code/ch09/9.1.2.1.container_with_most_water.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxArea(int[] height) { 3 | int res = 0, area = 0; 4 | for (int i = 0; i < height.length; i++) { 5 | for (int j = i + 1; j < height.length; j++) { 6 | area = Math.min(height[i], height[j]) * (j - i); 7 | res = Math.max(res, area); 8 | } 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /code/ch09/9.1.2.1.container_with_most_water.py: -------------------------------------------------------------------------------- 1 | class Solution : 2 | def maxArea(self, height:List[int])->int: 3 | res, area = 0, 0 4 | for i in range(0, len(height), 1): 5 | fo j in range(i + 1, len(height), 1): 6 | area = (j - i) * min(height[i], height[j]) 7 | if area > res: 8 | res = area 9 | 10 | return res 11 | 12 | -------------------------------------------------------------------------------- /code/ch09/9.1.2.2.container_with_most_water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int res = 0; 5 | int area = 0; 6 | int left = 0, right = height.size() - 1; 7 | 8 | while(left < right) { 9 | area = (right - left) * min(height[right], height[left]); 10 | res = max(res, area); 11 | if (height[left] < height[right]) 12 | left++; 13 | else 14 | right--; 15 | } 16 | 17 | return res; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch09/9.1.2.2.container_with_most_water.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxArea(int[] height) { 3 | int left = 0, right = height.length - 1, res = 0, area = 0; 4 | 5 | while (left < right) { 6 | area = Math.min(height[left], height[right]) * (right - left); 7 | res = Math.max(res, area); 8 | if (height[left] < height[right]) 9 | left++; 10 | else 11 | right++; 12 | } 13 | return res; 14 | } 15 | } -------------------------------------------------------------------------------- /code/ch09/9.1.2.2.container_with_most_water.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxArea(self, height:List[int])->int: 3 | #init the param 4 | left, right, width, res = 0, len(height) - 1, len(height - 1, 0) 5 | 6 | for w in range(width, 0, -1): 7 | if height[left] < height[right]: 8 | res, left = max(res, height[left] * w), left + 1 9 | else: 10 | res, right = max(res, height[right] * w), right - 1 11 | 12 | return res 13 | -------------------------------------------------------------------------------- /code/ch09/9.2.1.1.linked_list_cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | unordered_set seenNode; 13 | while (head) { 14 | if (seenNode.find(head) != seenNode.end()) 15 | return true; 16 | seenNode.insert(head); 17 | head = head->next; 18 | } 19 | return false; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /code/ch09/9.2.1.1.linked_list_cycle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasCycle(self, head:ListNode)->bool: 3 | if head is None: 4 | return False 5 | 6 | node_set = {} 7 | while head.next: 8 | node_set[head] = True 9 | 10 | if head.next in node_set: 11 | return True 12 | 13 | head = head.next 14 | 15 | return False 16 | -------------------------------------------------------------------------------- /code/ch09/9.2.1.2.linked_list_cycle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasCycle(self, head:ListNode)->bool: 3 | if head == None or head.next == None: 4 | return False 5 | 6 | pslow = pfast = head 7 | while pfast != None and pfast.next != None: 8 | pslow = pslow.next 9 | pfast = pfast.next.next 10 | if pfast == pslow: 11 | break 12 | 13 | if pfast == None or pfast.next == None: 14 | return False 15 | elif pfast == pslow: 16 | return True 17 | 18 | return False 19 | -------------------------------------------------------------------------------- /code/ch09/9.2.2.1.long_substr_without_repeat_char.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def allUnique(self, s:str)->bool: 3 | dic = {} 4 | for i in range(0, len(s), 1): 5 | if s[i] in dic: 6 | return False 7 | else: 8 | dic[s[i]] = 1 9 | 10 | return True 11 | 12 | def lengthOfLongestSubstring(self, s:str)->int: 13 | res = 0 14 | 15 | for i in range(0, len(s), 1): 16 | for j in range(0, len(s), 1): 17 | if self.allUnique(s[i : j + 1]): 18 | res = max(res, j - i + 1) 19 | else: 20 | break 21 | 22 | return res 23 | -------------------------------------------------------------------------------- /code/ch09/9.2.2.2.long_substr_without_repeat_char.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLongestSubstring(String s) { 3 | int res = 0; 4 | for (int left = 0; left < s.length(); left++) { 5 | Set set = new HashSet<>(); 6 | int right = left; 7 | while (right < s.length()) { 8 | if (set.contains(s.charAt(right))) 9 | break; 10 | else { 11 | set.add(s.charAt(right)); 12 | res = Math.max(res, right - left + 1); 13 | right++; 14 | } 15 | } 16 | } 17 | return res; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch09/9.2.2.2.long_substr_without_repeat_char.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthofLongestSubstring(self, s:str)->int: 3 | res, left, right = 0, 0, 0 4 | settings = set() 5 | 6 | for left in range(0, len(s), 1): 7 | right = left 8 | while (right < len(s)): 9 | if (s[right] in settings): 10 | break 11 | else: 12 | settings.add(s[right]) 13 | res = max(res, right - left + 1) 14 | right = right + 1 15 | 16 | settings.clear() 17 | 18 | return res 19 | -------------------------------------------------------------------------------- /code/ch09/9.2.2.4.long_substr_without_repeat_char.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | int res = 0, left = 0, right = 0; 5 | int charset[256] = {-1}; 6 | 7 | while (right < s.size() && left + res < s.size()) { 8 | left = max(left, charset[s[right]]); 9 | res = max(res, right - left + 1); 10 | charset[s[right]] = right + 1; 11 | right++; 12 | } 13 | 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /code/ch09/9.2.2.4.long_substr_without_repeat_char.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLongestSubstring(String s) { 3 | int res = 0, left = 0, right = 0; 4 | Map map = new HashMap<>(); 5 | while (right < s.length() && left < s.length() - res) { 6 | if (map.containsKey(s.charAt(right))) 7 | left = Math.max(left, map.get(s.charAt(right)) + 1); 8 | 9 | map.put(s.charAt(right), right); 10 | res = Math.max(res, right - left + 1); 11 | right++; 12 | } 13 | 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch09/9.2.2.4.long_substr_without_repeat_char.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLongestSubstring(self, s:str)->int: 3 | res, left, right = 0, 0, 0 4 | dic = {} 5 | 6 | while right < len(s) and left + res < len(s): 7 | if s[right] in dic: 8 | left = max(left, dic[s[right]] + 1) 9 | 10 | dic[s[right]] = right 11 | res = max(res, right - left + 1) 12 | right = right + 1 13 | 14 | return res 15 | -------------------------------------------------------------------------------- /code/ch10/10.1.1.ClimbingStairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | if (n == 1) return 1; 5 | 6 | int dp[n + 1]; 7 | dp[1] = 1; 8 | dp[2] = 2; 9 | 10 | for (int i = 3; i <= n; i++) 11 | dp[i] = dp[i - 1] + dp[i - 2]; 12 | 13 | return dp[n]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /code/ch10/10.1.1.ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | package ch10.code; 2 | 3 | public class ClimbingStairs { 4 | public int climbStairs(int n) { 5 | if (n < 3) 6 | return n; 7 | 8 | int first = 1,second = 2; 9 | 10 | for (int i = 3; i < n + 1; i++) { 11 | second = first + second; 12 | first = second - first; 13 | } 14 | return second; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch10/10.1.1.ClimbingStairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def climbStairs(self, n: int) -> int: 3 | if n < 2: 4 | return n 5 | dp = [0] * (n + 1) 6 | dp[1], dp[2] = 1, 2 7 | for i in range(3, n + 1): 8 | dp[i] = dp[i - 1] + dp[i - 2] 9 | return dp[n] 10 | -------------------------------------------------------------------------------- /code/ch10/10.1.2.ClimbingStairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | if (n < 2) return n; 5 | 6 | int first = 1; 7 | int second = 2; 8 | 9 | for (int i = 3; i < n + 1; i++) { 10 | second = first + second; 11 | first = second - first; 12 | } 13 | 14 | return second; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /code/ch10/10.1.2.ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | package ch10.code; 2 | 3 | public class ClimbingStairs { 4 | public int climbStairs(int n) { 5 | if (n < 3) 6 | return n; 7 | 8 | int[] dp = new int[n + 1]; 9 | dp[1] = 1; 10 | dp[2] = 2; 11 | for (int i = 3; i < n+1; i++) { 12 | dp[i] = dp[i - 1] + dp[i - 2]; 13 | } 14 | return dp[n]; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch10/10.1.2.ClimbingStairs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def climbStairs(self, n: int) -> int: 3 | if n < 2: 4 | return n 5 | first, second = 1, 2 6 | for i in range(3, n + 1): 7 | # third = first + second 8 | # first = second 9 | # second = third 10 | second = first + second 11 | first = second - first 12 | return second 13 | -------------------------------------------------------------------------------- /code/ch10/10.2.1-house-rob.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | int helper(vector& nums, int start) { 10 | if(start >= nums.size()) return 0; 11 | 12 | return max(helper(nums, start+1), nums[start] + helper(nums, start+2)); 13 | } 14 | int rob(vector& nums) { 15 | return helper(nums, 0); 16 | } 17 | }; -------------------------------------------------------------------------------- /code/ch10/10.2.1-house-rob.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int helper(int[] nums, int start) { 3 | if(start >= nums.length) return 0; 4 | 5 | return Math.max(helper(nums, start+1), nums[start] + helper(nums, start+2)); 6 | } 7 | public int rob(int[] nums) { 8 | return helper(nums, 0); 9 | } 10 | } -------------------------------------------------------------------------------- /code/ch10/10.2.1-house-rob.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def rob(self, nums: List[int]) -> int: 6 | if len(nums) <= 0: 7 | return 0 8 | return max(self.rob(nums[1:]), nums[0] + self.rob(nums[2:])) 9 | -------------------------------------------------------------------------------- /code/ch10/10.2.2-house-rob.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | int helper(vector& nums, vector&memo, int start) { 10 | if(start >= nums.size()) return 0; 11 | if(memo[start] != -1) return memo[start]; 12 | 13 | memo[start] = max(helper(nums, memo, start+1), nums[start] + helper(nums, memo, start+2)); 14 | 15 | return memo[start]; 16 | } 17 | int rob(vector& nums) { 18 | vector memo(nums.size(), -1); 19 | return helper(nums, memo, 0); 20 | } 21 | }; -------------------------------------------------------------------------------- /code/ch10/10.2.2-house-rob.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int helper(int[] nums, int[] memo, int start) { 3 | if(start >= nums.length) return 0; 4 | if(memo[start] != -1) return memo[start]; 5 | 6 | memo[start] = Math.max(helper(nums, memo, start+1), nums[start] + helper(nums,memo, start+2)); 7 | 8 | return memo[start]; 9 | } 10 | public int rob(int[] nums) { 11 | int[] memo = new int[nums.length]; 12 | 13 | for (int i = 0; i < nums.length; i++) memo[i] = -1; 14 | return helper(nums, memo, 0); 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch10/10.2.2-house-rob.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def rob(self, nums: List[int]) -> int: 6 | memo = [-1 for x in range(len(nums) + 1)] 7 | memo[-1] = 0 8 | 9 | return self.helper(0, nums, memo) 10 | 11 | def helper(self, n: int, nums: List[int], memo: List[int]) -> int: 12 | if n >= len(nums): 13 | return 0 14 | if memo[n] != -1: 15 | return memo[n] 16 | 17 | memo[n] = max( 18 | self.helper(n + 1, nums, memo), self.helper(n + 2, nums, memo) + nums[n], 19 | ) 20 | return memo[n] 21 | -------------------------------------------------------------------------------- /code/ch10/10.2.3-house-rob.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | int rob(vector& nums) { 10 | if(nums.size() <= 0) return 0; 11 | 12 | vector memo(nums.size()+1); 13 | 14 | memo[nums.size()-1] = nums[nums.size()-1]; 15 | 16 | for(int i = nums.size()-2; i >= 0; i--) { 17 | memo[i] = max(memo[i+1], memo[i+2] + nums[i]); 18 | } 19 | return memo[0]; 20 | } 21 | }; -------------------------------------------------------------------------------- /code/ch10/10.2.3-house-rob.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public int rob(int[] nums) { 4 | if(nums.length <= 0) return 0; 5 | 6 | int[] memo = new int[nums.length+1]; 7 | memo[nums.length-1] = nums[nums.length-1]; 8 | 9 | for (int i = nums.length-2; i >= 0; i--) { 10 | memo[i] = Math.max(memo[i+1], memo[i+2]+nums[i]); 11 | } 12 | return memo[0]; 13 | } 14 | } -------------------------------------------------------------------------------- /code/ch10/10.2.3-house-rob.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def rob(self, nums: List[int]) -> int: 6 | if not nums: 7 | return 0 8 | memo = [0 for x in range(len(nums) + 1)] 9 | memo[-2] = nums[-1] 10 | 11 | for i in range(len(nums) - 2, -1, -1): 12 | memo[i] = max(memo[i + 1], memo[i + 2] + nums[i]) 13 | 14 | return memo[0] 15 | -------------------------------------------------------------------------------- /code/ch10/10.2.4-house-rob.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | int rob(vector& nums) { 10 | if(nums.size() <= 0) return 0; 11 | 12 | int prev, curr; 13 | 14 | for(int i = nums.size()-1; i >= 0; i--) { 15 | int tmp = curr; 16 | curr = max(curr, nums[i] + prev); 17 | prev = tmp; 18 | } 19 | return curr; 20 | } 21 | }; -------------------------------------------------------------------------------- /code/ch10/10.2.4-house-rob.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public int rob(int[] nums) { 4 | if(nums.length <= 0) return 0; 5 | 6 | int prev = 0, curr = 0; 7 | 8 | for (int i = nums.length-1; i >= 0; i--) { 9 | int tmp = curr; 10 | curr = Math.max(curr, nums[i] + prev); 11 | prev = tmp; 12 | } 13 | return curr; 14 | } 15 | } -------------------------------------------------------------------------------- /code/ch10/10.2.4-house-rob.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def rob(self, nums: List[int]) -> int: 6 | 7 | prev = 0 8 | curr = 0 9 | 10 | for i in range(len(nums) - 1, -1, -1): 11 | temp = curr 12 | curr = max(curr, nums[i] + prev) 13 | prev = temp 14 | 15 | return curr 16 | -------------------------------------------------------------------------------- /code/ch10/10.2.5-house-rob-II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | if(nums.length == 1) return nums[0]; 4 | 5 | int prev = 0, curr = 0; 6 | 7 | for(int i = 0; i < nums.length - 1; i++) { 8 | int tmp = curr; 9 | curr = Math.max(curr, nums[i] + prev); 10 | prev = tmp; 11 | } 12 | 13 | int res = curr; 14 | 15 | prev = 0; 16 | curr = 0; 17 | 18 | for(int i = 1; i < nums.length; i++) { 19 | int tmp = curr; 20 | curr = Math.max(curr, nums[i] + prev); 21 | prev = tmp; 22 | } 23 | 24 | return Math.max(res, curr); 25 | } 26 | } -------------------------------------------------------------------------------- /code/ch10/10.2.5-house-rob-II.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def rob(self, nums: List[int]) -> int: 6 | if len(nums) == 1: 7 | return nums[0] 8 | prev = 0 9 | curr = 0 10 | 11 | for i in range(len(nums) - 1): 12 | temp = curr 13 | curr = max(curr, nums[i] + prev) 14 | prev = temp 15 | 16 | res = curr 17 | 18 | prev = 0 19 | curr = 0 20 | 21 | for i in range(1, len(nums)): 22 | temp = curr 23 | curr = max(curr, nums[i] + prev) 24 | prev = temp 25 | 26 | return max(res, curr) 27 | -------------------------------------------------------------------------------- /code/ch10/10.3.1.uniquePaths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int uniquePaths(int m, int n) { 6 | int **d = (int **)malloc(m * sizeof(int *)); 7 | for (int i = 0; i < m; i++) { 8 | d[i] = (int *)malloc(n * sizeof(int)); 9 | for (int j = 0; j < n; j++) d[i][j] = 1; 10 | } 11 | for (int col = 1; col < m; col++) 12 | for (int row = 1; row < n; row++) 13 | d[col][row] = d[col - 1][row] + d[col][row - 1]; 14 | int ans = d[m - 1][n - 1]; 15 | for (int i = 0; i < m; i++) free(d[i]); 16 | free(d); 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch10/10.3.1.uniquePaths.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int[][] d = new int[m][n]; 4 | for (int i = 0; i < m; i++) 5 | for (int j = 0; j < n; j++) d[i][j] = 1; 6 | for (int col = 1; col < m; col++) 7 | for (int row = 1; row < n; row++) 8 | d[col][row] = d[col - 1][row] + d[col][row - 1]; 9 | return d[m - 1][n - 1]; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /code/ch10/10.3.1.uniquePaths.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniquePaths(self, m: int, n: int) -> int: 3 | d = [[1] * n for _ in range(m)] 4 | 5 | for col in range(1, m): 6 | for row in range(1, n): 7 | d[col][row] = d[col - 1][row] + d[col][row - 1] 8 | 9 | return d[m - 1][n - 1] 10 | -------------------------------------------------------------------------------- /code/ch10/10.3.2.uniquePaths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int uniquePaths(int m, int n) { 6 | int *dp = (int *)malloc(n * sizeof(int)); 7 | for (int i = 0; i < n; i++) dp[i] = 1; 8 | for (int i = 1; i < m; i++) 9 | for (int j = 1; j < n; j++) 10 | dp[j] += dp[j - 1]; 11 | int ans = dp[n - 1]; 12 | free(dp); 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /code/ch10/10.3.2.uniquePaths.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int[] dp = new int[n]; 4 | for (int i = 0; i < n; i++) dp[i] = 1; 5 | for (int i = 1; i < m; i++) 6 | for (int j = 1; j < n; j++) 7 | dp[j] += dp[j - 1]; 8 | return dp[n - 1]; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/ch10/10.3.2.uniquePaths.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniquePaths(self, m: int, n: int) -> int: 3 | dp = [1] * n 4 | for _ in range(1, m): 5 | for j in range(1, n): 6 | dp[j] += dp[j - 1] 7 | return dp[n - 1] 8 | -------------------------------------------------------------------------------- /code/ch10/10.3.3.uniquePaths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | map, int> visited; 8 | int uniquePaths(int m, int n) { 9 | if (visited.count(make_pair(m, n))) 10 | return visited[make_pair(m, n)]; 11 | if (m == 1 || n == 1) return 1; 12 | int cnt = uniquePaths(m - 1, n) + \ 13 | uniquePaths(m, n - 1); 14 | return visited[make_pair(m, n)] = cnt; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /code/ch10/10.3.3.uniquePaths.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | visited = dict() 3 | 4 | def uniquePaths(self, m: int, n: int) -> int: 5 | if (m, n) in self.visited: 6 | return self.visited[(m, n)] 7 | if m == 1 or n == 1: 8 | return 1 9 | cnt = self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1) 10 | self.visited[(m, n)] = cnt 11 | return cnt 12 | -------------------------------------------------------------------------------- /code/ch10/10.4.1.CoinChange.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector& coins, int amount) { 4 | int Max = amount + 1; 5 | vector dp(amount + 1, Max); 6 | dp[0] = 0; 7 | 8 | for (int i = 1; i <= amount; i++) { 9 | for (int j = 0; j < coins.size(); j++) { 10 | if (coins[j] <= i) { 11 | dp[i] = min(dp[i], dp[i - coins[j]] + 1); 12 | } 13 | } 14 | } 15 | 16 | return dp[amount] > amount ? -1 : dp[amount]; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /code/ch10/10.4.1.CoinChange.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class CoinChange { 3 | public int coinChange(int[] coins, int amount) { 4 | if (amount == 0) 5 | return 0; 6 | 7 | int[] dp = new int[amount + 1]; 8 | Arrays.fill(dp, amount + 1); 9 | dp[0] = 0; 10 | 11 | for (int i = 1; i < amount + 1; i++) { 12 | for (int coin : coins) { 13 | if (coin <= i) { 14 | dp[i] = Math.min(dp[i], dp[i - coin] + 1); 15 | } 16 | } 17 | } 18 | return dp[amount] == amount + 1 ? -1 : dp[amount]; 19 | } 20 | } -------------------------------------------------------------------------------- /code/ch10/10.4.1.CoinChange.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def coinChange(self, coins: List[int], amount: int) -> int: 6 | if amount == 0: 7 | return 0 8 | # use amount + 1 to indicate bigger than any 9 | dp = [amount + 1] * (amount + 1) 10 | dp[0] = 0 11 | for i in range(1, amount + 1): 12 | for coin in coins: 13 | if coin <= i: 14 | dp[i] = min(dp[i], dp[i - coin] + 1) 15 | return -1 if dp[amount] == (amount + 1) else dp[amount] 16 | -------------------------------------------------------------------------------- /code/ch10/10.4.2.CoinChange2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int change(int amount, vector& coins) { 4 | vector dp(amount + 1, 0); 5 | dp[0] = 1; 6 | 7 | for (int coin: coins) { 8 | for (int i = coin; i < amount + 1; i++) 9 | dp[i] += dp[i - coin]; 10 | } 11 | 12 | return dp[amount]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /code/ch10/10.4.2.CoinChange2.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class CoinChange2 { 4 | public int change(int amount, int[] coins) { 5 | int[] dp = new int[amount + 1]; 6 | dp[0] = 1; 7 | for (int coin : coins) { 8 | if (coin > amount) 9 | continue; 10 | for (int i = coin; i < amount + 1; i++) { 11 | dp[i] += dp[i - coin]; 12 | } 13 | } 14 | return dp[amount]; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch10/10.4.2.CoinChange2.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def change(self, amount: int, coins: List[int]) -> int: 6 | dp = [0] * (amount + 1) 7 | dp[0] = 1 8 | for coin in coins: 9 | for x in range(coin, amount + 1): 10 | dp[x] += dp[x - coin] 11 | return dp[amount] 12 | -------------------------------------------------------------------------------- /code/ch11/11.1.1_maxslidingwindow.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | vector res = {}; 5 | 6 | int length = nums.size(); 7 | if (length * k == 0) 8 | return res; 9 | 10 | for (int i = 0; i < length - k + 1; i++) { 11 | int max_val = INT_MIN; 12 | 13 | for (int j = i; j < i + k; j++) { 14 | max_val = max(max_val, nums[j]); 15 | } 16 | 17 | res.push_back(max_val); 18 | } 19 | 20 | return res; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /code/ch11/11.1.1_maxslidingwindow.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] maxSlidingWindow(int[] nums, int k) { 3 | if (k == 1) 4 | return nums; 5 | int turn = nums.length - k + 1; 6 | int[] res = new int[turn]; 7 | for (int i = 0; i < turn; i++) { 8 | res[i] = getMax(nums, i, k); 9 | } 10 | return res; 11 | } 12 | 13 | private int getMax(int[] nums, int left, int k) { 14 | int max = nums[left]; 15 | for (int i = 0; i < k-1; i++) { 16 | left++; 17 | max = Math.max(max, nums[left]); 18 | } 19 | return max; 20 | } 21 | } -------------------------------------------------------------------------------- /code/ch11/11.1.1_maxslidingwindow.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSlidingWindow(self, nums:List[int], k:int)->List[int]: 3 | length = len(nums) 4 | if length * k == 0: 5 | return [] 6 | 7 | output = [] 8 | # 遍历所有可能的窗口 9 | for i in range(length - k + 1): 10 | max_val = -sys.maxsize - 1 11 | # 找到一个窗口中的最大值 12 | for j in range(i, i + k): 13 | max_val = max(max_val, nums[j]) 14 | 15 | # 将最大值放到数组列表里 16 | output.append(max_val) 17 | 18 | return output 19 | -------------------------------------------------------------------------------- /code/ch11/11.1.2_maxslidingwindow.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | vector res; 5 | multiset mysetting; 6 | for (int i = 0; i < nums.size(); i++) { 7 | mysetting.insert(nums[i]); 8 | if (i >= k - 1) { 9 | res.push_back(*mysetting.rbegin()); 10 | mysetting.erase(mysetting.find(nums[i - k + 1])); 11 | } 12 | } 13 | return res; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /code/ch11/11.1.2_maxslidingwindow.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: 3 | d = collections.deque() 4 | out = [] 5 | 6 | for i, n in enumerate(nums): 7 | # 移除所有比当前元素小的元素 8 | while d and nums[d[-1]] < n: 9 | d.pop() 10 | d += i, 11 | if d[0] == i - k: 12 | d.popleft() 13 | if i >= k - 1: 14 | out.append(nums[d[0]]) 15 | 16 | return out 17 | -------------------------------------------------------------------------------- /code/ch11/11.3.1_characterReplacement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def characterReplacement(self, s: str, k: int) -> int: 3 | res = low = high = 0 4 | counts = collections.Counter() 5 | 6 | for high in range(1, len(s) + 1): 7 | counts[s[high - 1]] += 1 8 | max_char_n = counts.most_common(1)[0][1] 9 | 10 | if high - low - max_char_n > k: 11 | counts[s[low]] -= 1 12 | low += 1 13 | 14 | return high -low 15 | -------------------------------------------------------------------------------- /code/ch11/11.3.2_characterReplacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int characterReplacement(string s, int k) { 4 | int res = 0; 5 | int maxf = 0; 6 | vector count(26, 0); 7 | 8 | for (int i = 0; i < s.size(); i++) { 9 | count[s[i] - 'A'] += 1; 10 | maxf = max(maxf, count[s[i] - 'A']); 11 | 12 | if (res - maxf < k) 13 | res += 1; 14 | else 15 | count[s[i - res] - 'A'] -= 1; 16 | } 17 | 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /code/ch11/11.3.2_characterReplacement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int characterReplacement(String s, int k) { 3 | int res = 0, tmpMax = 0; 4 | int[] counts = new int[26]; 5 | 6 | for (int i = 0; i < s.length(); i++) { 7 | counts[s.charAt(i) - 'A']++; 8 | tmpMax = Math.max(tmpMax, counts[s.charAt(i) - 'A']); 9 | 10 | if (res - tmpMax < k) 11 | res++; 12 | else 13 | counts[s.charAt(i - res) - 'A']--; 14 | } 15 | return res; 16 | } 17 | } -------------------------------------------------------------------------------- /code/ch11/11.3.2_characterReplacement.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def characterReplacement(self, s: str, k: int) -> int: 3 | max_char_n = res = 0 4 | count = collections.Counter() 5 | for i in range(len(s)): 6 | count[s[i]] += 1 7 | max_char_n = max(max_char_n, count[s[i]]) 8 | 9 | if res - max_char_n < k: 10 | res += 1 11 | else: 12 | count[s[i - res]] -= 1 13 | 14 | return res 15 | -------------------------------------------------------------------------------- /code/ch12/12.1.1.StoneGame.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | plublic: 3 | bool stoneGame(vector& piles) { 4 | return true; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /code/ch12/12.1.1.StoneGame.java: -------------------------------------------------------------------------------- 1 | package ch12.code; 2 | 3 | public class 12.1.1.StoneGame { 4 | public boolean stoneGame(int[] piles) { 5 | return true; 6 | } 7 | } -------------------------------------------------------------------------------- /code/ch12/12.1.1.StoneGame.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def stoneGame(self, piles: List[int]) -> bool: 6 | return True 7 | -------------------------------------------------------------------------------- /code/ch12/12.2.2.PredictTheWinner.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool PredictTheWinner(vector& nums) { 4 | int len = nums.size(); 5 | 6 | vector> dp; 7 | for (int i = 0; i < nums.size(); i++) 8 | dp.push_back(vector(nums.size(), 0)); 9 | 10 | for (int i = 0;i < nums.size(); i++) 11 | dp[i][i] = nums[i]; 12 | 13 | for (int i = len - 1; i > -1; i--) 14 | for (int j = i + 1; j < len; j++) 15 | dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 16 | 17 | return dp[0][len - 1] >= 0; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch12/12.2.2.PredictTheWinner.java: -------------------------------------------------------------------------------- 1 | package ch12.code; 2 | 3 | public class 12.2.2.PredictTheWinner { 4 | public boolean PredictTheWinner(int[] nums) { 5 | int[][] dp = new int[nums.length][nums.length]; 6 | for (int i = 0; i < nums.length; i++) { 7 | dp[i][i] = nums[i]; 8 | } 9 | 10 | for (int i = nums.length - 1; i >= 0; i--) { 11 | for (int j = i + 1; j < nums.length; j++) 12 | dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 13 | } 14 | return dp[0][nums.length - 1] >= 0; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch12/12.2.2.PredictTheWinner.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def PredictTheWinner(self, nums: List[int]) -> bool: 6 | n = len(nums) 7 | 8 | dp = [[0] * n for _ in range(n)] 9 | 10 | for i in range(n): 11 | dp[i][i] = nums[i] 12 | 13 | for i in range(n - 1, -1, -1): 14 | for j in range(i + 1, n): 15 | dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]) 16 | return dp[0][n - 1] >= 0 17 | 18 | -------------------------------------------------------------------------------- /code/ch12/12.2.3.PredictTheWinner.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool PredictTheWinner(vector& nums) { 4 | int len = nums.size(); 5 | 6 | if (len % 2 == 0 || len == 1) return true; 7 | 8 | vector dp(len, 0); 9 | 10 | for (int i = len - 1; i > -1; i--) { 11 | dp[i] = nums[i]; 12 | for (int j = i + 1; j < len; j++) { 13 | dp[j] = max(nums[i] - dp[j], nums[j] - dp[j - 1]); 14 | } 15 | } 16 | 17 | return dp[len - 1] >= 0; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch12/12.2.3.PredictTheWinner.java: -------------------------------------------------------------------------------- 1 | package ch12.code; 2 | 3 | public class 12.2.3.PredictTheWinner { 4 | public boolean PredictTheWinner(int[] nums) { 5 | if (nums.length == 1 || nums.length % 2 == 0) 6 | return true; 7 | 8 | int[] dp = new int[nums.length]; 9 | 10 | for (int i = nums.length - 1; i >= 0; i--) { 11 | dp[i] = nums[i]; 12 | for (int j = i + 1; j < nums.length; j++) 13 | dp[j] = Math.max(nums[i] - dp[j], nums[j] - dp[j - 1]); 14 | } 15 | return dp[nums.length - 1] >= 0; 16 | } 17 | } -------------------------------------------------------------------------------- /code/ch12/12.2.3.PredictTheWinner.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def PredictTheWinner(self, nums: List[int]) -> bool: 6 | n = len(nums) 7 | if n % 2 == 0 or n == 1: 8 | return True 9 | dp = [0] * n 10 | 11 | for i in range(n - 1, -1, -1): 12 | dp[i] = nums[i] 13 | for j in range(i + 1, n): 14 | dp[j] = max(nums[i] - dp[j], nums[j] - dp[j - 1]) 15 | return dp[-1] >= 0 16 | 17 | -------------------------------------------------------------------------------- /code/ch12/12.3.1.nimGame.cpp: -------------------------------------------------------------------------------- 1 | //TLE 2 | class Solution { 3 | public: 4 | bool canWinNim(int n) { 5 | bool res = false; 6 | bool *mem = (bool *)malloc((n + 1) * sizeof(bool)); 7 | memset(mem, true, (n + 1) * sizeof(bool)); 8 | 9 | for (int i = 4; i < n + 1; i++) { 10 | if (!(mem[i - 1] && mem[i - 2] && mem[i - 3])) 11 | mem[i] = true; 12 | else 13 | mem[i] = false; 14 | } 15 | 16 | res = mem[n]; 17 | free(mem); 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /code/ch12/12.3.1.nimGame.java: -------------------------------------------------------------------------------- 1 | package ch12.code; 2 | 3 | public class NimGame { 4 | public boolean canWinNim(int n) { 5 | boolean[] mem = new boolean[n + 1]; 6 | mem[1] = true; 7 | mem[2] = true; 8 | mem[3] = true; 9 | 10 | for (int i = 4; i < n + 1; i++) { 11 | if (!(mem[i - 1] && mem[i - 2] && mem[i - 3])) 12 | mem[i] = true; 13 | } 14 | return mem[n]; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch12/12.3.1.nimGame.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canWinNim(self, n: int) -> bool: 3 | mem = [True] * (n + 1) 4 | for i in range(4, n + 1): 5 | if not (mem[i - 1] and mem[i - 2] and mem[i - 3]): 6 | mem[i] = True 7 | else: 8 | mem[i] = False 9 | return mem[n] -------------------------------------------------------------------------------- /code/ch12/12.3.2.nimGame.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canWinNim(int n) { 4 | if (n < 4) return true; 5 | 6 | int a = true, b = true, c = true; 7 | 8 | for (int i = 4; i < n + 1; i++) { 9 | int current = !(a && b && c); 10 | a = b; 11 | b = c; 12 | c = current; 13 | } 14 | 15 | return c; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /code/ch12/12.3.2.nimGame.java: -------------------------------------------------------------------------------- 1 | package ch12.code; 2 | 3 | public class NimGame { 4 | public boolean canWinNim(int n) { 5 | if (n < 4) 6 | return true; 7 | 8 | boolean a = true, b = true, c = true; 9 | 10 | for (int i = 4; i < n + 1; i++) { 11 | boolean current = a && b && c ? false : true; 12 | a = b; 13 | b = c; 14 | c = current; 15 | } 16 | 17 | return c; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch12/12.3.2.nimGame.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canWinNim(self, n: int) -> bool: 3 | if n < 4: 4 | return True 5 | a, b, c = True, True, True 6 | for i in range(4, n + 1): 7 | current = not (a and b and c) 8 | a, b, c = b, c, current 9 | return c 10 | -------------------------------------------------------------------------------- /code/ch12/12.3.3.nimGame.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canWinNim(int n) { 4 | return n % 4 != 0; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /code/ch12/12.3.3.nimGame.java: -------------------------------------------------------------------------------- 1 | package ch12.code; 2 | 3 | public class NimGame { 4 | public boolean canWinNim(int n) { 5 | if (n <= 0) { 6 | return false; 7 | } 8 | return 0 != n % 4; 9 | } 10 | } -------------------------------------------------------------------------------- /code/ch12/12.3.3.nimGame.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canWinNim3(self, n: int) -> bool: 3 | return n % 4 != 0 4 | 5 | -------------------------------------------------------------------------------- /code/ch12/12.4.1.GetMoneyAmount.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int cost(int low, int high) { 4 | if (low >= high) return 0; 5 | 6 | int res = INT_MAX; 7 | for (int i = low; i < high + 1; i++) { 8 | int tmp = i + max(cost(low, i - 1), cost(i + 1, high)); 9 | res = min(res, tmp); 10 | } 11 | 12 | return res; 13 | } 14 | public: 15 | int getMoneyAmount(int n) { 16 | return cost(1, n); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /code/ch12/12.4.1.GetMoneyAmount.java: -------------------------------------------------------------------------------- 1 | public class GetMoneyAmount { 2 | 3 | public int cost(int low, int high) { 4 | if (low >= high) 5 | return 0; 6 | int res = Integer.MAX_VALUE; 7 | for (int i = low; i <= high; i++) { 8 | int tmp = i + Math.max(cost(i + 1, high), cost(low, i - 1)); 9 | res = Math.min(tmp, res); 10 | } 11 | 12 | return res; 13 | } 14 | public int getMoneyAmount(int n) { 15 | return cost(1, n); 16 | } 17 | } -------------------------------------------------------------------------------- /code/ch12/12.4.1.GetMoneyAmount.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | class Solution: 5 | def getMoneyAmount(self, n: int) -> int: 6 | def cost(low: int, high: int) -> int: 7 | if low >= high: 8 | return 0 9 | res = sys.maxsize 10 | for i in range(low, high + 1): 11 | tmp = i + max( 12 | cost(low, i - 1), cost(i + 1, high) 13 | ) 14 | res = min(res, tmp) 15 | return res 16 | 17 | return cost(1, n) 18 | -------------------------------------------------------------------------------- /code/ch13/13.1.1.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | public class Solution { 3 | 4 | public int maxProfitOrigin(int[] prices) { 5 | int max = 0; 6 | for (int i = prices.length - 1; i > 0; i--) { 7 | for (int j = i - 1; j >= 0; j--) { 8 | if (prices[i] - prices[j] > max) 9 | max = prices[i] - prices[j]; 10 | } 11 | } 12 | return max; 13 | } 14 | } -------------------------------------------------------------------------------- /code/ch13/13.1.1.best_time_to_buy_and_sell_stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int maxprofit = 0; 5 | 6 | for (int i = 0; i < prices.size(); i++) { 7 | for (int j = i + 1; j < prices.size(); j++) { 8 | if (prices[j] - prices[i] > maxprofit) 9 | maxprofit = prices[j] - prices[i]; 10 | } 11 | } 12 | 13 | return maxprofit; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /code/ch13/13.1.1.best_time_to_buy_and_sell_stock.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def maxProfit(self, prices: List[int]) -> int: 6 | max_diff = 0 7 | for i in range(1, len(prices)): 8 | for j in range(0, i): 9 | if prices[i] - prices[j] > max_diff: 10 | max_diff = prices[i] - prices[j] 11 | return max_diff 12 | -------------------------------------------------------------------------------- /code/ch13/13.1.2.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | public class Solution { 3 | public int maxProfit(int[] prices) { 4 | if (prices == null || prices.length <= 1) 5 | return 0; 6 | 7 | int min = prices[0]; 8 | int max = 0; 9 | 10 | for (int i = 1; i < prices.length; i++) { 11 | max = Math.max(prices[i] - min, max); 12 | min = Math.min(min, prices[i]); 13 | } 14 | return max; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch13/13.1.2.best_time_to_buy_and_sell_stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | if (prices.size() == 0) 5 | return 0; 6 | 7 | int max_diff = 0; 8 | int min_price = prices[0]; 9 | 10 | for (int i = 1; i < prices.size(); i++) { 11 | if (min_price > prices[i]) 12 | min_price = prices[i]; 13 | max_diff = max(max_diff, prices[i] - min_price); 14 | } 15 | 16 | return max_diff; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /code/ch13/13.1.2.best_time_to_buy_and_sell_stock.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | import sys 3 | 4 | 5 | class Solution: 6 | def maxProfit(self, prices: List[int]) -> int: 7 | max_diff = 0 8 | min_price = sys.maxsize 9 | for i in range(len(prices)): 10 | if min_price > prices[i]: 11 | min_price = prices[i] 12 | max_diff = max(max_diff, prices[i] - min_price) 13 | return max_diff 14 | -------------------------------------------------------------------------------- /code/ch13/13.2.1.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | public class Solution { 3 | public int maxProfit(int[] prices) { 4 | if (prices == null || prices.length < 2) 5 | return 0; 6 | 7 | int ans = 0; 8 | for (int i = 1; i < prices.length; i++) { 9 | if (prices[i] - prices[i - 1] > 0) 10 | ans += prices[i] - prices[i - 1]; 11 | } 12 | return ans; 13 | } 14 | } -------------------------------------------------------------------------------- /code/ch13/13.2.best_time_to_buy_and_sell_stock_II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int maxprofit = 0; 5 | int size = prices.size(); 6 | 7 | for (int i = 1; i < size; i++) { 8 | if (prices[i] > prices[i - 1]) 9 | maxprofit += prices[i] - prices[i - 1]; 10 | } 11 | 12 | return maxprofit; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /code/ch13/13.2.best_time_to_buy_and_sell_stock_II.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def maxProfit(self, prices: List[int]) -> int: 6 | if len(prices) <= 1: 7 | return 0 8 | max_profit = 0 9 | for i in range(1, len(prices)): 10 | if prices[i] - prices[i - 1] > 0: 11 | max_profit += prices[i] - prices[i - 1] 12 | return max_profit 13 | -------------------------------------------------------------------------------- /code/ch13/13.3.1.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | 3 | public class Solution { 4 | public int maxProfitOrigin(int[] prices, int fee) { 5 | if (prices == null || prices.length < 2) 6 | return 0; 7 | 8 | int[] buy = new int[prices.length], sell = new int[prices.length]; 9 | buy[0] = -prices[0]; 10 | sell[0] = 0; 11 | 12 | for (int i = 1; i < prices.length; i++) { 13 | buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i]); 14 | sell[i] = Math.max(buy[i - 1] + prices[i] - fee, sell[i - 1]); 15 | } 16 | 17 | return sell[prices.length - 1]; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch13/13.3.1.best_time_to_buy_and_sell_stock_with_fee.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def maxProfit(self, prices: List[int], fee: int) -> int: 6 | if len(prices) <= 1: 7 | return 0 8 | # buy = [0 for i in range(len(prices))] 9 | # sell = [0 for i in range(len(prices))] 10 | buy, sell = [0] * len(prices), [0] * len(prices) 11 | buy[0] = -prices[0] 12 | for i in range(1, len(prices)): 13 | buy[i] = max(buy[i - 1], sell[i - 1] - prices[i]) 14 | sell[i] = max(buy[i - 1] + prices[i] - fee, sell[i - 1]) 15 | return sell[-1] 16 | -------------------------------------------------------------------------------- /code/ch13/13.3.2.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | 3 | public class Solution { 4 | public int maxProfit(int[] prices, int fee) { 5 | if (prices == null || prices.length < 2) 6 | return 0; 7 | 8 | int preBuy = -prices[0]; 9 | int preSell = 0; 10 | 11 | for (int i = 1; i < prices.length; i++) { 12 | int buy = Math.max(preBuy, preSell - prices[i]); 13 | int sell = Math.max(preBuy + prices[i] - fee, preSell); 14 | 15 | preBuy = buy; 16 | preSell = sell; 17 | } 18 | return preSell; 19 | } 20 | } -------------------------------------------------------------------------------- /code/ch13/13.3.2.best_time_to_buy_and_sell_stock_with_fee.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices, int fee) { 4 | if (prices.size() <= 1) 5 | return 0; 6 | 7 | int pre_buy = -prices[0]; 8 | int pre_sell = 0; 9 | 10 | for (int i = 1; i < prices.size(); i++) { 11 | int buy = max(pre_buy, pre_sell - prices[i]); 12 | int sell = max(pre_buy + prices[i] - fee, pre_sell); 13 | pre_buy = buy; 14 | pre_sell = sell; 15 | } 16 | 17 | return pre_sell; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch13/13.3.2.best_time_to_buy_and_sell_stock_with_fee.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def maxProfit(self, prices: List[int], fee: int) -> int: 6 | if len(prices) <= 1: 7 | return 0 8 | pre_buy = -prices[0] 9 | pre_sell = 0 10 | for i in range(1, len(prices)): 11 | buy = max(pre_buy, pre_sell - prices[i]) 12 | sell = max(pre_buy + prices[i] - fee, pre_sell) 13 | pre_buy = buy 14 | pre_sell = sell 15 | return pre_sell 16 | -------------------------------------------------------------------------------- /code/ch13/13.4.1.best_time_to_buy_and_sell_stock_with_cooldown.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def maxProfit(self, prices: List[int]) -> int: 6 | if len(prices) <= 1: 7 | return 0 8 | buy, sell = [0] * len(prices), [0] * len(prices) 9 | buy[0] = -prices[0] 10 | buy[1] = max((0 - prices[1]), buy[0]) 11 | sell[1] = max((buy[0] + prices[1]), sell[0]) 12 | for i in range(2, len(prices)): 13 | buy[i] = max((sell[i - 2] - prices[i]), buy[i - 1]) 14 | sell[i] = max((buy[i - 1] + prices[i]), sell[i - 1]) 15 | return sell[-1] 16 | -------------------------------------------------------------------------------- /code/ch13/13.4.2.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Solution { 6 | public int maxProfit(int[] prices) { 7 | if (prices == null || prices.length < 2) 8 | return 0; 9 | 10 | int b0 = -prices[0]; 11 | int b1 = b0; 12 | 13 | int s0 = 0; 14 | int s1 = 0; 15 | int s2 = 0; 16 | for (int i = 0; i < prices.length; i++) { 17 | b0 = Math.max(b1, s2 - prices[i]); 18 | s0 = Math.max(s1, b0 + prices[i]); 19 | 20 | s2 = s1; 21 | s1 = s0; 22 | b1 = b0; 23 | } 24 | return s0; 25 | } 26 | } -------------------------------------------------------------------------------- /code/ch13/13.4.2.best_time_to_buy_and_sell_stock_with_cooldown.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | if (prices.size() <= 1) 5 | return 0; 6 | 7 | int b0 = -prices[0]; 8 | int b1 = b0; 9 | int s0 = 0, s1 = 0, s2 = 0; 10 | 11 | for (int i = 0; i < prices.size(); i++) { 12 | b0 = max(b1, s2 - prices[i]); 13 | s0 = max(s1, b0 + prices[i]); 14 | s2 = s1; 15 | s1 = s0; 16 | b1 = b0; 17 | } 18 | 19 | return s0; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /code/ch13/13.4.2.best_time_to_buy_and_sell_stock_with_cooldown.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | 6 | def maxProfit(self, prices: List[int]) -> int: 7 | if len(prices) <= 1: 8 | return 0 9 | b0 = -prices[0] 10 | b1 = b0 11 | s0, s1, s2 = 0, 0, 0 12 | for i in range(0, len(prices)): 13 | b0 = max(b1, s2 - prices[i]) 14 | s0 = max(s1, b0 + prices[i]) 15 | s2 = s1 16 | s1 = s0 17 | b1 = b0 18 | return s0 19 | -------------------------------------------------------------------------------- /code/ch13/13.5.1.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | public class Solution { 3 | public int maxProfit(int k, int[] prices) { 4 | if (prices == null || prices.length < 2) 5 | return 0; 6 | 7 | int[][] dp = new int[k + 1][prices.length]; 8 | 9 | for (int i = 1; i <= k; i++) { 10 | int minPrice = prices[0]; 11 | for (int j = 1; j < prices.length; j++) { 12 | minPrice = Math.min(minPrice, prices[j] - dp[i - 1][j - 1]); 13 | dp[i][j] = Math.max(dp[i][j - 1], prices[j] - minPrice); 14 | } 15 | } 16 | return dp[k][prices.length - 1]; 17 | } 18 | } -------------------------------------------------------------------------------- /code/ch13/13.6.1.Solution.cpp: -------------------------------------------------------------------------------- 1 | int maxProfit(int k, vector& prices) { 2 | if (prices.size() <= 1) 3 | return 0; 4 | 5 | int *dp = new int[prices.size() + 1](); 6 | min_prices = prices[0]; 7 | 8 | for (int i = 1; i < prices.size(); i++) { 9 | min_price = min(min_price, prices[i] - dp[i - 1]); 10 | dp[i + 1] = max(dp[i], prices[i] - min_price); 11 | } 12 | 13 | int res = dp[prices.size() - 1]; 14 | 15 | delete [] dp; 16 | 17 | return res; 18 | } 19 | -------------------------------------------------------------------------------- /code/ch13/13.6.1.Solution.java: -------------------------------------------------------------------------------- 1 | package ch13.code; 2 | 3 | public class Solution { 4 | public int maxProfit(int[] prices) { 5 | if (prices == null || prices.length < 2) 6 | return 0; 7 | int[] dp = new int[prices.length + 1]; 8 | int min = prices[0]; 9 | 10 | for (int i = 1; i < prices.length; i++) { 11 | min = Math.min(min, prices[i] - dp[i - 1]); 12 | dp[i + 1] = Math.max(dp[i], prices[i] - min); 13 | } 14 | return dp[dp.length - 1]; 15 | } 16 | } -------------------------------------------------------------------------------- /code/ch13/13.6.1.Solution.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def maxProfit(self, prices: List[int]) -> int: 6 | if len(prices) <= 1: 7 | return 0 8 | dp = [0] * (len(prices)+1) 9 | min_price = prices[0] 10 | 11 | for i in range(1, len(prices)): 12 | min_price = min(min_price, prices[i] - dp[i - 1]) 13 | dp[i + 1] = max(dp[i], prices[i] - min_price) 14 | return dp[-1] -------------------------------------------------------------------------------- /code/ch14/14.1.1_21.merge-two-sorted-lists.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 4 | if (l1 == nullptr) { 5 | return l2; 6 | } else if (l2 == nullptr) { 7 | return l1; 8 | } else if (l1->val < l2->val) { 9 | l1->next = mergeTwoLists(l1->next, l2); 10 | return l1; 11 | } else { 12 | l2->next = mergeTwoLists(l1, l2->next); 13 | return l2; 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /code/ch14/14.1.1_21.merge-two-sorted-lists.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 3 | if (l1 == null) { 4 | return l2; 5 | } 6 | else if (l2 == null) { 7 | return l1; 8 | } 9 | else if (l1.val < l2.val) { 10 | l1.next = mergeTwoLists(l1.next, l2); 11 | return l1; 12 | } 13 | else { 14 | l2.next = mergeTwoLists(l1, l2.next); 15 | return l2; 16 | } 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch14/14.1.1_21.merge-two-sorted-lists.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | # Definition for singly-linked list. 3 | class ListNode: 4 | def __init__(self, x): 5 | self.val = x 6 | self.next = None 7 | 8 | 9 | class Solution: 10 | def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: 11 | if l1 is None: 12 | return l2 13 | if l2 is None: 14 | return l1 15 | if l1.val < l2.val: 16 | l1.next = self.mergeTwoLists(l1.next, l2) 17 | return l1 18 | l2.next = self.mergeTwoLists(l1, l2.next) 19 | return l2 -------------------------------------------------------------------------------- /code/ch14/14.1.2_21.merge-two-sorted-lists.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 3 | ListNode prehead = new ListNode(-1); 4 | 5 | ListNode prev = prehead; 6 | while (l1 != null && l2 != null) { 7 | if (l1.val <= l2.val) { 8 | prev.next = l1; 9 | l1 = l1.next; 10 | } else { 11 | prev.next = l2; 12 | l2 = l2.next; 13 | } 14 | prev = prev.next; 15 | } 16 | 17 | // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可 18 | prev.next = l1 == null ? l2 : l1; 19 | 20 | return prehead.next; 21 | } 22 | } -------------------------------------------------------------------------------- /code/ch14/14.2.1_215.kth-largest-element-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int findKthLargest(vector &nums, int k) { 9 | int size = nums.size(); 10 | sort(begin(nums), end(nums)); 11 | return nums[size - k]; 12 | } 13 | }; -------------------------------------------------------------------------------- /code/ch14/14.2.1_215.kth-largest-element-in-an-array.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class Solution { 4 | 5 | public int findKthLargest(int[] nums, int k) { 6 | int len = nums.length; 7 | Arrays.sort(nums); 8 | return nums[len - k]; 9 | } 10 | } -------------------------------------------------------------------------------- /code/ch14/14.2.1_215.kth-largest-element-in-an-array.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def findKthLargest(self, nums: List[int], k: int) -> int: 6 | nums.sort() 7 | return nums[-k] 8 | -------------------------------------------------------------------------------- /code/ch14/14.2.2_215.kth-largest-element-in-an-array.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def findKthLargest(self, nums: List[int], k: int) -> int: 6 | import heapq 7 | 8 | heap = [] 9 | 10 | for i in range(k): 11 | heap.append(nums[i]) 12 | 13 | heapq.heapify(heap) 14 | 15 | for i in range(k, len(nums)): 16 | if nums[i] > heap[0]: 17 | heapq.heappop(heap) 18 | heapq.heappush(heap, nums[i]) 19 | 20 | return heap[0] 21 | -------------------------------------------------------------------------------- /code/ch14/14.2.3_215.kth-largest-element-in-an-array.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Solution: 5 | def findKthLargest(self, nums: List[int], k: int) -> int: 6 | import heapq 7 | 8 | return heapq.nlargest(k, nums)[-1] 9 | -------------------------------------------------------------------------------- /code/ch14/14.3.1_240.search-a-2d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | for(int i = 0; i < matrix.size(); i++) 5 | for(int j = 0; j < matrix[0].size(); j++) { 6 | if(matrix[i][j] == target) return true; 7 | } 8 | return false; 9 | } 10 | }; -------------------------------------------------------------------------------- /code/ch14/14.3.1_240.search-a-2d-matrix-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | for (int i = 0; i < matrix.length; i++) { 4 | for (int j = 0; j < matrix[0].length; j++) { 5 | if (matrix[i][j] == target) { 6 | return true; 7 | } 8 | } 9 | } 10 | 11 | return false; 12 | } 13 | } -------------------------------------------------------------------------------- /code/ch14/14.3.1_240.search-a-2d-matrix-ii.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | class Solution: 3 | def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: 4 | """ 5 | :type matrix: List[List[int]] 6 | :type target: int 7 | :rtype: bool 8 | """ 9 | if not matrix or not matrix[0]: 10 | return False 11 | 12 | m = len(matrix) 13 | n = len(matrix[0]) 14 | 15 | for i in range(m): 16 | for j in range(n): 17 | if matrix[i][j] == target: 18 | return True 19 | 20 | return False 21 | -------------------------------------------------------------------------------- /code/ch14/14.3.2_240.search-a-2d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | for(int i = 0; i < matrix.size(); i++) 5 | for(int j = 0; j < matrix[0].size(); j++) { 6 | if(matrix[i][j] == target) return true; 7 | } 8 | return false; 9 | } 10 | }; -------------------------------------------------------------------------------- /code/ch14/14.3.2_240.search-a-2d-matrix-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | for (int i = 0; i < matrix.length; i++) { 4 | for (int j = 0; j < matrix[0].length; j++) { 5 | if (matrix[i][j] == target) { 6 | return true; 7 | } 8 | } 9 | } 10 | 11 | return false; 12 | } 13 | } -------------------------------------------------------------------------------- /code/ch14/14.3.3_240.search-a-2d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | if(matrix.size() == 0 || matrix[0].size() == 0) return false; 5 | int i = matrix.size() - 1; 6 | int j = 0; 7 | while(i >= 0 && j < matrix[0].size()){ 8 | if(matrix[i][j] > target) 9 | --i; 10 | else if(matrix[i][j] < target) 11 | ++j; 12 | else return true; 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /code/ch14/14.3.4_240.search-a-2d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | if(matrix.size() == 0 || matrix[0].size() == 0) return false; 5 | int i = matrix.size() - 1; 6 | int j = 0; 7 | while(i >= 0 && j < matrix[0].size()){ 8 | if(matrix[i][j] > target) 9 | --i; 10 | else if(matrix[i][j] < target) 11 | ++j; 12 | else return true; 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /code/ch14/14.3.5_240.search-a-2d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | if(matrix.size() == 0 || matrix[0].size() == 0) return false; 5 | int i = matrix.size() - 1; 6 | int j = 0; 7 | while(i >= 0 && j < matrix[0].size()){ 8 | if(matrix[i][j] > target) 9 | --i; 10 | else if(matrix[i][j] < target) 11 | ++j; 12 | else return true; 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /code/ch14/14.3.5_240.search-a-2d-matrix-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | // start our "pointer" in the bottom-left 4 | int row = matrix.length-1; 5 | int col = 0; 6 | 7 | while (row >= 0 && col < matrix[0].length) { 8 | if (matrix[row][col] > target) { 9 | row--; 10 | } else if (matrix[row][col] < target) { 11 | col++; 12 | } else { // found it 13 | return true; 14 | } 15 | } 16 | 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch14/14.3.6_240.search-a-2d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& matrix, int target) { 4 | if(matrix.size() == 0 || matrix[0].size() == 0) return false; 5 | int i = matrix.size() - 1; 6 | int j = 0; 7 | while(i >= 0 && j < matrix[0].size()){ 8 | if(matrix[i][j] > target) 9 | --i; 10 | else if(matrix[i][j] < target) 11 | ++j; 12 | else return true; 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /code/ch14/14.3.6_240.search-a-2d-matrix-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | // start our "pointer" in the bottom-left 4 | int row = matrix.length-1; 5 | int col = 0; 6 | 7 | while (row >= 0 && col < matrix[0].length) { 8 | if (matrix[row][col] > target) { 9 | row--; 10 | } else if (matrix[row][col] < target) { 11 | col++; 12 | } else { // found it 13 | return true; 14 | } 15 | } 16 | 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch14/14.3.6_240.search-a-2d-matrix-ii.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | class Solution: 3 | def searchMatrix(self, matrix: List[List[int]], target: int): 4 | if len(matrix) == 0 or len(matrix[0]) == 0: 5 | return False 6 | 7 | m = len(matrix) 8 | n = len(matrix[0]) 9 | 10 | row = m - 1 11 | col = 0 12 | 13 | while col < n and row >= 0: 14 | if matrix[row][col] > target: 15 | row -= 1 16 | elif matrix[row][col] < target: 17 | col += 1 18 | else: 19 | return True 20 | 21 | return False 22 | -------------------------------------------------------------------------------- /code/ch15/15.1.1.assign-cookies.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findContentChildren(self, g: List[int], s: List[int]) -> int: 3 | # 对孩子胃口以及饼干排序 4 | g.sort() 5 | s.sort() 6 | ans = 0 7 | # 饼干下标 8 | idx = 0 9 | # 遍历每个孩子 10 | for i in range(len(g)): 11 | # 遍历剩余的饼干 12 | while idx < len(s): 13 | if s[idx] >= g[i]: 14 | ans += 1 15 | idx += 1 16 | break 17 | else: 18 | idx += 1 19 | return ans 20 | -------------------------------------------------------------------------------- /code/ch15/15.2.1.jump-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | bool dp[nums.size()]; 5 | memset(dp, false, sizeof(dp)); 6 | dp[0] = true; 7 | for (int i = 1; i < nums.size(); i++) { 8 | for (int j = 0; j < i; j++) { 9 | if (j + nums[j] >= i) { 10 | dp[i] = dp[i] | dp[j]; 11 | } 12 | } 13 | } 14 | return dp[nums.size() - 1]; 15 | } 16 | }; -------------------------------------------------------------------------------- /code/ch15/15.2.1.jump-game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | boolean[] dp = new boolean[nums.length]; 4 | dp[0] = true; 5 | for (int i = 1; i < nums.length; i++) { 6 | for (int j = 0; j < i; j++) { 7 | if (j + nums[j] >= i) { 8 | dp[i] = dp[i] | dp[j]; 9 | } 10 | } 11 | } 12 | return dp[nums.length - 1]; 13 | } 14 | } -------------------------------------------------------------------------------- /code/ch15/15.2.1.jump-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums: List[int]) -> bool: 3 | dp = [False] * len(nums) 4 | dp[0] = True 5 | for i in range(1, len(nums)): 6 | for j in range(0, i): 7 | if j + nums[j] >= i: 8 | dp[i] = dp[i] | dp[j] 9 | return dp[len(nums) - 1] 10 | -------------------------------------------------------------------------------- /code/ch15/15.2.2.jump-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int n = nums.size(); 5 | int dp[n]; 6 | memset(dp, 0, sizeof(dp)); 7 | dp[0] = nums[0]; 8 | for (int i = 1; i < n; i++) { 9 | if (dp[i - 1] < i) { 10 | dp[i] = dp[i - 1]; 11 | } else { 12 | if (dp[i - 1] > i + nums[i]) { 13 | dp[i] = dp[i - 1]; 14 | } else { 15 | dp[i] = i + nums[i]; 16 | } 17 | } 18 | } 19 | return dp[n - 1] >= n - 1; 20 | } 21 | }; -------------------------------------------------------------------------------- /code/ch15/15.2.2.jump-game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int n = nums.length; 4 | int[] dp = new int[n]; 5 | dp[0] = nums[0]; 6 | for (int i = 1; i < n; i++) { 7 | if (dp[i - 1] < i) { 8 | dp[i] = dp[i - 1]; 9 | } else { 10 | dp[i] = Math.max(dp[i - 1], i + nums[i]); 11 | } 12 | } 13 | return dp[n - 1] >= n - 1; 14 | } 15 | } -------------------------------------------------------------------------------- /code/ch15/15.2.2.jump-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums: List[int]) -> bool: 3 | n = len(nums) 4 | dp = [0] * n 5 | dp[0] = nums[0] 6 | for i in range(1, n): 7 | if dp[i - 1] < i: 8 | dp[i] = dp[i - 1] 9 | else: 10 | dp[i] = max(dp[i - 1], i + nums[i]) 11 | return dp[n - 1] >= n - 1 12 | -------------------------------------------------------------------------------- /code/ch15/15.2.3.jump-game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int begin = 0, end = 0; 4 | while (true) { 5 | // 下次考虑的最远位置 6 | int next_end = end; 7 | // min(end + 1, len(nums)) 防止越界 8 | for (int i = begin; i < Math.min(end + 1, nums.length); i++) { 9 | next_end = Math.max(next_end, i + nums[i]); 10 | } 11 | if (next_end == end) { 12 | break; 13 | } 14 | begin = end + 1; 15 | end = next_end; 16 | } 17 | return end >= nums.length - 1; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch15/15.2.3.jump-game.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums: List[int]) -> bool: 3 | # 可跳跃范围 4 | begin, end = 0, 0 5 | # 下次考虑的最远位置,终点位置 6 | next_end = end, target = len(nums) - 1 7 | while next_end < target: 8 | # min(end + 1, len(nums)) 防止越界 9 | for i in range(begin, min(end + 1, len(nums))): 10 | next_end = max(next_end, i + nums[i]) 11 | # 范围不发生变化,终点不可达 12 | if next_end == end: 13 | return False 14 | begin, end = end + 1, next_end 15 | return True 16 | -------------------------------------------------------------------------------- /code/ch15/15.3.1.task-scheduler.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def leastInterval(self, tasks: List[str], n: int) -> int: 3 | t_map = [0] * 26 4 | for t in tasks: 5 | t_map[ord(t) - ord("A")] += 1 6 | t_map.sort() 7 | # max_num 为最高次数,cnt 为最高次数的任务种类 8 | max_num, cnt = t_map[25], 0 9 | for i in range(26): 10 | if t_map[i] == max_num: 11 | cnt += 1 12 | return max((max_num - 1) * (n + 1) + cnt, len(tasks)) 13 | -------------------------------------------------------------------------------- /code/ch15/15.4.1.candy.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def candy(self, ratings: List[int]) -> int: 3 | left_ans, right_ans = [1] * len(ratings), [1] * len(ratings) 4 | ans = 0 5 | # 考虑左相邻孩子条件 6 | for i in range(1, len(ratings)): 7 | if ratings[i] > ratings[i - 1]: 8 | left_ans[i] = left_ans[i - 1] + 1 9 | # 考虑右相邻孩子条件 10 | for i in range(len(ratings) - 2, -1, -1): 11 | if ratings[i] > ratings[i + 1]: 12 | right_ans[i] = right_ans[i + 1] + 1 13 | # 合并两个条件结果 14 | for i in range(0, len(ratings)): 15 | ans += max(left_ans[i], right_ans[i]) 16 | return ans 17 | -------------------------------------------------------------------------------- /code/ch16/16.3.1.subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | vector path; 5 | vector> subsets(vector& nums) { 6 | dfs(nums, 0); 7 | return ans; 8 | } 9 | void dfs(vector& nums, int idx) { 10 | if (idx == nums.size()) { 11 | ans.push_back(vector(path)); 12 | return; 13 | } 14 | path.push_back(nums[idx]); 15 | dfs(nums, idx + 1); 16 | path.pop_back(); 17 | dfs(nums, idx + 1); 18 | } 19 | }; -------------------------------------------------------------------------------- /code/ch16/16.3.1.subsets.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | List> ans = new ArrayList<>(); 3 | public List> subsets(int[] nums) { 4 | dfs(nums, 0, new ArrayList<>()); 5 | return ans; 6 | } 7 | public void dfs(int[] nums, int idx, List path) { 8 | if (idx == nums.length) { 9 | ans.add(new ArrayList<>(path)); 10 | return; 11 | } 12 | path.add(nums[idx]); 13 | dfs(nums, idx + 1, path); 14 | path.remove(path.size() - 1); 15 | dfs(nums, idx + 1, path); 16 | } 17 | } -------------------------------------------------------------------------------- /code/ch16/16.3.1.subsets.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subsets(self, nums: List[int]) -> List[List[int]]: 3 | ans = [] 4 | n = len(nums) 5 | 6 | def dfs(idx: int, path: List[int]): 7 | if idx == n: 8 | ans.append(path[:]) 9 | return 10 | path.append(nums[idx]) 11 | dfs(idx + 1, path) 12 | path.pop() 13 | dfs(idx + 1, path) 14 | 15 | dfs(0, []) 16 | return ans 17 | -------------------------------------------------------------------------------- /code/ch16/16.4.1.permutations.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def permute(self, nums: List[int]) -> List[List[int]]: 3 | ans = [] 4 | n = len(nums) 5 | visited = set() 6 | 7 | def dfs(path: List[int]): 8 | if len(path) == n: 9 | ans.append(path[:]) 10 | return 11 | for i in range(n): 12 | if i not in visited: 13 | visited.add(i) 14 | path.append(nums[i]) 15 | dfs(path) 16 | path.pop() 17 | visited.remove(i) 18 | 19 | dfs([]) 20 | return ans 21 | -------------------------------------------------------------------------------- /code/ch17/17.2.1.largestRectangleArea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int largestRectangleArea(vector& heights) { 8 | int n = heights.size(), ans = 0; 9 | if (n != 0) ans = heights[0]; 10 | for (int i = 0; i < n; i++) { 11 | int height = heights[i]; 12 | for (int j = i; j < n; j++) { 13 | height = min(height, heights[j]); 14 | ans = max(ans, (j - i + 1) * height); 15 | } 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch17/17.2.1.largestRectangleArea.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.Math; 3 | 4 | public class Solution { 5 | public int largestRectangleArea(int[] heights) { 6 | int n = heights.length, ans = 0; 7 | if (n != 0) ans = heights[0]; 8 | for (int i = 0; i < n; i++) { 9 | int height = heights[i]; 10 | for (int j = i; j < n; j++) { 11 | height = Math.min(height, heights[j]); 12 | ans = Math.max(ans, (j - i + 1) * height); 13 | } 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /code/ch17/17.2.1.largestRectangleArea.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestRectangleArea(self, heights: List[int]) -> int: 3 | n, ans = len(heights), 0 4 | if n != 0: 5 | ans = heights[0] 6 | for i in range(n): 7 | height = heights[i] 8 | for j in range(i, n): 9 | height = min(height, heights[j]) 10 | ans = max(ans, (j - i + 1) * height) 11 | return ans 12 | -------------------------------------------------------------------------------- /code/ch17/17.2.2.largestRectangleArea.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestRectangleArea(self, heights: List[int]) -> int: 3 | n = len(heights) 4 | l, r, ans = [-1] * n, [n] * n, 0 5 | for i in range(1, n): 6 | j = i - 1 7 | while j >= 0 and heights[j] >= heights[i]: 8 | j -= 1 9 | l[i] = j 10 | for i in range(n - 2, -1, -1): 11 | j = i + 1 12 | while j < n and heights[j] >= heights[i]: 13 | j += 1 14 | r[i] = j 15 | for i in range(n): 16 | ans = max(ans, heights[i] * (r[i] - l[i] - 1)) 17 | return ans 18 | -------------------------------------------------------------------------------- /code/ch17/17.2.3.largestRectangleArea.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestRectangleArea(self, heights: List[int]) -> int: 3 | n = len(heights) 4 | l, r, ans = [-1] * n, [n] * n, 0 5 | 6 | for i in range(1, n): 7 | j = i - 1 8 | while j >= 0 and heights[j] >= heights[i]: 9 | j = l[j] 10 | l[i] = j 11 | for i in range(n - 2, -1, -1): 12 | j = i + 1 13 | while j < n and heights[j] >= heights[i]: 14 | j = r[j] 15 | r[i] = j 16 | for i in range(n): 17 | ans = max(ans, heights[i] * (r[i] - l[i] - 1)) 18 | return ans 19 | -------------------------------------------------------------------------------- /code/ch17/17.2.4.largestRectangleArea.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestRectangleArea(self, heights: List[int]) -> int: 3 | n, heights, st, ans = len(heights), [0] + heights + [0], [], 0 4 | for i in range(n + 2): 5 | while st and heights[st[-1]] > heights[i]: 6 | ans = max(ans, heights[st.pop(-1)] * (i - st[-1] - 1)) 7 | st.append(i) 8 | return ans 9 | -------------------------------------------------------------------------------- /code/ch17/17.3.2.dayOfTheWeek.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string dayOfTheWeek(int day, int month, int year) { 7 | string list_of_days[] = { 8 | "Saturday", "Sunday", "Monday", "Tuesday", 9 | "Wednesday", "Thursday", "Friday" 10 | }; 11 | if (month == 1 || month == 2) { 12 | year -= 1; 13 | month += 12; 14 | } 15 | int y = year % 100, c = year / 100; 16 | int weekday = (day + 13 * (month + 1) / 5 + y + \ 17 | y / 4 + c / 4 + 5 * c) % 7; 18 | return list_of_days[weekday]; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /code/ch17/17.3.2.dayOfTheWeek.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Solution { 4 | public String dayOfTheWeek(int day, int month, int year) { 5 | String[] list_of_days = { 6 | "Saturday", "Sunday", "Monday", "Tuesday", 7 | "Wednesday", "Thursday", "Friday" 8 | }; 9 | if (month == 1 || month == 2) { 10 | year -= 1; 11 | month += 12; 12 | } 13 | int y = year % 100, c = year / 100; 14 | int weekday = (day + 13 * (month + 1) / 5 + y 15 | + y / 4 + c / 4 + 5 * c) % 7; 16 | return list_of_days[weekday]; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/ch17/17.3.2.dayOfTheWeek.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def dayOfTheWeek(self, day: int, month: int, year: int) -> str: 3 | list_of_days = [ 4 | "Saturday", 5 | "Sunday", 6 | "Monday", 7 | "Tuesday", 8 | "Wednesday", 9 | "Thursday", 10 | "Friday", 11 | ] 12 | if month == 1 or month == 2: 13 | year = year - 1 14 | month = month + 12 15 | y = year % 100 16 | c = year // 100 17 | weekday = int((day + 13 * (month + 1) // 5 + y + y // 4 + c // 4 + 5 * c) % 7) 18 | required_day = list_of_days[weekday] 19 | return required_day 20 | -------------------------------------------------------------------------------- /code/ch17/17.4.2.canMeasureWater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int gcd(int a, int b) { 7 | int smaller = min(a, b); 8 | while (smaller) { 9 | if (a % smaller == 0 && b % smaller == 0) 10 | return smaller; 11 | smaller -= 1; 12 | } 13 | } 14 | bool canMeasureWater(int x, int y, int z) { 15 | if (x + y < z) return false; 16 | if (z == 0) return true; 17 | if (x == 0) return y == z; 18 | if (y == 0) return x == z; 19 | return z % gcd(x, y) == 0; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /code/ch17/17.4.2.canMeasureWater.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int gcd(int a, int b) { 5 | int smaller = Math.min(a, b); 6 | while (smaller != 0) { 7 | if (a % smaller == 0 && b % smaller == 0) 8 | return smaller; 9 | smaller -= 1; 10 | } 11 | } 12 | boolean canMeasureWater(int x, int y, int z) { 13 | if (x + y < z) return false; 14 | if (z == 0) return true; 15 | if (x == 0) return y == z; 16 | if (y == 0) return x == z; 17 | return z % gcd(x, y) == 0; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/ch17/17.4.2.canMeasureWater.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canMeasureWater(self, x: int, y: int, z: int) -> bool: 3 | if x + y < z: 4 | return False 5 | 6 | if z == 0: 7 | return True 8 | 9 | if x == 0: 10 | return y == z 11 | 12 | if y == 0: 13 | return x == z 14 | 15 | def GCD(a: int, b: int) -> int: 16 | smaller = min(a, b) 17 | while smaller: 18 | if a % smaller == 0 and b % smaller == 0: 19 | return smaller 20 | smaller -= 1 21 | 22 | return z % GCD(x, y) == 0 23 | -------------------------------------------------------------------------------- /code/ch17/17.4.3.canMeasureWater.cpp: -------------------------------------------------------------------------------- 1 | int gcd(int a, int b) { 2 | return (b == 0) ? a : gcd(b, a % b); 3 | } 4 | -------------------------------------------------------------------------------- /code/ch17/17.4.3.canMeasureWater.java: -------------------------------------------------------------------------------- 1 | public class Gcd { 2 | public int gcd(int a, int b) { 3 | return (b == 0) ? a : gcd(b, a % b); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /code/ch17/17.4.3.canMeasureWater.py: -------------------------------------------------------------------------------- 1 | def GCD(a: int, b: int) -> int: 2 | return a if b == 0 else GCD(b, a % b) 3 | -------------------------------------------------------------------------------- /code/ch17/17.4.4.canMeasureWater.cpp: -------------------------------------------------------------------------------- 1 | int gcd(int a, int b) { 2 | if (a == b) return a; 3 | if (a < b) 4 | return gcd(b - a, a); 5 | return gcd(a - b, b); 6 | } 7 | -------------------------------------------------------------------------------- /code/ch17/17.4.4.canMeasureWater.java: -------------------------------------------------------------------------------- 1 | public class Gcd { 2 | public int gcd(int a, int b) { 3 | if (a == b) return a; 4 | if (a < b) 5 | return gcd(b - a, a); 6 | return gcd(a - b, b); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /code/ch17/17.4.4.canMeasureWater.py: -------------------------------------------------------------------------------- 1 | def GCD(a: int, b: int) -> int: 2 | if a == b: 3 | return a 4 | if a < b: 5 | return GCD(b - a, a) 6 | return GCD(a - b, b) 7 | -------------------------------------------------------------------------------- /code/ch17/17.5.1.poorPigs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Solution { 5 | public: 6 | int poorPigs(int buckets, int minutesToDie, 7 | int minutesToTest) { 8 | int cnt = 0; 9 | while (pow(minutesToTest / minutesToDie + 1, cnt) <\ 10 | buckets) 11 | cnt += 1; 12 | return cnt; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /code/ch17/17.5.1.poorPigs.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int poorPigs(int buckets, int minutesToDie, 5 | int minutesToTest) { 6 | int cnt = 0; 7 | while (Math.pow(minutesToTest / minutesToDie + 1, cnt) 8 | < buckets) 9 | cnt += 1; 10 | return cnt; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/ch17/17.5.1.poorPigs.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int: 3 | cnt = 0 4 | while (minutesToTest / minutesToDie + 1) ** cnt < buckets: 5 | cnt += 1 6 | return cnt 7 | -------------------------------------------------------------------------------- /code/ch17/17.5.2.poorPigs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Solution { 5 | public: 6 | int poorPigs(int buckets, int minutesToDie, 7 | int minutesToTest) { 8 | return ceil(log(buckets) / log(minutesToTest /\ 9 | minutesToDie + 1)); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /code/ch17/17.5.2.poorPigs.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int poorPigs(int buckets, int minutesToDie, 5 | int minutesToTest) { 6 | return (int)Math.ceil(Math.log(buckets) / Math.log( 7 | minutesToTest / minutesToDie + 1 8 | )); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/ch17/17.5.2.poorPigs.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | class Solution: 5 | def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int: 6 | return math.ceil(math.log(buckets, minutesToTest / minutesToDie + 1)) 7 | -------------------------------------------------------------------------------- /code/ch18/18.1.1.cpp: -------------------------------------------------------------------------------- 1 | int bs(vector& nums, int target) { 2 | int n = nums.size(); 3 | int l = 0, h = n - 1; 4 | while (l <= h) { 5 | int mid = l + (h - l) / 2; 6 | if (nums[mid] == target) { 7 | return mid; 8 | } else if (nums[mid] < target) { 9 | l = mid + 1; 10 | } else { 11 | h = mid - 1; 12 | } 13 | } 14 | return -1; 15 | } -------------------------------------------------------------------------------- /code/ch18/18.1.1.java: -------------------------------------------------------------------------------- 1 | public int bs(int[] nums, int target) { 2 | int n = nums.length; 3 | int l = 0, h = n - 1; 4 | while (l <= h) { 5 | int mid = l + (h - l) / 2; 6 | if (nums[mid] == target) { 7 | return mid; 8 | } else if (nums[mid] < target) { 9 | l = mid + 1; 10 | } else { 11 | h = mid - 1; 12 | } 13 | } 14 | return -1; 15 | } -------------------------------------------------------------------------------- /code/ch18/18.1.1.py: -------------------------------------------------------------------------------- 1 | # 查找 nums 数组中元素值为 target 的下标,如果不存在则返回 -1 2 | def bs(nums: [], target: int) -> int: 3 | n = len(nums) 4 | l, h = 0, n - 1 5 | while l <= h: 6 | mid = l + (h - l) // 2 7 | if nums[mid] == target: 8 | return mid 9 | elif nums[mid] < target: 10 | l = mid + 1 11 | else: 12 | h = mid - 1 13 | return -1 14 | -------------------------------------------------------------------------------- /code/ch18/18.1.2.cpp: -------------------------------------------------------------------------------- 1 | #查找第一个大于等于 x 的元素 2 | int bs(vector& nums, int x) { 3 | int l = 0, h = nums.size() - 1; 4 | while (l <= h) { 5 | int mid = l + (h - l) / 2; 6 | if (l == h) { 7 | break; 8 | } else if (nums[mid] >= x) { 9 | h = mid; 10 | } else { 11 | l = mid + 1; 12 | } 13 | } 14 | return nums[l]; 15 | } -------------------------------------------------------------------------------- /code/ch18/18.1.2.java: -------------------------------------------------------------------------------- 1 | # 查找第一个大于等于 x 的元素 2 | public int bs(int[] nums, int x) { 3 | int l = 0, h = nums.length - 1; 4 | while (l <= h) { 5 | int mid = l + (h - l) / 2; 6 | if (l == h) { 7 | break; 8 | } else if (nums[mid] >= x) { 9 | h = mid; 10 | } else { 11 | l = mid + 1; 12 | } 13 | } 14 | return nums[l]; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /code/ch18/18.1.2.py: -------------------------------------------------------------------------------- 1 | # 查找第一个大于等于 x 的元素 2 | def bs(nums: List[int], x: int) -> int: 3 | l, h = 0, len(nums) - 1 4 | while l <= h: 5 | mid = l + (h - l) // 2 6 | if l == h: 7 | break 8 | elif nums[mid] >= x: 9 | h = mid 10 | else: 11 | l = mid + 1 12 | return nums[l] 13 | -------------------------------------------------------------------------------- /code/ch18/18.1.3.cpp: -------------------------------------------------------------------------------- 1 | #查找最后一个小于等于 x 的元素 2 | int bs(vector& nums, int x) { 3 | int l = 0, h = nums.size() - 1; 4 | while (l <= h) { 5 | int mid = l + (h - l) / 2; 6 | if (l == h || l + 1 == h) { 7 | break; 8 | } else if (nums[mid] <= x) { 9 | l = mid; 10 | } else { 11 | h = mid - 1; 12 | } 13 | } 14 | if (nums[h] <= x) { 15 | return nums[h]; 16 | } else { 17 | return nums[l]; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch18/18.1.3.java: -------------------------------------------------------------------------------- 1 | # 查找最后一个小于等于 x 的元素 2 | public int bs(int[] nums, int x) { 3 | int l = 0, h = nums.length - 1; 4 | while (l <= h) { 5 | int mid = l + (h - l) / 2; 6 | if (l == h || l + 1 == h) { 7 | break; 8 | } else if (nums[mid] <= x) { 9 | l = mid; 10 | } else { 11 | h = mid - 1; 12 | } 13 | } 14 | if (nums[h] <= x) { 15 | return nums[h]; 16 | } else { 17 | return nums[l]; 18 | } 19 | } -------------------------------------------------------------------------------- /code/ch18/18.1.3.py: -------------------------------------------------------------------------------- 1 | # 查找最后一个小于等于 x 的元素 2 | def bs(nums: List[int], x: int) -> int: 3 | l, h = 0, len(nums) - 1 4 | while l <= h: 5 | mid = l + (h - l) // 2 6 | if l == h or l + 1 == h: 7 | break 8 | elif nums[mid] <= x: 9 | l = mid 10 | else: 11 | h = mid - 1 12 | if nums[h] <= x: 13 | return nums[h] 14 | else: 15 | return nums[l] 16 | -------------------------------------------------------------------------------- /code/ch18/18.6.1.cpp: -------------------------------------------------------------------------------- 1 | bool isPrime(int n) { 2 | if (n <= 1) { 3 | return false; 4 | } 5 | int i = 2; 6 | while (i * i <= n) { 7 | if (n % i == 0) { 8 | return false; 9 | } 10 | i += 1; 11 | } 12 | return true; 13 | } -------------------------------------------------------------------------------- /code/ch18/18.6.1.java: -------------------------------------------------------------------------------- 1 | public boolean isPrime(int n) { 2 | if (n <= 1) { 3 | return false; 4 | } 5 | int i = 2; 6 | while (i * i <= n) { 7 | if (n % i == 0) { 8 | return false; 9 | } 10 | i += 1; 11 | } 12 | return true; 13 | } -------------------------------------------------------------------------------- /code/ch18/18.6.1.py: -------------------------------------------------------------------------------- 1 | def isPrime(n: int) -> boolean: 2 | if n <= 1: 3 | return False 4 | i = 2 5 | while i * i <= n: 6 | if n % i == 0: 7 | return False 8 | i += 1 9 | return True 10 | -------------------------------------------------------------------------------- /code/ch18/18.6.2.cpp: -------------------------------------------------------------------------------- 1 | int countPrimes(int n) { 2 | if (n <= 1) { 3 | return 0; 4 | } 5 | vector tmp(n, true); 6 | int ans = 0; 7 | tmp[0] = false; 8 | tmp[1] = false; 9 | for (int i = 2; i < n; i++) { 10 | if (!tmp[i]) { 11 | continue; 12 | } 13 | // 查找 i 倍数的优化细节:i * i开始 14 | for (int j = i * i; j < n; j += i) { 15 | tmp[j] = false; 16 | } 17 | } 18 | for (int i = 0; i < n; i++) { 19 | if (tmp[i]) { 20 | ans += 1; 21 | } 22 | } 23 | return ans; 24 | } -------------------------------------------------------------------------------- /code/ch18/18.6.2.java: -------------------------------------------------------------------------------- 1 | public int countPrimes(int n) { 2 | if (n <= 1) { 3 | return 0; 4 | } 5 | boolean[] tmp = new boolean[n]; 6 | Arrays.fill(tmp, true); 7 | int ans = 0; 8 | tmp[0] = false; 9 | tmp[1] = false; 10 | for (int i = 2; i < n; i++) { 11 | if (!tmp[i]) { 12 | continue; 13 | } 14 | // 查找 i 倍数的优化细节:i * i开始 15 | for (int j = i * i; j < n; j += i) { 16 | tmp[j] = false; 17 | } 18 | } 19 | for (int i = 0; i < n; i++) { 20 | if (tmp[i]) { 21 | ans += 1; 22 | } 23 | } 24 | return ans; 25 | } -------------------------------------------------------------------------------- /code/ch18/18.6.2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPrimes(self, n: int) -> int: 3 | if n <= 1: 4 | return 0 5 | tmp = [True for i in range(n)] 6 | ans = 0 7 | tmp[0] = False 8 | tmp[1] = False 9 | for i in range(2, n): 10 | if not tmp[i]: 11 | continue 12 | # 查找 i 倍数的优化细节:i * i开始 13 | for j in range(i * i, n, i): 14 | tmp[j] = False 15 | for i in range(n): 16 | if tmp[i]: 17 | ans += 1 18 | return ans 19 | -------------------------------------------------------------------------------- /code/ch18/18.6.3.cpp: -------------------------------------------------------------------------------- 1 | int gcd(int a, int b) { 2 | return b == 0 ? a : gcd(b, a % b); 3 | } -------------------------------------------------------------------------------- /code/ch18/18.6.3.java: -------------------------------------------------------------------------------- 1 | public int gcd(int a, int b) { 2 | return b == 0 ? a : gcd(b, a % b); 3 | } -------------------------------------------------------------------------------- /code/ch18/18.6.3.py: -------------------------------------------------------------------------------- 1 | def gcd(a: int, b: int) -> int: 2 | return a if b == 0 else gcd(b, a % b) 3 | -------------------------------------------------------------------------------- /code/ch18/18.6.4.cpp: -------------------------------------------------------------------------------- 1 | int gcd(int a, int b) { 2 | return b == 0 ? a : gcd(b, a % b); 3 | } 4 | 5 | int lcm(int a, int b) { 6 | return a * b / gcd(a, b); 7 | } -------------------------------------------------------------------------------- /code/ch18/18.6.4.java: -------------------------------------------------------------------------------- 1 | public int gcd(int a, int b) { 2 | return b == 0 ? a : gcd(b, a % b); 3 | } 4 | 5 | public int lcm(int a, int b) { 6 | return a * b / gcd(a, b); 7 | } -------------------------------------------------------------------------------- /code/ch18/18.6.4.py: -------------------------------------------------------------------------------- 1 | def gcd(a: int, b: int) -> int: 2 | return a if b == 0 else gcd(b, a % b) 3 | 4 | 5 | def lcm(a: int, b: int) -> int: 6 | return a * b // gcd(a, b) 7 | -------------------------------------------------------------------------------- /code/ch19/19.1.1. 189.rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void rotate(vector& nums, int k) { 7 | vector copy = nums; 8 | int n = nums.size(); 9 | for (int i = 0; i < n; i++) 10 | nums[(k + i) % n] = copy[i]; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /code/ch19/19.1.1. 189.rotate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void rotate(int[] nums, int k) { 3 | int[] copy = new int[nums.length]; 4 | int n = nums.length; 5 | for (int i = 0; i < n; i++) copy[i] = nums[i]; 6 | for (int i = 0; i < n; i++) 7 | nums[(k + i) % n] = copy[i]; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /code/ch19/19.1.1. 189.rotate.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | copy = nums.copy() 4 | n = len(nums) 5 | 6 | for i in range(n): 7 | nums[(k + i) % n] = copy[i] 8 | -------------------------------------------------------------------------------- /code/ch19/19.1.2. 189.rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void rotate(vector& nums, int k) { 7 | vector copy = nums; 8 | int n = nums.size(); 9 | if (k % n == 0) return; 10 | for (int i = 0; i < n; i++) 11 | nums[(k + i) % n] = copy[i]; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /code/ch19/19.1.2. 189.rotate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void rotate(int[] nums, int k) { 3 | int[] copy = new int[nums.length]; 4 | int n = nums.length; 5 | for (int i = 0; i < n; i++) copy[i] = nums[i]; 6 | if (k % n == 0) return; 7 | for (int i = 0; i < n; i++) 8 | nums[(k + i) % n] = copy[i]; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/ch19/19.1.2. 189.rotate.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | copy = nums.copy() 4 | n = len(nums) 5 | if k % n == 0: 6 | return 7 | 8 | for i in range(n): 9 | nums[(k + i) % n] = copy[i] 10 | -------------------------------------------------------------------------------- /code/ch19/19.1.3. 189.rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void rotate(vector& nums, int k) { 7 | int n = nums.size(), t, offset = n - k % n; 8 | if (offset == 0) return; 9 | while (offset) { 10 | t = nums[0]; 11 | offset -= 1; 12 | for (int i = 0; i < n - 1; i++) 13 | nums[i] = nums[i + 1]; 14 | nums[n - 1] = t; 15 | } 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /code/ch19/19.1.3. 189.rotate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void rotate(int[] nums, int k) { 3 | int n = nums.length, t, offset = n - k % n; 4 | if (offset == 0) return; 5 | while (offset != 0) { 6 | t = nums[0]; 7 | offset -= 1; 8 | for (int i = 0; i < n - 1; i++) 9 | nums[i] = nums[i + 1]; 10 | nums[n - 1] = t; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /code/ch19/19.1.3. 189.rotate.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | """ 4 | Do not return anything, modify nums in-place instead. 5 | """ 6 | n = len(nums) 7 | t = None 8 | offset = n - k % n # 右移变左移 9 | if offset == 0: 10 | return 11 | while offset: 12 | t = nums[0] 13 | offset -= 1 14 | for i in range(n - 1): 15 | nums[i] = nums[i + 1] 16 | nums[n - 1] = t 17 | -------------------------------------------------------------------------------- /code/ch19/19.1.4. 189.rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector rotate(vector& nums, int k) { 7 | int n = nums.size(), offset = k % n; 8 | for (int i = 0; i < n; i++) nums.push_back(nums[i]); 9 | vector ans; 10 | for (int i = n - offset; i < n * 2 - offset; i++) 11 | ans.push_back(nums[i]); 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /code/ch19/19.1.4. 189.rotate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] rotate(int[] nums, int k) { 3 | int n = nums.length, offset = k % n; 4 | int[] ano_nums = new int[2 * n]; 5 | for (int i = 0; i < n; i++) 6 | ano_nums[i] = ano_nums[n + i] = nums[i]; 7 | int[] ans = new int[n]; 8 | for (int i = n - offset; i < n * 2 - offset; i++) 9 | ans[i - n + offset] = ano_nums[i]; 10 | return ans; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/ch19/19.1.4. 189.rotate.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | """ 4 | Do not return anything, modify nums in-place instead. 5 | """ 6 | n = len(nums) 7 | offset = k % n 8 | nums = nums + nums.copy() 9 | return nums[n - offset : n * 2 - offset] 10 | -------------------------------------------------------------------------------- /code/ch19/19.1.5. 189.rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void reverse(vector& arr, int start, int end) { 7 | while (start < end) { 8 | int t = arr[start]; 9 | arr[start] = arr[end]; 10 | arr[end] = t; 11 | start += 1; 12 | end -= 1; 13 | } 14 | } 15 | void rotate(vector& nums, int k) { 16 | int n = nums.size(), offset = k % n; 17 | if (offset == 0) return; 18 | reverse(nums, 0, n - offset - 1); 19 | reverse(nums, n - offset, n - 1); 20 | reverse(nums, 0, n - 1); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /code/ch19/19.1.5. 189.rotate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void reverse(int[] arr, int start, int end) { 3 | while (start < end) { 4 | int t = arr[start]; 5 | arr[start] = arr[end]; 6 | arr[end] = t; 7 | start += 1; 8 | end -= 1; 9 | } 10 | } 11 | public void rotate(int[] nums, int k) { 12 | int n = nums.length, offset = k % n; 13 | if (offset == 0) return; 14 | reverse(nums, 0, n - offset - 1); 15 | reverse(nums, n - offset, n - 1); 16 | reverse(nums, 0, n - 1); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/ch19/19.2.1. 61.rotateRight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | ListNode *rotateRight(ListNode *head, int k) { 6 | if (head == NULL || head->next == NULL) return head; 7 | ListNode *p1 = head, *res = NULL; 8 | int n = 1; 9 | while (p1 && p1->next) { 10 | p1 = p1->next; 11 | n += 1; 12 | } 13 | int cur = 1; 14 | ListNode *p2 = head; 15 | while (cur < n - k % n) { 16 | p2 = p2->next; 17 | cur += 1; 18 | } 19 | p1->next = head; 20 | res = p2->next; 21 | p2->next = NULL; 22 | return res; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /code/ch19/19.2.1. 61.rotateRight.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode rotateRight(ListNode head, int k) { 3 | if (head == null || head.next == null) return head; 4 | ListNode p1 = head, res = null; 5 | int n = 1; 6 | while (p1 != null && p1.next != null) { 7 | p1 = p1.next; 8 | n += 1; 9 | } 10 | int cur = 1; 11 | ListNode p2 = head; 12 | while (cur < n - k % n) { 13 | p2 = p2.next; 14 | cur += 1; 15 | } 16 | p1.next = head; 17 | res = p2.next; 18 | p2.next = null; 19 | return res; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code/ch19/19.2.1. 61.rotateRight.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotateRight(self, head: ListNode, k: int) -> ListNode: 3 | if head == None or head.next == None: 4 | return head 5 | p1 = head 6 | res = None 7 | n = 1 8 | 9 | while p1 and p1.next: 10 | p1 = p1.next 11 | n += 1 12 | cur = 1 13 | p2 = head 14 | while cur < n - k % n: 15 | p2 = p2.next 16 | cur += 1 17 | p1.next = head 18 | res = p2.next 19 | p2.next = None 20 | 21 | return res 22 | -------------------------------------------------------------------------------- /code/ch19/19.3.4. 72.minDistance.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minDistance(self, word1: str, word2: str) -> int: 3 | m = len(word1) 4 | n = len(word2) 5 | cur = [0] * (n + 1) 6 | pre = None 7 | 8 | for i in range(1, n + 1): 9 | cur[i] = i 10 | for i in range(1, m + 1): 11 | pre = cur[0] 12 | cur[0] = i 13 | for j in range(1, n + 1): 14 | temp = cur[j] 15 | if word1[i - 1] == word2[j - 1]: 16 | cur[j] = pre 17 | else: 18 | cur[j] = min(cur[j], cur[j - 1], pre) + 1 19 | pre = temp 20 | return cur[n] 21 | -------------------------------------------------------------------------------- /code/ch19/19.4.1. 215.findKthLargest.py: -------------------------------------------------------------------------------- 1 | from heapq import nlargest 2 | 3 | 4 | class Solution: 5 | def findKthLargest(self, nums: List[int], k: int) -> int: 6 | return nlargest(k, nums)[-1] 7 | -------------------------------------------------------------------------------- /code/ch19/19.4.2. 703.KthLargest.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | 4 | class KthLargest: 5 | def __init__(self, k: int, nums: List[int]): 6 | self.k = k 7 | self.nums = heapq.nlargest(k, nums + [float("-inf")]) 8 | heapq.heapify(self.nums) 9 | 10 | def add(self, val: int) -> int: 11 | heapq.heappushpop(self.nums, val) 12 | return self.nums[0] 13 | -------------------------------------------------------------------------------- /code/ch19/19.4.3. 230.kthSmallest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | int countNodes(TreeNode *node) { 6 | if (node == NULL) return 0; 7 | int l = countNodes(node->left); 8 | int r = countNodes(node->right); 9 | return l + r + 1; 10 | } 11 | int kthSmallest(TreeNode *root, int k) { 12 | int cnt = countNodes(root->left); 13 | if (cnt == k - 1) return root->val; 14 | else if (cnt > k - 1) 15 | return kthSmallest(root->left, k); 16 | else 17 | return kthSmallest(root->right, k - cnt - 1); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /code/ch19/19.4.3. 230.kthSmallest.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countNodes(TreeNode node) { 3 | if (node == null) return 0; 4 | int l = countNodes(node.left); 5 | int r = countNodes(node.right); 6 | return l + r + 1; 7 | } 8 | public int kthSmallest(TreeNode root, int k) { 9 | int cnt = countNodes(root.left); 10 | if (cnt == k - 1) return root.val; 11 | else if (cnt > k - 1) 12 | return kthSmallest(root.left, k); 13 | else 14 | return kthSmallest(root.right, k - cnt - 1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /code/ch19/19.4.3. 230.kthSmallest.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kthSmallest(self, root: TreeNode, k: int) -> int: 3 | # 返回该节点以及其所有子节点的个数 4 | def countNodes(node) -> int: 5 | if node == None: 6 | return 0 7 | l = countNodes(node.left) 8 | r = countNodes(node.right) 9 | return l + r + 1 10 | 11 | cnt = countNodes(root.left) 12 | if cnt == k - 1: 13 | return root.val 14 | elif cnt > k - 1: 15 | return self.kthSmallest(root.left, k) 16 | 17 | return self.kthSmallest(root.right, k - cnt - 1) 18 | -------------------------------------------------------------------------------- /code/ch19/19.4.5. 668.findKthNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int countNotGreater(int mid, int m, int n) { 8 | int cnt = 0; 9 | for (int i = 1; i <= m; i++) 10 | cnt += min(mid / i, n); 11 | return cnt; 12 | } 13 | int findKthNumber(int m, int n, int k) { 14 | int lo = 1, hi = m * n; 15 | while (lo < hi) { 16 | int mid = (lo + hi) / 2; 17 | if (countNotGreater(mid, m, n) < k) 18 | lo = mid + 1; 19 | else 20 | hi = mid; 21 | } 22 | return lo; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /code/ch19/19.4.5. 668.findKthNumber.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int countNotGreater(int mid, int m, int n) { 5 | int cnt = 0; 6 | for (int i = 1; i <= m; i++) 7 | cnt += Math.min(mid / i, n); 8 | return cnt; 9 | } 10 | public int findKthNumber(int m, int n, int k) { 11 | int lo = 1, hi = m * n; 12 | while (lo < hi) { 13 | int mid = (lo + hi) / 2; 14 | if (countNotGreater(mid, m, n) < k) 15 | lo = mid + 1; 16 | else 17 | hi = mid; 18 | } 19 | return lo; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code/ch19/19.4.5. 668.findKthNumber.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthNumber(self, m: int, n: int, k: int) -> int: 3 | lo = 1 4 | hi = m * n 5 | 6 | def countNotGreater(mid: int, m: int, n: int) -> int: 7 | cnt = 0 8 | for i in range(1, m + 1): 9 | cnt += min(mid // i, n) 10 | return cnt 11 | 12 | while lo < hi: 13 | mid = (lo + hi) // 2 14 | if countNotGreater(mid, m, n) < k: 15 | lo = mid + 1 16 | else: 17 | hi = mid 18 | return lo 19 | -------------------------------------------------------------------------------- /code/ch20/20.2.1. 1291.sequentialDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector sequentialDigits(int low, int high) { 8 | string numbers("123456789"); 9 | vector ins; 10 | int n = numbers.size(); 11 | for (int i = 1; i < n; i++) 12 | for (int j = 0; j < n - i; j++) 13 | ins.push_back(atoi(numbers.substr(j, i + 1).c_str())); 14 | vector ans; 15 | for (int i = 0; i < ins.size(); i++) 16 | if (ins[i] >= low && ins[i] <= high) 17 | ans.push_back(ins[i]); 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /code/ch20/20.2.1. 1291.sequentialDigits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public List sequentialDigits(int low, int high) { 5 | String numbers = "123456789"; 6 | List ins = new ArrayList<>(); 7 | int n = numbers.length(); 8 | for (int i = 1; i < n; i++) 9 | for (int j = 0; j < n - i; j++) 10 | ins.add(Integer.parseInt(numbers.substring( 11 | j, i + j + 1 12 | ))); 13 | List ans = new ArrayList<>(); 14 | for (int i = 0; i < ins.size(); i++) 15 | if (ins.get(i) >= low && ins.get(i) <= high) 16 | ans.add(ins.get(i)); 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/ch20/20.2.1. 1291.sequentialDigits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sequentialDigits(self, low: int, high: int) -> List[int]: 3 | numbers = "123456789" 4 | ins = [] 5 | n = len(numbers) 6 | for i in range(1, n): 7 | for j in range(n - i): 8 | ins.append(int(numbers[j : i + j + 1])) 9 | return [x for x in ins if x >= low and x <= high] 10 | -------------------------------------------------------------------------------- /code/ch20/20.2.2. 1291.sequentialDigits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sequentialDigits(self, low: int, high: int) -> List[int]: 3 | numbers = "123456789" 4 | ins = [] 5 | n = len(numbers) 6 | for i in range(1, n): 7 | for j in range(n - i): 8 | ins.append(int(numbers[j : i + j + 1])) 9 | return ins[bisect.bisect_left(ins, low) : bisect.bisect(ins, high)] 10 | -------------------------------------------------------------------------------- /code/ch20/20.3.1. 1277.countSquares.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | public class Solution { 4 | public int countSquares(int[][] matrix) { 5 | int res = 0, m = matrix.length, 6 | n = matrix[0].length; 7 | int[][] dp = new int[m + 1][n + 1]; 8 | for (int i = 0; i <= m; i++) 9 | for (int j = 0; j <= n; j++) dp[i][j] = 0; 10 | for (int i = 1; i <= m; i++) 11 | for (int j = 1; j <= n; j++) { 12 | if (matrix[i - 1][j - 1] == 1) 13 | dp[i][j] = Math.min(Math.min(dp[i - 1][j], 14 | dp[i][j - 1]), dp[i - 1][j - 1]) + 1; 15 | res += dp[i][j]; 16 | } 17 | return res; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/ch20/20.3.1. 1277.countSquares.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSquares(self, matrix: List[List[int]]) -> int: 3 | res = 0 4 | m = len(matrix) 5 | n = len(matrix[0]) 6 | dp = [[0] * (n + 1) for _ in range(m + 1)] 7 | for i in range(1, m + 1): 8 | for j in range(1, n + 1): 9 | if matrix[i - 1][j - 1] == 1: 10 | dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 11 | res += dp[i][j] 12 | return res 13 | -------------------------------------------------------------------------------- /code/ch20/20.3.2. 1297.maxFreq.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int: 3 | counter, res = {}, 0 4 | for i in range(0, len(s) - minSize + 1): 5 | for length in range(minSize, maxSize + 1): 6 | if i + length > len(s): 7 | break 8 | sub = s[i : i + length] 9 | if len(set(sub)) <= maxLetters: 10 | counter[sub] = counter.get(sub, 0) + 1 11 | res = max(res, counter[sub]) 12 | return res 13 | -------------------------------------------------------------------------------- /code/ch20/20.3.3. 1297.maxFreq.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int: 3 | counter, res = {}, 0 4 | for i in range(0, len(s) - minSize + 1): 5 | sub = s[i : i + minSize] 6 | if len(set(sub)) <= maxLetters: 7 | counter[sub] = counter.get(sub, 0) + 1 8 | res = max(res, counter[sub]) 9 | return res 10 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | leetcode-solution.cn -------------------------------------------------------------------------------- /external/dark-mode.d.ts: -------------------------------------------------------------------------------- 1 | export default class Darken { 2 | private dark: boolean 3 | constructor( 4 | options?: { 5 | container?: string | null 6 | default?: string 7 | toggle?: string | null 8 | remember?: string | null 9 | usePrefersColorScheme?: boolean 10 | class?: string 11 | variables?: object 12 | onChange?: (active: boolean) => void 13 | }, 14 | callback?: (active: boolean) => void 15 | ) 16 | toggle(): void 17 | on(): void 18 | off(): void 19 | } 20 | -------------------------------------------------------------------------------- /googlee3ba7abca18bfdec.html: -------------------------------------------------------------------------------- 1 | google-site-verification: googlee3ba7abca18bfdec.html -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel' 3 | } 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/public/favicon.ico -------------------------------------------------------------------------------- /public/googlee3ba7abca18bfdec.html: -------------------------------------------------------------------------------- 1 | google-site-verification: googlee3ba7abca18bfdec.html -------------------------------------------------------------------------------- /scripts/deploy.sh: -------------------------------------------------------------------------------- 1 | yarn 2 | yarn build 3 | cp ./docs/CNAME ./dist/CNAME 4 | cp ./dist/index.html ./dist/404.html 5 | gco gh-pages 6 | cp -R ./dist/* . 7 | git add . 8 | git commit -m '自动发布' 9 | git push -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 16 | -------------------------------------------------------------------------------- /src/apis/github.js: -------------------------------------------------------------------------------- 1 | import request from './request' 2 | 3 | export function getGithubContent(url, config = {}) { 4 | return request({ 5 | url: '/api/v1/github/content', 6 | params: { 7 | url, 8 | ...config.params 9 | } 10 | }) 11 | } 12 | 13 | export function getCommentApp() { 14 | return request({ 15 | url: '/api/v1/github/comment-app' 16 | }) 17 | } 18 | -------------------------------------------------------------------------------- /src/apis/meta.js: -------------------------------------------------------------------------------- 1 | import request from './request' 2 | 3 | export function getMeta() { 4 | return request({ url: '/api/v1/meta' }) 5 | } 6 | -------------------------------------------------------------------------------- /src/apis/user.js: -------------------------------------------------------------------------------- 1 | import request from './request' 2 | export function logout() { 3 | window.localStorage.removeItem('token') 4 | return request({ 5 | url: '/api/v1/user/logout' 6 | }) 7 | } 8 | const TOKEN_PEROID = 30 * 24 * 60 * 60 * 1000 9 | export function getUserInfo(code) { 10 | return request({ 11 | url: `/api/v1/user?code=${code || ''}` 12 | }).then(r => { 13 | window.localStorage.setItem( 14 | 'token', 15 | JSON.stringify({ 16 | expireAt: new Date().getTime() + TOKEN_PEROID, 17 | value: r.token 18 | }) 19 | ) 20 | return r 21 | }) 22 | } 23 | -------------------------------------------------------------------------------- /src/assets/card.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/assets/github.png -------------------------------------------------------------------------------- /src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/assets/logo.png -------------------------------------------------------------------------------- /src/assets/official_account.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/assets/official_account.png -------------------------------------------------------------------------------- /src/assets/official_account_QR_code.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/assets/official_account_QR_code.jpg -------------------------------------------------------------------------------- /src/assets/wechat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/assets/wechat.png -------------------------------------------------------------------------------- /src/assets/wechat_QR_code.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/assets/wechat_QR_code.jpg -------------------------------------------------------------------------------- /src/assets/zhihu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/assets/zhihu.png -------------------------------------------------------------------------------- /src/auth.ts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leetcode-pp/leetcode-pp/f38e342645fbd33fb34d8996fe3b074818f9f8a3/src/auth.ts -------------------------------------------------------------------------------- /src/common/less/layout.less: -------------------------------------------------------------------------------- 1 | @media screen and(min-width: 1080px) { 2 | .w-1280 { 3 | width: 1280px; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /src/components/LayoutContent/LayoutContent.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/components/LayoutContent/index.ts: -------------------------------------------------------------------------------- 1 | import LayoutContent from './LayoutContent.vue' 2 | 3 | export default LayoutContent 4 | -------------------------------------------------------------------------------- /src/components/LayoutFooter/index.ts: -------------------------------------------------------------------------------- 1 | import LayoutFooter from './LayoutFooter.vue' 2 | 3 | export default LayoutFooter 4 | -------------------------------------------------------------------------------- /src/components/LayoutHeader/index.ts: -------------------------------------------------------------------------------- 1 | import LayoutHeader from './LayoutHeader.vue' 2 | 3 | export default LayoutHeader 4 | -------------------------------------------------------------------------------- /src/components/lazy_use.ts: -------------------------------------------------------------------------------- 1 | import './components_use' 2 | -------------------------------------------------------------------------------- /src/enhancement/darken-mode.less: -------------------------------------------------------------------------------- 1 | .darken { 2 | // background-color: #d1d5da; 3 | background-color: rgb(16, 44, 56); 4 | #app { 5 | color: #999; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/enhancement/index.ts: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | // import darkMode from './darken-mode' 3 | 4 | import nprogress from './nprogress' 5 | 6 | Vue.use(nprogress) 7 | // Vue.use(darkMode) 8 | -------------------------------------------------------------------------------- /src/enhancement/nprogress.ts: -------------------------------------------------------------------------------- 1 | import router from '@/router' 2 | import nProgress from 'nprogress' 3 | import 'nprogress/nprogress.css' 4 | 5 | const installNProgress = { 6 | install: function() { 7 | router.beforeEach((to, from, next) => { 8 | nProgress.start() 9 | next() 10 | }) 11 | router.afterEach(() => nProgress.done()) 12 | } 13 | } 14 | export default installNProgress 15 | -------------------------------------------------------------------------------- /src/external.d.ts: -------------------------------------------------------------------------------- 1 | declare module '*.less' 2 | declare module '*.js' 3 | -------------------------------------------------------------------------------- /src/layouts/BasicLayout.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/layouts/index.ts: -------------------------------------------------------------------------------- 1 | import Layout from './Layout.vue' 2 | import '@/common/less/layout.less' 3 | 4 | export default Layout 5 | -------------------------------------------------------------------------------- /src/override.less: -------------------------------------------------------------------------------- 1 | .ant-layout { 2 | background-color: unset; 3 | } 4 | .ant-layout-header { 5 | background-color: unset; 6 | } 7 | -------------------------------------------------------------------------------- /src/router/index.ts: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import VueRouter, { RouterOptions } from 'vue-router' 3 | import { asyncRouterMap } from './router.config' 4 | 5 | Vue.use(VueRouter) 6 | 7 | const router = new VueRouter({ 8 | mode: 'history', 9 | routes: asyncRouterMap 10 | } as RouterOptions) 11 | export default router 12 | -------------------------------------------------------------------------------- /src/shims-tsx.d.ts: -------------------------------------------------------------------------------- 1 | import Vue, { VNode } from 'vue' 2 | 3 | declare global { 4 | namespace JSX { 5 | // tslint:disable no-empty-interface 6 | interface Element extends VNode {} 7 | // tslint:disable no-empty-interface 8 | interface ElementClass extends Vue {} 9 | interface IntrinsicElements { 10 | [elem: string]: any 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/shims-vue.d.ts: -------------------------------------------------------------------------------- 1 | declare module '*.vue' { 2 | import Vue from 'vue' 3 | export default Vue 4 | } 5 | -------------------------------------------------------------------------------- /src/store/index.ts: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Vuex from 'vuex' 3 | 4 | Vue.use(Vuex) 5 | 6 | export default new Vuex.Store({ 7 | state: {}, 8 | mutations: {}, 9 | actions: {}, 10 | modules: {} 11 | }) 12 | -------------------------------------------------------------------------------- /src/themes/index.js: -------------------------------------------------------------------------------- 1 | import blue from './blue' 2 | import purple from './purple' 3 | import wechat from './wechat' 4 | import vue from './vue' 5 | 6 | export default { 7 | blue, 8 | vue, 9 | wechat, 10 | purple 11 | } 12 | -------------------------------------------------------------------------------- /src/themes/menu.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/themes/outline-base.less: -------------------------------------------------------------------------------- 1 | .outline { 2 | position: fixed; 3 | background-color: #fff; 4 | top: 100px; 5 | right: 20px; 6 | width: 285px; 7 | display: flex; 8 | flex-direction: column; 9 | padding: 8px 0 16px 0; 10 | max-height: 500px; 11 | overflow: scroll; 12 | text-align: justify; 13 | li { 14 | list-style: none; 15 | width: auto; 16 | text-align: left; 17 | a { 18 | display: list-item; 19 | padding: 0 8px 0 11px; 20 | font-weight: 700; 21 | width: 100%; 22 | color: rgba(51, 51, 51, 0.6); 23 | font-size: 14px; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/utils/browser.js: -------------------------------------------------------------------------------- 1 | export function copyToClipboard(str) { 2 | try { 3 | const el = document.createElement('textarea') 4 | el.value = str 5 | document.body.appendChild(el) 6 | el.select() 7 | document.execCommand('copy') 8 | document.body.removeChild(el) 9 | return true 10 | } catch (err) { 11 | return false 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/utils/markdown-it-li.js: -------------------------------------------------------------------------------- 1 | function makeRule(md) { 2 | return function replaceListItem() { 3 | // eslint-disable-next-line 4 | md.renderer.rules.list_item_open = function replaceOpen() { 5 | return '
  • ' 6 | } 7 | // eslint-disable-next-line 8 | md.renderer.rules.list_item_close = function replaceClose() { 9 | return '

  • ' 10 | } 11 | } 12 | } 13 | 14 | export default md => { 15 | md.core.ruler.push('replace-li', makeRule(md)) 16 | } 17 | -------------------------------------------------------------------------------- /src/utils/markdown-it-removepre.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | // 用来移除微信自定义代码前方的 pre code 3 | export default md => { 4 | const oldFence = md.renderer.rules.fence 5 | md.renderer.rules.fence = (tokens, idx, options, env, slf) => { 6 | const old = oldFence(tokens, idx, options, env, slf) 7 | const preReg = /
    /
     8 |     // 微信专属
     9 |     if (preReg.exec(old)) {
    10 |       const pre = preReg.exec(old)[0]
    11 |       const post = `
    ` 12 | return old.replace(pre, '').replace(post, '') 13 | } 14 | // 自定义 15 | else { 16 | return old 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/utils/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "module", 3 | "devDependencies": { 4 | "node-fetch": "^2.6.1" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/utils/storage.test.js: -------------------------------------------------------------------------------- 1 | import { useDatabase } from './storage.js' 2 | import fetch from 'node-fetch' 3 | 4 | const users = useDatabase('91algo4-users', { 5 | fetch 6 | }) 7 | 8 | // users 9 | // .create({ 10 | // name: 'lucky', 11 | // blog: 'https://lucifer.ren/blog', 12 | // createTime: new Date().getTime() 13 | // }) 14 | // .then(console.log) 15 | 16 | users.read().then(console.log) 17 | -------------------------------------------------------------------------------- /src/utils/style.js: -------------------------------------------------------------------------------- 1 | export const replaceStyle = (id, css) => { 2 | const style = document.getElementById(id) 3 | try { 4 | style.innerHTML = css 5 | } catch (e) { 6 | console.log(e) 7 | style.styleSheet.cssText = css 8 | } 9 | const head = document.getElementsByTagName('head')[0] 10 | head.appendChild(style) 11 | } 12 | -------------------------------------------------------------------------------- /src/views/91/91.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/views/91/db/baseContest.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": 288, 3 | "time": 1649557800000, 4 | "passed": true 5 | } -------------------------------------------------------------------------------- /src/views/Book.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 15 | -------------------------------------------------------------------------------- /src/views/ProblemDetail.md: -------------------------------------------------------------------------------- 1 | 3rd party libiries used: 2 | - hightlight.js 3 | - markdown-it 4 | - axios 5 | 6 | Executing Steps: 7 | 1. Once component mounted it will call `getProlem` to get the question detail based on the `id`(from `this.$route.params`) 8 | 2. If it successfully loaded the problem, it will make another call `getSolution` to get the soltution for this problem 9 | 3. Once solution text is retrieved, it'll use `markdown-it` to convert markdown to HTML. Then it will render the HTML via Vue directive `v-html` 10 | 4. After it is rendered, it'll envoke `highlight.js` to find style the content from within `` tags 11 | -------------------------------------------------------------------------------- /src/views/Solution.md: -------------------------------------------------------------------------------- 1 | 执行步骤: 2 | 1. 渲染仓库里各种专题的标题(静态数据) 3 | 2. 当用户点击某个专题的标题时,跳转至题解详情界面,同时通过路由传递该专题的github地址 4 | -------------------------------------------------------------------------------- /src/views/SolutionDetail.md: -------------------------------------------------------------------------------- 1 | 第三方依赖: 2 | - hightlight.js 3 | - markdown-it 4 | - axios 5 | - js-base64 6 | 7 | 执行步骤: 8 | 1. 组件安装好后,将调用`getSolution`以基于`专题的github地址`获取专题内容(`来自this.$route.query.url`) 9 | 2. 如果成功加载了该专题的内容,它将使用markdown-it将markdown转换为HTML。 然后它将通过Vue指令`v-html`呈现HTML。 10 | 3. 渲染后,它会调用`highlight.js`以在标签内查找样式的内容 -------------------------------------------------------------------------------- /src/views/github.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "path": "../../.." 5 | }, 6 | { 7 | "path": "../../../../work/code/91alg-1" 8 | }, 9 | { 10 | "path": "../../../../project" 11 | }, 12 | { 13 | "path": "../../../../project/qier-player/src" 14 | } 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /tests/unit/example.spec.ts: -------------------------------------------------------------------------------- 1 | import { shallowMount } from '@vue/test-utils' 2 | import HelloWorld from '@/components/HelloWorld.vue' 3 | 4 | describe('HelloWorld.vue', () => { 5 | it('renders props.msg when passed', () => { 6 | const msg = 'new message' 7 | const wrapper = shallowMount(HelloWorld, { 8 | propsData: { msg } 9 | }) 10 | expect(wrapper.text()).toMatch(msg) 11 | }) 12 | }) 13 | --------------------------------------------------------------------------------