├── .gitignore ├── tests ├── IntersectionOfTwoArraysIITest.cpp ├── main.cpp ├── NimGameTest.cpp ├── BulbSwitcherTest.cpp ├── NQueensIITest.cpp ├── EliminationGameTest.cpp ├── FindTheCelebrityTest.cpp ├── Numberof1BitsTest.cpp ├── UglyNumberIITest.cpp ├── HIndexTest.cpp ├── NumberOfDigitOneTest.cpp ├── HIndexIITest.cpp ├── AddDigitsTest.cpp ├── HappyNumberTest.cpp ├── RectangleAreaTest.cpp ├── BasicCalculatorTest.cpp ├── GrayCodeTest.cpp ├── IntegerToEnglishWordsTest.cpp ├── UniqueBinarySearchTreesTest.cpp ├── BurstBalloonsTest.cpp ├── FactorialTrailingZeroesTest.cpp ├── IntegerBreakTest.cpp ├── MissingNumberTest.cpp ├── PalindromePartitioningIITest.cpp ├── UniquePathsTest.cpp ├── PerfectSquaresTest.cpp ├── BitwiseANDOfNumbersRangeTest.cpp ├── UglyNumberTest.cpp ├── FindPeakElementTest.cpp ├── GuessNumberHigherOrLowerIITest.cpp ├── FlattenNestedListIteratorTest.cpp ├── LargestNumberTest.cpp ├── MajorityElementTest.cpp ├── PascalTriangleIITest.cpp ├── SingleNumberIITest.cpp ├── ValidAnagramTest.cpp ├── BullsAndCowsTest.cpp ├── ValidPerfectSquareTest.cpp ├── CMakeLists.txt ├── ImplementTrieTest.cpp ├── IsSubsequenceTest.cpp ├── EditDistanceTest.cpp ├── WaterAndJugProblemTest.cpp ├── FindTheDuplicateNumberTest.cpp ├── PaintHouseTest.cpp ├── ReverseBitsTest.cpp ├── AddBinaryTest.cpp ├── ClimbingStairsTest.cpp ├── FindTheDifferenceTest.cpp ├── ValidPalindromeTest.cpp ├── CombinationSumIVTest.cpp ├── CountingBitsTest.cpp ├── PaintHouseIITest.cpp ├── TwoSumIITest.cpp ├── DistinctSubsequencesTest.cpp ├── TwoSumIIITest.cpp ├── SplitArrayLargestSumTest.cpp ├── MoveZeroesTest.cpp ├── ShortestPalindromeTest.cpp ├── JumpGameTest.cpp ├── LongestConsecutiveSequenceTest.cpp ├── HouseRobberTest.cpp ├── InterleavingStringTest.cpp ├── RemoveDuplicateLettersTest.cpp ├── FindMinimumInRotatedSortedArrayTest.cpp ├── RotateArrayTest.cpp ├── HouseRobberIITest.cpp ├── FindMinimumInRotatedSortedArrayIITest.cpp ├── FirstUniqueCharacterInAStringTest.cpp ├── IsomorphicStringsTest.cpp ├── KthLargestElementInAnArrayTest.cpp ├── ExcelSheetColumnTitleTest.cpp ├── SlidingWindowMaximumTest.cpp ├── BestTimeToBuyAndSellStockTest.cpp ├── ProductOfArrayExceptSelfTest.cpp ├── ContainsDuplicateIITest.cpp ├── TopKFrequentElementsTest.cpp ├── PalindromePermutationTest.cpp ├── ZigZagConversionTest.cpp ├── SummaryRangesTest.cpp ├── DungeonGameTest.cpp ├── TriangleTest.cpp ├── MaximumGapTest.cpp ├── ArithmeticSlicesTest.cpp ├── FirstBadVersionTest.cpp ├── PermutationSequenceTest.cpp ├── ContainsDuplicateTest.cpp ├── DesignHitCounterTest.cpp ├── IntersectionOfTwoArraysTest.cpp ├── SearchA2DMatrixTest.cpp ├── CountAndSayTest.cpp ├── BasicCalculatorIITest.cpp ├── SplitArrayWithEqualSumTest.cpp ├── SqrtXTest.cpp ├── CourseScheduleTest.cpp ├── PeekingIteratorTest.cpp ├── SpiralMatrixIITest.cpp ├── CoinChangeTest.cpp ├── MergeSortedArrayTest.cpp ├── PaintFenceTest.cpp ├── RangeSumQueryImmutableTest.cpp ├── BestTimeToBuyAndSellStockIITest.cpp ├── ValidNumberTest.cpp ├── BestTimeToBuyAndSellStockIIITest.cpp ├── CountPrimesTest.cpp ├── SuperUglyNumberTest.cpp ├── AddAndSearchWordTest.cpp ├── MovingAverageFromDataStreamTest.cpp ├── SpiralMatrixTest.cpp ├── ValidParenthesesTest.cpp ├── WordPatternTest.cpp ├── MaxPointsOnALineTest.cpp ├── MaximumSizeSubarraySumEqualsKTest.cpp ├── PascalTriangleTest.cpp ├── UniqueWordAbbreviationTest.cpp ├── 3SumClosestTest.cpp ├── InsertDeleteGetRandomTest.cpp ├── FrogJumpTest.cpp ├── SimplifyPathTest.cpp ├── WordLadderTest.cpp ├── BestTimeToBuyAndSellStockWithCooldownTest.cpp ├── GuessNumberHigherOrLowerTest.cpp ├── ImplementQueueUsingStacksTest.cpp ├── PowerOfTwoTest.cpp ├── LongestIncreasingSubsequenceTest.cpp ├── MajorityElementIITest.cpp ├── ReverseWordsInAStringTest.cpp └── SortColorsTest.cpp ├── src ├── NimGame.cpp ├── AddDigits.cpp ├── BulbSwitcher.cpp ├── DeleteNodeInALinkedList.cpp ├── Numberof1Bits.cpp ├── PowerOfTwo.cpp ├── FactorialTrailingZeroes.cpp ├── CMakeLists.txt ├── ExcelSheetColumnNumber.cpp ├── LoggerRateLimiter.cpp ├── RemoveElement.cpp ├── JumpGame.cpp ├── ExcelSheetColumnTitle.cpp ├── PascalTriangleII.cpp ├── RangeSumQueryImmutable.cpp ├── CountingBits.cpp ├── ReverseInteger.cpp ├── SingleNumberII.cpp ├── RemoveDuplicatesFromSortedArray.cpp ├── CombinationSumIV.cpp ├── LinkedListCycle.cpp ├── LengthOfLastWord.cpp ├── PalindromeNumber.cpp ├── SameTree.cpp ├── MoveZeroes.cpp ├── SortColors.cpp ├── PathSum.cpp ├── PerfectSquares.cpp ├── ContainsDuplicate.cpp ├── TwoSumIII.cpp ├── BitwiseANDOfNumbersRange.cpp ├── FindPeakElement.cpp ├── RotateImage.cpp ├── UniqueBinarySearchTrees.cpp ├── IntegerBreak.cpp ├── AddBinary.cpp ├── PascalTriangle.cpp ├── ClimbingStairs.cpp ├── MovingAverageFromDataStream.cpp ├── UniquePaths.cpp ├── PalindromePermutation.cpp ├── PlusOne.cpp ├── RemoveDuplicatesFromSortedArrayII.cpp ├── WaterAndJugProblem.cpp ├── MajorityElement.cpp ├── PaintFence.cpp ├── RemoveLinkedListElements.cpp ├── HIndexII.cpp ├── SwapNodesInPairs.cpp ├── NumberOfDigitOne.cpp ├── SingleNumberIII.cpp ├── TwoSumII.cpp ├── BestTimeToBuyAndSellStockII.cpp ├── FirstUniqueCharacterInAString.cpp ├── LongestCommonPrefix.cpp ├── ProductOfArrayExceptSelf.cpp ├── InorderSuccessorInBST.cpp ├── IsSubsequence.cpp ├── ClosestBinarySearchTreeValue.cpp ├── FindMinimumInRotatedSortedArray.cpp ├── FindTheDifference.cpp ├── SqrtX.cpp ├── UglyNumber.cpp ├── HouseRobber.cpp ├── FindMedianFromDataStream.cpp ├── MinStack.cpp ├── ValidAnagram.cpp ├── MissingNumber.cpp └── IsomorphicStrings.cpp ├── include ├── NimGame.hpp ├── SqrtX.hpp ├── Powxn.hpp ├── PaintFence.hpp ├── PowerOfTwo.hpp ├── AddDigits.hpp ├── BulbSwitcher.hpp ├── CountPrimes.hpp ├── UglyNumber.hpp ├── EliminationGame.hpp ├── IntegerBreak.hpp ├── ReverseInteger.hpp ├── UniquePaths.hpp ├── ClimbingStairs.hpp ├── NumberOfDigitOne.hpp ├── UglyNumberII.hpp ├── PerfectSquares.hpp ├── PalindromeNumber.hpp ├── PathSum.hpp ├── ValidPerfectSquare.hpp ├── ReorderList.hpp ├── SameTree.hpp ├── DivideTwoIntegers.hpp ├── FrogJump.hpp ├── JumpGame.hpp ├── DecodeWays.hpp ├── LinkedListCycle.hpp ├── PlusOne.hpp ├── TwoSum.hpp ├── UniqueBinarySearchTrees.hpp ├── 3Sum.hpp ├── AddBinary.hpp ├── CountAndSay.hpp ├── FactorialTrailingZeroes.hpp ├── HIndexII.hpp ├── HouseRobber.hpp ├── JumpGameII.hpp ├── PaintHouse.hpp ├── PartitionList.hpp ├── SortColors.hpp ├── Triangle.hpp ├── ReverseBits.hpp ├── TwoSumII.hpp ├── ValidAnagram.hpp ├── 4Sum.hpp ├── AddTwoNumbers.hpp ├── CountingBits.hpp ├── GuessNumberHigherOrLowerII.hpp ├── MoveZeroes.hpp ├── RomanToInteger.hpp ├── RotateArray.hpp ├── WordPattern.hpp ├── BurstBalloons.hpp ├── CoinChange.hpp ├── InsertionSortList.hpp ├── IntegerToRoman.hpp ├── IsSubsequence.hpp ├── LinkedListCycleII.hpp ├── OddEvenLinkedList.hpp ├── PaintHouseII.hpp ├── ReverseLinkedList.hpp ├── RotateImage.hpp ├── SwapNodesInPairs.hpp ├── BitwiseANDOfNumbersRange.hpp ├── BullsAndCows.hpp ├── EditDistance.hpp ├── FindTheCelebrity.hpp ├── Numberof1Bits.hpp ├── PalindromeLinkedList.hpp ├── SingleNumberII.hpp ├── StringToInteger.hpp ├── ValidPalindrome.hpp ├── ValidParentheses.hpp ├── ValidSudoku.hpp ├── DungeonGame.hpp ├── MissingNumber.hpp ├── ScrambleString.hpp ├── MajorityElement.hpp ├── MaximalSquare.hpp ├── MinimumPathSum.hpp ├── NextPermutation.hpp ├── PascalTriangle.hpp ├── RemoveElement.hpp ├── SortList.hpp ├── SpiralMatrix.hpp ├── SpiralMatrixII.hpp ├── WildcardMatching.hpp ├── ArithmeticSlices.hpp ├── FindPeakElement.hpp ├── LengthOfLastWord.hpp ├── MultiplyStrings.hpp ├── PascalTriangleII.hpp ├── SingleNumberIII.hpp ├── TrappingRainWater.hpp ├── 3SumClosest.hpp ├── CountCompleteTreeNodes.hpp ├── FindTheDifference.hpp ├── IsomorphicStrings.hpp ├── SetMatrixZeroes.hpp ├── WaterAndJugProblem.hpp ├── ZigZagConversion.hpp ├── CombinationSumIV.hpp ├── ContainsDuplicate.hpp ├── CountNumbersWithUniqueDigits.hpp ├── DeleteNodeInALinkedList.hpp ├── MergeTwoSortedLists.hpp ├── ReverseLinkedListII.hpp ├── ReverseNodesInKGroup.hpp ├── ShortestPalindrome.hpp ├── SuperUglyNumber.hpp ├── GrayCode.hpp ├── InorderSuccessorInBST.hpp ├── MajorityElementII.hpp ├── MinimumDepthOfBinaryTree.hpp ├── PalindromePermutation.hpp ├── UniquePathsII.hpp ├── WordBreak.hpp ├── DistinctSubsequences.hpp ├── ExcelSheetColumnTitle.hpp ├── HappyNumber.hpp ├── SearchA2DMatrix.hpp ├── SearchForARange.hpp ├── ExcelSheetColumnNumber.hpp ├── InterleavingString.hpp ├── PalindromePartitioningII.hpp ├── ReverseWordsInAString.hpp ├── WiggleSortII.hpp ├── ContainerWithMostWater.hpp ├── FindTheDuplicateNumber.hpp ├── FirstMissingPositive.hpp ├── MaximumGap.hpp ├── MaximumProductSubarray.hpp ├── MergeSortedArray.hpp ├── MinimumWindowSubstring.hpp ├── RemoveDuplicateLetters.hpp ├── RemoveLinkedListElements.hpp ├── ContainsDuplicateII.hpp ├── PartitionEqualSubsetSum.hpp ├── SearchInsertPosition.hpp ├── SlidingWindowMaximum.hpp ├── TopKFrequentElements.hpp ├── BalancedBinaryTree.hpp ├── LongestValidParentheses.hpp ├── SearchA2DMatrixII.hpp ├── ClosestBinarySearchTreeValue.hpp ├── BasicCalculator.hpp ├── HouseRobberII.hpp ├── LargestDivisibleSubset.hpp ├── LongestCommonPrefix.hpp ├── RemoveNthNodeFromEndOfList.hpp ├── BestTimeToBuyAndSellStock.hpp ├── ContainsDuplicateIII.hpp ├── LongestConsecutiveSequence.hpp ├── MergeKSortedLists.hpp ├── ProductOfArrayExceptSelf.hpp ├── FirstUniqueCharacterInAString.hpp ├── RemoveDuplicatesFromSortedList.hpp ├── RepeatedDNASequences.hpp ├── BasicCalculatorII.hpp ├── SearchInRotatedSortedArray.hpp ├── SumRootToLeafNumbers.hpp ├── BestTimeToBuyAndSellStockII.hpp ├── CountUnivalueSubtrees.hpp ├── IntersectionOfTwoLinkedLists.hpp ├── KthLargestElementInAnArray.hpp ├── LargestRectangleInHistogram.hpp ├── MinimumHeightTrees.hpp ├── BestTimeToBuyAndSellStockIII.hpp ├── BestTimeToBuyAndSellStockIV.hpp ├── FractionToRecurringDecimal.hpp ├── HIndex.hpp ├── SearchinRotatedSortedArrayII.hpp ├── TargetSum.hpp ├── FindMinimumInRotatedSortedArray.hpp ├── IntersectionOfTwoArraysII.hpp ├── MaximumProductOfWordLengths.hpp ├── MaximumSizeSubarraySumEqualsK.hpp ├── SimplifyPath.hpp ├── ValidNumber.hpp ├── GroupAnagrams.hpp ├── LargestNumber.hpp ├── RemoveDuplicatesFromSortedArray.hpp ├── Subsets.hpp ├── SymmetricTree.hpp ├── MaximalRectangle.hpp ├── NQueensII.hpp ├── BinaryTreeMaximumPathSum.hpp ├── DifferentWaysToAddParentheses.hpp ├── FindMinimumInRotatedSortedArrayII.hpp ├── SumOfLeftLeaves.hpp ├── Combinations.hpp ├── MaxSumOfRectangleNoLargerThanK.hpp ├── MinStack.hpp ├── NumberOfIslands.hpp ├── RangeSumQueryImmutable.hpp ├── RemoveDuplicatesFromSortedArrayII.hpp ├── ReverseWordsInAStringII.hpp ├── FirstBadVersion.hpp ├── RegularExpressionMatching.hpp ├── RotateList.hpp ├── SummaryRanges.hpp ├── SurroundedRegions.hpp ├── KthSmallestElementInASortedMatrix.hpp ├── SplitArrayLargestSum.hpp ├── InvertBinaryTree.hpp ├── NQueens.hpp ├── BinaryTreeLevelOrderTraversal.hpp ├── BestTimeToBuyAndSellStockWithCooldown.hpp ├── GameOfLife.hpp ├── GuessNumberHigherOrLower.hpp ├── MissingRanges.hpp ├── PalindromePairs.hpp ├── WallsAndGates.hpp ├── BinaryTreeLevelOrderTraversalII.hpp ├── ListNode.hpp ├── LowestCommonAncestorOfABinarySearchTree.hpp ├── MaximumDepthOfBinaryTree.hpp ├── SubsetsII.hpp ├── ClosestBinarySearchTreeValueII.hpp ├── Flatten2DVector.hpp ├── LongestSubstringWithoutRepeatingCharacters.hpp ├── Permutations.hpp ├── RemoveDuplicatesFromSortedListII.hpp ├── SplitArrayWithEqualSum.hpp ├── SudokuSolver.hpp ├── GenerateParentheses.hpp ├── RussianDollEnvelopes.hpp ├── BinaryTreePaths.hpp ├── CombinationSumIII.hpp ├── WiggleSubsequence.hpp ├── WordSearch.hpp ├── BinaryTreeZigzagLevelOrderTraversal.hpp ├── ImplementStrStr.hpp ├── PathSumII.hpp ├── SubstringWithConcatenationOfAllWords.hpp ├── ZigzagIterator.hpp ├── FindLeavesOfBinaryTree.hpp ├── PermutationsII.hpp ├── BinaryTreeLongestConsecutiveSequence.hpp ├── BinaryTreeUpsideDown.hpp ├── WordLadder.hpp ├── CombinationSum.hpp ├── CourseSchedule.hpp ├── MedianOfTwoSortedArrays.hpp ├── UniqueWordAbbreviation.hpp ├── HouseRobberIII.hpp ├── PermutationSequence.hpp ├── RangeSumQuery2DImmutable.hpp ├── CombinationSumII.hpp ├── ConvertSortedListToBinarySearchTree.hpp ├── PalindromePartitioning.hpp ├── TwoSumIII.hpp ├── MaximumSubarray.hpp ├── ValidateBinarySearchTree.hpp ├── MovingAverageFromDataStream.hpp ├── RestoreIPAddresses.hpp ├── ExpressionAddOperators.hpp ├── CountOfSmallerNumbersAfterSelf.hpp ├── FindMedianFromDataStream.hpp └── MaxPointsOnALine.hpp └── CMakeLists.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | build/ 3 | -------------------------------------------------------------------------------- /tests/IntersectionOfTwoArraysIITest.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/main.cpp: -------------------------------------------------------------------------------- 1 | #define CATCH_CONFIG_MAIN 2 | #include "catch.hpp" 3 | -------------------------------------------------------------------------------- /src/NimGame.cpp: -------------------------------------------------------------------------------- 1 | #include "NimGame.hpp" 2 | 3 | bool NimGame::canWinNim(int n) { 4 | return n % 4; 5 | } -------------------------------------------------------------------------------- /src/AddDigits.cpp: -------------------------------------------------------------------------------- 1 | #include "AddDigits.hpp" 2 | 3 | int AddDigits::addDigits(int num) { 4 | return 1 + (num - 1) % 9; 5 | } -------------------------------------------------------------------------------- /include/NimGame.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NIM_GAME_HPP_ 2 | #define NIM_GAME_HPP_ 3 | 4 | class NimGame { 5 | public: 6 | bool canWinNim(int n); 7 | }; 8 | 9 | #endif // NIM_GAME_HPP_ -------------------------------------------------------------------------------- /include/SqrtX.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SQRT_X_HPP_ 2 | #define SQRT_X_HPP_ 3 | 4 | class SqrtX { 5 | public: 6 | int mySqrt(int x); 7 | }; 8 | 9 | #endif // SQRT_X_HPP_ 10 | -------------------------------------------------------------------------------- /include/Powxn.hpp: -------------------------------------------------------------------------------- 1 | #ifndef POWXN_HPP_ 2 | #define POWXN_HPP_ 3 | 4 | class Powxn { 5 | public: 6 | double myPow(double x, int n); 7 | }; 8 | 9 | #endif // POWXN_HPP_ 10 | -------------------------------------------------------------------------------- /include/PaintFence.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PAINT_FENCE_HPP_ 2 | #define PAINT_FENCE_HPP_ 3 | 4 | class PaintFence { 5 | public: 6 | int numWays(int n, int k); 7 | }; 8 | 9 | #endif // PAINT_FENCE_HPP_ -------------------------------------------------------------------------------- /include/PowerOfTwo.hpp: -------------------------------------------------------------------------------- 1 | #ifndef POWER_OF_TWO_HPP_ 2 | #define POWER_OF_TWO_HPP_ 3 | 4 | class PowerOfTwo { 5 | public: 6 | bool isPowerOfTwo(int n); 7 | }; 8 | 9 | #endif // POWER_OF_TWO_HPP_ -------------------------------------------------------------------------------- /src/BulbSwitcher.cpp: -------------------------------------------------------------------------------- 1 | #include "BulbSwitcher.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int BulbSwitcher::bulbSwitch(int n) { 8 | return sqrt(n); 9 | } 10 | -------------------------------------------------------------------------------- /include/AddDigits.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ADD_DIGITS_HPP_ 2 | #define ADD_DIGITS_HPP_ 3 | 4 | class AddDigits { 5 | public: 6 | int addDigits(int num); 7 | }; 8 | 9 | #endif // ADD_DIGITS_HPP_ 10 | -------------------------------------------------------------------------------- /include/BulbSwitcher.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BULB_SWITCHER_HPP_ 2 | #define BULB_SWITCHER_HPP_ 3 | 4 | class BulbSwitcher { 5 | public: 6 | int bulbSwitch(int n); 7 | }; 8 | 9 | #endif // BULB_SWITCHER_HPP_ -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) 2 | 3 | project(LeetCode CXX) 4 | 5 | set(CMAKE_EXPORT_COMPILE_COMMANDS "ON") 6 | 7 | add_subdirectory(src) 8 | add_subdirectory(tests) 9 | -------------------------------------------------------------------------------- /include/CountPrimes.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNT_PRIMES_HPP_ 2 | #define COUNT_PRIMES_HPP_ 3 | 4 | class CountPrimes { 5 | public: 6 | int countPrimes(int n); 7 | }; 8 | 9 | #endif // COUNT_PRIMES_HPP_ 10 | -------------------------------------------------------------------------------- /include/UglyNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UGLY_NUMBER_HPP_ 2 | #define UGLY_NUMBER_HPP_ 3 | 4 | class UglyNumber { 5 | public: 6 | 7 | bool isUgly(int num); 8 | }; 9 | 10 | #endif // UGLY_NUMBER_HPP_ 11 | -------------------------------------------------------------------------------- /include/EliminationGame.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ELIMINATION_GAME_HPP_ 2 | #define ELIMINATION_GAME_HPP_ 3 | 4 | class EliminationGame { 5 | public: 6 | int lastRemaining(int n); 7 | }; 8 | 9 | #endif // ELIMINATION_GAME_HPP_ -------------------------------------------------------------------------------- /include/IntegerBreak.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INTEGER_BREAK_HPP_ 2 | #define INTEGER_BREAK_HPP_ 3 | 4 | class IntegerBreak { 5 | public: 6 | int integerBreak(int n); 7 | }; 8 | 9 | #endif // INTEGER_BREAK_HPP_ 10 | -------------------------------------------------------------------------------- /include/ReverseInteger.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_INTEGER_HPP 2 | #define REVERSE_INTEGER_HPP 3 | 4 | class ReverseInteger { 5 | public: 6 | int reverse(int x); 7 | }; 8 | 9 | #endif // REVERSE_INTEGER_HPP 10 | -------------------------------------------------------------------------------- /include/UniquePaths.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UNIQUE_PATHS_HPP_ 2 | #define UNIQUE_PATHS_HPP_ 3 | 4 | class UniquePaths { 5 | public: 6 | int uniquePaths(int m, int n); 7 | }; 8 | 9 | #endif // UNIQUE_PATHS_HPP_ 10 | -------------------------------------------------------------------------------- /src/DeleteNodeInALinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include "DeleteNodeInALinkedList.hpp" 2 | 3 | void DeleteNodeInALinkedList::deleteNode(ListNode *node) { 4 | node->val = node->next->val; 5 | node->next = node->next->next; 6 | } -------------------------------------------------------------------------------- /include/ClimbingStairs.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLIMBING_STAIRS_HPP_ 2 | #define CLIMBING_STAIRS_HPP_ 3 | 4 | class ClimbingStairs { 5 | public: 6 | int climbStairs(int n); 7 | }; 8 | 9 | #endif // CLIMBING_STAIRS_HPP_ 10 | -------------------------------------------------------------------------------- /include/NumberOfDigitOne.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NUMBER_OF_DIGIT_ONE_HPP_ 2 | #define NUMBER_OF_DIGIT_ONE_HPP_ 3 | 4 | class NumberOfDigitOne { 5 | public: 6 | int countDigitOne(int n); 7 | }; 8 | 9 | #endif // NUMBER_OF_DIGIT_ONE_HPP_ -------------------------------------------------------------------------------- /include/UglyNumberII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UGLY_NUMBER_II_HPP_ 2 | #define UGLY_NUMBER_II_HPP_ 3 | 4 | class UglyNumberII { 5 | public: 6 | 7 | int nthUglyNumber(int n); 8 | }; 9 | 10 | #endif // UGLY_NUMBER_II_HPP_ 11 | -------------------------------------------------------------------------------- /include/PerfectSquares.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERFECT_SQUARES_HPP_ 2 | #define PERFECT_SQUARES_HPP_ 3 | 4 | class PerfectSquares { 5 | public: 6 | 7 | int numSquares(int n); 8 | }; 9 | 10 | #endif // PERFECT_SQUARES_HPP_ 11 | -------------------------------------------------------------------------------- /tests/NimGameTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "NimGame.hpp" 4 | 5 | TEST_CASE("Nim Game") { 6 | NimGame s; 7 | SECTION("Sample test") { 8 | REQUIRE_FALSE(s.canWinNim(4)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /include/PalindromeNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PALINDROME_NUMBER_HPP_ 2 | #define PALINDROME_NUMBER_HPP_ 3 | 4 | class PalindromeNumber { 5 | public: 6 | bool isPalindrome(int x); 7 | }; 8 | 9 | #endif // PALINDROME_NUMBER_HPP_ 10 | -------------------------------------------------------------------------------- /include/PathSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PATH_SUM_HPP_ 2 | #define PATH_SUM_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class PathSum { 7 | public: 8 | bool hasPathSum(TreeNode *root, int sum); 9 | }; 10 | 11 | #endif // PATH_SUM_HPP_ 12 | -------------------------------------------------------------------------------- /include/ValidPerfectSquare.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VALID_PERFECT_SQUARE_HPP_ 2 | #define VALID_PERFECT_SQUARE_HPP_ 3 | 4 | class ValidPerfectSquare { 5 | public: 6 | bool isPerfectSquare(int num); 7 | }; 8 | 9 | #endif // VALID_PERFECT_SQUARE_HPP_ -------------------------------------------------------------------------------- /src/Numberof1Bits.cpp: -------------------------------------------------------------------------------- 1 | #include "Numberof1Bits.hpp" 2 | 3 | int Numberof1Bits::hammingWeight(uint32_t n) { 4 | int ret = 0; 5 | while (n) { 6 | n &= (n - 1); 7 | ret++; 8 | } 9 | return ret; 10 | } 11 | -------------------------------------------------------------------------------- /src/PowerOfTwo.cpp: -------------------------------------------------------------------------------- 1 | #include "PowerOfTwo.hpp" 2 | 3 | bool PowerOfTwo::isPowerOfTwo(int n) { 4 | if (n <= 0) 5 | return false; 6 | 7 | while ((n & 1) == 0) 8 | n = n >> 1; 9 | 10 | return n == 1; 11 | } -------------------------------------------------------------------------------- /tests/BulbSwitcherTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BulbSwitcher.hpp" 4 | 5 | TEST_CASE("Bulb Switcher") { 6 | BulbSwitcher s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.bulbSwitch(3) == 1); 9 | } 10 | } -------------------------------------------------------------------------------- /tests/NQueensIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "NQueensII.hpp" 4 | 5 | TEST_CASE("N-Queens II") { 6 | NQueensII s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.totalNQueens(4) == 2); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /include/ReorderList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REORDER_LIST_HPP_ 2 | #define REORDER_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class ReorderList { 7 | public: 8 | void reorderList(ListNode *head); 9 | }; 10 | 11 | #endif // REORDER_LIST_HPP_ -------------------------------------------------------------------------------- /include/SameTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SAME_TREE_HPP_ 2 | #define SAME_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class SameTree { 7 | public: 8 | bool isSameTree(TreeNode *p, TreeNode *q); 9 | }; 10 | 11 | #endif // SAME_TREE_HPP_ 12 | -------------------------------------------------------------------------------- /include/DivideTwoIntegers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DIVIDE_TWO_INTEGERS_HPP_ 2 | #define DIVIDE_TWO_INTEGERS_HPP_ 3 | 4 | class DivideTwoIntegers { 5 | public: 6 | int divide(int dividend, int divisor); 7 | }; 8 | 9 | #endif // DIVIDE_TWO_INTEGERS_HPP_ 10 | -------------------------------------------------------------------------------- /include/FrogJump.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FROG_JUMP_HPP_ 2 | #define FROG_JUMP_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FrogJump { 9 | public: 10 | bool canCross(vector &stones); 11 | }; 12 | 13 | #endif // FROG_JUMP_HPP_ -------------------------------------------------------------------------------- /src/FactorialTrailingZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include "FactorialTrailingZeroes.hpp" 2 | 3 | int FactorialTrailingZeroes::trailingZeroes(int n) { 4 | int result = 0; 5 | 6 | while (n) 7 | result += (n /= 5); 8 | 9 | return result; 10 | } 11 | -------------------------------------------------------------------------------- /tests/EliminationGameTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "EliminationGame.hpp" 4 | 5 | TEST_CASE("Elimination Game") { 6 | EliminationGame s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.lastRemaining(9) == 6); 9 | } 10 | } -------------------------------------------------------------------------------- /tests/FindTheCelebrityTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FindTheCelebrity.hpp" 4 | 5 | TEST_CASE("Find the Celebrity") { 6 | SECTION("Sample test") { 7 | //TODO: Finish the test 8 | REQUIRE(true); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Numberof1BitsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "Numberof1Bits.hpp" 4 | 5 | TEST_CASE("Number of 1 Bits") { 6 | Numberof1Bits s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.hammingWeight(11) == 3); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/UglyNumberIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "UglyNumberII.hpp" 4 | 5 | TEST_CASE("Ugly Number II") { 6 | UglyNumberII s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.nthUglyNumber(10) == 12); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /include/JumpGame.hpp: -------------------------------------------------------------------------------- 1 | #ifndef JUMP_GAME_HPP_ 2 | #define JUMP_GAME_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class JumpGame { 9 | public: 10 | bool canJump(vector &nums); 11 | }; 12 | 13 | #endif // JUMP_GAME_HPP_ 14 | -------------------------------------------------------------------------------- /include/DecodeWays.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DECODE_WAYS_HPP_ 2 | #define DECODE_WAYS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class DecodeWays { 9 | public: 10 | int numDecodings(string s); 11 | }; 12 | 13 | #endif // DECODE_WAYS_HPP_ 14 | -------------------------------------------------------------------------------- /include/LinkedListCycle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LINKED_LIST_CYCLE_HPP_ 2 | #define LINKED_LIST_CYCLE_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class LinkedListCycle { 7 | public: 8 | bool hasCycle(ListNode *head); 9 | }; 10 | 11 | #endif // LINKED_LIST_CYCLE_HPP_ -------------------------------------------------------------------------------- /include/PlusOne.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PLUS_ONE_HPP_ 2 | #define PLUS_ONE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PlusOne { 9 | public: 10 | vector plusOne(vector &digits); 11 | }; 12 | 13 | #endif // PLUS_ONE_HPP_ 14 | -------------------------------------------------------------------------------- /include/TwoSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TWO_SUM_HPP_ 2 | #define TWO_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class TwoSum { 9 | public: 10 | vector twoSum(vector &nums, int target); 11 | }; 12 | 13 | #endif // TWO_SUM_HPP_ 14 | -------------------------------------------------------------------------------- /include/UniqueBinarySearchTrees.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UNIQUE_BINARY_SEARCH_TREES_HPP_ 2 | #define UNIQUE_BINARY_SEARCH_TREES_HPP_ 3 | 4 | class UniqueBinarySearchTrees { 5 | public: 6 | int numTrees(int n); 7 | }; 8 | 9 | #endif // UNIQUE_BINARY_SEARCH_TREES_HPP_ 10 | -------------------------------------------------------------------------------- /include/3Sum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef THREE_SUM_HPP_ 2 | #define THREE_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ThreeSum { 9 | public: 10 | vector> threeSum(vector &nums); 11 | }; 12 | 13 | #endif // THREE_SUM_HPP_ 14 | -------------------------------------------------------------------------------- /include/AddBinary.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ADD_BINARY_HPP_ 2 | #define ADD_BINARY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class AddBinary { 9 | public: 10 | string addBinary(string a, string b); 11 | }; 12 | 13 | #endif // ADD_BINARY_HPP_ 14 | -------------------------------------------------------------------------------- /include/CountAndSay.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNT_AND_SAY_HPP_ 2 | #define COUNT_AND_SAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CountAndSay { 9 | public: 10 | string countAndSay(int n); 11 | }; 12 | 13 | #endif // COUNT_AND_SAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/FactorialTrailingZeroes.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FACTORIAL_TRAILING_ZEROES_HPP_ 2 | #define FACTORIAL_TRAILING_ZEROES_HPP_ 3 | 4 | class FactorialTrailingZeroes { 5 | public: 6 | int trailingZeroes(int n); 7 | }; 8 | 9 | #endif // FACTORIAL_TRAILING_ZEROES_HPP_ 10 | -------------------------------------------------------------------------------- /include/HIndexII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef H_INDEX_II_HPP_ 2 | #define H_INDEX_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class HIndexII { 9 | public: 10 | 11 | int hIndex(vector &citations); 12 | }; 13 | 14 | #endif // H_INDEX_II_HPP_ 15 | -------------------------------------------------------------------------------- /include/HouseRobber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOUSE_ROBBER_HPP_ 2 | #define HOUSE_ROBBER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class HouseRobber { 9 | public: 10 | int rob(vector &nums); 11 | }; 12 | 13 | #endif // HOUSE_ROBBER_HPP_ 14 | -------------------------------------------------------------------------------- /include/JumpGameII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef JUMP_GAME_II_HPP_ 2 | #define JUMP_GAME_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class JumpGameII { 9 | public: 10 | int jump(vector &nums); 11 | }; 12 | 13 | #endif // JUMP_GAME_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/PaintHouse.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PAINT_HOUSE_HPP_ 2 | #define PAINT_HOUSE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PaintHouse { 9 | public: 10 | int minCost(vector> &costs); 11 | }; 12 | 13 | #endif // PAINT_HOUSE_HPP_ -------------------------------------------------------------------------------- /include/PartitionList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PARTITION_LIST_HPP_ 2 | #define PARTITION_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class PartitionList { 7 | public: 8 | ListNode *partition(ListNode *head, int x); 9 | }; 10 | 11 | #endif // PARTITION_LIST_HPP_ 12 | -------------------------------------------------------------------------------- /include/SortColors.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SORT_COLORS_HPP_ 2 | #define SORT_COLORS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SortColors { 9 | public: 10 | void sortColors(vector &nums); 11 | }; 12 | 13 | #endif // SORT_COLORS_HPP_ 14 | -------------------------------------------------------------------------------- /include/Triangle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TRIANGLE_HPP_ 2 | #define TRIANGLE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Triangle { 9 | public: 10 | int minimumTotal(vector> &triangle); 11 | }; 12 | 13 | #endif // TRIANGLE_HPP_ 14 | -------------------------------------------------------------------------------- /tests/HIndexTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "HIndex.hpp" 4 | 5 | TEST_CASE("H-Index") { 6 | HIndex s; 7 | SECTION("Sample test") { 8 | vector nums{3, 0, 6, 1, 5}; 9 | REQUIRE(s.hIndex(nums) == 3); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/NumberOfDigitOneTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "NumberOfDigitOne.hpp" 4 | 5 | TEST_CASE("Number of Digit One") { 6 | NumberOfDigitOne s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.countDigitOne(13) == 6); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /include/ReverseBits.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_BITS_HPP_ 2 | #define REVERSE_BITS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ReverseBits { 9 | public: 10 | uint32_t reverseBits(uint32_t n); 11 | }; 12 | 13 | #endif // REVERSE_BITS_HPP_ 14 | -------------------------------------------------------------------------------- /include/TwoSumII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TWO_SUM_II_HPP_ 2 | #define TWO_SUM_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class TwoSumII { 9 | public: 10 | vector twoSum(vector& numbers, int target); 11 | }; 12 | 13 | #endif // TWO_SUM_II_HPP_ -------------------------------------------------------------------------------- /include/ValidAnagram.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VALID_ANAGRAM_HPP_ 2 | #define VALID_ANAGRAM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ValidAnagram { 9 | public: 10 | bool isAnagram(string s, string t); 11 | }; 12 | 13 | #endif // VALID_ANAGRAM_HPP_ -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories(${LeetCode_SOURCE_DIR}/include) 2 | 3 | file(GLOB SOURCES "*.cpp") 4 | 5 | add_library(Solutions ${SOURCES}) 6 | set_property(TARGET Solutions PROPERTY CXX_STANDARD 11) 7 | set_property(TARGET Solutions PROPERTY CXX_STANDARD_REQUIRED ON) 8 | -------------------------------------------------------------------------------- /tests/HIndexIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "HIndexII.hpp" 4 | 5 | TEST_CASE("H-Index II") { 6 | HIndexII s; 7 | SECTION("Sample test") { 8 | vector nums{0, 1, 3, 5, 6}; 9 | REQUIRE(s.hIndex(nums) == 3); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/4Sum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FOUR_SUM_HPP_ 2 | #define FOUR_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FourSum { 9 | public: 10 | vector> fourSum(vector &nums, int target); 11 | }; 12 | 13 | #endif // FOUR_SUM_HPP_ 14 | -------------------------------------------------------------------------------- /include/AddTwoNumbers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ADD_TWO_NUMBERS_HPP_ 2 | #define ADD_TWO_NUMBERS_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class AddTwoNumbers { 7 | public: 8 | ListNode *addTwoNumbers(ListNode *l1, ListNode *l2); 9 | }; 10 | 11 | #endif // ADD_TWO_NUMBERS_HPP_ 12 | -------------------------------------------------------------------------------- /include/CountingBits.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNTING_BITS_HPP_ 2 | #define COUNTING_BITS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CountingBits { 9 | public: 10 | vector countBits(int num); 11 | }; 12 | 13 | #endif // COUNTING_BITS_HPP_ 14 | -------------------------------------------------------------------------------- /include/GuessNumberHigherOrLowerII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GUESS_NUMBER_HIGHER_OR_LOWER_II_HPP_ 2 | #define GUESS_NUMBER_HIGHER_OR_LOWER_II_HPP_ 3 | 4 | class GuessNumberHigherOrLowerII { 5 | public: 6 | int getMoneyAmount(int n); 7 | }; 8 | 9 | #endif // GUESS_NUMBER_HIGHER_OR_LOWER_II_HPP_ -------------------------------------------------------------------------------- /include/MoveZeroes.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MOVE_ZEROES_HPP_ 2 | #define MOVE_ZEROES_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MoveZeroes { 9 | public: 10 | 11 | void moveZeroes(vector &nums); 12 | }; 13 | 14 | #endif // MOVE_ZEROES_HPP_ 15 | -------------------------------------------------------------------------------- /include/RomanToInteger.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ROMAN_TO_INTEGER_HPP 2 | #define ROMAN_TO_INTEGER_HPP 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RomanToInteger { 9 | public: 10 | int romanToInt(string s); 11 | }; 12 | 13 | #endif // ROMAN_TO_INTEGER_HPP 14 | -------------------------------------------------------------------------------- /include/RotateArray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ROTATE_ARRAY_HPP_ 2 | #define ROTATE_ARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RotateArray { 9 | public: 10 | void rotate(vector &nums, int k); 11 | }; 12 | 13 | #endif // ROTATE_ARRAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/WordPattern.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WORD_PATTERN_HPP_ 2 | #define WORD_PATTERN_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class WordPattern { 9 | public: 10 | bool wordPattern(string pattern, string str); 11 | }; 12 | 13 | #endif // WORD_PATTERN_HPP_ -------------------------------------------------------------------------------- /src/ExcelSheetColumnNumber.cpp: -------------------------------------------------------------------------------- 1 | #include "ExcelSheetColumnNumber.hpp" 2 | 3 | int ExcelSheetColumnNumber::titleToNumber(string s) { 4 | int result = 0; 5 | 6 | for (auto c : s) 7 | result = result * 26 + (c - 'A' + 1); 8 | 9 | return result; 10 | } 11 | -------------------------------------------------------------------------------- /tests/AddDigitsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "AddDigits.hpp" 4 | 5 | TEST_CASE("Add Digits") { 6 | AddDigits s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.addDigits(10) == 1); 9 | REQUIRE(s.addDigits(39) == 3); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/HappyNumberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "HappyNumber.hpp" 4 | 5 | TEST_CASE("Happy Number") { 6 | HappyNumber s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.isHappy(19)); 9 | REQUIRE_FALSE(s.isHappy(3)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/RectangleAreaTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "RectangleArea.hpp" 4 | 5 | TEST_CASE("Rectangle Area") { 6 | RectangleArea s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.computeArea(-5, -2, 3, 1, -3, -3, 3, 3) == 42); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /include/BurstBalloons.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BURST_BALLOONS_HPP_ 2 | #define BURST_BALLOONS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BurstBalloons { 9 | public: 10 | int maxCoins(vector &nums); 11 | }; 12 | 13 | #endif // BURST_BALLOONS_HPP_ 14 | -------------------------------------------------------------------------------- /include/CoinChange.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COIN_CHANGE_HPP_ 2 | #define COIN_CHANGE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CoinChange { 9 | public: 10 | int coinChange(vector &coins, int amount); 11 | }; 12 | 13 | #endif // COIN_CHANGE_HPP_ 14 | -------------------------------------------------------------------------------- /include/InsertionSortList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INSERTION_SORT_LIST_HPP_ 2 | #define INSERTION_SORT_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class InsertionSortList { 7 | public: 8 | ListNode *insertionSortList(ListNode *head); 9 | }; 10 | 11 | #endif // INSERTION_SORT_LIST_HPP_ -------------------------------------------------------------------------------- /include/IntegerToRoman.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INTEGER_TO_ROMAN_HPP_ 2 | #define INTEGER_TO_ROMAN_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class IntegerToRoman { 9 | public: 10 | string intToRoman(int num); 11 | }; 12 | 13 | #endif // INTEGER_TO_ROMAN_HPP_ 14 | -------------------------------------------------------------------------------- /include/IsSubsequence.hpp: -------------------------------------------------------------------------------- 1 | #ifndef IS_SUBSEQUENCE_HPP_ 2 | #define IS_SUBSEQUENCE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class IsSubsequence { 9 | public: 10 | bool isSubsequence(string s, string t); 11 | }; 12 | 13 | #endif // IS_SUBSEQUENCE_HPP_ -------------------------------------------------------------------------------- /include/LinkedListCycleII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LINKED_LIST_CYCLE_II_HPP_ 2 | #define LINKED_LIST_CYCLE_II_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class LinkedListCycleII { 7 | public: 8 | ListNode *detectCycle(ListNode *head); 9 | }; 10 | 11 | #endif // LINKED_LIST_CYCLE_II_HPP_ -------------------------------------------------------------------------------- /include/OddEvenLinkedList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ODD_EVEN_LINKED_LIST_HPP_ 2 | #define ODD_EVEN_LINKED_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class OddEvenLinkedList { 7 | public: 8 | ListNode *oddEvenList(ListNode *head); 9 | }; 10 | 11 | #endif // ODD_EVEN_LINKED_LIST_HPP_ -------------------------------------------------------------------------------- /include/PaintHouseII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PAINT_HOUSE_II_HPP_ 2 | #define PAINT_HOUSE_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PaintHouseII { 9 | public: 10 | int minCostII(vector> &costs); 11 | }; 12 | 13 | #endif // PAINT_HOUSE_II_HPP_ -------------------------------------------------------------------------------- /include/ReverseLinkedList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_LINKED_LIST_HPP_ 2 | #define REVERSE_LINKED_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class ReverseLinkedList { 7 | public: 8 | ListNode *reverseList(ListNode *head); 9 | }; 10 | 11 | #endif // REVERSE_LINKED_LIST_HPP_ 12 | -------------------------------------------------------------------------------- /include/RotateImage.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ROTATE_IMAGE_HPP_ 2 | #define ROTATE_IMAGE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RotateImage { 9 | public: 10 | void rotate(vector> &matrix); 11 | }; 12 | 13 | #endif // ROTATE_IMAGE_HPP_ 14 | -------------------------------------------------------------------------------- /include/SwapNodesInPairs.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SWAP_NODES_IN_PAIRS_HPP_ 2 | #define SWAP_NODES_IN_PAIRS_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class SwapNodesInPairs { 7 | public: 8 | ListNode *swapPairs(ListNode *head); 9 | }; 10 | 11 | #endif // SWAP_NODES_IN_PAIRS_HPP_ 12 | -------------------------------------------------------------------------------- /tests/BasicCalculatorTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BasicCalculator.hpp" 4 | 5 | TEST_CASE("Basic Calculator") { 6 | BasicCalculator s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.calculate(" 1 - 3 + 4 - (3 -1 + 3)") == -3); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/GrayCodeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "GrayCode.hpp" 4 | 5 | TEST_CASE("Gray Code") { 6 | GrayCode s; 7 | SECTION("Sample test") { 8 | vector expected{0, 1, 3, 2}; 9 | REQUIRE(s.grayCode(2) == expected); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/BitwiseANDOfNumbersRange.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BITWISE_AND_OF_NUMBERS_RANGE_HPP_ 2 | #define BITWISE_AND_OF_NUMBERS_RANGE_HPP_ 3 | 4 | class BitwiseANDOfNumbersRange { 5 | public: 6 | int rangeBitwiseAnd(int m, int n); 7 | }; 8 | 9 | #endif // BITWISE_AND_OF_NUMBERS_RANGE_HPP_ 10 | -------------------------------------------------------------------------------- /include/BullsAndCows.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BULLS_AND_COWS_HPP_ 2 | #define BULLS_AND_COWS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BullsAndCows { 9 | public: 10 | string getHint(string secret, string guess); 11 | }; 12 | 13 | #endif // BULLS_AND_COWS_HPP_ -------------------------------------------------------------------------------- /include/EditDistance.hpp: -------------------------------------------------------------------------------- 1 | #ifndef EDIT_DISTANCE_HPP_ 2 | #define EDIT_DISTANCE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class EditDistance { 9 | public: 10 | int minDistance(string word1, string word2); 11 | }; 12 | 13 | #endif // EDIT_DISTANCE_HPP_ 14 | -------------------------------------------------------------------------------- /include/FindTheCelebrity.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_THE_CELEBRITY_HPP_ 2 | #define FIND_THE_CELEBRITY_HPP_ 3 | 4 | class FindTheCelebrity { 5 | // Knows API 6 | bool knows(int a, int b); 7 | 8 | public: 9 | int findCelebrity(int n); 10 | }; 11 | 12 | #endif // FIND_THE_CELEBRITY_HPP_ -------------------------------------------------------------------------------- /include/Numberof1Bits.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NUMBER_OF_1_BITS_HPP_ 2 | #define NUMBER_OF_1_BITS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Numberof1Bits { 9 | public: 10 | int hammingWeight(uint32_t n); 11 | }; 12 | 13 | #endif // NUMBER_OF_1_BITS_HPP_ 14 | -------------------------------------------------------------------------------- /include/PalindromeLinkedList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PALINDROME_LINKED_LIST_HPP_ 2 | #define PALINDROME_LINKED_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class PalindromeLinkedList { 7 | public: 8 | bool isPalindrome(ListNode *head); 9 | }; 10 | 11 | #endif // PALINDROME_LINKED_LIST_HPP_ -------------------------------------------------------------------------------- /include/SingleNumberII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SINGLE_NUMBER_II_HPP_ 2 | #define SINGLE_NUMBER_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SingleNumberII { 9 | public: 10 | int singleNumber(vector &nums); 11 | }; 12 | 13 | #endif // SINGLE_NUMBER_II_HPP_ -------------------------------------------------------------------------------- /include/StringToInteger.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STRING_TO_INTEGER_HPP_ 2 | #define STRING_TO_INTEGER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class StringToInteger { 9 | public: 10 | int myAtoi(string str); 11 | }; 12 | 13 | #endif // STRING_TO_INTEGER_HPP_ 14 | -------------------------------------------------------------------------------- /include/ValidPalindrome.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VALID_PALINDROME_HPP_ 2 | #define VALID_PALINDROME_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ValidPalindrome { 9 | public: 10 | bool isPalindrome(string s); 11 | }; 12 | 13 | #endif // VALID_PALINDROME_HPP_ 14 | -------------------------------------------------------------------------------- /include/ValidParentheses.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VALID_PARENTHESES_HPP_ 2 | #define VALID_PARENTHESES_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ValidParentheses { 9 | public: 10 | bool isValid(string s); 11 | }; 12 | 13 | #endif // VALID_PARENTHESES_HPP_ 14 | -------------------------------------------------------------------------------- /include/ValidSudoku.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VALID_SUDOKU_HPP_ 2 | #define VALID_SUDOKU_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ValidSudoku { 9 | public: 10 | bool isValidSudoku(vector> &board); 11 | }; 12 | 13 | #endif // VALID_SUDOKU_HPP_ 14 | -------------------------------------------------------------------------------- /tests/IntegerToEnglishWordsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "IntegerToEnglishWords.hpp" 4 | 5 | TEST_CASE("Integer to English Words") { 6 | IntegerToEnglishWords s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.numberToWords(1) == "One"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/UniqueBinarySearchTreesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "UniqueBinarySearchTrees.hpp" 4 | 5 | TEST_CASE("Unique Binary Search Trees") { 6 | UniqueBinarySearchTrees s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.numTrees(3) == 5); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /include/DungeonGame.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DUNGEON_GAME_HPP_ 2 | #define DUNGEON_GAME_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class DungeonGame { 9 | public: 10 | int calculateMinimumHP(vector> &dungeon); 11 | }; 12 | 13 | #endif // DUNGEON_GAME_HPP_ 14 | -------------------------------------------------------------------------------- /include/MissingNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MISSING_NUMBER_HPP_ 2 | #define MISSING_NUMBER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MissingNumber { 9 | public: 10 | 11 | int missingNumber(vector &nums); 12 | }; 13 | 14 | #endif // MISSING_NUMBER_HPP_ 15 | -------------------------------------------------------------------------------- /include/ScrambleString.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SCRAMBLE_STRING_HPP_ 2 | #define SCRAMBLE_STRING_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ScrambleString { 9 | public: 10 | bool isScramble(string s1, string s2); 11 | }; 12 | 13 | #endif // SCRAMBLE_STRING_HPP_ 14 | -------------------------------------------------------------------------------- /tests/BurstBalloonsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BurstBalloons.hpp" 4 | 5 | TEST_CASE("Burst Balloons") { 6 | BurstBalloons s; 7 | SECTION("Sample test") { 8 | vector nums{3, 1, 5, 8}; 9 | REQUIRE(s.maxCoins(nums) == 167); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/FactorialTrailingZeroesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FactorialTrailingZeroes.hpp" 4 | 5 | TEST_CASE("Factorial Trailing Zeroes") { 6 | FactorialTrailingZeroes s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.trailingZeroes(5) == 1); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/IntegerBreakTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "IntegerBreak.hpp" 4 | 5 | TEST_CASE("Integer Break") { 6 | IntegerBreak s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.integerBreak(2) == 1); 9 | REQUIRE(s.integerBreak(10) == 36); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/MissingNumberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MissingNumber.hpp" 4 | 5 | TEST_CASE("Missing Number") { 6 | MissingNumber s; 7 | SECTION("Sample test") { 8 | vector nums{0, 1, 3}; 9 | REQUIRE(s.missingNumber(nums) == 2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/PalindromePartitioningIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PalindromePartitioningII.hpp" 4 | 5 | TEST_CASE("Palindrome Partitioning II") { 6 | PalindromePartitioningII s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.minCut("aab") == 1); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/UniquePathsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "UniquePaths.hpp" 4 | 5 | TEST_CASE("Unique Paths") { 6 | UniquePaths s; 7 | SECTION("Normal tests") { 8 | REQUIRE(s.uniquePaths(1, 1) == 1); 9 | REQUIRE(s.uniquePaths(2, 2) == 2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/MajorityElement.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAJORITY_ELEMENT_HPP_ 2 | #define MAJORITY_ELEMENT_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MajorityElement { 9 | public: 10 | int majorityElement(vector &nums); 11 | }; 12 | 13 | #endif // MAJORITY_ELEMENT_HPP_ 14 | -------------------------------------------------------------------------------- /include/MaximalSquare.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMAL_SQUARE_HPP_ 2 | #define MAXIMAL_SQUARE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MaximalSquare { 9 | public: 10 | int maximalSquare(vector> &matrix); 11 | }; 12 | 13 | #endif // MAXIMAL_SQUARE_HPP_ 14 | -------------------------------------------------------------------------------- /include/MinimumPathSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MINIMUM_PATH_SUM_HPP_ 2 | #define MINIMUM_PATH_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MinimumPathSum { 9 | public: 10 | int minPathSum(vector> &grid); 11 | }; 12 | 13 | #endif // MINIMUM_PATH_SUM_HPP_ 14 | -------------------------------------------------------------------------------- /include/NextPermutation.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NEXT_PERMUTATION_HPP_ 2 | #define NEXT_PERMUTATION_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class NextPermutation { 9 | public: 10 | void nextPermutation(vector &nums); 11 | }; 12 | 13 | #endif // NEXT_PERMUTATION_HPP_ 14 | -------------------------------------------------------------------------------- /include/PascalTriangle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PASCAL_TRIANGLE_HPP_ 2 | #define PASCAL_TRIANGLE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PascalTriangle { 9 | public: 10 | vector> generate(int numRows); 11 | }; 12 | 13 | #endif // PASCAL_TRIANGLE_HPP_ 14 | -------------------------------------------------------------------------------- /include/RemoveElement.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_ELEMENT_HPP_ 2 | #define REMOVE_ELEMENT_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RemoveElement { 9 | public: 10 | int removeElement(vector &nums, int val); 11 | }; 12 | 13 | #endif // REMOVE_ELEMENT_HPP_ 14 | -------------------------------------------------------------------------------- /include/SortList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SORT_LIST_HPP_ 2 | #define SORT_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class SortList { 7 | public: 8 | ListNode *sortList(ListNode *head); 9 | 10 | private: 11 | ListNode *merge(ListNode *a, ListNode *b); 12 | }; 13 | 14 | #endif // SORT_LIST_HPP_ -------------------------------------------------------------------------------- /include/SpiralMatrix.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SPIRAL_MATRIX_HPP_ 2 | #define SPIRAL_MATRIX_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SpiralMatrix { 9 | public: 10 | vector spiralOrder(vector> &matrix); 11 | }; 12 | 13 | #endif // SPIRAL_MATRIX_HPP_ 14 | -------------------------------------------------------------------------------- /include/SpiralMatrixII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SPIRAL_MATRIX_II_HPP_ 2 | #define SPIRAL_MATRIX_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SpiralMatrixII { 9 | public: 10 | vector> generateMatrix(int n); 11 | }; 12 | 13 | #endif // SPIRAL_MATRIX_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/WildcardMatching.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WILDCARD_MATCHING_HPP_ 2 | #define WILDCARD_MATCHING_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class WildcardMatching { 9 | public: 10 | bool isMatch(string s, string p); 11 | }; 12 | 13 | #endif // WILDCARD_MATCHING_HPP_ 14 | -------------------------------------------------------------------------------- /tests/PerfectSquaresTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PerfectSquares.hpp" 4 | 5 | TEST_CASE("Perfect Squares") { 6 | PerfectSquares s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.numSquares(12) == 3); 9 | REQUIRE(s.numSquares(13) == 2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/ArithmeticSlices.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ARITHMETIC_SLICES_HPP_ 2 | #define ARITHMETIC_SLICES_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ArithmeticSlices { 9 | public: 10 | int numberOfArithmeticSlices(vector& A); 11 | }; 12 | 13 | #endif // ARITHMETIC_SLICES_HPP_ -------------------------------------------------------------------------------- /include/FindPeakElement.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_PEAK_ELEMENT_HPP_ 2 | #define FIND_PEAK_ELEMENT_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FindPeakElement { 9 | public: 10 | int findPeakElement(vector &nums); 11 | }; 12 | 13 | #endif // FIND_PEAK_ELEMENT_HPP_ 14 | -------------------------------------------------------------------------------- /include/LengthOfLastWord.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LENGTH_OF_LAST_WORD_HPP_ 2 | #define LENGTH_OF_LAST_WORD_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class LengthOfLastWord { 9 | public: 10 | int lengthOfLastWord(string s); 11 | }; 12 | 13 | #endif // LENGTH_OF_LAST_WORD_HPP_ 14 | -------------------------------------------------------------------------------- /include/MultiplyStrings.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MULTIPLY_STRINGS_HPP_ 2 | #define MULTIPLY_STRINGS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MultiplyStrings { 9 | public: 10 | string multiply(string num1, string num2); 11 | }; 12 | 13 | #endif // MULTIPLY_STRINGS_HPP_ 14 | -------------------------------------------------------------------------------- /include/PascalTriangleII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PASCAL_TRIANGLE_II_HPP_ 2 | #define PASCAL_TRIANGLE_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PascalTriangleII { 9 | public: 10 | vector getRow(int rowIndex); 11 | }; 12 | 13 | #endif // PASCAL_TRIANGLE_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/SingleNumberIII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SINGLE_NUMBER_III_HPP_ 2 | #define SINGLE_NUMBER_III_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SingleNumberIII { 9 | public: 10 | vector singleNumber(vector &nums); 11 | }; 12 | 13 | #endif // SINGLE_NUMBER_III_HPP_ -------------------------------------------------------------------------------- /include/TrappingRainWater.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TRAPPING_RAIN_WATER_HPP_ 2 | #define TRAPPING_RAIN_WATER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class TrappingRainWater { 9 | public: 10 | int trap(vector &height); 11 | }; 12 | 13 | #endif // TRAPPING_RAIN_WATER_HPP_ 14 | -------------------------------------------------------------------------------- /tests/BitwiseANDOfNumbersRangeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BitwiseANDOfNumbersRange.hpp" 4 | 5 | TEST_CASE("Bitwise AND of Numbers Range") { 6 | BitwiseANDOfNumbersRange s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.rangeBitwiseAnd(5, 7) == 4); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/UglyNumberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "UglyNumber.hpp" 4 | 5 | TEST_CASE("Ugly Number") { 6 | UglyNumber s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.isUgly(6)); 9 | REQUIRE(s.isUgly(8)); 10 | REQUIRE_FALSE(s.isUgly(14)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /include/3SumClosest.hpp: -------------------------------------------------------------------------------- 1 | #ifndef THREE_SUM_CLOSEST_HPP_ 2 | #define THREE_SUM_CLOSEST_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ThreeSumClosest { 9 | public: 10 | int threeSumClosest(vector &nums, int target); 11 | }; 12 | 13 | #endif // THREE_SUM_CLOSEST_HPP_ 14 | -------------------------------------------------------------------------------- /include/CountCompleteTreeNodes.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNT_COMPLETE_TREE_NODES_HPP_ 2 | #define COUNT_COMPLETE_TREE_NODES_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class CountCompleteTreeNodes { 7 | public: 8 | int countNodes(TreeNode *root); 9 | }; 10 | 11 | #endif // COUNT_COMPLETE_TREE_NODES_HPP_ 12 | -------------------------------------------------------------------------------- /include/FindTheDifference.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_THE_DIFFERENCE_HPP_ 2 | #define FIND_THE_DIFFERENCE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FindTheDifference { 9 | public: 10 | char findTheDifference(string s, string t); 11 | }; 12 | 13 | #endif // FIND_THE_DIFFERENCE_HPP_ -------------------------------------------------------------------------------- /include/IsomorphicStrings.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ISOMORPHIC_STRINGS_HPP_ 2 | #define ISOMORPHIC_STRINGS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class IsomorphicStrings { 9 | public: 10 | bool isIsomorphic(string s, string t); 11 | }; 12 | 13 | #endif // ISOMORPHIC_STRINGS_HPP_ 14 | -------------------------------------------------------------------------------- /include/SetMatrixZeroes.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SET_MATRIX_ZEROES_HPP_ 2 | #define SET_MATRIX_ZEROES_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SetMatrixZeroes { 9 | public: 10 | void setZeroes(vector> &matrix); 11 | }; 12 | 13 | #endif // SET_MATRIX_ZEROES_HPP_ 14 | -------------------------------------------------------------------------------- /include/WaterAndJugProblem.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WATER_AND_JUG_PROBLEM_HPP_ 2 | #define WATER_AND_JUG_PROBLEM_HPP_ 3 | 4 | class WaterAndJugProblem { 5 | public: 6 | bool canMeasureWater(int x, int y, int z); 7 | 8 | private: 9 | int gcd(int x, int y); 10 | }; 11 | 12 | #endif // WATER_AND_JUG_PROBLEM_HPP_ -------------------------------------------------------------------------------- /include/ZigZagConversion.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ZIG_ZAG_CONVERSION_HPP_ 2 | #define ZIG_ZAG_CONVERSION_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ZigZagConversion { 9 | public: 10 | string convert(string s, int numRows); 11 | }; 12 | 13 | #endif // ZIG_ZAG_CONVERSION_HPP_ 14 | -------------------------------------------------------------------------------- /tests/FindPeakElementTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FindPeakElement.hpp" 4 | 5 | TEST_CASE("Find Peak Element") { 6 | FindPeakElement s; 7 | SECTION("Sample test") { 8 | vector nums{1, 2, 3, 1}; 9 | REQUIRE(s.findPeakElement(nums) == 2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/GuessNumberHigherOrLowerIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "GuessNumberHigherOrLowerII.hpp" 4 | 5 | TEST_CASE("Guess Number Higher or Lower II") { 6 | GuessNumberHigherOrLowerII s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.getMoneyAmount(1) == 0); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /include/CombinationSumIV.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMBINATION_SUM_IV_HPP_ 2 | #define COMBINATION_SUM_IV_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CombinationSumIV { 9 | public: 10 | int combinationSum4(vector& nums, int target); 11 | }; 12 | 13 | #endif // COMBINATION_SUM_IV_HPP_ -------------------------------------------------------------------------------- /include/ContainsDuplicate.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONTAINS_DUPLICATE_HPP_ 2 | #define CONTAINS_DUPLICATE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ContainsDuplicate { 9 | public: 10 | bool containsDuplicate(vector &nums); 11 | }; 12 | 13 | #endif // CONTAINS_DUPLICATE_HPP_ 14 | -------------------------------------------------------------------------------- /include/CountNumbersWithUniqueDigits.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNT_NUMBERS_WITH_UNIQUE_DIGITS_HPP_ 2 | #define COUNT_NUMBERS_WITH_UNIQUE_DIGITS_HPP_ 3 | 4 | class CountNumbersWithUniqueDigits { 5 | public: 6 | int countNumbersWithUniqueDigits(int n); 7 | }; 8 | 9 | #endif // COUNT_NUMBERS_WITH_UNIQUE_DIGITS_HPP_ 10 | -------------------------------------------------------------------------------- /include/DeleteNodeInALinkedList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DELETE_NODE_IN_A_LINKED_LIST_HPP_ 2 | #define DELETE_NODE_IN_A_LINKED_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class DeleteNodeInALinkedList { 7 | public: 8 | void deleteNode(ListNode *node); 9 | }; 10 | 11 | #endif // DELETE_NODE_IN_A_LINKED_LIST_HPP_ -------------------------------------------------------------------------------- /include/MergeTwoSortedLists.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MERGE_TWO_SORTED_LISTS_HPP_ 2 | #define MERGE_TWO_SORTED_LISTS_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class MergeTwoSortedLists { 7 | public: 8 | ListNode *mergeTwoLists(ListNode *l1, ListNode *l2); 9 | }; 10 | 11 | #endif // MERGE_TWO_SORTED_LISTS_HPP_ 12 | -------------------------------------------------------------------------------- /include/ReverseLinkedListII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_LINKED_LIST_II_HPP_ 2 | #define REVERSE_LINKED_LIST_II_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class ReverseLinkedListII { 7 | public: 8 | ListNode *reverseBetween(ListNode *head, int m, int n); 9 | }; 10 | 11 | #endif // REVERSE_LINKED_LIST_II_HPP_ 12 | -------------------------------------------------------------------------------- /include/ReverseNodesInKGroup.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_NODES_IN_K_GROUP_HPP_ 2 | #define REVERSE_NODES_IN_K_GROUP_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class ReverseNodesInKGroup { 7 | public: 8 | ListNode *reverseKGroup(ListNode *head, int k); 9 | }; 10 | 11 | #endif // REVERSE_NODES_IN_K_GROUP_HPP_ 12 | -------------------------------------------------------------------------------- /include/ShortestPalindrome.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SHORTEST_PALINDROME_HPP_ 2 | #define SHORTEST_PALINDROME_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ShortestPalindrome { 9 | public: 10 | string shortestPalindrome(string s); 11 | }; 12 | 13 | #endif // SHORTEST_PALINDROME_HPP_ 14 | -------------------------------------------------------------------------------- /include/SuperUglyNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUPER_UGLY_NUMBER_HPP_ 2 | #define SUPER_UGLY_NUMBER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SuperUglyNumber { 9 | public: 10 | int nthSuperUglyNumber(int n, vector &primes); 11 | }; 12 | 13 | #endif // SUPER_UGLY_NUMBER_HPP_ 14 | -------------------------------------------------------------------------------- /src/LoggerRateLimiter.cpp: -------------------------------------------------------------------------------- 1 | #include "LoggerRateLimiter.hpp" 2 | 3 | bool LoggerRateLimiter::shouldPrintMessage(int timestamp, string message) { 4 | if (log.find(message) == log.end() || log[message] <= timestamp) { 5 | log[message] = timestamp + 10; 6 | return true; 7 | } 8 | return false; 9 | } -------------------------------------------------------------------------------- /tests/FlattenNestedListIteratorTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FlattenNestedListIterator.hpp" 4 | 5 | TEST_CASE("Flatten Nested List Iterator") { 6 | // NestedIterator s; 7 | SECTION("Normal test") { 8 | // TODO: call for tests 9 | REQUIRE(true); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/LargestNumberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "LargestNumber.hpp" 4 | 5 | TEST_CASE("Largest Number") { 6 | LargestNumber s; 7 | SECTION("Sample test") { 8 | vector nums{3, 30, 34, 5, 9}; 9 | REQUIRE(s.largestNumber(nums) == "9534330"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/MajorityElementTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MajorityElement.hpp" 4 | 5 | TEST_CASE("Majority Element") { 6 | MajorityElement s; 7 | SECTION("Sample test") { 8 | vector nums{1, 0, 1, 0, 0, 0}; 9 | REQUIRE(s.majorityElement(nums) == 0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/PascalTriangleIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PascalTriangleII.hpp" 4 | 5 | TEST_CASE("Pascal's Triangle II") { 6 | PascalTriangleII s; 7 | SECTION("Sample test") { 8 | vector expected{1, 3, 3, 1}; 9 | REQUIRE(s.getRow(3) == expected); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/SingleNumberIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SingleNumberII.hpp" 4 | 5 | TEST_CASE("Single Number II") { 6 | SingleNumberII s; 7 | SECTION("Sample test") { 8 | vector nums{-1, 2, 3, 2, -1, 2, -1}; 9 | REQUIRE(s.singleNumber(nums) == 3); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/ValidAnagramTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ValidAnagram.hpp" 4 | 5 | TEST_CASE("Valid Anagram") { 6 | ValidAnagram s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.isAnagram("anagram", "nagaram")); 9 | REQUIRE_FALSE(s.isAnagram("rat", "car")); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/GrayCode.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GRAY_CODE_HPP_ 2 | #define GRAY_CODE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class GrayCode { 9 | public: 10 | vector grayCode(int n); 11 | 12 | private: 13 | void helper(int n, vector &res); 14 | }; 15 | 16 | #endif // GRAY_CODE_HPP_ 17 | -------------------------------------------------------------------------------- /include/InorderSuccessorInBST.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INORDER_SUCCESSOR_IN_BST_HPP_ 2 | #define INORDER_SUCCESSOR_IN_BST_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class InorderSuccessorInBST { 7 | public: 8 | TreeNode *inorderSuccessor(TreeNode *root, TreeNode *p); 9 | }; 10 | 11 | #endif // INORDER_SUCCESSOR_IN_BST_HPP_ -------------------------------------------------------------------------------- /include/MajorityElementII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAJORITY_ELEMENT_II_HPP_ 2 | #define MAJORITY_ELEMENT_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MajorityElementII { 9 | public: 10 | vector majorityElement(vector &nums); 11 | }; 12 | 13 | #endif // MAJORITY_ELEMENT_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/MinimumDepthOfBinaryTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MINIMUM_DEPTH_OF_BINARY_TREE_HPP_ 2 | #define MINIMUM_DEPTH_OF_BINARY_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class MinimumDepthOfBinaryTree { 7 | public: 8 | int minDepth(TreeNode *root); 9 | }; 10 | 11 | #endif // MINIMUM_DEPTH_OF_BINARY_TREE_HPP_ 12 | -------------------------------------------------------------------------------- /include/PalindromePermutation.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PALINDROME_PERMUTATION_HPP_ 2 | #define PALINDROME_PERMUTATION_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PalindromePermutation { 9 | public: 10 | bool canPermutePalindrome(string s); 11 | }; 12 | 13 | #endif // PALINDROME_PERMUTATION_HPP_ -------------------------------------------------------------------------------- /include/UniquePathsII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UNIQUE_PATHS_II_HPP_ 2 | #define UNIQUE_PATHS_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class UniquePathsII { 9 | public: 10 | int uniquePathsWithObstacles(vector> &obstacleGrid); 11 | }; 12 | 13 | #endif // UNIQUE_PATHS_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/WordBreak.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WORD_BREAK_HPP_ 2 | #define WORD_BREAK_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class WordBreak { 10 | public: 11 | bool wordBreak(string s, unordered_set &wordDict); 12 | }; 13 | 14 | #endif // WORD_BREAK_HPP_ 15 | -------------------------------------------------------------------------------- /tests/BullsAndCowsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BullsAndCows.hpp" 4 | 5 | TEST_CASE("Bulls and Cows") { 6 | BullsAndCows s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.getHint("1807", "7810") == "1A3B"); 9 | REQUIRE(s.getHint("1123", "0111") == "1A1B"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/ValidPerfectSquareTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ValidPerfectSquare.hpp" 4 | 5 | TEST_CASE("Valid Perfect Square") { 6 | ValidPerfectSquare s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.isPerfectSquare(16)); 9 | REQUIRE_FALSE(s.isPerfectSquare(14)); 10 | } 11 | } -------------------------------------------------------------------------------- /include/DistinctSubsequences.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DISTINCT_SUBSEQUENCES_HPP_ 2 | #define DISTINCT_SUBSEQUENCES_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class DistinctSubsequences { 9 | public: 10 | int numDistinct(string s, string t); 11 | }; 12 | 13 | #endif // DISTINCT_SUBSEQUENCES_HPP_ 14 | -------------------------------------------------------------------------------- /include/ExcelSheetColumnTitle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef EXCEL_SHEET_COLUMN_TITLE_HPP_ 2 | #define EXCEL_SHEET_COLUMN_TITLE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ExcelSheetColumnTitle { 9 | public: 10 | string convertToTitle(int n); 11 | }; 12 | 13 | #endif // EXCEL_SHEET_COLUMN_TITLE_HPP_ 14 | -------------------------------------------------------------------------------- /include/HappyNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HAPPY_NUMBER_HPP_ 2 | #define HAPPY_NUMBER_HPP_ 3 | 4 | class HappyNumber { 5 | public: 6 | bool isHappy(int n); 7 | private: 8 | int digitSquareSum(int n); 9 | bool isHappyHash(int n); 10 | bool isHappyTwoPointers(int n); 11 | }; 12 | 13 | #endif // HAPPY_NUMBER_HPP_ 14 | -------------------------------------------------------------------------------- /include/SearchA2DMatrix.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_A_2D_MATRIX_HPP_ 2 | #define SEARCH_A_2D_MATRIX_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SearchA2DMatrix { 9 | public: 10 | bool searchMatrix(vector> &matrix, int target); 11 | }; 12 | 13 | #endif // SEARCH_A_2D_MATRIX_HPP_ 14 | -------------------------------------------------------------------------------- /include/SearchForARange.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_FOR_A_RANGE_HPP_ 2 | #define SEARCH_FOR_A_RANGE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SearchForARange { 9 | public: 10 | vector searchRange(vector &nums, int target); 11 | }; 12 | 13 | #endif // SEARCH_FOR_A_RANGE_HPP_ 14 | -------------------------------------------------------------------------------- /src/RemoveElement.cpp: -------------------------------------------------------------------------------- 1 | #include "RemoveElement.hpp" 2 | 3 | int RemoveElement::removeElement(vector &nums, int val) { 4 | int index = 0; 5 | 6 | for (int i = 0; i < nums.size(); i++) { 7 | if (nums[i] != val) 8 | nums[index++] = nums[i]; 9 | } 10 | 11 | return index; 12 | } 13 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories(${LeetCode_SOURCE_DIR}/include) 2 | 3 | file(GLOB SOURCES "*.cpp") 4 | 5 | add_executable(Test ${SOURCES}) 6 | 7 | target_link_libraries(Test Solutions) 8 | set_property(TARGET Test PROPERTY CXX_STANDARD 11) 9 | set_property(TARGET Test PROPERTY CXX_STANDARD_REQUIRED ON) 10 | -------------------------------------------------------------------------------- /tests/ImplementTrieTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ImplementTrie.hpp" 4 | 5 | TEST_CASE("Implement Trie") { 6 | Trie trie; 7 | SECTION("Sample test") { 8 | trie.insert("ab"); 9 | REQUIRE_FALSE(trie.search("a")); 10 | REQUIRE(trie.startsWith("a")); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/IsSubsequenceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "IsSubsequence.hpp" 4 | 5 | TEST_CASE("Is Subsequence") { 6 | IsSubsequence s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.isSubsequence("abc", "ahbgdc")); 9 | REQUIRE_FALSE(s.isSubsequence("axc", "ahbgdc")); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/ExcelSheetColumnNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef EXCEL_SHEET_COLUMN_NUMBER_HPP_ 2 | #define EXCEL_SHEET_COLUMN_NUMBER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ExcelSheetColumnNumber { 9 | public: 10 | int titleToNumber(string s); 11 | }; 12 | 13 | #endif // EXCEL_SHEET_COLUMN_NUMBER_HPP_ 14 | -------------------------------------------------------------------------------- /include/InterleavingString.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INTERLEAVING_STRING_HPP_ 2 | #define INTERLEAVING_STRING_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class InterleavingString { 9 | public: 10 | bool isInterleave(string s1, string s2, string s3); 11 | }; 12 | 13 | #endif // INTERLEAVING_STRING_HPP_ 14 | -------------------------------------------------------------------------------- /include/PalindromePartitioningII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PALINDROME_PARTITIONING_II_HPP_ 2 | #define PALINDROME_PARTITIONING_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PalindromePartitioningII { 9 | public: 10 | int minCut(string s); 11 | }; 12 | 13 | #endif // PALINDROME_PARTITIONING_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/ReverseWordsInAString.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_WORDS_IN_A_STRING_HPP_ 2 | #define REVERSE_WORDS_IN_A_STRING_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ReverseWordsInAString { 9 | public: 10 | void reverseWords(string &s); 11 | }; 12 | 13 | #endif // REVERSE_WORDS_IN_A_STRING_HPP_ 14 | -------------------------------------------------------------------------------- /include/WiggleSortII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WIGGLE_SORT_II_HPP_ 2 | #define WIGGLE_SORT_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class WiggleSortII { 9 | public: 10 | void wiggleSort(vector &nums); 11 | 12 | private: 13 | int m(int idx, int n); 14 | }; 15 | 16 | #endif // WIGGLE_SORT_II_HPP_ -------------------------------------------------------------------------------- /src/JumpGame.cpp: -------------------------------------------------------------------------------- 1 | #include "JumpGame.hpp" 2 | 3 | bool JumpGame::canJump(vector &nums) { 4 | int right = 0; 5 | 6 | for (int i = 0; i < nums.size(); i++) { 7 | if (i > right) 8 | return false; 9 | 10 | right = max(i + nums[i], right); 11 | } 12 | 13 | return true; 14 | } 15 | -------------------------------------------------------------------------------- /tests/EditDistanceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "EditDistance.hpp" 4 | 5 | TEST_CASE("Edit Distance") { 6 | EditDistance s; 7 | SECTION("Normal tests") { 8 | REQUIRE(s.minDistance("test", "fast") == 2); 9 | REQUIRE(s.minDistance("distance", "distance") == 0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/ContainerWithMostWater.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONTAINER_WITH_MOST_WATER_HPP_ 2 | #define CONTAINER_WITH_MOST_WATER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ContainerWithMostWater { 9 | public: 10 | int maxArea(vector &height); 11 | }; 12 | 13 | #endif // CONTAINER_WITH_MOST_WATER_HPP_ 14 | -------------------------------------------------------------------------------- /include/FindTheDuplicateNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_THE_DUPLICATE_NUMBER_HPP_ 2 | #define FIND_THE_DUPLICATE_NUMBER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FindTheDuplicateNumber { 9 | public: 10 | int findDuplicate(vector nums); 11 | }; 12 | 13 | #endif // FIND_THE_DUPLICATE_NUMBER_HPP_ -------------------------------------------------------------------------------- /include/FirstMissingPositive.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIRST_MISSING_POSITIVE_HPP_ 2 | #define FIRST_MISSING_POSITIVE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FirstMissingPositive { 9 | public: 10 | int firstMissingPositive(vector &nums); 11 | }; 12 | 13 | #endif // FIRST_MISSING_POSITIVE_HPP_ 14 | -------------------------------------------------------------------------------- /include/MaximumGap.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMUM_GAP_HPP_ 2 | #define MAXIMUM_GAP_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MaximumGap { 9 | public: 10 | int maximumGap(vector &nums); 11 | 12 | private: 13 | int getNthDigit(int num, int n); 14 | }; 15 | 16 | #endif // MAXIMUM_GAP_HPP_ 17 | -------------------------------------------------------------------------------- /include/MaximumProductSubarray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMUM_PRODUCT_SUBARRAY_HPP_ 2 | #define MAXIMUM_PRODUCT_SUBARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MaximumProductSubarray { 9 | public: 10 | int maxProduct(vector &nums); 11 | }; 12 | 13 | #endif // MAXIMUM_PRODUCT_SUBARRAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/MergeSortedArray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MERGE_SORTED_ARRAY_HPP_ 2 | #define MERGE_SORTED_ARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MergeSortedArray { 9 | public: 10 | void merge(vector &nums1, int m, vector &nums2, int n); 11 | }; 12 | 13 | #endif // MERGE_SORTED_ARRAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/MinimumWindowSubstring.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MINIMUM_WINDOW_SUBSTRING_HPP_ 2 | #define MINIMUM_WINDOW_SUBSTRING_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MinimumWindowSubstring { 9 | public: 10 | string minWindow(string s, string t); 11 | }; 12 | 13 | #endif // MINIMUM_WINDOW_SUBSTRING_HPP_ 14 | -------------------------------------------------------------------------------- /include/RemoveDuplicateLetters.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_DUPLICATE_LETTERS_HPP_ 2 | #define REMOVE_DUPLICATE_LETTERS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RemoveDuplicateLetters { 9 | public: 10 | string removeDuplicateLetters(string s); 11 | }; 12 | 13 | #endif // REMOVE_DUPLICATE_LETTERS_HPP_ -------------------------------------------------------------------------------- /include/RemoveLinkedListElements.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_LINKED_LIST_ELEMENTS_HPP_ 2 | #define REMOVE_LINKED_LIST_ELEMENTS_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class RemoveLinkedListElements { 7 | public: 8 | ListNode *removeElements(ListNode *head, int val); 9 | }; 10 | 11 | #endif // REMOVE_LINKED_LIST_ELEMENTS_HPP_ 12 | -------------------------------------------------------------------------------- /src/ExcelSheetColumnTitle.cpp: -------------------------------------------------------------------------------- 1 | #include "ExcelSheetColumnTitle.hpp" 2 | 3 | string ExcelSheetColumnTitle::convertToTitle(int n) { 4 | string ret; 5 | 6 | while (n) { 7 | char c = 'A' + (n - 1) % 26; 8 | ret = string(1, c) + ret; 9 | n = (n - 1) / 26; 10 | } 11 | 12 | return ret; 13 | } 14 | -------------------------------------------------------------------------------- /tests/WaterAndJugProblemTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "WaterAndJugProblem.hpp" 4 | 5 | TEST_CASE("Water and Jug Problem") { 6 | WaterAndJugProblem s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.canMeasureWater(3, 5, 4)); 9 | REQUIRE_FALSE(s.canMeasureWater(2, 6, 5)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/ContainsDuplicateII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONTAINS_DUPLICATE_II_HPP_ 2 | #define CONTAINS_DUPLICATE_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ContainsDuplicateII { 9 | public: 10 | bool containsNearbyDuplicate(vector &nums, int k); 11 | }; 12 | 13 | #endif // CONTAINS_DUPLICATE_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/PartitionEqualSubsetSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PARTITION_EQUAL_SUBSET_SUM_HPP_ 2 | #define PARTITION_EQUAL_SUBSET_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PartitionEqualSubsetSum { 9 | public: 10 | bool canPartition(vector &nums); 11 | }; 12 | 13 | #endif // PARTITION_EQUAL_SUBSET_SUM_HPP_ -------------------------------------------------------------------------------- /include/SearchInsertPosition.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_INSERT_POSITION_HPP_ 2 | #define SEARCH_INSERT_POSITION_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SearchInsertPosition { 9 | public: 10 | int searchInsert(vector &nums, int target); 11 | }; 12 | 13 | #endif // SEARCH_INSERT_POSITION_HPP_ 14 | -------------------------------------------------------------------------------- /include/SlidingWindowMaximum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SLIDING_WINDOW_MAXIMUM_HPP_ 2 | #define SLIDING_WINDOW_MAXIMUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SlidingWindowMaximum { 9 | public: 10 | vector maxSlidingWindow(vector &nums, int k); 11 | }; 12 | 13 | #endif // SLIDING_WINDOW_MAXIMUM_HPP_ -------------------------------------------------------------------------------- /include/TopKFrequentElements.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TOP_K_FREQUENT_ELEMENTS_HPP_ 2 | #define TOP_K_FREQUENT_ELEMENTS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class TopKFrequentElements { 9 | public: 10 | vector topKFrequent(vector &nums, int k); 11 | }; 12 | 13 | #endif // TOP_K_FREQUENT_ELEMENTS_HPP_ -------------------------------------------------------------------------------- /tests/FindTheDuplicateNumberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FindTheDuplicateNumber.hpp" 4 | 5 | TEST_CASE("Find the Duplicate Number") { 6 | FindTheDuplicateNumber s; 7 | SECTION("Sample test") { 8 | vector nums{3, 1, 2, 3}; 9 | REQUIRE(s.findDuplicate(nums) == 3); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/PaintHouseTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PaintHouse.hpp" 4 | 5 | TEST_CASE("Paint House") { 6 | PaintHouse s; 7 | SECTION("Sample test") { 8 | vector> costs{{1, 2, 3}, 9 | {4, 5, 6}}; 10 | REQUIRE(s.minCost(costs) == 6); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/ReverseBitsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ReverseBits.hpp" 4 | 5 | TEST_CASE("Reverse Bits") { 6 | ReverseBits s; 7 | SECTION("Sample test") { 8 | uint32_t input = 43261596; 9 | uint32_t expected = 964176192; 10 | REQUIRE(s.reverseBits(input) == expected); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /include/BalancedBinaryTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BALANCED_BINARY_TREE_HPP_ 2 | #define BALANCED_BINARY_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class BalancedBinaryTree { 7 | public: 8 | bool isBalanced(TreeNode *root); 9 | 10 | private: 11 | int maxDepth(TreeNode *root); 12 | }; 13 | 14 | #endif // BALANCED_BINARY_TREE_HPP_ 15 | -------------------------------------------------------------------------------- /include/LongestValidParentheses.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LONGEST_VALID_PARENTHESES_HPP_ 2 | #define LONGEST_VALID_PARENTHESES_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class LongestValidParentheses { 9 | public: 10 | int longestValidParentheses(string s); 11 | }; 12 | 13 | #endif // LONGEST_VALID_PARENTHESES_HPP_ 14 | -------------------------------------------------------------------------------- /include/SearchA2DMatrixII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_A_2D_MATRIX_II_HPP_ 2 | #define SEARCH_A_2D_MATRIX_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SearchA2DMatrixII { 9 | public: 10 | bool searchMatrix(vector> &matrix, int target); 11 | }; 12 | 13 | #endif // SEARCH_A_2D_MATRIX_II_HPP_ 14 | 15 | -------------------------------------------------------------------------------- /src/PascalTriangleII.cpp: -------------------------------------------------------------------------------- 1 | #include "PascalTriangleII.hpp" 2 | 3 | vector PascalTriangleII::getRow(int rowIndex) { 4 | vector r(rowIndex + 1, 0); 5 | r[0] = 1; 6 | 7 | for (int i = 1; i < rowIndex + 1; i++) 8 | for (int j = i; j >= 1; j--) 9 | r[j] += r[j - 1]; 10 | 11 | return r; 12 | } 13 | -------------------------------------------------------------------------------- /tests/AddBinaryTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "AddBinary.hpp" 4 | 5 | TEST_CASE("Add Binary") { 6 | AddBinary s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.addBinary("11", "1") == "100"); 9 | } 10 | SECTION("Basic addition") { 11 | REQUIRE(s.addBinary("0", "1") == "1"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/ClimbingStairsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ClimbingStairs.hpp" 4 | 5 | TEST_CASE("Climbing Stairs") { 6 | ClimbingStairs s; 7 | SECTION("Normal tests") { 8 | REQUIRE(s.climbStairs(1) == 1); 9 | REQUIRE(s.climbStairs(2) == 2); 10 | REQUIRE(s.climbStairs(3) == 3); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/FindTheDifferenceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FindTheDifference.hpp" 4 | 5 | TEST_CASE("Find the Difference") { 6 | FindTheDifference sol; 7 | SECTION("Sample tests") { 8 | string s = "abcd"; 9 | string t = "abcde"; 10 | REQUIRE(sol.findTheDifference(s, t) == 'e'); 11 | } 12 | } -------------------------------------------------------------------------------- /tests/ValidPalindromeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ValidPalindrome.hpp" 4 | 5 | TEST_CASE("Valid Palindrome") { 6 | ValidPalindrome s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.isPalindrome("A man, a plan, a canal: Panama")); 9 | REQUIRE_FALSE(s.isPalindrome("race a car")); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/ClosestBinarySearchTreeValue.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLOSEST_BINARY_SEARCH_TREE_VALUE_HPP_ 2 | #define CLOSEST_BINARY_SEARCH_TREE_VALUE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class ClosestBinarySearchTreeValue { 7 | public: 8 | int closestValue(TreeNode *root, double target); 9 | }; 10 | 11 | #endif // CLOSEST_BINARY_SEARCH_TREE_VALUE_HPP_ -------------------------------------------------------------------------------- /src/RangeSumQueryImmutable.cpp: -------------------------------------------------------------------------------- 1 | #include "RangeSumQueryImmutable.hpp" 2 | 3 | NumArray::NumArray(vector &nums) : accs(nums.begin(), nums.end()) { 4 | for (int i = 1; i < nums.size(); i++) 5 | accs[i] += accs[i - 1]; 6 | } 7 | 8 | int NumArray::sumRange(int i, int j) { 9 | return i == 0 ? accs[j] : accs[j] - accs[i - 1]; 10 | } -------------------------------------------------------------------------------- /tests/CombinationSumIVTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "CombinationSumIV.hpp" 4 | 5 | TEST_CASE("Combination Sum IV") { 6 | CombinationSumIV s; 7 | SECTION("Sample tests") { 8 | vector nums{1, 2, 3}; 9 | int target = 4; 10 | REQUIRE(s.combinationSum4(nums, target) == 7); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/CountingBitsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "CountingBits.hpp" 4 | 5 | TEST_CASE("Counting Bits") { 6 | CountingBits s; 7 | SECTION("Sample test") { 8 | vector expected{0, 1, 1, 2, 1, 2}; 9 | vector result = s.countBits(5); 10 | REQUIRE(result == expected); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/PaintHouseIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PaintHouseII.hpp" 4 | 5 | TEST_CASE("Paint House II") { 6 | PaintHouseII s; 7 | SECTION("Sample test") { 8 | vector> costs{{1, 2, 3}, 9 | {4, 5, 6}}; 10 | REQUIRE(s.minCostII(costs) == 6); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/TwoSumIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "TwoSumII.hpp" 4 | 5 | TEST_CASE("Two Sum II") { 6 | TwoSumII s; 7 | SECTION("Sample test") { 8 | vector numbers{2, 7, 11, 15}; 9 | int target = 9; 10 | vector ans{1, 2}; 11 | REQUIRE(s.twoSum(numbers, target) == ans); 12 | } 13 | } -------------------------------------------------------------------------------- /include/BasicCalculator.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BASIC_CALCULATOR_HPP_ 2 | #define BASIC_CALCULATOR_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BasicCalculator { 9 | public: 10 | int calculate(string s); 11 | 12 | private: 13 | int calc(int left, int right, char op); 14 | }; 15 | 16 | #endif // BASIC_CALCULATOR_HPP_ 17 | -------------------------------------------------------------------------------- /include/HouseRobberII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOUSE_ROBBER_II_HPP_ 2 | #define HOUSE_ROBBER_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class HouseRobberII { 9 | public: 10 | int rob(vector &nums); 11 | 12 | private: 13 | int helper(vector &nums, int a, int b); 14 | }; 15 | 16 | #endif // HOUSE_ROBBER_II_HPP_ 17 | -------------------------------------------------------------------------------- /include/LargestDivisibleSubset.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LARGEST_DIVISIBLE_SUBSET_HPP_ 2 | #define LARGEST_DIVISIBLE_SUBSET_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class LargestDivisibleSubset { 9 | public: 10 | vector largestDivisibleSubset(vector &nums); 11 | }; 12 | 13 | #endif // LARGEST_DIVISIBLE_SUBSET_HPP_ 14 | -------------------------------------------------------------------------------- /include/LongestCommonPrefix.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LONGEST_COMMON_PREFIX_HPP_ 2 | #define LONGEST_COMMON_PREFIX_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class LongestCommonPrefix { 10 | public: 11 | string longestCommonPrefix(vector &strs); 12 | }; 13 | 14 | #endif // LONGEST_COMMON_PREFIX_HPP_ 15 | -------------------------------------------------------------------------------- /include/RemoveNthNodeFromEndOfList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_NTH_NODE_FROM_END_OF_LIST_HPP_ 2 | #define REMOVE_NTH_NODE_FROM_END_OF_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class RemoveNthNodeFromEndOfList { 7 | public: 8 | ListNode *removeNthFromEnd(ListNode *head, int n); 9 | }; 10 | 11 | #endif // REMOVE_NTH_NODE_FROM_END_OF_LIST_HPP_ 12 | -------------------------------------------------------------------------------- /src/CountingBits.cpp: -------------------------------------------------------------------------------- 1 | #include "CountingBits.hpp" 2 | 3 | vector CountingBits::countBits(int num) { 4 | vector ret(num + 1, 0); 5 | 6 | if (num == 0) return ret; 7 | 8 | ret[1] = 1; 9 | 10 | for (int i = 2; i <= num; i++) 11 | ret[i] = (i & 0x1) ? ret[i / 2] + 1 : ret[i / 2]; 12 | 13 | return ret; 14 | } 15 | -------------------------------------------------------------------------------- /src/ReverseInteger.cpp: -------------------------------------------------------------------------------- 1 | #include "ReverseInteger.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int ReverseInteger::reverse(int x) { 8 | long res = 0; 9 | 10 | while (x) { 11 | res = res * 10 + x % 10; 12 | x /= 10; 13 | } 14 | 15 | return (res < INT_MIN || res > INT_MAX) ? 0 : res; 16 | } 17 | -------------------------------------------------------------------------------- /src/SingleNumberII.cpp: -------------------------------------------------------------------------------- 1 | #include "SingleNumberII.hpp" 2 | 3 | int SingleNumberII::singleNumber(vector &nums) { 4 | int a = 0, b = 0; 5 | for (auto c : nums) { 6 | int _a = (a & ~b & ~c) | (~a & b & c); 7 | int _b = (~a & b & ~c) | (~a & ~b & c); 8 | a = _a; 9 | b = _b; 10 | } 11 | return a | b; 12 | } -------------------------------------------------------------------------------- /tests/DistinctSubsequencesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "DistinctSubsequences.hpp" 4 | 5 | TEST_CASE("Distinct Subsequences") { 6 | DistinctSubsequences s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.numDistinct("rabbbit", "rabbit") == 3); 9 | REQUIRE(s.numDistinct("aabb", "ab") == 4); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/TwoSumIIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "TwoSumIII.hpp" 4 | 5 | TEST_CASE("Two Sum III - Data structure design") { 6 | TwoSumIII s; 7 | SECTION("Sample test") { 8 | s.add(1); 9 | s.add(3); 10 | s.add(5); 11 | REQUIRE(s.find(4)); 12 | REQUIRE_FALSE(s.find(7)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /include/BestTimeToBuyAndSellStock.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BEST_TIME_TO_BUY_AND_SELL_STOCK_HPP_ 2 | #define BEST_TIME_TO_BUY_AND_SELL_STOCK_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BestTimeToBuyAndSellStock { 9 | public: 10 | int maxProfit(vector &prices); 11 | }; 12 | 13 | #endif // BEST_TIME_TO_BUY_AND_SELL_STOCK_HPP_ 14 | -------------------------------------------------------------------------------- /include/ContainsDuplicateIII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONTAINS_DUPLICATE_III_HPP_ 2 | #define CONTAINS_DUPLICATE_III_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ContainsDuplicateIII { 9 | public: 10 | bool containsNearbyAlmostDuplicate(vector &nums, int k, int t); 11 | }; 12 | 13 | #endif // CONTAINS_DUPLICATE_III_HPP_ 14 | -------------------------------------------------------------------------------- /include/LongestConsecutiveSequence.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LONGEST_CONSECUTIVE_SEQUENCE_HPP_ 2 | #define LONGEST_CONSECUTIVE_SEQUENCE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class LongestConsecutiveSequence { 9 | public: 10 | int longestConsecutive(vector &nums); 11 | }; 12 | 13 | #endif // LONGEST_CONSECUTIVE_SEQUENCE_HPP_ 14 | -------------------------------------------------------------------------------- /include/MergeKSortedLists.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MERGE_K_SORTED_LISTS_HPP_ 2 | #define MERGE_K_SORTED_LISTS_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class MergeKSortedLists { 11 | public: 12 | ListNode *mergeKLists(vector &lists); 13 | }; 14 | 15 | #endif // MERGE_K_SORTED_LISTS_HPP_ 16 | -------------------------------------------------------------------------------- /include/ProductOfArrayExceptSelf.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PRODUCT_OF_ARRAY_EXCEPT_SELF_HPP_ 2 | #define PRODUCT_OF_ARRAY_EXCEPT_SELF_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ProductOfArrayExceptSelf { 9 | public: 10 | vector productExceptSelf(vector &nums); 11 | }; 12 | 13 | #endif // PRODUCT_OF_ARRAY_EXCEPT_SELF_HPP_ -------------------------------------------------------------------------------- /tests/SplitArrayLargestSumTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SplitArrayLargestSum.hpp" 4 | 5 | TEST_CASE("Split Array Largest Sum") { 6 | SplitArrayLargestSum s; 7 | SECTION("Sample test") { 8 | vector nums{7, 2, 5, 10, 8}; 9 | int m = 2; 10 | REQUIRE(s.splitArray(nums, m) == 18); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /include/FirstUniqueCharacterInAString.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIRST_UNIQUE_CHARACTER_IN_A_STRING_HPP_ 2 | #define FIRST_UNIQUE_CHARACTER_IN_A_STRING_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FirstUniqueCharacterInAString { 9 | public: 10 | int firstUniqChar(string s); 11 | }; 12 | 13 | #endif // FIRST_UNIQUE_CHARACTER_IN_A_STRING_HPP_ -------------------------------------------------------------------------------- /include/RemoveDuplicatesFromSortedList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_DUPLICATES_FROM_SORTED_LIST_HPP_ 2 | #define REMOVE_DUPLICATES_FROM_SORTED_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class RemoveDuplicatesFromSortedList { 7 | public: 8 | ListNode *deleteDuplicates(ListNode *head); 9 | }; 10 | 11 | #endif // REMOVE_DUPLICATES_FROM_SORTED_LIST_HPP_ 12 | -------------------------------------------------------------------------------- /include/RepeatedDNASequences.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REPEATED_DNA_SEQUENCES_HPP_ 2 | #define REPEATED_DNA_SEQUENCES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class RepeatedDNASequences { 10 | public: 11 | vector findRepeatedDnaSequences(string s); 12 | }; 13 | 14 | #endif // REPEATED_DNA_SEQUENCES_HPP_ 15 | -------------------------------------------------------------------------------- /src/RemoveDuplicatesFromSortedArray.cpp: -------------------------------------------------------------------------------- 1 | #include "RemoveDuplicatesFromSortedArray.hpp" 2 | 3 | int RemoveDuplicatesFromSortedArray::removeDuplicates(vector &nums) { 4 | int j = 0; 5 | 6 | for (int i = 0; i < nums.size(); i++) 7 | if (j == 0 || nums[i] != nums[j - 1]) 8 | nums[j++] = nums[i]; 9 | 10 | return j; 11 | } 12 | -------------------------------------------------------------------------------- /tests/MoveZeroesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MoveZeroes.hpp" 4 | 5 | TEST_CASE("Move Zeroes") { 6 | MoveZeroes s; 7 | SECTION("Sample test") { 8 | vector nums{0, 1, 0, 3, 12}; 9 | vector expected{1, 3, 12, 0, 0}; 10 | s.moveZeroes(nums); 11 | REQUIRE(nums == expected); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/ShortestPalindromeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ShortestPalindrome.hpp" 4 | 5 | TEST_CASE("Shortest Palindrome") { 6 | ShortestPalindrome s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.shortestPalindrome("aacecaaa") == "aaacecaaa"); 9 | REQUIRE(s.shortestPalindrome("abcd") == "dcbabcd"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/BasicCalculatorII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BASIC_CALCULATOR_II_HPP_ 2 | #define BASIC_CALCULATOR_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BasicCalculatorII { 9 | public: 10 | int calculate(string s); 11 | 12 | private: 13 | int calc(int left, int right, char op); 14 | }; 15 | 16 | #endif // BASIC_CALCULATOR_II_HPP_ 17 | -------------------------------------------------------------------------------- /include/SearchInRotatedSortedArray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_IN_ROTATED_SORTED_ARRAY_HPP_ 2 | #define SEARCH_IN_ROTATED_SORTED_ARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SearchInRotatedSortedArray { 9 | public: 10 | int search(vector &nums, int target); 11 | }; 12 | 13 | #endif // SEARCH_IN_ROTATED_SORTED_ARRAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/SumRootToLeafNumbers.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUM_ROOT_TO_LEAF_NUMBERS_HPP_ 2 | #define SUM_ROOT_TO_LEAF_NUMBERS_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class SumRootToLeafNumbers { 7 | public: 8 | int sumNumbers(TreeNode *root); 9 | 10 | private: 11 | int sumNumbersRec(TreeNode *root, int n); 12 | }; 13 | 14 | #endif // SUM_ROOT_TO_LEAF_NUMBERS_HPP_ 15 | -------------------------------------------------------------------------------- /src/CombinationSumIV.cpp: -------------------------------------------------------------------------------- 1 | #include "CombinationSumIV.hpp" 2 | 3 | int CombinationSumIV::combinationSum4(vector &nums, int target) { 4 | vector dp(target + 1, 0); 5 | dp[0] = 1; 6 | 7 | for (int i = 1; i <= target; i++) 8 | for (auto d : nums) 9 | if (d <= i) dp[i] += dp[i - d]; 10 | 11 | return dp[target]; 12 | } 13 | -------------------------------------------------------------------------------- /src/LinkedListCycle.cpp: -------------------------------------------------------------------------------- 1 | #include "LinkedListCycle.hpp" 2 | 3 | bool LinkedListCycle::hasCycle(ListNode *head) { 4 | ListNode *slow = head; 5 | ListNode *fast = head; 6 | while (fast && fast->next) { 7 | slow = slow->next; 8 | fast = fast->next->next; 9 | if (slow == fast) return true; 10 | } 11 | 12 | return false; 13 | } -------------------------------------------------------------------------------- /tests/JumpGameTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "JumpGame.hpp" 4 | 5 | TEST_CASE("Jump Game") { 6 | JumpGame s; 7 | SECTION("Sample tests") { 8 | vector nums_1{2, 3, 1, 1, 4}; 9 | REQUIRE(s.canJump(nums_1)); 10 | vector nums_2{3, 2, 1, 0, 4}; 11 | REQUIRE_FALSE(s.canJump(nums_2)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/LongestConsecutiveSequenceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "LongestConsecutiveSequence.hpp" 4 | 5 | TEST_CASE("Longest Consecutive Sequence") { 6 | LongestConsecutiveSequence s; 7 | SECTION("Sample test") { 8 | vector nums{100, 4, 200, 1, 3, 2}; 9 | REQUIRE(s.longestConsecutive(nums) == 4); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /include/BestTimeToBuyAndSellStockII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BEST_TIME_TO_BUY_AND_SELL_STOCK_II_HPP_ 2 | #define BEST_TIME_TO_BUY_AND_SELL_STOCK_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BestTimeToBuyAndSellStockII { 9 | public: 10 | int maxProfit(vector &prices); 11 | }; 12 | 13 | #endif // BEST_TIME_TO_BUY_AND_SELL_STOCK_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/CountUnivalueSubtrees.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNT_UNIVALUE_SUBTREES_HPP_ 2 | #define COUNT_UNIVALUE_SUBTREES_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class CountUnivalueSubtrees { 7 | public: 8 | int countUnivalSubtrees(TreeNode *root); 9 | 10 | private: 11 | bool isUnival(TreeNode *root, int &global); 12 | }; 13 | 14 | #endif // COUNT_UNIVALUE_SUBTREES_HPP_ -------------------------------------------------------------------------------- /include/IntersectionOfTwoLinkedLists.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INTERSECTION_OF_TWO_LINKED_LISTS_HPP_ 2 | #define INTERSECTION_OF_TWO_LINKED_LISTS_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class IntersectionOfTwoLinkedLists { 7 | public: 8 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB); 9 | }; 10 | 11 | #endif // INTERSECTION_OF_TWO_LINKED_LISTS_HPP_ 12 | -------------------------------------------------------------------------------- /include/KthLargestElementInAnArray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef KTH_LARGEST_ELEMENT_IN_AN_ARRAY_HPP_ 2 | #define KTH_LARGEST_ELEMENT_IN_AN_ARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class KthLargestElementInAnArray { 9 | public: 10 | int findKthLargest(vector &nums, int k); 11 | }; 12 | 13 | #endif // KTH_LARGEST_ELEMENT_IN_AN_ARRAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/LargestRectangleInHistogram.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LARGEST_RECTANGLE_IN_HISTOGRAM_HPP_ 2 | #define LARGEST_RECTANGLE_IN_HISTOGRAM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class LargestRectangleInHistogram { 9 | public: 10 | int largestRectangleArea(vector &height); 11 | }; 12 | 13 | #endif // LARGEST_RECTANGLE_IN_HISTOGRAM_HPP_ 14 | -------------------------------------------------------------------------------- /include/MinimumHeightTrees.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MINIMUM_HEIGHT_TREES_HPP_ 2 | #define MINIMUM_HEIGHT_TREES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class MinimumHeightTrees { 10 | public: 11 | vector findMinHeightTrees(int n, vector> &edges); 12 | }; 13 | 14 | #endif // MINIMUM_HEIGHT_TREES_HPP_ 15 | -------------------------------------------------------------------------------- /src/LengthOfLastWord.cpp: -------------------------------------------------------------------------------- 1 | #include "LengthOfLastWord.hpp" 2 | 3 | int LengthOfLastWord::lengthOfLastWord(string s) { 4 | int len = 0; 5 | 6 | for (int i = 0; i < s.size(); i++) { 7 | if (s[i] != ' ') 8 | len++; 9 | else if (i + 1 < s.size() && s[i + 1] != ' ') 10 | len = 0; 11 | } 12 | 13 | return len; 14 | } 15 | -------------------------------------------------------------------------------- /src/PalindromeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include "PalindromeNumber.hpp" 2 | 3 | bool PalindromeNumber::isPalindrome(int x) { 4 | if (x < 0) 5 | return false; 6 | 7 | int p = x; 8 | int q = 0; 9 | 10 | while (p >= 10) { 11 | q *= 10; 12 | q += p % 10; 13 | p /= 10; 14 | } 15 | 16 | return q == x / 10 && p == x % 10; 17 | } 18 | -------------------------------------------------------------------------------- /tests/HouseRobberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "HouseRobber.hpp" 4 | 5 | TEST_CASE("House Robber") { 6 | HouseRobber s; 7 | SECTION("Normal tests") { 8 | vector nums1{1, 2, 3, 4}; 9 | REQUIRE(s.rob(nums1) == 6); 10 | vector nums2{9, 1, 9, 9, 1, 9}; 11 | REQUIRE(s.rob(nums2) == 27); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/InterleavingStringTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "InterleavingString.hpp" 4 | 5 | TEST_CASE("Interleaving String") { 6 | InterleavingString s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.isInterleave("aabcc", "dbbca", "aadbbcbcac")); 9 | REQUIRE_FALSE(s.isInterleave("aabcc", "dbbca", "aadbbbaccc")); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/RemoveDuplicateLettersTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "RemoveDuplicateLetters.hpp" 4 | 5 | TEST_CASE("Remove Duplicate Letters") { 6 | RemoveDuplicateLetters s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.removeDuplicateLetters("bcabc") == "abc"); 9 | REQUIRE(s.removeDuplicateLetters("cbacdcbc") == "acdb"); 10 | } 11 | } -------------------------------------------------------------------------------- /include/BestTimeToBuyAndSellStockIII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BEST_TIME_TO_BUY_AND_SELL_STOCK_III_HPP_ 2 | #define BEST_TIME_TO_BUY_AND_SELL_STOCK_III_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BestTimeToBuyAndSellStockIII { 9 | public: 10 | int maxProfit(vector &prices); 11 | }; 12 | 13 | #endif // BEST_TIME_TO_BUY_AND_SELL_STOCK_III_HPP_ 14 | -------------------------------------------------------------------------------- /include/BestTimeToBuyAndSellStockIV.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BEST_TIME_TO_BUY_AND_SELL_STOCK_IV_HPP_ 2 | #define BEST_TIME_TO_BUY_AND_SELL_STOCK_IV_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BestTimeToBuyAndSellStockIV { 9 | public: 10 | int maxProfit(int k, vector &prices); 11 | }; 12 | 13 | #endif // BEST_TIME_TO_BUY_AND_SELL_STOCK_IV_HPP_ 14 | -------------------------------------------------------------------------------- /include/FractionToRecurringDecimal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FRACTION_TO_RECURRING_DECIMAL_HPP_ 2 | #define FRACTION_TO_RECURRING_DECIMAL_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FractionToRecurringDecimal { 9 | public: 10 | string fractionToDecimal(int numerator, int denominator); 11 | }; 12 | 13 | #endif // FRACTION_TO_RECURRING_DECIMAL_HPP_ 14 | -------------------------------------------------------------------------------- /include/HIndex.hpp: -------------------------------------------------------------------------------- 1 | #ifndef H_INDEX_HPP_ 2 | #define H_INDEX_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class HIndex { 9 | public: 10 | int hIndex(vector &citations); 11 | 12 | private: 13 | int hIndexSort(vector &citations); 14 | 15 | int hIndexHashTable(vector &citations); 16 | }; 17 | 18 | #endif // H_INDEX_HPP_ 19 | -------------------------------------------------------------------------------- /include/SearchinRotatedSortedArrayII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_IN_ROTATED_SORTED_ARRAY_II_HPP_ 2 | #define SEARCH_IN_ROTATED_SORTED_ARRAY_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SearchinRotatedSortedArrayII { 9 | public: 10 | bool search(vector &nums, int target); 11 | }; 12 | 13 | #endif // SEARCH_IN_ROTATED_SORTED_ARRAY_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/TargetSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TARGET_SUM_HPP_ 2 | #define TARGET_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class TargetSum { 9 | public: 10 | int findTargetSumWays(vector& nums, int S); 11 | 12 | private: 13 | void dfs(vector& nums, int S, int depth, int local, int& global); 14 | }; 15 | 16 | #endif // TARGET_SUM_HPP_ 17 | -------------------------------------------------------------------------------- /src/SameTree.cpp: -------------------------------------------------------------------------------- 1 | #include "SameTree.hpp" 2 | 3 | bool SameTree::isSameTree(TreeNode *p, TreeNode *q) { 4 | if (p == nullptr && q == nullptr) 5 | return true; 6 | 7 | if (p == nullptr || q == nullptr) 8 | return false; 9 | 10 | return (p->val == q->val) && isSameTree(p->left, q->left) && 11 | isSameTree(p->right, q->right); 12 | } 13 | -------------------------------------------------------------------------------- /tests/FindMinimumInRotatedSortedArrayTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FindMinimumInRotatedSortedArray.hpp" 4 | 5 | TEST_CASE("Find Minimum in Rotated Sorted Array") { 6 | FindMinimumInRotatedSortedArray s; 7 | SECTION("Sample test") { 8 | vector nums{4, 5, 6, 7, 0, 1, 2}; 9 | REQUIRE(s.findMin(nums) == 0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/RotateArrayTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "RotateArray.hpp" 4 | 5 | TEST_CASE("Rotate Array") { 6 | RotateArray s; 7 | SECTION("Sample test") { 8 | vector nums{1, 2, 3, 4, 5, 6, 7}; 9 | vector expected{5, 6, 7, 1, 2, 3, 4}; 10 | s.rotate(nums, 3); 11 | REQUIRE(nums == expected); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /include/FindMinimumInRotatedSortedArray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_MINIMUM_IN_ROTATED_SORTED_ARRAY_HPP_ 2 | #define FIND_MINIMUM_IN_ROTATED_SORTED_ARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FindMinimumInRotatedSortedArray { 9 | public: 10 | int findMin(vector &nums); 11 | }; 12 | 13 | #endif // FIND_MINIMUM_IN_ROTATED_SORTED_ARRAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/IntersectionOfTwoArraysII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INTERSECTION_OF_TWO_ARRAYS_II_HPP_ 2 | #define INTERSECTION_OF_TWO_ARRAYS_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class IntersectionOfTwoArraysII { 9 | public: 10 | vector intersection(vector &nums1, vector &nums2); 11 | }; 12 | 13 | #endif // INTERSECTION_OF_TWO_ARRAYS_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/MaximumProductOfWordLengths.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMUM_PRODUCT_OF_WORD_LENGTHS_HPP_ 2 | #define MAXIMUM_PRODUCT_OF_WORD_LENGTHS_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class MaximumProductOfWordLengths { 10 | public: 11 | int maxProduct(vector &words); 12 | }; 13 | 14 | #endif // MAXIMUM_PRODUCT_OF_WORD_LENGTHS_HPP_ -------------------------------------------------------------------------------- /include/MaximumSizeSubarraySumEqualsK.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMUM_SIZE_SUBARRAY_SUM_EQUALS_K_HPP_ 2 | #define MAXIMUM_SIZE_SUBARRAY_SUM_EQUALS_K_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MaximumSizeSubarraySumEqualsK { 9 | public: 10 | int maxSubArrayLen(vector &nums, int k); 11 | }; 12 | 13 | #endif // MAXIMUM_SIZE_SUBARRAY_SUM_EQUALS_K_HPP_ -------------------------------------------------------------------------------- /include/SimplifyPath.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLIFY_PATH_HPP_ 2 | #define SIMPLIFY_PATH_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class SimplifyPath { 10 | public: 11 | string simplifyPath(string path); 12 | 13 | private: 14 | void splitPath(string &path, vector &dirs); 15 | }; 16 | 17 | #endif // SIMPLIFY_PATH_HPP_ 18 | -------------------------------------------------------------------------------- /include/ValidNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VALID_NUMBER_HPP_ 2 | #define VALID_NUMBER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ValidNumber { 9 | public: 10 | bool isNumber(string s); 11 | 12 | private: 13 | bool isNumber_Normal(string s); 14 | 15 | bool isNumber_StateMachine(const char *s); 16 | }; 17 | 18 | #endif // VALID_NUMBER_HPP_ 19 | -------------------------------------------------------------------------------- /tests/HouseRobberIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "HouseRobberII.hpp" 4 | 5 | TEST_CASE("House Robber II") { 6 | HouseRobberII s; 7 | SECTION("Normal tests") { 8 | vector nums1{1, 2, 3, 4}; 9 | REQUIRE(s.rob(nums1) == 6); 10 | vector nums2{9, 1, 9, 9, 1, 9}; 11 | REQUIRE(s.rob(nums2) == 19); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /include/GroupAnagrams.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GROUP_ANAGRAMS_HPP_ 2 | #define GROUP_ANAGRAMS_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class GroupAnagrams { 10 | public: 11 | vector> groupAnagrams(vector &strs); 12 | 13 | private: 14 | string strSort(string &s); 15 | }; 16 | 17 | #endif // GROUP_ANAGRAMS_HPP_ 18 | -------------------------------------------------------------------------------- /include/LargestNumber.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LARGEST_NUMBER_HPP_ 2 | #define LARGEST_NUMBER_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class LargestNumber { 10 | public: 11 | string largestNumber(vector &nums); 12 | 13 | private: 14 | string largestNumber(vector &nums); 15 | }; 16 | 17 | #endif // LARGEST_NUMBER_HPP_ 18 | -------------------------------------------------------------------------------- /include/RemoveDuplicatesFromSortedArray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_DUPLICATES_FROM_SORTED_ARRAY_HPP_ 2 | #define REMOVE_DUPLICATES_FROM_SORTED_ARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RemoveDuplicatesFromSortedArray { 9 | public: 10 | int removeDuplicates(vector &nums); 11 | }; 12 | 13 | #endif // REMOVE_DUPLICATES_FROM_SORTED_ARRAY_HPP_ 14 | -------------------------------------------------------------------------------- /include/Subsets.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUBSETS_HPP_ 2 | #define SUBSETS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Subsets { 9 | public: 10 | vector> subsets(vector &nums); 11 | 12 | private: 13 | void backtrack(vector &nums, int begin, vector &local, vector> &global); 14 | }; 15 | 16 | #endif // SUBSETS_HPP_ 17 | -------------------------------------------------------------------------------- /include/SymmetricTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SYMMETRIC_TREE_HPP_ 2 | #define SYMMETRIC_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class SymmetricTree { 7 | public: 8 | bool isSymmetric(TreeNode *root); 9 | 10 | private: 11 | bool isSymmetricHelper(TreeNode *l, TreeNode *r); 12 | 13 | bool isSymmetricIter(TreeNode *root); 14 | }; 15 | 16 | #endif // SYMMETRIC_TREE_HPP_ 17 | -------------------------------------------------------------------------------- /src/MoveZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include "MoveZeroes.hpp" 2 | 3 | void MoveZeroes::moveZeroes(vector &nums) { 4 | int tail = 0; 5 | 6 | for (int i = 0; i < nums.size(); i++) { 7 | if (nums[i] != 0) { 8 | nums[tail] = nums[i]; 9 | tail++; 10 | } 11 | } 12 | 13 | for (; tail < nums.size(); tail++) 14 | nums[tail] = 0; 15 | } 16 | -------------------------------------------------------------------------------- /tests/FindMinimumInRotatedSortedArrayIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FindMinimumInRotatedSortedArrayII.hpp" 4 | 5 | TEST_CASE("Find Minimum in Rotated Sorted Array II") { 6 | FindMinimumInRotatedSortedArrayII s; 7 | SECTION("Sample test") { 8 | vector nums{4, 6, 6, 7, 0, 1, 2}; 9 | REQUIRE(s.findMin(nums) == 0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/FirstUniqueCharacterInAStringTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FirstUniqueCharacterInAString.hpp" 4 | 5 | TEST_CASE("First Unique Character in a String") { 6 | FirstUniqueCharacterInAString s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.firstUniqChar("leetcode") == 0); 9 | REQUIRE(s.firstUniqChar("loveleetcode") == 2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/IsomorphicStringsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "IsomorphicStrings.hpp" 4 | 5 | TEST_CASE("Isomorphic Strings") { 6 | IsomorphicStrings s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.isIsomorphic("egg", "add")); 9 | REQUIRE_FALSE(s.isIsomorphic("foo", "bar")); 10 | REQUIRE(s.isIsomorphic("paper", "title")); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/KthLargestElementInAnArrayTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "KthLargestElementInAnArray.hpp" 4 | 5 | TEST_CASE("Kth Largest Element in an Array") { 6 | KthLargestElementInAnArray s; 7 | SECTION("Sample test") { 8 | vector nums{3, 2, 1, 5, 6, 4}; 9 | int k = 2; 10 | REQUIRE(s.findKthLargest(nums, k) == 5); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /include/MaximalRectangle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMAL_RECTANGLE_HPP_ 2 | #define MAXIMAL_RECTANGLE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MaximalRectangle { 9 | public: 10 | int maximalRectangle(vector> &matrix); 11 | 12 | private: 13 | int largestRectangleArea(vector &heights); 14 | }; 15 | 16 | #endif // MAXIMAL_RECTANGLE_HPP_ 17 | -------------------------------------------------------------------------------- /include/NQueensII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NQUEENS_II_HPP_ 2 | #define NQUEENS_II_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class NQueensII { 10 | public: 11 | int totalNQueens(int n); 12 | 13 | private: 14 | void backtrack(int n, int depth, vector &used, vector &cur, int &result); 15 | }; 16 | 17 | #endif // NQUEENS_II_HPP_ 18 | -------------------------------------------------------------------------------- /src/SortColors.cpp: -------------------------------------------------------------------------------- 1 | #include "SortColors.hpp" 2 | 3 | void SortColors::sortColors(vector &nums) { 4 | int i = 0, j = 0, k = nums.size() - 1; 5 | 6 | while (j <= k) { 7 | if (nums[j] < 1) 8 | swap(nums[i++], nums[j++]); 9 | else if (nums[j] > 1) 10 | swap(nums[j], nums[k--]); 11 | else 12 | j++; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/ExcelSheetColumnTitleTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ExcelSheetColumnTitle.hpp" 4 | 5 | TEST_CASE("Excel Sheet Column Title") { 6 | ExcelSheetColumnTitle s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.convertToTitle(1) == "A"); 9 | REQUIRE(s.convertToTitle(26) == "Z"); 10 | REQUIRE(s.convertToTitle(27) == "AA"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/SlidingWindowMaximumTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SlidingWindowMaximum.hpp" 4 | 5 | TEST_CASE("Sliding Window Maximum") { 6 | SlidingWindowMaximum s; 7 | SECTION("Sample test") { 8 | vector nums{1, 3, 1, 2, 0, 5}; 9 | vector expected{3, 3, 2, 5}; 10 | REQUIRE(s.maxSlidingWindow(nums, 3) == expected); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /include/BinaryTreeMaximumPathSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_MAXIMUM_PATH_SUM_HPP_ 2 | #define BINARY_TREE_MAXIMUM_PATH_SUM_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class BinaryTreeMaximumPathSum { 7 | public: 8 | int maxPathSum(TreeNode *root); 9 | 10 | private: 11 | int maxPathDown(TreeNode *root, int &maxValue); 12 | }; 13 | 14 | #endif // BINARY_TREE_MAXIMUM_PATH_SUM_HPP_ 15 | -------------------------------------------------------------------------------- /include/DifferentWaysToAddParentheses.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DIFFERENT_WAYS_TO_ADD_PARENTHESES_HPP_ 2 | #define DIFFERENT_WAYS_TO_ADD_PARENTHESES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class DifferentWaysToAddParentheses { 10 | public: 11 | vector diffWaysToCompute(string input); 12 | }; 13 | 14 | #endif // DIFFERENT_WAYS_TO_ADD_PARENTHESES_HPP_ -------------------------------------------------------------------------------- /include/FindMinimumInRotatedSortedArrayII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_MINIMUM_IN_ROTATED_SORTED_ARRAY_II_HPP_ 2 | #define FIND_MINIMUM_IN_ROTATED_SORTED_ARRAY_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FindMinimumInRotatedSortedArrayII { 9 | public: 10 | int findMin(vector &nums); 11 | }; 12 | 13 | #endif // FIND_MINIMUM_IN_ROTATED_SORTED_ARRAY_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/SumOfLeftLeaves.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUM_OF_LEFT_LEAVES_HPP_ 2 | #define SUM_OF_LEFT_LEAVES_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class SumOfLeftLeaves { 7 | public: 8 | int sumOfLeftLeaves(TreeNode *root); 9 | private: 10 | int sumOfLeftLeaves_Recursive(TreeNode *root); 11 | int sumOfLeftLeaves_Iterative(TreeNode *root); 12 | }; 13 | 14 | #endif // SUM_OF_LEFT_LEAVES_HPP_ -------------------------------------------------------------------------------- /src/PathSum.cpp: -------------------------------------------------------------------------------- 1 | #include "PathSum.hpp" 2 | 3 | bool PathSum::hasPathSum(TreeNode *root, int sum) { 4 | if (root == nullptr) 5 | return false; 6 | 7 | if (root->val == sum && root->left == nullptr && root->right == nullptr) 8 | return true; 9 | 10 | return hasPathSum(root->left, sum - root->val) || 11 | hasPathSum(root->right, sum - root->val); 12 | } 13 | -------------------------------------------------------------------------------- /src/PerfectSquares.cpp: -------------------------------------------------------------------------------- 1 | #include "PerfectSquares.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int PerfectSquares::numSquares(int n) { 8 | vector dp(n + 1, n); 9 | 10 | dp[0] = 0; 11 | for (int i = 1; i <= n; i++) 12 | for (int j = 1; j * j <= i; j++) 13 | dp[i] = min(dp[i], dp[i - j * j] + 1); 14 | 15 | return dp[n]; 16 | } 17 | -------------------------------------------------------------------------------- /tests/BestTimeToBuyAndSellStockTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BestTimeToBuyAndSellStock.hpp" 4 | 5 | TEST_CASE("Best Time to Buy and Sell Stock") { 6 | BestTimeToBuyAndSellStock s; 7 | SECTION("Normal test") { 8 | vector prices{10, 11, 7, 10, 6}; 9 | // buy at 7 and sell at 10 10 | REQUIRE(s.maxProfit(prices) == 3); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/ProductOfArrayExceptSelfTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ProductOfArrayExceptSelf.hpp" 4 | 5 | TEST_CASE("Product of Array Except Self") { 6 | ProductOfArrayExceptSelf s; 7 | SECTION("Sample test") { 8 | vector nums{0, 4, 0}; 9 | vector expected{0, 0, 0}; 10 | REQUIRE(s.productExceptSelf(nums) == expected); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /include/Combinations.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMBINATIONS_HPP_ 2 | #define COMBINATIONS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Combinations { 9 | public: 10 | vector> combine(int n, int k); 11 | 12 | private: 13 | void backtrack(int n, int k, int depth, vector &cur, vector> &result); 14 | }; 15 | 16 | #endif // COMBINATIONS_HPP_ 17 | -------------------------------------------------------------------------------- /include/MaxSumOfRectangleNoLargerThanK.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAX_SUM_OF_RECTANGLE_NO_LARGER_THAN_K_HPP_ 2 | #define MAX_SUM_OF_RECTANGLE_NO_LARGER_THAN_K_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MaxSumOfRectangleNoLargerThanK { 9 | public: 10 | int maxSumSubmatrix(vector> &matrix, int k); 11 | }; 12 | 13 | #endif // MAX_SUM_OF_RECTANGLE_NO_LARGER_THAN_K_HPP_ -------------------------------------------------------------------------------- /include/MinStack.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MIN_STACK_HPP_ 2 | #define MIN_STACK_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MinStack { 9 | public: 10 | void push(int x); 11 | 12 | void pop(); 13 | 14 | int top(); 15 | 16 | int getMin(); 17 | 18 | private: 19 | stack wholeStack; 20 | stack minStack; 21 | }; 22 | 23 | #endif // MIN_STACK_HPP_ 24 | -------------------------------------------------------------------------------- /include/NumberOfIslands.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NUMBER_OF_ISLANDS_HPP_ 2 | #define NUMBER_OF_ISLANDS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class NumberOfIslands { 9 | public: 10 | int numIslands(vector> &grid); 11 | 12 | private: 13 | void bfs(vector> &grid, int i, int j, int m, int n); 14 | }; 15 | 16 | #endif // NUMBER_OF_ISLANDS_HPP_ 17 | -------------------------------------------------------------------------------- /include/RangeSumQueryImmutable.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RANGE_SUM_QUERY_IMMUTABLE_HPP_ 2 | #define RANGE_SUM_QUERY_IMMUTABLE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class NumArray { 9 | public: 10 | NumArray(vector &nums); 11 | 12 | int sumRange(int i, int j); 13 | 14 | private: 15 | vector accs; 16 | }; 17 | 18 | #endif // RANGE_SUM_QUERY_IMMUTABLE_HPP_ 19 | -------------------------------------------------------------------------------- /include/RemoveDuplicatesFromSortedArrayII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_DUPLICATES_FROM_SORTED_ARRAY_II_HPP_ 2 | #define REMOVE_DUPLICATES_FROM_SORTED_ARRAY_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RemoveDuplicatesFromSortedArrayII { 9 | public: 10 | int removeDuplicates(vector &nums); 11 | }; 12 | 13 | #endif // REMOVE_DUPLICATES_FROM_SORTED_ARRAY_II_HPP_ 14 | -------------------------------------------------------------------------------- /include/ReverseWordsInAStringII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REVERSE_WORDS_IN_A_STRING_II_HPP_ 2 | #define REVERSE_WORDS_IN_A_STRING_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ReverseWordsInAStringII { 9 | public: 10 | void reverseWords(string &s); 11 | 12 | private: 13 | void reverse(string &s, int i, int j); 14 | }; 15 | 16 | #endif // REVERSE_WORDS_IN_A_STRING_II_HPP_ -------------------------------------------------------------------------------- /src/ContainsDuplicate.cpp: -------------------------------------------------------------------------------- 1 | #include "ContainsDuplicate.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | bool ContainsDuplicate::containsDuplicate(vector &nums) { 8 | unordered_set records; 9 | 10 | for (auto d : nums) { 11 | if (records.count(d)) return true; 12 | 13 | records.insert(d); 14 | } 15 | 16 | return false; 17 | } 18 | -------------------------------------------------------------------------------- /src/TwoSumIII.cpp: -------------------------------------------------------------------------------- 1 | #include "TwoSumIII.hpp" 2 | 3 | void TwoSumIII::add(int number) { 4 | lookup[number]++; 5 | } 6 | 7 | bool TwoSumIII::find(int value) { 8 | for (auto p : lookup) { 9 | int gap = value - p.first; 10 | if (lookup.find(gap) == lookup.end()) continue; 11 | if (gap != p.first || lookup[gap] > 1) return true; 12 | } 13 | return false; 14 | } 15 | -------------------------------------------------------------------------------- /tests/ContainsDuplicateIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ContainsDuplicateII.hpp" 4 | 5 | TEST_CASE("Contains Duplicate II") { 6 | ContainsDuplicateII s; 7 | SECTION("Sample test") { 8 | vector nums{1, 0, -1, 0, -2, 2}; 9 | REQUIRE_FALSE(s.containsNearbyDuplicate(nums, 1)); 10 | REQUIRE(s.containsNearbyDuplicate(nums, 2)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/TopKFrequentElementsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "TopKFrequentElements.hpp" 4 | 5 | TEST_CASE("Top K Frequent Elements") { 6 | TopKFrequentElements s; 7 | SECTION("Sample test") { 8 | vector nums{1, 1, 1, 2, 2, 3}; 9 | int k = 2; 10 | vector expected{1, 2}; 11 | REQUIRE(s.topKFrequent(nums, k) == expected); 12 | } 13 | } -------------------------------------------------------------------------------- /include/FirstBadVersion.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIRST_BAD_VERSION_HPP_ 2 | #define FIRST_BAD_VERSION_HPP_ 3 | 4 | class FirstBadVersion { 5 | public: 6 | typedef bool (*VersionChecker)(int); 7 | 8 | public: 9 | FirstBadVersion(VersionChecker checker); 10 | 11 | int firstBadVersion(int n); 12 | 13 | private: 14 | VersionChecker isBadVersion; 15 | }; 16 | 17 | #endif // FIRST_BAD_VERSION_HPP_ 18 | -------------------------------------------------------------------------------- /include/RegularExpressionMatching.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REGULAR_EXPRESSION_MATCHING_HPP_ 2 | #define REGULAR_EXPRESSION_MATCHING_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class RegularExpressionMatching { 9 | public: 10 | bool isMatch(string s, string p); 11 | 12 | private: 13 | bool isMatch(char _s, char _p); 14 | }; 15 | 16 | #endif // REGULAR_EXPRESSION_MATCHING_HPP_ 17 | -------------------------------------------------------------------------------- /include/RotateList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ROTATE_LIST_HPP_ 2 | #define ROTATE_LIST_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class RotateList { 7 | public: 8 | ListNode *rotateRight(ListNode *head, int k); 9 | 10 | private: 11 | ListNode *rotateRight_Cycle(ListNode *head, int k); 12 | 13 | ListNode *rotateRight_TwoPointers(ListNode *head, int k); 14 | }; 15 | 16 | #endif // ROTATE_LIST_HPP_ 17 | -------------------------------------------------------------------------------- /include/SummaryRanges.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUMMARY_RANGES_HPP_ 2 | #define SUMMARY_RANGES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class SummaryRanges { 10 | public: 11 | vector summaryRanges(vector &nums); 12 | 13 | private: 14 | void addRange(int start, int end, vector &res); 15 | }; 16 | 17 | #endif // SUMMARY_RANGES_HPP_ 18 | -------------------------------------------------------------------------------- /include/SurroundedRegions.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SURROUNDED_REGIONS_HPP_ 2 | #define SURROUNDED_REGIONS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SurroundedRegions { 9 | public: 10 | void solve(vector> &board); 11 | 12 | private: 13 | void bfs(vector> &board, int i, int j, int m, int n); 14 | }; 15 | 16 | #endif // SURROUNDED_REGIONS_HPP_ 17 | -------------------------------------------------------------------------------- /src/BitwiseANDOfNumbersRange.cpp: -------------------------------------------------------------------------------- 1 | #include "BitwiseANDOfNumbersRange.hpp" 2 | 3 | int BitwiseANDOfNumbersRange::rangeBitwiseAnd(int m, int n) { 4 | int result = 0; 5 | int base = 1; 6 | 7 | while (m != 0) { 8 | if (m == n && m % 2) 9 | result += base; 10 | 11 | m /= 2; 12 | n /= 2; 13 | base *= 2; 14 | } 15 | 16 | return result; 17 | } 18 | -------------------------------------------------------------------------------- /src/FindPeakElement.cpp: -------------------------------------------------------------------------------- 1 | #include "FindPeakElement.hpp" 2 | 3 | int FindPeakElement::findPeakElement(vector &nums) { 4 | int l = 0, r = nums.size() - 1; 5 | 6 | while (l + 1 < r) { 7 | int m = l + (r - l) / 2; 8 | if (nums[m - 1] > nums[m]) 9 | r = m; 10 | else 11 | l = m; 12 | } 13 | 14 | return nums[l] < nums[r] ? r : l; 15 | } 16 | -------------------------------------------------------------------------------- /src/RotateImage.cpp: -------------------------------------------------------------------------------- 1 | #include "RotateImage.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | void RotateImage::rotate(vector> &matrix) { 8 | reverse(matrix.begin(), matrix.end()); 9 | 10 | for (int i = 0; i < matrix.size(); i++) { 11 | for (int j = i + 1; j < matrix.size(); j++) 12 | swap(matrix[i][j], matrix[j][i]); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/PalindromePermutationTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PalindromePermutation.hpp" 4 | 5 | TEST_CASE("Palindrome Permutation") { 6 | PalindromePermutation s; 7 | SECTION("Sample tests") { 8 | REQUIRE_FALSE(s.canPermutePalindrome("code")); 9 | REQUIRE(s.canPermutePalindrome("aab")); 10 | REQUIRE(s.canPermutePalindrome("carerac")); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/ZigZagConversionTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ZigZagConversion.hpp" 4 | 5 | TEST_CASE("ZigZag Conversion") { 6 | ZigZagConversion s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.convert("PAYPALISHIRING", 3) == "PAHNAPLSIIGYIR"); 9 | } 10 | SECTION("Single row") { 11 | REQUIRE(s.convert("PAYPALISHIRING", 1) == "PAYPALISHIRING"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /include/KthSmallestElementInASortedMatrix.hpp: -------------------------------------------------------------------------------- 1 | #ifndef KTH_SMALLEST_ELEMENT_IN_A_SORTED_MATRIX_HPP_ 2 | #define KTH_SMALLEST_ELEMENT_IN_A_SORTED_MATRIX_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class KthSmallestElementInASortedMatrix { 9 | public: 10 | int kthSmallest(vector> &matrix, int k); 11 | }; 12 | 13 | #endif // KTH_SMALLEST_ELEMENT_IN_A_SORTED_MATRIX_HPP_ -------------------------------------------------------------------------------- /include/SplitArrayLargestSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SPLIT_ARRAY_LARGEST_SUM_HPP_ 2 | #define SPLIT_ARRAY_LARGEST_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SplitArrayLargestSum { 9 | public: 10 | int splitArray(vector &nums, int m); 11 | 12 | private: 13 | bool canSplit(vector &nums, int m, long long bucket); 14 | }; 15 | 16 | #endif // SPLIT_ARRAY_LARGEST_SUM_HPP_ -------------------------------------------------------------------------------- /src/UniqueBinarySearchTrees.cpp: -------------------------------------------------------------------------------- 1 | #include "UniqueBinarySearchTrees.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int UniqueBinarySearchTrees::numTrees(int n) { 8 | vector dp(n + 1, 0); 9 | dp[0] = 1; 10 | 11 | for (int i = 1; i <= n; i++) 12 | for (int j = 1; j <= i; j++) 13 | dp[i] += dp[j - 1] * dp[i - j]; 14 | 15 | return dp[n]; 16 | } 17 | -------------------------------------------------------------------------------- /tests/SummaryRangesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SummaryRanges.hpp" 4 | 5 | TEST_CASE("Summary Ranges") { 6 | SummaryRanges s; 7 | SECTION("Sample test") { 8 | vector nums{0, 1, 2, 4, 5, 7}; 9 | vector expected{"0->2", "4->5", "7"}; 10 | vector result = s.summaryRanges(nums); 11 | REQUIRE(expected == result); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /include/InvertBinaryTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INVERT_BINARY_TREE_HPP_ 2 | #define INVERT_BINARY_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class InvertBinaryTree { 7 | public: 8 | TreeNode *invertTree(TreeNode *root); 9 | 10 | private: 11 | TreeNode *invertTreeRecursive(TreeNode *root); 12 | 13 | TreeNode *invertTreeIterative(TreeNode *root); 14 | }; 15 | 16 | #endif // INVERT_BINARY_TREE_HPP_ 17 | -------------------------------------------------------------------------------- /include/NQueens.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NQUEENS_HPP_ 2 | #define NQUEENS_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class NQueens { 10 | public: 11 | vector> solveNQueens(int n); 12 | 13 | private: 14 | void backtrack(int n, int depth, vector &used, vector &cur, vector> &result); 15 | }; 16 | 17 | #endif // NQUEENS_HPP_ 18 | -------------------------------------------------------------------------------- /src/IntegerBreak.cpp: -------------------------------------------------------------------------------- 1 | #include "IntegerBreak.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int IntegerBreak::integerBreak(int n) { 8 | vector dp(n + 1, 0); 9 | dp[1] = 1; 10 | 11 | for (int i = 2; i <= n; i++) { 12 | for (int k = 1; k <= i - 1; k++) 13 | dp[i] = max(dp[i], k * max(i - k, dp[i - k])); 14 | } 15 | 16 | return dp[n]; 17 | } 18 | -------------------------------------------------------------------------------- /tests/DungeonGameTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "DungeonGame.hpp" 4 | 5 | TEST_CASE("Dungeon Game") { 6 | DungeonGame s; 7 | SECTION("Sample test") { 8 | vector> dungeon{ 9 | {-2, -3, 3}, 10 | {-5, -10, 1}, 11 | {10, 30, -5} 12 | }; 13 | REQUIRE(s.calculateMinimumHP(dungeon) == 7); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/TriangleTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "Triangle.hpp" 4 | 5 | TEST_CASE("Triangle") { 6 | Triangle s; 7 | SECTION("Sample test") { 8 | vector> triangle{ 9 | {2}, 10 | {3, 4}, 11 | {6, 5, 7}, 12 | {4, 1, 8, 3} 13 | }; 14 | REQUIRE(s.minimumTotal(triangle) == 11); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /include/BinaryTreeLevelOrderTraversal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_LEVEL_ORDER_TRAVERSAL_HPP_ 2 | #define BINARY_TREE_LEVEL_ORDER_TRAVERSAL_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | #include "TreeNode.hpp" 9 | 10 | class BinaryTreeLevelOrderTraversal { 11 | public: 12 | vector> levelOrder(TreeNode *root); 13 | }; 14 | 15 | #endif // BINARY_TREE_LEVEL_ORDER_TRAVERSAL_HPP_ 16 | -------------------------------------------------------------------------------- /tests/MaximumGapTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MaximumGap.hpp" 4 | 5 | TEST_CASE("Maximum Gap") { 6 | MaximumGap s; 7 | SECTION("Sample test") { 8 | vector nums{1, 7, 3, 3}; 9 | REQUIRE(s.maximumGap(nums) == 4); 10 | } 11 | SECTION("Normal test") { 12 | vector nums{1, 10000000}; 13 | REQUIRE(s.maximumGap(nums) == 9999999); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /include/BestTimeToBuyAndSellStockWithCooldown.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BEST_TIME_TO_BUY_AND_SELL_STOCK_WITH_COOLDOWN_HPP_ 2 | #define BEST_TIME_TO_BUY_AND_SELL_STOCK_WITH_COOLDOWN_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class BestTimeToBuyAndSellStockWithCooldown { 9 | public: 10 | int maxProfit(vector &prices); 11 | }; 12 | 13 | #endif // BEST_TIME_TO_BUY_AND_SELL_STOCK_WITH_COOLDOWN_HPP_ 14 | -------------------------------------------------------------------------------- /include/GameOfLife.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GAME_OF_LIFE_HPP_ 2 | #define GAME_OF_LIFE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class GameOfLife { 9 | public: 10 | void gameOfLife(vector> &board); 11 | 12 | private: 13 | int count(vector> &board, int i, int j); 14 | 15 | bool isLive(vector> &board, int i, int j); 16 | }; 17 | 18 | #endif // GAME_OF_LIFE_HPP_ -------------------------------------------------------------------------------- /include/GuessNumberHigherOrLower.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GUESS_NUMBER_HIGHER_OR_LOWER_HPP_ 2 | #define GUESS_NUMBER_HIGHER_OR_LOWER_HPP_ 3 | 4 | class GuessNumberHigherOrLower { 5 | public: 6 | typedef int (*GuessHint)(int); 7 | 8 | public: 9 | GuessNumberHigherOrLower(GuessHint h); 10 | 11 | int guessNumber(int n); 12 | 13 | private: 14 | GuessHint guess; 15 | }; 16 | 17 | #endif // GUESS_NUMBER_HIGHER_OR_LOWER_HPP_ -------------------------------------------------------------------------------- /include/MissingRanges.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MISSING_RANGES_HPP_ 2 | #define MISSING_RANGES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class MissingRanges { 10 | public: 11 | vector findMissingRanges(vector &nums, int lower, int upper); 12 | 13 | private: 14 | void addRange(int left, int right, vector &res); 15 | }; 16 | 17 | #endif // MISSING_RANGES_HPP_ -------------------------------------------------------------------------------- /include/PalindromePairs.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PALINDROME_PAIRS_HPP_ 2 | #define PALINDROME_PAIRS_HPP_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class PalindromePairs { 11 | public: 12 | vector> palindromePairs(vector &words); 13 | 14 | private: 15 | bool isPalindrome(string str); 16 | }; 17 | 18 | #endif // PALINDROME_PAIRS_HPP_ -------------------------------------------------------------------------------- /src/AddBinary.cpp: -------------------------------------------------------------------------------- 1 | #include "AddBinary.hpp" 2 | 3 | string AddBinary::addBinary(string a, string b) { 4 | string s = ""; 5 | int c = 0, i = a.size() - 1, j = b.size() - 1; 6 | 7 | while (i >= 0 || j >= 0 || c == 1) { 8 | c += i >= 0 ? a[i--] - '0' : 0; 9 | c += j >= 0 ? b[j--] - '0' : 0; 10 | s = char(c % 2 + '0') + s; 11 | c /= 2; 12 | } 13 | 14 | return s; 15 | } 16 | -------------------------------------------------------------------------------- /src/PascalTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include "PascalTriangle.hpp" 2 | 3 | vector> PascalTriangle::generate(int numRows) { 4 | vector> r(numRows); 5 | 6 | for (int i = 0; i < numRows; i++) { 7 | r[i].resize(i + 1); 8 | r[i][0] = r[i][i] = 1; 9 | 10 | for (int j = 1; j < i; j++) 11 | r[i][j] = r[i - 1][j - 1] + r[i - 1][j]; 12 | } 13 | 14 | return r; 15 | } 16 | -------------------------------------------------------------------------------- /tests/ArithmeticSlicesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ArithmeticSlices.hpp" 4 | 5 | TEST_CASE("Arithmetic Slices") { 6 | ArithmeticSlices s; 7 | SECTION("Sample tests") { 8 | vector nums0{}; 9 | REQUIRE(s.numberOfArithmeticSlices(nums0) == 0); 10 | 11 | vector nums1{1, 2, 3, 4}; 12 | REQUIRE(s.numberOfArithmeticSlices(nums1) == 3); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/FirstBadVersionTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FirstBadVersion.hpp" 4 | 5 | TEST_CASE("First Bad Version") { 6 | const int N = 2126753390; 7 | const int M = 1702766719; 8 | auto isBad = [](int version) { 9 | return version >= M; 10 | }; 11 | FirstBadVersion s(isBad); 12 | SECTION("Sample test") { 13 | REQUIRE(s.firstBadVersion(N) == M); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/PermutationSequenceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PermutationSequence.hpp" 4 | 5 | TEST_CASE("Permutation Sequence") { 6 | PermutationSequence s; 7 | SECTION("Sample tests") { 8 | vector expected{"123", "132", "213", "231", "312", "321"}; 9 | 10 | for (int k = 1; k <= 6; k++) 11 | REQUIRE(s.getPermutation(3, k) == expected[k - 1]); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /include/WallsAndGates.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WALLS_AND_GATES_HPP_ 2 | #define WALLS_AND_GATES_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class WallsAndGates { 9 | public: 10 | void wallsAndGates(vector> &rooms); 11 | 12 | private: 13 | void bfs(vector> &rooms, int i, int j, int m, int n); 14 | 15 | const int INF = 2147483647; 16 | }; 17 | 18 | #endif // WALLS_AND_GATES_HPP_ -------------------------------------------------------------------------------- /src/ClimbingStairs.cpp: -------------------------------------------------------------------------------- 1 | #include "ClimbingStairs.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int ClimbingStairs::climbStairs(int n) { 8 | if (n == 0 || n == 1 || n == 2) 9 | return n; 10 | 11 | vector dp(n + 1, 0); 12 | dp[0] = 0, dp[1] = 1, dp[2] = 2; 13 | 14 | for (int i = 3; i <= n; i++) 15 | dp[i] = dp[i - 1] + dp[i - 2]; 16 | 17 | return dp[n]; 18 | } 19 | -------------------------------------------------------------------------------- /src/MovingAverageFromDataStream.cpp: -------------------------------------------------------------------------------- 1 | #include "MovingAverageFromDataStream.hpp" 2 | 3 | MovingAverage::MovingAverage(int size) : total(0), size(size) { 4 | 5 | } 6 | 7 | double MovingAverage::next(int val) { 8 | if (data.size() == size) { 9 | total -= data.front(); 10 | data.pop_front(); 11 | } 12 | total += val; 13 | data.push_back(val); 14 | return (double) total / data.size(); 15 | } 16 | -------------------------------------------------------------------------------- /src/UniquePaths.cpp: -------------------------------------------------------------------------------- 1 | #include "UniquePaths.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int UniquePaths::uniquePaths(int m, int n) { 8 | if (m == 0 || n == 0) 9 | return 0; 10 | 11 | vector dp(n, 1); 12 | 13 | for (int i = 1; i < m; i++) { 14 | for (int j = 1; j < n; j++) 15 | dp[j] += dp[j - 1]; 16 | } 17 | 18 | return dp[n - 1]; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /tests/ContainsDuplicateTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ContainsDuplicate.hpp" 4 | 5 | TEST_CASE("Contains Duplicate") { 6 | ContainsDuplicate s; 7 | SECTION("Sample test") { 8 | vector nums_1{1, 0, -1, 0, -2, 2}; 9 | REQUIRE(s.containsDuplicate(nums_1)); 10 | vector nums_2{1, 0, -1, 3, -2, 2}; 11 | REQUIRE_FALSE(s.containsDuplicate(nums_2)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/DesignHitCounterTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "DesignHitCounter.hpp" 4 | 5 | TEST_CASE("Design Hit Counter") { 6 | HitCounter s; 7 | SECTION("Sample test") { 8 | s.hit(1); 9 | s.hit(2); 10 | s.hit(3); 11 | REQUIRE(s.getHits(4) == 3); 12 | s.hit(300); 13 | REQUIRE(s.getHits(300) == 4); 14 | REQUIRE(s.getHits(301) == 3); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /include/BinaryTreeLevelOrderTraversalII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_LEVEL_ORDER_TRAVERSAL_II_HPP_ 2 | #define BINARY_TREE_LEVEL_ORDER_TRAVERSAL_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | #include "TreeNode.hpp" 9 | 10 | class BinaryTreeLevelOrderTraversalII { 11 | public: 12 | vector> levelOrderBottom(TreeNode *root); 13 | }; 14 | 15 | #endif // BINARY_TREE_LEVEL_ORDER_TRAVERSAL_II_HPP_ 16 | -------------------------------------------------------------------------------- /include/ListNode.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIST_NODE_HPP_ 2 | #define LIST_NODE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct ListNode { 9 | int val; 10 | ListNode *next; 11 | 12 | ListNode(int x) : val(x), next(nullptr) {} 13 | }; 14 | 15 | int list_len(ListNode *head); 16 | 17 | string list_serialize(ListNode *head); 18 | 19 | void list_free(ListNode *head); 20 | 21 | #endif // LIST_NODE_HPP_ 22 | -------------------------------------------------------------------------------- /include/LowestCommonAncestorOfABinarySearchTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LOWEST_COMMON_ANCESTOR_OF_A_BINARY_SEARCH_TREE_HPP_ 2 | #define LOWEST_COMMON_ANCESTOR_OF_A_BINARY_SEARCH_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class LowestCommonAncestorOfABinarySearchTree { 7 | public: 8 | TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q); 9 | }; 10 | 11 | #endif // LOWEST_COMMON_ANCESTOR_OF_A_BINARY_SEARCH_TREE_HPP_ -------------------------------------------------------------------------------- /include/MaximumDepthOfBinaryTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMUM_DEPTH_OF_BINARY_TREE_HPP_ 2 | #define MAXIMUM_DEPTH_OF_BINARY_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class MaximumDepthOfBinaryTree { 7 | public: 8 | int maxDepth(TreeNode *root); 9 | 10 | private: 11 | int maxDepthRecursive(TreeNode *root); 12 | 13 | int maxDepthIterative(TreeNode *root); 14 | }; 15 | 16 | #endif // MAXIMUM_DEPTH_OF_BINARY_TREE_HPP_ 17 | -------------------------------------------------------------------------------- /include/SubsetsII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUBSETS_II_HPP_ 2 | #define SUBSETS_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SubsetsII { 9 | public: 10 | vector> subsetsWithDup(vector &nums); 11 | 12 | private: 13 | void 14 | backtrack(vector &nums, int begin, vector &visited, vector &local, vector> &global); 15 | }; 16 | 17 | #endif // SUBSETS_II_HPP_ 18 | -------------------------------------------------------------------------------- /tests/IntersectionOfTwoArraysTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "IntersectionOfTwoArrays.hpp" 4 | 5 | TEST_CASE("Intersection of Two Arrays") { 6 | IntersectionOfTwoArrays s; 7 | SECTION("Sample test") { 8 | vector nums1{1, 2, 2, 1}; 9 | vector nums2{2, 2}; 10 | vector expected{2}; 11 | REQUIRE(s.intersection(nums1, nums2) == expected); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /tests/SearchA2DMatrixTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SearchA2DMatrix.hpp" 4 | 5 | TEST_CASE("Search A 2D Matrix") { 6 | SearchA2DMatrix s; 7 | SECTION("Sample tests") { 8 | vector> matrix{ 9 | {1, 3, 5, 7}, 10 | {10, 11, 16, 20}, 11 | {23, 30, 34, 50} 12 | }; 13 | REQUIRE(s.searchMatrix(matrix, 3)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /include/ClosestBinarySearchTreeValueII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLOSEST_BINARY_SEARCH_TREE_VALUE_II_HPP_ 2 | #define CLOSEST_BINARY_SEARCH_TREE_VALUE_II_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class ClosestBinarySearchTreeValueII { 11 | public: 12 | vector closestKValues(TreeNode *root, double target, int k); 13 | }; 14 | 15 | #endif // CLOSEST_BINARY_SEARCH_TREE_VALUE_II_HPP_ -------------------------------------------------------------------------------- /include/Flatten2DVector.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FLATTEN_2D_VECTOR_HPP_ 2 | #define FLATTEN_2D_VECTOR_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Vector2D { 9 | public: 10 | Vector2D(vector > &vec2d); 11 | 12 | int next(); 13 | 14 | bool hasNext(); 15 | 16 | private: 17 | int rowId; 18 | int colId; 19 | vector> vec2d; 20 | }; 21 | 22 | #endif // FLATTEN_2D_VECTOR_HPP_ -------------------------------------------------------------------------------- /include/LongestSubstringWithoutRepeatingCharacters.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LONGEST_SUBSTRING_WITHOUT_REPEATING_CHARACTERS_HPP_ 2 | #define LONGEST_SUBSTRING_WITHOUT_REPEATING_CHARACTERS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class LongestSubstringWithoutRepeatingCharacters { 9 | public: 10 | int lengthOfLongestSubstring(string s); 11 | }; 12 | 13 | #endif // LONGEST_SUBSTRING_WITHOUT_REPEATING_CHARACTERS_HPP_ 14 | -------------------------------------------------------------------------------- /include/Permutations.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERMUTATIONS_HPP_ 2 | #define PERMUTATIONS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Permutations { 9 | public: 10 | vector> permute(vector &nums); 11 | 12 | private: 13 | void 14 | backtrack(vector &nums, int begin, vector &visited, vector &local, vector> &global); 15 | }; 16 | 17 | #endif // PERMUTATIONS_HPP_ 18 | -------------------------------------------------------------------------------- /include/RemoveDuplicatesFromSortedListII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REMOVE_DUPLICATES_FROM_SORTED_LIST_II_HPP_ 2 | #define REMOVE_DUPLICATES_FROM_SORTED_LIST_II_HPP_ 3 | 4 | #include "ListNode.hpp" 5 | 6 | class RemoveDuplicatesFromSortedListII { 7 | public: 8 | ListNode *deleteDuplicates(ListNode *head); 9 | 10 | private: 11 | ListNode *findDuplicates(ListNode *head); 12 | }; 13 | 14 | #endif // REMOVE_DUPLICATES_FROM_SORTED_LIST_II_HPP_ 15 | -------------------------------------------------------------------------------- /include/SplitArrayWithEqualSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SPLIT_ARRAY_WITH_EQUAL_SUM_HPP_ 2 | #define SPLIT_ARRAY_WITH_EQUAL_SUM_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class SplitArrayWithEqualSum { 10 | public: 11 | bool splitArray(vector& nums); 12 | 13 | private: 14 | bool splitEqual(vector& agg, int i, int j, set& v); 15 | }; 16 | 17 | #endif // SPLIT_ARRAY_WITH_EQUAL_SUM_HPP_ -------------------------------------------------------------------------------- /include/SudokuSolver.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUDOKU_SOLVER_HPP_ 2 | #define SUDOKU_SOLVER_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SudokuSolver { 9 | public: 10 | void solveSudoku(vector> &board); 11 | 12 | private: 13 | bool solve(vector> &board); 14 | 15 | bool isValid(vector> &board, int m, int n, char d); 16 | }; 17 | 18 | #endif // SUDOKU_SOLVER_HPP_ 19 | -------------------------------------------------------------------------------- /src/PalindromePermutation.cpp: -------------------------------------------------------------------------------- 1 | #include "PalindromePermutation.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | bool PalindromePermutation::canPermutePalindrome(string s) { 8 | vector count(256, 0); 9 | int odd = 0; 10 | for (auto c : s) { 11 | count[c]++; 12 | if (count[c] % 2) 13 | odd++; 14 | else 15 | odd--; 16 | } 17 | return odd <= 1; 18 | } -------------------------------------------------------------------------------- /src/PlusOne.cpp: -------------------------------------------------------------------------------- 1 | #include "PlusOne.hpp" 2 | 3 | vector PlusOne::plusOne(vector &digits) { 4 | int n = digits.size(); 5 | 6 | for (int i = n - 1; i >= 0; --i) { 7 | if (digits[i] == 9) 8 | digits[i] = 0; 9 | else { 10 | digits[i]++; 11 | return digits; 12 | } 13 | } 14 | 15 | digits[0] = 1; 16 | digits.push_back(0); 17 | return digits; 18 | } 19 | -------------------------------------------------------------------------------- /tests/CountAndSayTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "CountAndSay.hpp" 4 | 5 | TEST_CASE("Count and Say") { 6 | CountAndSay s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.countAndSay(1) == "1"); 9 | REQUIRE(s.countAndSay(2) == "11"); 10 | REQUIRE(s.countAndSay(3) == "21"); 11 | REQUIRE(s.countAndSay(4) == "1211"); 12 | REQUIRE(s.countAndSay(5) == "111221"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /include/GenerateParentheses.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GENERTATE_PARENTHESES_HPP_ 2 | #define GENERTATE_PARENTHESES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class GenerateParentheses { 10 | public: 11 | vector generateParenthesis(int n); 12 | 13 | private: 14 | void helper(vector &global, string &local, int left, int right); 15 | }; 16 | 17 | #endif // GENERTATE_PARENTHESES_HPP_ 18 | -------------------------------------------------------------------------------- /include/RussianDollEnvelopes.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RUSSIAN_DOLL_ENVELOPES_HPP_ 2 | #define RUSSIAN_DOLL_ENVELOPES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class RussianDollEnvelopes { 10 | public: 11 | int maxEnvelopes(vector> &envelopes); 12 | 13 | private: 14 | int binarySearch(vector &tails, int end, int target); 15 | }; 16 | 17 | #endif // RUSSIAN_DOLL_ENVELOPES_HPP_ -------------------------------------------------------------------------------- /src/RemoveDuplicatesFromSortedArrayII.cpp: -------------------------------------------------------------------------------- 1 | #include "RemoveDuplicatesFromSortedArrayII.hpp" 2 | 3 | int RemoveDuplicatesFromSortedArrayII::removeDuplicates(vector &nums) { 4 | int K = 2; 5 | int j = K; 6 | 7 | if (nums.size() <= K) 8 | return nums.size(); 9 | 10 | for (int i = K; i < nums.size(); i++) 11 | if (nums[j - K] != nums[i]) 12 | nums[j++] = nums[i]; 13 | 14 | return j; 15 | } 16 | -------------------------------------------------------------------------------- /src/WaterAndJugProblem.cpp: -------------------------------------------------------------------------------- 1 | #include "WaterAndJugProblem.hpp" 2 | 3 | bool WaterAndJugProblem::canMeasureWater(int x, int y, int z) { 4 | if (x + y < z) return false; 5 | if (x == z || y == z || x + y == z) return true; 6 | return z % gcd(x, y) == 0; 7 | } 8 | 9 | int WaterAndJugProblem::gcd(int x, int y) { 10 | while (y) { 11 | int t = y; 12 | y = x % y; 13 | x = t; 14 | } 15 | return x; 16 | } -------------------------------------------------------------------------------- /tests/BasicCalculatorIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BasicCalculatorII.hpp" 4 | 5 | TEST_CASE("Basic Calculator II") { 6 | BasicCalculatorII s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.calculate("3+2*2") == 7); 9 | REQUIRE(s.calculate(" 3/2 ") == 1); 10 | REQUIRE(s.calculate(" 3+5 / 2 ") == 5); 11 | REQUIRE(s.calculate("100000000/1/2/3/4/5/6/7/8/9/10") == 27); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/SplitArrayWithEqualSumTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SplitArrayWithEqualSum.hpp" 4 | 5 | TEST_CASE("Split Array With Equal Sum") { 6 | SplitArrayWithEqualSum s; 7 | SECTION("Sample tests") { 8 | vector nums1{1, 2, 1, 2, 1, 2, 1}; 9 | REQUIRE(s.splitArray(nums1)); 10 | 11 | vector nums2{1, 2, -1, 1, 2, 5, 2, 5, 2}; 12 | REQUIRE(s.splitArray(nums2)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/SqrtXTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SqrtX.hpp" 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | TEST_CASE("Sqrt X") { 10 | SqrtX s; 11 | SECTION("Sample tests") { 12 | for (int i = 0; i <= 10; i++) 13 | REQUIRE(s.mySqrt(i) == (int) sqrt(i)); 14 | } 15 | SECTION("Boundary test") { 16 | REQUIRE(s.mySqrt(INT_MAX) == (int) sqrt(INT_MAX)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /include/BinaryTreePaths.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_PATHS_HPP_ 2 | #define BINARY_TREE_PATHS_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | #include "TreeNode.hpp" 8 | 9 | using namespace std; 10 | 11 | class BinaryTreePaths { 12 | public: 13 | vector binaryTreePaths(TreeNode *root); 14 | 15 | private: 16 | void dfs(TreeNode *root, string path, vector &ret); 17 | }; 18 | 19 | #endif // BINARY_TREE_PATHS_HPP_ -------------------------------------------------------------------------------- /include/CombinationSumIII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMBINATION_SUM_III_HPP_ 2 | #define COMBINATION_SUM_III_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CombinationSumIII { 9 | public: 10 | vector> combinationSum3(int k, int n); 11 | 12 | private: 13 | void backtrack(int k, int target, int depth, int sum, vector &cur, vector> &result); 14 | }; 15 | 16 | #endif // COMBINATION_SUM_III_HPP_ 17 | -------------------------------------------------------------------------------- /include/WiggleSubsequence.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WIGGLE_SUBSEQUENCE_HPP_ 2 | #define WIGGLE_SUBSEQUENCE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class WiggleSubsequence { 9 | public: 10 | int wiggleMaxLength(vector &nums); 11 | 12 | private: 13 | int wiggleMaxLength_DynamicProgramming(vector &nums); 14 | int wiggleMaxLength_Linear(vector &nums); 15 | }; 16 | 17 | #endif // WIGGLE_SUBSEQUENCE_HPP_ 18 | -------------------------------------------------------------------------------- /include/WordSearch.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WORD_SEARCH_HPP_ 2 | #define WORD_SEARCH_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class WordSearch { 10 | public: 11 | bool exist(vector> &board, string word); 12 | 13 | private: 14 | bool backtrack(vector> &board, string &word, int depth, int i, int j, vector> &used); 15 | }; 16 | 17 | #endif // WORD_SEARCH_HPP_ 18 | -------------------------------------------------------------------------------- /src/MajorityElement.cpp: -------------------------------------------------------------------------------- 1 | #include "MajorityElement.hpp" 2 | 3 | int MajorityElement::majorityElement(vector &nums) { 4 | int result = nums[0]; 5 | int counter = 1; 6 | 7 | for (int i = 1; i < nums.size(); i++) { 8 | counter += (nums[i] == result) ? 1 : -1; 9 | 10 | if (counter == 0) { 11 | result = nums[i]; 12 | counter = 1; 13 | } 14 | } 15 | 16 | return result; 17 | } 18 | -------------------------------------------------------------------------------- /src/PaintFence.cpp: -------------------------------------------------------------------------------- 1 | #include "PaintFence.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int PaintFence::numWays(int n, int k) { 8 | if (n == 0 || k == 0) return 0; 9 | if (n == 1) return k; 10 | 11 | vector dp(n, 0); 12 | dp[0] = k; 13 | dp[1] = k * k; 14 | 15 | for (int i = 2; i < n; i++) 16 | dp[i] = (k - 1) * dp[i - 1] + (k - 1) * dp[i - 2]; 17 | 18 | return dp[n - 1]; 19 | } 20 | -------------------------------------------------------------------------------- /src/RemoveLinkedListElements.cpp: -------------------------------------------------------------------------------- 1 | #include "RemoveLinkedListElements.hpp" 2 | 3 | ListNode *RemoveLinkedListElements::removeElements(ListNode *head, int val) { 4 | ListNode dummy(0); 5 | dummy.next = head; 6 | 7 | ListNode *cur = &dummy; 8 | 9 | while (cur) { 10 | if (cur->next && cur->next->val == val) cur->next = cur->next->next; 11 | else cur = cur->next; 12 | } 13 | 14 | return dummy.next; 15 | } 16 | -------------------------------------------------------------------------------- /tests/CourseScheduleTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "CourseSchedule.hpp" 4 | 5 | TEST_CASE("Course Schedule") { 6 | CourseSchedule s; 7 | SECTION("Sample tests") { 8 | vector> pre1{{1, 0}}; 9 | vector> pre2{{1, 0}, 10 | {0, 1}}; 11 | REQUIRE(s.canFinish(2, pre1)); 12 | REQUIRE_FALSE(s.canFinish(2, pre2)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/PeekingIteratorTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PeekingIterator.hpp" 4 | 5 | TEST_CASE("Peeking Iterator") { 6 | SECTION("Sample test") { 7 | vector nums{1, 2, 3}; 8 | PeekingIterator s(nums); 9 | REQUIRE(s.next() == 1); 10 | REQUIRE(s.peek() == 2); 11 | REQUIRE(s.next() == 2); 12 | REQUIRE(s.next() == 3); 13 | REQUIRE_FALSE(s.hasNext()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/SpiralMatrixIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SpiralMatrixII.hpp" 4 | 5 | using namespace std; 6 | 7 | TEST_CASE("Spiral Matrix II") { 8 | SpiralMatrixII s; 9 | SECTION("Sample test") { 10 | vector> expected{ 11 | {1, 2, 3}, 12 | {8, 9, 4}, 13 | {7, 6, 5} 14 | }; 15 | REQUIRE(s.generateMatrix(3) == expected); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /include/BinaryTreeZigzagLevelOrderTraversal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_ZIGZAG_LEVEL_ORDER_TRAVERSAL_HPP_ 2 | #define BINARY_TREE_ZIGZAG_LEVEL_ORDER_TRAVERSAL_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class BinaryTreeZigzagLevelOrderTraversal { 11 | public: 12 | vector> zigzagLevelOrder(TreeNode *root); 13 | }; 14 | 15 | #endif // BINARY_TREE_ZIGZAG_LEVEL_ORDER_TRAVERSAL_HPP_ 16 | -------------------------------------------------------------------------------- /include/ImplementStrStr.hpp: -------------------------------------------------------------------------------- 1 | #ifndef IMPLEMENT_STRSTR_HPP_ 2 | #define IMPLEMENT_STRSTR_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ImplementStrStr { 9 | public: 10 | int strStr(string haystack, string needle); 11 | 12 | private: 13 | int strStr_BruteForce(string &haystack, string &needle); 14 | 15 | int strStr_RabinKarp(string &haystack, string &needle); 16 | }; 17 | 18 | #endif // IMPLEMENT_STRSTR_HPP_ 19 | -------------------------------------------------------------------------------- /include/PathSumII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PATH_SUM_II_HPP_ 2 | #define PATH_SUM_II_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class PathSumII { 11 | public: 12 | vector> pathSum(TreeNode *root, int sum); 13 | 14 | private: 15 | void pathSum(TreeNode *root, vector &path, int sum, 16 | vector> &res); 17 | }; 18 | 19 | #endif // PATH_SUM_II_HPP_ 20 | -------------------------------------------------------------------------------- /include/SubstringWithConcatenationOfAllWords.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUBSTRING_WITH_CONCATENATION_OF_ALL_WORDS_HPP_ 2 | #define SUBSTRING_WITH_CONCATENATION_OF_ALL_WORDS_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class SubstringWithConcatenationOfAllWords { 10 | public: 11 | vector findSubstring(string s, vector &words); 12 | }; 13 | 14 | #endif // SUBSTRING_WITH_CONCATENATION_OF_ALL_WORDS_HPP_ 15 | -------------------------------------------------------------------------------- /include/ZigzagIterator.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ZIGZAG_ITERATOR_HPP_ 2 | #define ZIGZAG_ITERATOR_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class ZigzagIterator { 9 | public: 10 | ZigzagIterator(vector &v1, vector &v2); 11 | 12 | int next(); 13 | 14 | bool hasNext(); 15 | 16 | private: 17 | int rowId; 18 | vector colIds; 19 | vector> data; 20 | }; 21 | 22 | #endif // ZIGZAG_ITERATOR_HPP_ -------------------------------------------------------------------------------- /src/HIndexII.cpp: -------------------------------------------------------------------------------- 1 | #include "HIndexII.hpp" 2 | 3 | int HIndexII::hIndex(vector &citations) { 4 | int n = citations.size(); 5 | int h = 0; 6 | int l = 0; 7 | int r = n - 1; 8 | 9 | while (l <= r) { 10 | int m = (l + r) / 2; 11 | 12 | if (citations[m] >= n - m) { 13 | r = m - 1; 14 | h = n - m; 15 | } else 16 | l = m + 1; 17 | } 18 | 19 | return h; 20 | } 21 | -------------------------------------------------------------------------------- /src/SwapNodesInPairs.cpp: -------------------------------------------------------------------------------- 1 | #include "SwapNodesInPairs.hpp" 2 | 3 | ListNode *SwapNodesInPairs::swapPairs(ListNode *head) { 4 | ListNode dummy(0); 5 | dummy.next = head; 6 | 7 | for (ListNode *p = &dummy; p->next && p->next->next; p = p->next->next) { 8 | ListNode *t = p->next; 9 | p->next = t->next; 10 | t->next = p->next->next; 11 | p->next->next = t; 12 | } 13 | 14 | return dummy.next; 15 | } 16 | -------------------------------------------------------------------------------- /tests/CoinChangeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "CoinChange.hpp" 4 | 5 | TEST_CASE("Coin Change") { 6 | CoinChange s; 7 | SECTION("Sample tests") { 8 | vector coins1{1, 2, 5}; 9 | int amount1 = 11; 10 | REQUIRE(s.coinChange(coins1, amount1) == 3); 11 | 12 | vector coins2{2}; 13 | int amount2 = 3; 14 | REQUIRE(s.coinChange(coins2, amount2) == -1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/MergeSortedArrayTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MergeSortedArray.hpp" 4 | 5 | TEST_CASE("Merge Sorted Array") { 6 | MergeSortedArray s; 7 | SECTION("Sample test") { 8 | vector nums1{1, 3, 5, 7, 0, 0, 0, 0}; 9 | vector nums2{2, 4, 6, 8}; 10 | vector expected{1, 2, 3, 4, 5, 6, 7, 8}; 11 | s.merge(nums1, 4, nums2, 4); 12 | REQUIRE(nums1 == expected); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/PaintFenceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PaintFence.hpp" 4 | 5 | TEST_CASE("Paint Fence") { 6 | PaintFence s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.numWays(1, 1) == 1); 9 | REQUIRE(s.numWays(2, 1) == 1); 10 | REQUIRE(s.numWays(3, 1) == 0); 11 | REQUIRE(s.numWays(2, 2) == 4); 12 | REQUIRE(s.numWays(3, 2) == 6); 13 | REQUIRE(s.numWays(4, 2) == 10); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/RangeSumQueryImmutableTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "RangeSumQueryImmutable.hpp" 4 | 5 | TEST_CASE("Range Sum Query - Immutable") { 6 | SECTION("Sample test") { 7 | vector nums{-2, 0, 3, -5, 2, -1}; 8 | NumArray numArray(nums); 9 | REQUIRE(numArray.sumRange(0, 2) == 1); 10 | REQUIRE(numArray.sumRange(2, 5) == -1); 11 | REQUIRE(numArray.sumRange(0, 5) == -3); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /include/FindLeavesOfBinaryTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_LEAVES_OF_BINARY_TREE_HPP_ 2 | #define FIND_LEAVES_OF_BINARY_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class FindLeavesOfBinaryTree { 11 | public: 12 | vector> findLeaves(TreeNode *root); 13 | 14 | private: 15 | int height(TreeNode *root, vector> &ret); 16 | }; 17 | 18 | #endif // FIND_LEAVES_OF_BINARY_TREE_HPP_ -------------------------------------------------------------------------------- /include/PermutationsII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERMUTATIONS_II_HPP_ 2 | #define PERMUTATIONS_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class PermutationsII { 9 | public: 10 | vector> permuteUnique(vector &nums); 11 | 12 | private: 13 | void 14 | backtrack(vector &nums, int begin, vector &visited, vector &local, vector> &global); 15 | }; 16 | 17 | #endif // PERMUTATIONS_II_HPP_ 18 | -------------------------------------------------------------------------------- /src/NumberOfDigitOne.cpp: -------------------------------------------------------------------------------- 1 | #include "NumberOfDigitOne.hpp" 2 | 3 | // The solution is taken from the following post: 4 | // http://blog.csdn.net/xudli/article/details/46798619 5 | int NumberOfDigitOne::countDigitOne(int n) { 6 | int ret = 0; 7 | 8 | for (long d = 1; d <= n; d = d * 10) { 9 | int a = n / d; 10 | int b = n % d; 11 | ret += ((a + 8) / 10) * d + (a % 10 == 1) * (b + 1); 12 | } 13 | 14 | return ret; 15 | } -------------------------------------------------------------------------------- /src/SingleNumberIII.cpp: -------------------------------------------------------------------------------- 1 | #include "SingleNumberIII.hpp" 2 | 3 | vector SingleNumberIII::singleNumber(vector &nums) { 4 | int flag = 0; 5 | 6 | for (auto num : nums) 7 | flag ^= num; 8 | 9 | flag &= -flag; 10 | vector ret{0, 0}; 11 | 12 | for (auto num : nums) { 13 | if (num & flag) 14 | ret[0] ^= num; 15 | else 16 | ret[1] ^= num; 17 | } 18 | 19 | return ret; 20 | } -------------------------------------------------------------------------------- /tests/BestTimeToBuyAndSellStockIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BestTimeToBuyAndSellStockII.hpp" 4 | 5 | TEST_CASE("Best Time to Buy and Sell Stock II") { 6 | BestTimeToBuyAndSellStockII s; 7 | SECTION("Normal test") { 8 | vector prices{10, 11, 7, 10, 6}; 9 | // buy at 10 and sell at 11, earn 1 10 | // buy at 7 and sell at 10, earn 3 11 | REQUIRE(s.maxProfit(prices) == 4); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/ValidNumberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ValidNumber.hpp" 4 | 5 | TEST_CASE("Valid Number") { 6 | ValidNumber s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.isNumber("0")); 9 | REQUIRE(s.isNumber(" 0.1 ")); 10 | REQUIRE_FALSE(s.isNumber("abc")); 11 | REQUIRE_FALSE(s.isNumber("1 a")); 12 | REQUIRE(s.isNumber("2e10")); 13 | REQUIRE_FALSE(s.isNumber(" ")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /include/BinaryTreeLongestConsecutiveSequence.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_LONGEST_CONSECUTIVE_SEQUENCE_HPP_ 2 | #define BINARY_TREE_LONGEST_CONSECUTIVE_SEQUENCE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class BinaryTreeLongestConsecutiveSequence { 7 | public: 8 | int longestConsecutive(TreeNode *root); 9 | 10 | private: 11 | void dfs(TreeNode *root, int cur, int target, int &global); 12 | }; 13 | 14 | #endif // BINARY_TREE_LONGEST_CONSECUTIVE_SEQUENCE_HPP_ -------------------------------------------------------------------------------- /include/BinaryTreeUpsideDown.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_UPSIDE_DOWN_HPP_ 2 | #define BINARY_TREE_UPSIDE_DOWN_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class BinaryTreeUpsideDown { 7 | public: 8 | TreeNode *upsideDownBinaryTree(TreeNode *root); 9 | 10 | private: 11 | TreeNode *upsideDownBinaryTree_Recursive(TreeNode *root); 12 | 13 | TreeNode *upsideDownBinaryTree_Iterative(TreeNode *root); 14 | }; 15 | 16 | #endif // BINARY_TREE_UPSIDE_DOWN_HPP_ -------------------------------------------------------------------------------- /include/WordLadder.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WORD_LADDER_HPP_ 2 | #define WORD_LADDER_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class WordLadder { 10 | public: 11 | int ladderLength(string beginWord, string endWord, unordered_set &wordList); 12 | 13 | private: 14 | void bfs(unordered_set &wordList, string beginWord, string endWord, int &dist); 15 | }; 16 | 17 | #endif // WORD_LADDER_HPP_ 18 | -------------------------------------------------------------------------------- /src/TwoSumII.cpp: -------------------------------------------------------------------------------- 1 | #include "TwoSumII.hpp" 2 | 3 | vector TwoSumII::twoSum(vector &numbers, int target) { 4 | int l = 0, r = numbers.size() - 1; 5 | 6 | while (l < r) { 7 | int sum = numbers[l] + numbers[r]; 8 | if (sum == target) 9 | return vector {l + 1, r + 1}; 10 | if (sum < target) 11 | l++; 12 | else 13 | r--; 14 | } 15 | 16 | return vector {0, 0}; 17 | } -------------------------------------------------------------------------------- /tests/BestTimeToBuyAndSellStockIIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BestTimeToBuyAndSellStockIII.hpp" 4 | 5 | TEST_CASE("Best Time to Buy and Sell Stock III") { 6 | BestTimeToBuyAndSellStockIII s; 7 | SECTION("Normal test") { 8 | vector prices{10, 11, 7, 10, 6}; 9 | // buy at 10 and sell at 11, earn 1 10 | // buy at 7 and sell at 10, earn 3 11 | REQUIRE(s.maxProfit(prices) == 4); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/CountPrimesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "CountPrimes.hpp" 4 | 5 | TEST_CASE("Count Primes") { 6 | CountPrimes s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.countPrimes(1) == 0); 9 | REQUIRE(s.countPrimes(2) == 0); 10 | REQUIRE(s.countPrimes(3) == 1); 11 | REQUIRE(s.countPrimes(4) == 2); 12 | REQUIRE(s.countPrimes(5) == 2); 13 | REQUIRE(s.countPrimes(6) == 3); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/SuperUglyNumberTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SuperUglyNumber.hpp" 4 | 5 | TEST_CASE("Super Ugly Number") { 6 | SuperUglyNumber s; 7 | SECTION("Sample tests") { 8 | vector primes{2, 7, 13, 19}; 9 | vector expected{0, 1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32}; 10 | 11 | for (int i = 1; i <= 12; i++) 12 | REQUIRE(s.nthSuperUglyNumber(i, primes) == expected[i]); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/AddAndSearchWordTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "AddAndSearchWord.hpp" 4 | 5 | TEST_CASE("Add And Search Word") { 6 | WordDictionary s; 7 | SECTION("Sample test") { 8 | s.addWord("bad"); 9 | s.addWord("dad"); 10 | s.addWord("mad"); 11 | REQUIRE_FALSE(s.search("pad")); 12 | REQUIRE(s.search("bad")); 13 | REQUIRE(s.search(".ad")); 14 | REQUIRE(s.search("b..")); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/MovingAverageFromDataStreamTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MovingAverageFromDataStream.hpp" 4 | 5 | TEST_CASE("Moving Average from Data Stream") { 6 | 7 | SECTION("Sample test") { 8 | MovingAverage m(3); 9 | REQUIRE(m.next(1) == 1.0); 10 | REQUIRE(m.next(10) == (1 + 10) / 2.0); 11 | REQUIRE(m.next(3) == (1 + 10 + 3) / 3.0); 12 | REQUIRE(m.next(5) == (10 + 3 + 5) / 3.0); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/SpiralMatrixTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SpiralMatrix.hpp" 4 | 5 | TEST_CASE("Spiral Matrix") { 6 | SpiralMatrix s; 7 | SECTION("Sample test") { 8 | vector> matrix{ 9 | {1, 2, 3}, 10 | {4, 5, 6}, 11 | {7, 8, 9} 12 | }; 13 | vector expected{1, 2, 3, 6, 9, 8, 7, 4, 5}; 14 | REQUIRE(s.spiralOrder(matrix) == expected); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/ValidParenthesesTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ValidParentheses.hpp" 4 | 5 | TEST_CASE("Valid Parentheses") { 6 | ValidParentheses s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.isValid("()")); 9 | REQUIRE(s.isValid("()[]{}")); 10 | REQUIRE_FALSE(s.isValid("(]")); 11 | REQUIRE_FALSE(s.isValid("([)]")); 12 | } 13 | SECTION("Empty string") { 14 | REQUIRE(s.isValid("")); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/WordPatternTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "WordPattern.hpp" 4 | 5 | TEST_CASE("Word Pattern") { 6 | WordPattern s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.wordPattern("abba", "dog cat cat dog")); 9 | REQUIRE_FALSE(s.wordPattern("abba", "dog cat cat fish")); 10 | REQUIRE_FALSE(s.wordPattern("aaaa", "dog cat cat dog")); 11 | REQUIRE_FALSE(s.wordPattern("abba", "dog dog dog dog")); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/BestTimeToBuyAndSellStockII.cpp: -------------------------------------------------------------------------------- 1 | #include "BestTimeToBuyAndSellStockII.hpp" 2 | 3 | int BestTimeToBuyAndSellStockII::maxProfit(vector &prices) { 4 | int sz = prices.size(); 5 | 6 | if (sz <= 1) 7 | return 0; 8 | 9 | int result = 0; 10 | 11 | for (int i = 1; i < sz; i++) { 12 | int diff = prices[i] - prices[i - 1]; 13 | 14 | if (diff > 0) 15 | result += diff; 16 | } 17 | 18 | return result; 19 | } 20 | -------------------------------------------------------------------------------- /src/FirstUniqueCharacterInAString.cpp: -------------------------------------------------------------------------------- 1 | #include "FirstUniqueCharacterInAString.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int FirstUniqueCharacterInAString::firstUniqChar(string s) { 8 | vector freq(26, 0); 9 | 10 | for (int i = 0; i < s.size(); i++) 11 | freq[s[i] - 'a']++; 12 | for (int i = 0; i < s.length(); i++) 13 | if (freq[s[i] - 'a'] == 1) 14 | return i; 15 | 16 | return -1; 17 | } 18 | -------------------------------------------------------------------------------- /src/LongestCommonPrefix.cpp: -------------------------------------------------------------------------------- 1 | #include "LongestCommonPrefix.hpp" 2 | 3 | string LongestCommonPrefix::longestCommonPrefix(vector &strs) { 4 | if (strs.empty()) 5 | return ""; 6 | 7 | for (int idx = 0; idx < strs[0].size(); ++idx) { 8 | for (int i = 1; i < strs.size(); ++i) { 9 | if (strs[i][idx] != strs[0][idx]) 10 | return strs[0].substr(0, idx); 11 | } 12 | } 13 | 14 | return strs[0]; 15 | } 16 | -------------------------------------------------------------------------------- /src/ProductOfArrayExceptSelf.cpp: -------------------------------------------------------------------------------- 1 | #include "ProductOfArrayExceptSelf.hpp" 2 | 3 | vector ProductOfArrayExceptSelf::productExceptSelf(vector &nums) { 4 | int sz = nums.size(); 5 | vector ret(sz, 1); 6 | int acc = 1; 7 | 8 | for (int i = 1; i < sz; i++) 9 | ret[i] = (acc *= nums[i - 1]); 10 | 11 | acc = 1; 12 | 13 | for (int i = sz - 2; i >= 0; i--) 14 | ret[i] *= (acc *= nums[i + 1]); 15 | 16 | return ret; 17 | } -------------------------------------------------------------------------------- /tests/MaxPointsOnALineTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MaxPointsOnALine.hpp" 4 | 5 | TEST_CASE("Max Points on a Line") { 6 | MaxPointsOnALine s; 7 | SECTION("Sample tests") { 8 | vector points{{0, 0}, 9 | {1, 0}, 10 | {1, 1}, 11 | {0, 2}, 12 | {2, 2}}; 13 | REQUIRE(s.maxPoints(points) == 3); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/MaximumSizeSubarraySumEqualsKTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MaximumSizeSubarraySumEqualsK.hpp" 4 | 5 | TEST_CASE("Maximum Size Subarray Sum Equals k") { 6 | MaximumSizeSubarraySumEqualsK s; 7 | SECTION("Sample tests") { 8 | vector nums1{1, -1, 5, -2, 3}; 9 | REQUIRE(s.maxSubArrayLen(nums1, 3) == 4); 10 | vector nums2{-2, -1, 2, 1}; 11 | REQUIRE(s.maxSubArrayLen(nums2, 1) == 2); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/PascalTriangleTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PascalTriangle.hpp" 4 | 5 | TEST_CASE("Pascal's Triangle") { 6 | PascalTriangle s; 7 | SECTION("Sample test") { 8 | vector> expected{ 9 | {1}, 10 | {1, 1}, 11 | {1, 2, 1}, 12 | {1, 3, 3, 1}, 13 | {1, 4, 6, 4, 1} 14 | }; 15 | REQUIRE(s.generate(5) == expected); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /include/CombinationSum.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMBINATION_SUM_HPP_ 2 | #define COMBINATION_SUM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CombinationSum { 9 | public: 10 | vector> combinationSum(vector &candidates, int target); 11 | 12 | private: 13 | void 14 | backtrack(vector &candidates, int target, int depth, int sum, vector &cur, vector> &result); 15 | }; 16 | 17 | #endif // COMBINATION_SUM_HPP_ 18 | -------------------------------------------------------------------------------- /src/InorderSuccessorInBST.cpp: -------------------------------------------------------------------------------- 1 | #include "InorderSuccessorInBST.hpp" 2 | 3 | TreeNode *InorderSuccessorInBST::inorderSuccessor(TreeNode *root, TreeNode *p) { 4 | TreeNode *ret = nullptr; 5 | 6 | if (p == nullptr) return ret; 7 | 8 | while (root) { 9 | if (root->val > p->val) { 10 | ret = root; 11 | root = root->left; 12 | } else { 13 | root = root->right; 14 | } 15 | } 16 | 17 | return ret; 18 | } -------------------------------------------------------------------------------- /src/IsSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include "IsSubsequence.hpp" 2 | 3 | bool IsSubsequence::isSubsequence(string s, string t) { 4 | if (s.empty()) return true; 5 | if (t.empty() || s.size() > t.size()) return false; 6 | 7 | int i = 0, j = 0; 8 | while (i < s.size() && j < t.size()) { 9 | if (s[i] == t[j]) { 10 | i++; 11 | j++; 12 | } else { 13 | j++; 14 | } 15 | } 16 | 17 | return i == s.size(); 18 | } 19 | -------------------------------------------------------------------------------- /include/CourseSchedule.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COURSE_SCHEDULE_HPP_ 2 | #define COURSE_SCHEDULE_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class CourseSchedule { 10 | public: 11 | bool canFinish(int numCourses, vector> &prerequisites); 12 | 13 | private: 14 | bool dfs(vector> &graph, vector &marked, 15 | vector &onStack, int v); 16 | }; 17 | 18 | #endif // COURSE_SCHEDULE_HPP_ 19 | -------------------------------------------------------------------------------- /include/MedianOfTwoSortedArrays.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MEDIAN_OF_TWO_SORTED_ARRAYS_HPP_ 2 | #define MEDIAN_OF_TWO_SORTED_ARRAYS_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MedianOfTwoSortedArrays { 9 | public: 10 | double findMedianSortedArrays(vector &nums1, vector &nums2); 11 | 12 | private: 13 | double findKth(vector &nums1, vector &nums2, int st1, int st2, int k); 14 | }; 15 | 16 | #endif // MEDIAN_OF_TWO_SORTED_ARRAYS_HPP_ 17 | -------------------------------------------------------------------------------- /include/UniqueWordAbbreviation.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UNIQUE_WORD_ABBREVIATION_HPP_ 2 | #define UNIQUE_WORD_ABBREVIATION_HPP_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class ValidWordAbbr { 11 | public: 12 | ValidWordAbbr(vector &dictionary); 13 | 14 | bool isUnique(string word); 15 | 16 | private: 17 | unordered_map lookup; 18 | }; 19 | 20 | #endif // UNIQUE_WORD_ABBREVIATION_HPP_ -------------------------------------------------------------------------------- /tests/UniqueWordAbbreviationTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "UniqueWordAbbreviation.hpp" 4 | 5 | TEST_CASE("Unique Word Abbreviation") { 6 | SECTION("Sample test") { 7 | vector dict{"deer", "door", "cake", "card"}; 8 | ValidWordAbbr s(dict); 9 | REQUIRE_FALSE(s.isUnique("dear")); 10 | REQUIRE(s.isUnique("cart")); 11 | REQUIRE_FALSE(s.isUnique("cane")); 12 | REQUIRE(s.isUnique("make")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /include/HouseRobberIII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOUSE_ROBBER_III_HPP_ 2 | #define HOUSE_ROBBER_III_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class HouseRobberIII { 12 | public: 13 | int rob(TreeNode *root); 14 | 15 | private: 16 | int robMem(TreeNode *root, unordered_map &mem); 17 | 18 | vector robGreedy(TreeNode *root); 19 | }; 20 | 21 | #endif // HOUSE_ROBBER_III_HPP_ 22 | -------------------------------------------------------------------------------- /include/PermutationSequence.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERMUTATION_SEQUENCE_HPP_ 2 | #define PERMUTATION_SEQUENCE_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class PermutationSequence { 10 | public: 11 | string getPermutation(int n, int k); 12 | 13 | private: 14 | void 15 | backtrack(vector &nums, int begin, vector &visited, int &k, vector &local, vector &global); 16 | }; 17 | 18 | #endif // PERMUTATION_SEQUENCE_HPP_ 19 | -------------------------------------------------------------------------------- /include/RangeSumQuery2DImmutable.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RANGE_SUM_QUERY_2D_IMMUTABLE_HPP_ 2 | #define RANGE_SUM_QUERY_2D_IMMUTABLE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class NumMatrix { 9 | public: 10 | NumMatrix(vector> &matrix); 11 | 12 | int sumRegion(int row1, int col1, int row2, int col2); 13 | 14 | private: 15 | vector> acc; 16 | int m; 17 | int n; 18 | }; 19 | 20 | #endif // RANGE_SUM_QUERY_2D_IMMUTABLE_HPP_ 21 | -------------------------------------------------------------------------------- /src/ClosestBinarySearchTreeValue.cpp: -------------------------------------------------------------------------------- 1 | #include "ClosestBinarySearchTreeValue.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int ClosestBinarySearchTreeValue::closestValue(TreeNode *root, double target) { 8 | int closest = root->val; 9 | while (root) { 10 | if (abs(root->val - target) < abs(closest - target)) 11 | closest = root->val; 12 | root = root->val < target ? root->right : root->left; 13 | } 14 | return closest; 15 | } -------------------------------------------------------------------------------- /src/FindMinimumInRotatedSortedArray.cpp: -------------------------------------------------------------------------------- 1 | #include "FindMinimumInRotatedSortedArray.hpp" 2 | 3 | int FindMinimumInRotatedSortedArray::findMin(vector &nums) { 4 | int l = 0, r = nums.size() - 1; 5 | 6 | while (nums[l] > nums[r]) { 7 | // nums[l] to nums[r] is still rotated 8 | int m = l + (r - l) / 2; 9 | if (nums[l] <= nums[m]) 10 | l = m + 1; 11 | else 12 | r = m; 13 | } 14 | 15 | return nums[l]; 16 | } 17 | -------------------------------------------------------------------------------- /tests/3SumClosestTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "3SumClosest.hpp" 4 | 5 | TEST_CASE("3Sum Closest") { 6 | ThreeSumClosest s; 7 | SECTION("Sample test") { 8 | vector nums{-1, 2, 1, -4}; 9 | int target = 1; 10 | REQUIRE(s.threeSumClosest(nums, target) == 2); 11 | } 12 | SECTION("Exactly found") { 13 | vector nums{-1, 0, 1, 2, -1, -4}; 14 | REQUIRE(s.threeSumClosest(nums, -1) == -1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/InsertDeleteGetRandomTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "InsertDeleteGetRandom.hpp" 4 | 5 | TEST_CASE("Insert Delete GetRandom O(1)") { 6 | RandomizedSet s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.insert(0)); // {0} 9 | REQUIRE(s.insert(1)); // {0, 1} 10 | REQUIRE(s.remove(0)); // {0} 11 | REQUIRE(s.insert(2)); // {1, 2} 12 | REQUIRE(s.remove(1)); // {2} 13 | REQUIRE(s.getRandom() == 2); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /include/CombinationSumII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMBINATION_SUM_II_HPP_ 2 | #define COMBINATION_SUM_II_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CombinationSumII { 9 | public: 10 | vector> combinationSum2(vector &candidates, int target); 11 | 12 | private: 13 | void 14 | backtrack(vector &candidates, int target, int depth, int sum, vector &cur, vector> &result); 15 | }; 16 | 17 | #endif // COMBINATION_SUM_II_HPP_ 18 | -------------------------------------------------------------------------------- /include/ConvertSortedListToBinarySearchTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONVERT_SORTED_LIST_TO_BINARY_SEARCH_TREE_HPP_ 2 | #define CONVERT_SORTED_LIST_TO_BINARY_SEARCH_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | #include "ListNode.hpp" 6 | 7 | class ConvertSortedListToBinarySearchTree { 8 | public: 9 | TreeNode *sortedListToBST(ListNode *head); 10 | 11 | private: 12 | TreeNode *helper(ListNode *start, ListNode *end); 13 | }; 14 | 15 | #endif // CONVERT_SORTED_LIST_TO_BINARY_SEARCH_TREE_HPP_ 16 | -------------------------------------------------------------------------------- /include/PalindromePartitioning.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PALINDROME_PARTITIONING_HPP_ 2 | #define PALINDROME_PARTITIONING_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class PalindromePartitioning { 10 | public: 11 | vector> partition(string s); 12 | 13 | private: 14 | void dfs(const string &s, vector &path, vector> &result, 15 | int start); 16 | }; 17 | 18 | #endif // PALINDROME_PARTITIONING_HPP_ 19 | -------------------------------------------------------------------------------- /include/TwoSumIII.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TWO_SUM_III_HPP_ 2 | #define TWO_SUM_III_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class TwoSumIII { 9 | public: 10 | // Add the number to an internal data structure. 11 | void add(int number); 12 | 13 | // Find if there exists any pair of numbers which sum is equal to the value. 14 | bool find(int value); 15 | 16 | private: 17 | unordered_map lookup; 18 | }; 19 | 20 | #endif // TWO_SUM_III_HPP_ -------------------------------------------------------------------------------- /src/FindTheDifference.cpp: -------------------------------------------------------------------------------- 1 | #include "FindTheDifference.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | char FindTheDifference::findTheDifference(string s, string t) { 8 | vector count(256, 0); 9 | 10 | for (auto c : t) 11 | count[c]++; 12 | 13 | for (auto c : s) 14 | count[c]--; 15 | 16 | char ret = ' '; 17 | for (int i = 0; i < 256; i++) 18 | if (count[i]) 19 | ret = (char) i; 20 | 21 | return ret; 22 | } -------------------------------------------------------------------------------- /src/SqrtX.cpp: -------------------------------------------------------------------------------- 1 | #include "SqrtX.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int SqrtX::mySqrt(int x) { 8 | if (x <= 0) return 0; 9 | int64_t l = 1; 10 | int64_t r = x; 11 | while (l < r) { 12 | int64_t m = l + (r - l + 1) / 2; 13 | if (m * m == (int64_t) x) 14 | return (int) m; 15 | if (m * m < (int64_t) x) 16 | l = m; 17 | else 18 | r = m - 1; 19 | } 20 | return (int) l; 21 | } -------------------------------------------------------------------------------- /src/UglyNumber.cpp: -------------------------------------------------------------------------------- 1 | #include "UglyNumber.hpp" 2 | 3 | bool UglyNumber::isUgly(int num) { 4 | if (num <= 0) 5 | return false; 6 | 7 | while (true) { 8 | if (num == 1) 9 | break; 10 | else if (num % 2 == 0) 11 | num /= 2; 12 | else if (num % 3 == 0) 13 | num /= 3; 14 | else if (num % 5 == 0) 15 | num /= 5; 16 | else 17 | break; 18 | } 19 | 20 | return num == 1; 21 | } 22 | -------------------------------------------------------------------------------- /tests/FrogJumpTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "FrogJump.hpp" 4 | 5 | TEST_CASE("Frog Jump") { 6 | FrogJump s; 7 | SECTION("Sample tests") { 8 | vector stones1{0, 1, 3, 5, 6, 8, 12, 17}; 9 | REQUIRE(s.canCross(stones1)); 10 | 11 | vector stones2{0, 1, 2, 3, 4, 8, 9, 11}; 12 | REQUIRE_FALSE(s.canCross(stones2)); 13 | 14 | vector stones3{0, 2}; 15 | REQUIRE_FALSE(s.canCross(stones3)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/SimplifyPathTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SimplifyPath.hpp" 4 | 5 | TEST_CASE("Simplify Path") { 6 | SimplifyPath s; 7 | SECTION("Sample tests") { 8 | REQUIRE(s.simplifyPath("/home/") == "/home"); 9 | REQUIRE(s.simplifyPath("/a/./b/../../c/") == "/c"); 10 | } 11 | SECTION("Corner case tests") { 12 | REQUIRE(s.simplifyPath("/../") == "/"); 13 | REQUIRE(s.simplifyPath("/home//foo/") == "/home/foo"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /include/MaximumSubarray.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXIMUM_SUBARRAY_HPP_ 2 | #define MAXIMUM_SUBARRAY_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MaximumSubarray { 9 | public: 10 | int maxSubArray(vector &nums); 11 | 12 | private: 13 | int maxSubArray_DynamicProgramming(vector &nums); 14 | 15 | void maxSubArray_DivideAndConquer(vector &nums, int l, int r, int &mx, int &lmx, int &rmx, int &sum); 16 | }; 17 | 18 | #endif // MAXIMUM_SUBARRAY_HPP_ 19 | -------------------------------------------------------------------------------- /include/ValidateBinarySearchTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VALIDATE_BINARY_SEARCH_TREE_HPP_ 2 | #define VALIDATE_BINARY_SEARCH_TREE_HPP_ 3 | 4 | #include "TreeNode.hpp" 5 | 6 | class ValidateBinarySearchTree { 7 | public: 8 | bool isValidBST(TreeNode *root); 9 | 10 | public: 11 | bool isValidBST_PRESUC(TreeNode *root); 12 | 13 | bool isValidBST_INORDER(TreeNode *root); 14 | 15 | bool helper(TreeNode *node, TreeNode *&prev); 16 | }; 17 | 18 | #endif // VALIDATE_BINARY_SEARCH_TREE_HPP_ 19 | -------------------------------------------------------------------------------- /src/HouseRobber.cpp: -------------------------------------------------------------------------------- 1 | #include "HouseRobber.hpp" 2 | 3 | int HouseRobber::rob(vector &nums) { 4 | size_t n = nums.size(); 5 | 6 | if (n == 0) return 0; 7 | if (n == 1) return nums[0]; 8 | if (n == 2) return max(nums[0], nums[1]); 9 | 10 | vector dp(n + 1, 0); 11 | dp[1] = nums[0]; 12 | dp[2] = max(nums[0], nums[1]); 13 | 14 | for (int i = 3; i <= n; i++) 15 | dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1]); 16 | 17 | return dp[n]; 18 | } 19 | -------------------------------------------------------------------------------- /tests/WordLadderTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "WordLadder.hpp" 4 | 5 | TEST_CASE("Word Ladder") { 6 | WordLadder s; 7 | SECTION("Sample test") { 8 | string beginWord = "hit"; 9 | string endWord = "cog"; 10 | unordered_set wordList = {"hot", "dot", "dog", "lot", "log"}; 11 | // transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog" 12 | REQUIRE(s.ladderLength(beginWord, endWord, wordList) == 5); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /include/MovingAverageFromDataStream.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MOVING_AVERAGE_FROM_DATA_STREAM_HPP_ 2 | #define MOVING_AVERAGE_FROM_DATA_STREAM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MovingAverage { 9 | public: 10 | /** Initialize your data structure here. */ 11 | MovingAverage(int size); 12 | 13 | double next(int val); 14 | 15 | private: 16 | int total; 17 | int size; 18 | deque data; 19 | }; 20 | 21 | #endif // MOVING_AVERAGE_FROM_DATA_STREAM_HPP_ -------------------------------------------------------------------------------- /src/FindMedianFromDataStream.cpp: -------------------------------------------------------------------------------- 1 | #include "FindMedianFromDataStream.hpp" 2 | 3 | void MedianFinder::addNum(int num) { 4 | small.push(num); 5 | large.push(-small.top()); 6 | small.pop(); 7 | 8 | if (small.size() < large.size()) { 9 | small.push(-large.top()); 10 | large.pop(); 11 | } 12 | } 13 | 14 | double MedianFinder::findMedian() { 15 | return small.size() > large.size() 16 | ? small.top() 17 | : (small.top() - large.top()) / 2.0; 18 | } -------------------------------------------------------------------------------- /src/MinStack.cpp: -------------------------------------------------------------------------------- 1 | #include "MinStack.hpp" 2 | 3 | void MinStack::push(int x) { 4 | wholeStack.push(x); 5 | 6 | if (minStack.empty() || minStack.top() >= x) 7 | minStack.push(x); 8 | } 9 | 10 | void MinStack::pop() { 11 | if (minStack.top() == wholeStack.top()) 12 | minStack.pop(); 13 | 14 | wholeStack.pop(); 15 | } 16 | 17 | int MinStack::top() { 18 | return wholeStack.top(); 19 | } 20 | 21 | int MinStack::getMin() { 22 | return minStack.top(); 23 | } 24 | -------------------------------------------------------------------------------- /src/ValidAnagram.cpp: -------------------------------------------------------------------------------- 1 | #include "ValidAnagram.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | bool ValidAnagram::isAnagram(string s, string t) { 8 | if (s.size() != t.size()) return false; 9 | 10 | vector cnt(26, 0); 11 | 12 | for (int i = 0; i < s.size(); i++) { 13 | cnt[s[i] - 'a']++; 14 | cnt[t[i] - 'a']--; 15 | } 16 | 17 | for (int i = 0; i < 26; i++) 18 | if (cnt[i] != 0) return false; 19 | 20 | return true; 21 | } 22 | -------------------------------------------------------------------------------- /tests/BestTimeToBuyAndSellStockWithCooldownTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "BestTimeToBuyAndSellStockWithCooldown.hpp" 4 | 5 | TEST_CASE("Best Time to Buy and Sell Stock with Cooldown") { 6 | BestTimeToBuyAndSellStockWithCooldown s; 7 | SECTION("Sample tests") { 8 | vector prices1{1, 2, 3, 0, 2}; 9 | REQUIRE(s.maxProfit(prices1) == 3); 10 | vector prices2{6, 1, 3, 2, 4, 7}; 11 | REQUIRE(s.maxProfit(prices2) == 6); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/GuessNumberHigherOrLowerTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "GuessNumberHigherOrLower.hpp" 4 | 5 | TEST_CASE("Guess Number Higher or Lower") { 6 | const int n = 10; 7 | const int pick = 6; 8 | auto guess = [](int g) { 9 | if (pick < g) return -1; 10 | if (pick > g) return 1; 11 | return 0; 12 | }; 13 | GuessNumberHigherOrLower s(guess); 14 | SECTION("Sample test") { 15 | REQUIRE(s.guessNumber(n) == pick); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/ImplementQueueUsingStacksTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ImplementQueueUsingStacks.hpp" 4 | 5 | TEST_CASE("Implement Queue Using Stacks") { 6 | ImplementQueueUsingStacks s; 7 | SECTION("Sample test") { 8 | REQUIRE(s.empty()); 9 | s.push(1); 10 | REQUIRE(s.peek() == 1); 11 | s.push(2); 12 | REQUIRE(s.peek() == 1); 13 | s.pop(); 14 | REQUIRE(s.peek() == 2); 15 | REQUIRE_FALSE(s.empty()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/PowerOfTwoTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "PowerOfTwo.hpp" 4 | 5 | TEST_CASE("Power of Two") { 6 | PowerOfTwo s; 7 | SECTION("Sample tests") { 8 | REQUIRE_FALSE(s.isPowerOfTwo(-1)); 9 | REQUIRE_FALSE(s.isPowerOfTwo(0)); 10 | REQUIRE(s.isPowerOfTwo(1)); 11 | REQUIRE(s.isPowerOfTwo(2)); 12 | REQUIRE_FALSE(s.isPowerOfTwo(3)); 13 | REQUIRE(s.isPowerOfTwo(4)); 14 | REQUIRE_FALSE(s.isPowerOfTwo(5)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /include/RestoreIPAddresses.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RESTORE_IP_ADDRESSES_HPP_ 2 | #define RESTORE_IP_ADDRESSES_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class RestoreIPAddresses { 10 | private: 11 | vector res; 12 | public: 13 | vector restoreIpAddresses(string s); 14 | 15 | private: 16 | void bt(string s, int i, int segment, string addr); 17 | 18 | bool valid(string segment); 19 | }; 20 | 21 | #endif // RESTORE_IP_ADDRESSES_HPP_ 22 | -------------------------------------------------------------------------------- /src/MissingNumber.cpp: -------------------------------------------------------------------------------- 1 | #include "MissingNumber.hpp" 2 | 3 | int MissingNumber::missingNumber(vector &nums) { 4 | int n = nums.size(); 5 | 6 | for (int i = 0; i < n; i++) { 7 | int t = nums[i]; 8 | while (t < n && nums[t] != t) { 9 | int nt = nums[t]; 10 | nums[t] = t; 11 | t = nt; 12 | } 13 | } 14 | 15 | for (int i = 0; i < n; i++) 16 | if (nums[i] != i) 17 | return i; 18 | 19 | return n; 20 | } 21 | -------------------------------------------------------------------------------- /tests/LongestIncreasingSubsequenceTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "LongestIncreasingSubsequence.hpp" 4 | 5 | TEST_CASE("Longest Increasing Subsequence") { 6 | LongestIncreasingSubsequence s; 7 | SECTION("Sample test") { 8 | vector nums{10, 9, 2, 5, 3, 7, 101, 18}; 9 | REQUIRE(s.lengthOfLIS(nums) == 4); 10 | } 11 | SECTION("Non increasing sequence") { 12 | vector nums{2, 2}; 13 | REQUIRE(s.lengthOfLIS(nums) == 1); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/MajorityElementIITest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "MajorityElementII.hpp" 4 | 5 | TEST_CASE("Majority Element II") { 6 | MajorityElementII s; 7 | SECTION("Sample tests") { 8 | vector nums1{1, 2, 3, 4}; 9 | REQUIRE(s.majorityElement(nums1).empty()); 10 | vector nums2{0, 3, 4, 0}; 11 | vector expected2{0}; 12 | vector result2 = s.majorityElement(nums2); 13 | REQUIRE(expected2 == result2); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /include/ExpressionAddOperators.hpp: -------------------------------------------------------------------------------- 1 | #ifndef EXPRESSION_ADD_OPERATORS_HPP_ 2 | #define EXPRESSION_ADD_OPERATORS_HPP_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class ExpressionAddOperators { 10 | public: 11 | vector addOperators(string num, int target); 12 | 13 | private: 14 | void dfs(string num, int target, string expr, long currRes, long prevNum, 15 | vector &res); 16 | }; 17 | 18 | #endif // EXPRESSION_ADD_OPERATORS_HPP_ 19 | -------------------------------------------------------------------------------- /tests/ReverseWordsInAStringTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "ReverseWordsInAString.hpp" 4 | 5 | TEST_CASE("Reverse Words in a String") { 6 | ReverseWordsInAString s; 7 | SECTION("Sample test") { 8 | string str1 = "the sky is blue"; 9 | s.reverseWords(str1); 10 | REQUIRE(str1 == "blue is sky the"); 11 | 12 | string str2 = " the sky is blue "; 13 | s.reverseWords(str2); 14 | REQUIRE(str2 == "blue is sky the"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/SortColorsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | 3 | #include "SortColors.hpp" 4 | 5 | TEST_CASE("Sort Colors") { 6 | SortColors s; 7 | SECTION("Sample test") { 8 | vector nums{0, 1, 1, 0, 2, 1, 0}; 9 | vector expected{0, 0, 0, 1, 1, 1, 2}; 10 | s.sortColors(nums); 11 | REQUIRE(nums == expected); 12 | } 13 | SECTION("Empty input") { 14 | vector nums; 15 | s.sortColors(nums); 16 | REQUIRE(nums.empty()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /include/CountOfSmallerNumbersAfterSelf.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNT_OF_SMALLER_NUMBERS_AFTER_SELF_HPP_ 2 | #define COUNT_OF_SMALLER_NUMBERS_AFTER_SELF_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CountOfSmallerNumbersAfterSelf { 9 | public: 10 | vector countSmaller(vector &nums); 11 | 12 | private: 13 | int query(long index); 14 | 15 | void update(long index, int val); 16 | 17 | vector bit; 18 | }; 19 | 20 | #endif // COUNT_OF_SMALLER_NUMBERS_AFTER_SELF_HPP_ 21 | -------------------------------------------------------------------------------- /include/FindMedianFromDataStream.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FIND_MEDIAN_FROM_DATA_STREAM_HPP_ 2 | #define FIND_MEDIAN_FROM_DATA_STREAM_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MedianFinder { 9 | private: 10 | priority_queue small, large; 11 | 12 | public: 13 | // Adds a number into the data structure. 14 | void addNum(int num); 15 | 16 | // Returns the median of current data stream 17 | double findMedian(); 18 | }; 19 | 20 | #endif // FIND_MEDIAN_FROM_DATA_STREAM_HPP_ -------------------------------------------------------------------------------- /include/MaxPointsOnALine.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAX_POINTS_ON_A_LINE_HPP_ 2 | #define MAX_POINTS_ON_A_LINE_HPP_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct Point { 9 | int x; 10 | int y; 11 | 12 | Point() : x(0), y(0) {} 13 | 14 | Point(int a, int b) : x(a), y(b) {} 15 | }; 16 | 17 | class MaxPointsOnALine { 18 | public: 19 | int maxPoints(vector &points); 20 | 21 | private: 22 | int generateGCD(int a, int b); 23 | }; 24 | 25 | #endif // MAX_POINTS_ON_A_LINE_HPP_ -------------------------------------------------------------------------------- /src/IsomorphicStrings.cpp: -------------------------------------------------------------------------------- 1 | #include "IsomorphicStrings.hpp" 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | bool IsomorphicStrings::isIsomorphic(string s, string t) { 8 | if (s.size() != t.size()) return false; 9 | 10 | vector sMap(256, 0), tMap(256, 0); 11 | 12 | for (int i = 0; i < s.size(); i++) { 13 | if (sMap[s[i]] != tMap[t[i]]) 14 | return false; 15 | 16 | sMap[s[i]] = tMap[t[i]] = i + 1; 17 | } 18 | 19 | return true; 20 | } 21 | 22 | --------------------------------------------------------------------------------