├── .gitignore ├── .travis.yml ├── README.md ├── build.xml ├── doc ├── README.md ├── category_basic.md ├── category_bfs.md ├── category_binarysearch.md ├── category_dfs.md ├── category_dp.md ├── category_largescale.md ├── category_linkedlist.md ├── category_math.md ├── category_monotonicstack.md ├── category_partition.md ├── category_similar.md ├── category_subarray.md ├── category_tree.md ├── problems.tsv └── road.md ├── lib ├── jsoup-1.8.2.jar ├── junit.jar └── org.hamcrest.core_1.3.0.v201303031735.jar ├── src ├── _001_TwoSum │ ├── Practice.java │ └── Solution.java ├── _002_AddTwoNumbers │ ├── Practice.java │ └── Solution.java ├── _003_LongestSubstringWithoutRepeatingCharacters │ ├── Practice.java │ └── Solution.java ├── _004_MedianOfTwoSortedArrays │ ├── Practice.java │ └── Solution.java ├── _005_LongestPalindromicSubstring │ ├── Practice.java │ └── Solution.java ├── _006_ZigZagConversion │ ├── Practice.java │ └── Solution.java ├── _007_ReverseInteger │ ├── Practice.java │ └── Solution.java ├── _009_PalindromeNumber │ ├── Practice.java │ └── Solution.java ├── _010_RegularExpressionMatching │ ├── Practice.java │ ├── Solution.java │ ├── SolutionDFS.java │ └── SolutionMemo.java ├── _011_ContainerWithMostWater │ ├── Practice.java │ └── Solution.java ├── _012_IntegerToRoman │ ├── Practice.java │ └── Solution.java ├── _013_RomanToInteger │ ├── Practice.java │ └── Solution.java ├── _014_LongestCommonPrefix │ ├── Practice.java │ └── Solution.java ├── _015_3Sum │ ├── Practice.java │ └── Solution.java ├── _016_3SumClosest │ ├── Practice.java │ └── Solution.java ├── _017_LetterCombinationsOfAPhoneNumber │ ├── Practice.java │ ├── Solution.java │ └── SolutionBacktracking.java ├── _018_4Sum │ ├── Practice.java │ └── Solution.java ├── _019_RemoveNthNodeFromEndOfList │ ├── Practice.java │ └── Solution.java ├── _020_ValidParentheses │ ├── Practice.java │ ├── Solution.java │ └── SolutionVerbose.java ├── _021_MergeTwoSortedLists │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _022_GenerateParentheses │ ├── Practice.java │ ├── Solution.java │ └── SolutionDP.java ├── _023_MergeKSortedLists │ ├── Practice.java │ ├── Solution.java │ └── SolutionHeap.java ├── _024_SwapNodesInPairs │ ├── Practice.java │ └── Solution.java ├── _026_RemoveDuplicatesFromSortedArray │ ├── Practice.java │ └── Solution.java ├── _027_RemoveElement │ ├── Practice.java │ ├── Solution.java │ └── SolutionPartition.java ├── _028_ImplementStrStr │ ├── Practice.java │ └── Solution.java ├── _029_DivideTwoIntegers │ ├── Practice.java │ └── Solution.java ├── _030_SubstringWithConcatenationOfAllWords │ ├── Practice.java │ └── Solution.java ├── _031_NextPermutation │ ├── Practice.java │ └── Solution.java ├── _032_LongestValidParentheses │ ├── Practice.java │ └── Solution.java ├── _033_SearchInRotatedSortedArray │ ├── Practice.java │ ├── README.md │ └── Solution.java ├── _034_SearchForARange │ ├── Practice.java │ ├── Solution.java │ ├── SolutionOneBinarySearch.java │ └── SolutionSlow.java ├── _035_SearchInsertPosition │ ├── Practice.java │ ├── README.md │ └── Solution.java ├── _036_ValidSudoku │ ├── Practice.java │ ├── Solution.java │ └── SolutionBad.java ├── _037_SudokuSolver │ ├── Practice.java │ └── Solution.java ├── _038_CountAndSay │ ├── Practice.java │ └── Solution.java ├── _039_CombinationSum │ ├── Practice.java │ └── Solution.java ├── _040_CombinationSumII │ ├── Practice.java │ └── Solution.java ├── _041_FirstMissingPositive │ ├── Practice.java │ └── Solution.java ├── _042_TrappingRainWater │ ├── Practice.java │ ├── Solution.java │ └── SolutionStack.java ├── _043_MultiplyStrings │ ├── Practice.java │ ├── Solution.java │ └── SolutionBigInteger.java ├── _044_WildcardMatching │ ├── Practice.java │ ├── Solution.java │ ├── SolutionDP.java │ ├── SolutionDP2.java │ ├── SolutionMemo.java │ └── SolutionRecursive.java ├── _045_JumpGameII │ ├── Practice.java │ ├── Solution.java │ └── SolutionDP.java ├── _046_Permutations │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _047_PermutationsII │ ├── Practice.java │ ├── Solution.java │ ├── SolutionIterative.java │ └── SolutionIterativeSet.java ├── _048_RotateImage │ ├── Practice.java │ ├── Solution.java │ └── SolutionTwoSteps.java ├── _049_GroupAnagrams │ ├── Practice.java │ ├── Solution.java │ └── SolutionTLE.java ├── _050_PowxN │ ├── Practice.java │ └── Solution.java ├── _051_NQueens │ ├── Practice.java │ └── Solution.java ├── _052_NQueensII │ ├── Practice.java │ └── Solution.java ├── _053_MaximumSubarray │ ├── Practice.java │ └── Solution.java ├── _054_SpiralMatrix │ ├── Practice.java │ └── Solution.java ├── _055_JumpGame │ ├── Practice.java │ ├── Solution.java │ └── SolutionDP.java ├── _056_MergeIntervals │ ├── Practice.java │ └── Solution.java ├── _057_InsertInterval │ ├── Practice.java │ └── Solution.java ├── _058_LengthOfLastWord │ ├── Practice.java │ ├── Solution.java │ └── SolutionScan.java ├── _059_SpiralMatrixII │ ├── Practice.java │ ├── Solution.java │ └── SolutionDirection.java ├── _060_PermutationSequence │ ├── Practice.java │ └── Solution.java ├── _061_RotateList │ ├── Practice.java │ └── Solution.java ├── _062_UniquePaths │ ├── Practice.java │ ├── Solution.java │ ├── Solution1D.java │ └── SolutionMath.java ├── _063_UniquePathsII │ ├── Practice.java │ └── Solution.java ├── _064_MinimumPathSum │ ├── Practice.java │ └── Solution.java ├── _065_ValidNumber │ ├── Practice.java │ └── Solution.java ├── _066_PlusOne │ ├── Practice.java │ └── Solution.java ├── _067_AddBinary │ ├── Practice.java │ └── Solution.java ├── _068_TextJustification │ ├── Practice.java │ └── Solution.java ├── _069_Sqrtx │ ├── Practice.java │ ├── Solution.java │ ├── SolutionNewton.java │ └── SolutionTemplate.java ├── _070_ClimbingStairs │ ├── Practice.java │ ├── Solution.java │ └── SolutionMemo.java ├── _071_SimplifyPath │ ├── Practice.java │ ├── Solution.java │ └── SolutionParsing.java ├── _072_EditDistance │ ├── Practice.java │ ├── Solution.java │ ├── SolutionMemo.java │ ├── SolutionPrefix.java │ └── SolutionSuffix.java ├── _073_SetMatrixZeroes │ ├── Practice.java │ └── Solution.java ├── _074_SearchA2DMatrix │ ├── Practice.java │ ├── Solution.java │ └── SolutionConvertTo1D.java ├── _075_SortColors │ ├── Practice.java │ └── Solution.java ├── _076_MinimumWindowSubstring │ ├── Practice.java │ └── Solution.java ├── _077_Combinations │ ├── Practice.java │ └── Solution.java ├── _078_Subsets │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ └── SolutionRecursive.java ├── _079_WordSearch │ ├── Practice.java │ └── Solution.java ├── _080_RemoveDuplicatesFromSortedArrayII │ ├── Practice.java │ ├── Solution.java │ ├── SolutionGeneral.java │ └── SolutionGeneral2.java ├── _081_SearchInRotatedSortedArrayII │ ├── Practice.java │ └── Solution.java ├── _082_RemoveDuplicatesFromSortedListII │ ├── Practice.java │ ├── Solution.java │ ├── SolutionCount.java │ └── SolutionTail.java ├── _083_RemoveDuplicatesFromSortedList │ ├── Practice.java │ └── Solution.java ├── _084_LargestRectangleInHistogram │ ├── Practice.java │ ├── Solution.java │ └── SolutionTLE.java ├── _085_MaximalRectangle │ ├── Practice.java │ ├── Solution.java │ └── SolutionStack.java ├── _086_PartitionList │ ├── Practice.java │ └── Solution.java ├── _087_ScrambleString │ ├── Practice.java │ └── Solution.java ├── _088_MergeSortedArray │ ├── Practice.java │ └── Solution.java ├── _089_GrayCode │ ├── Practice.java │ ├── Solution.java │ └── SolutionIterative.java ├── _090_SubsetsII │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _091_DecodeWays │ ├── Practice.java │ ├── Solution.java │ ├── SolutionMemo.java │ └── SolutionSuffix.java ├── _092_ReverseLinkedListII │ ├── Practice.java │ └── Solution.java ├── _093_RestoreIPAddresses │ ├── Practice.java │ └── Solution.java ├── _094_BinaryTreeInorderTraversal │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _095_UniqueBinarySearchTreesII │ ├── Practice.java │ └── Solution.java ├── _096_UniqueBinarySearchTrees │ ├── Practice.java │ ├── Solution.java │ └── SolutionDFS.java ├── _097_InterleavingString │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ ├── SolutionBottomUp.java │ ├── SolutionDFS.java │ └── SolutionMemo.java ├── _098_ValidateBinarySearchTree │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ ├── SolutionNotGood.java │ └── SolutionPrev.java ├── _099_RecoverBinarySearchTree │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ ├── SolutionMorris.java │ └── SolutionN.java ├── _100_SameTree │ ├── Practice.java │ └── Solution.java ├── _101_SymmetricTree │ ├── Practice.java │ └── Solution.java ├── _102_BinaryTreeLevelOrderTraversal │ ├── Practice.java │ └── Solution.java ├── _103_BinaryTreeZigzagLevelOrderTraversal │ ├── Practice.java │ └── Solution.java ├── _104_MaximumDepthOfBinaryTree │ ├── Practice.java │ ├── Solution.java │ └── SolutionIterative.java ├── _105_ConstructBinaryTreeFromPreorderAndInorderTraversal │ ├── Practice.java │ └── Solution.java ├── _106_ConstructBinaryTreeFromInorderAndPostorderTraversal │ ├── Practice.java │ └── Solution.java ├── _107_BinaryTreeLevelOrderTraversalII │ ├── Practice.java │ └── Solution.java ├── _108_ConvertSortedArrayToBinarySearchTree │ ├── Practice.java │ └── Solution.java ├── _109_ConvertSortedListToBinarySearchTree │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _110_BalancedBinaryTree │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ └── SolutionNlgN.java ├── _111_MinimumDepthOfBinaryTree │ ├── Practice.java │ └── Solution.java ├── _112_PathSum │ ├── Practice.java │ └── Solution.java ├── _113_PathSumII │ ├── Practice.java │ ├── Solution.java │ └── SolutionWrong.java ├── _114_FlattenBinaryTreeToLinkedList │ ├── Practice.java │ └── Solution.java ├── _115_DistinctSubsequences │ ├── Practice.java │ ├── Solution.java │ ├── SolutionDFS.java │ └── SolutionMemo.java ├── _116_PopulatingNextRightPointersInEachNode │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _117_PopulatingNextRightPointersInEachNodeII │ ├── Practice.java │ └── Solution.java ├── _118_PascalsTriangle │ ├── Practice.java │ └── Solution.java ├── _119_PascalsTriangleII │ ├── Practice.java │ └── Solution.java ├── _120_Triangle │ ├── Practice.java │ ├── Solution.java │ └── Solution2.java ├── _121_BestTimeToBuyAndSellStock │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ └── Solution2.java ├── _122_BestTimeToBuyAndSellStockII │ ├── Practice.java │ └── Solution.java ├── _123_BestTimeToBuyAndSellStockIII │ ├── Practice.java │ ├── Solution.java │ └── Test8Input.txt ├── _124_BinaryTreeMaximumPathSum │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ └── SolutionTLE.java ├── _125_ValidPalindrome │ ├── Practice.java │ └── Solution.java ├── _126_WordLadderII │ ├── Practice.java │ ├── Solution.java │ ├── SolutionBottomUp.java │ └── SolutionMLE.java ├── _127_WordLadder │ ├── Practice.java │ ├── Solution.java │ └── SolutionOneEnd.java ├── _128_LongestConsecutiveSequence │ ├── Practice.java │ └── Solution.java ├── _129_SumRootToLeafNumbers │ ├── Practice.java │ └── Solution.java ├── _130_SurroundedRegions │ ├── Practice.java │ └── Solution.java ├── _131_PalindromePartitioning │ ├── Practice.java │ ├── Solution.java │ └── SolutionDP.java ├── _132_PalindromePartitioningII │ ├── Practice.java │ └── Solution.java ├── _133_CloneGraph │ ├── Practice.java │ ├── Solution.java │ └── SolutionDFS.java ├── _135_Candy │ ├── Practice.java │ └── Solution.java ├── _136_SingleNumber │ ├── Practice.java │ └── Solution.java ├── _138_CopyListWithRandomPointer │ ├── Practice.java │ ├── RandomListNode.java │ ├── Solution.java │ └── SolutionBFS.java ├── _139_WordBreak │ ├── Practice.java │ ├── Solution.java │ ├── SolutionDFS.java │ └── SolutionMemo.java ├── _140_WordBreakII │ ├── Practice.java │ ├── Solution.java │ └── SolutionBottomup.java ├── _141_LinkedListCycle │ ├── Practice.java │ ├── PracticeTest.java │ ├── Solution.java │ └── SolutionTest.java ├── _142_LinkedListCycleII │ ├── Practice.java │ ├── Solution.java │ └── SolutionSet.java ├── _143_ReorderList │ ├── Practice.java │ └── Solution.java ├── _144_BinaryTreePreorderTraversal │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ └── SolutionRecursive.java ├── _145_BinaryTreePostorderTraversal │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _146_LRUCache │ ├── Practice.java │ └── Solution.java ├── _147_InsertionSortList │ ├── Practice.java │ └── Solution.java ├── _148_SortList │ ├── Practice.java │ └── Solution.java ├── _149_MaxPointsOnALine │ ├── Practice.java │ ├── Solution.java │ └── SolutionSort.java ├── _150_EvaluateReversePolishNotation │ ├── Practice.java │ └── Solution.java ├── _151_ReverseWordsInAString │ ├── Practice.java │ ├── README.md │ ├── Solution.java │ └── SolutionAdvanced.java ├── _152_MaximumProductSubarray │ ├── Practice.java │ ├── Solution.java │ └── SolutionN.java ├── _153_FindMinimumInRotatedSortedArray │ ├── Practice.java │ ├── Solution.java │ └── SolutionTemplate.java ├── _154_FindMinimumInRotatedSortedArrayII │ ├── Practice.java │ └── Solution.java ├── _155_MinStack │ ├── Practice.java │ └── Solution.java ├── _157_ReadNCharactersGivenRead4 │ ├── Practice.java │ ├── Reader4.java │ └── Solution.java ├── _158_ReadNCharactersGivenRead4II │ ├── Practice.java │ └── Solution.java ├── _159_LongestSubstringWithAtMostTwoDistinctCharacters │ ├── Practice.java │ └── Solution.java ├── _160_IntersectionOfTwoLinkedLists │ ├── Practice.java │ ├── Solution.java │ └── SolutionLength.java ├── _161_OneEditDistance │ ├── Practice.java │ ├── Solution.java │ └── SolutionDP.java ├── _162_FindPeakElement │ ├── Practice.java │ └── Solution.java ├── _163_MissingRanges │ ├── Practice.java │ └── Solution.java ├── _164_MaximumGap │ ├── Practice.java │ └── Solution.java ├── _165_CompareVersionNumbers │ ├── Practice.java │ └── Solution.java ├── _166_FractionToRecurringDecimal │ ├── Practice.java │ └── Solution.java ├── _168_ExcelSheetColumnTitle │ ├── Practice.java │ └── Solution.java ├── _171_ExcelSheetColumnNumber │ ├── Practice.java │ └── Solution.java ├── _172_FactorialTrailingZeroes │ ├── Practice.java │ └── Solution.java ├── _173_BinarySearchTreeIterator │ ├── Practice.java │ └── Solution.java ├── _174_DungeonGame │ ├── Solution.java │ └── SolutionTest.java ├── _179_LargestNumber │ ├── Practice.java │ └── Solution.java ├── _186_ReverseWordsInAStringII │ ├── Practice.java │ └── Solution.java ├── _188_BestTimeToBuyAndSellStockIV │ └── Solution.java ├── _189_RotateArray │ ├── Practice.java │ └── Solution.java ├── _199_BinaryTreeRightSideView │ ├── Practice.java │ ├── Solution.java │ ├── SolutionOneQueue.java │ └── SolutionRecursive.java ├── _200_NumberOfIslands │ ├── Practice.java │ ├── Solution.java │ ├── SolutionBFS.java │ ├── SolutionBFSPoint.java │ └── SolutionWrong.java ├── _203_RemoveLinkedListElements │ ├── Practice.java │ └── Solution.java ├── _204_CountPrimes │ ├── Practice.java │ └── Solution.java ├── _206_ReverseLinkedList │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _207_CourseSchedule │ ├── Practice.java │ ├── Solution.java │ ├── SolutionDFS.java │ └── SolutionTLE.java ├── _208_ImplementTriePrefixTree │ ├── Practice.java │ ├── README.md │ └── Solution.java ├── _209_MinimumSizeSubarraySum │ ├── Practice.java │ ├── Solution.java │ └── SolutionSlidingWindow.java ├── _210_CourseScheduleII │ ├── Practice.java │ └── Solution.java ├── _211_AddAndSearchWordDataStructureDesign │ ├── Practice.java │ ├── README.md │ └── Solution.java ├── _212_WordSearchII │ ├── Practice.java │ └── Solution.java ├── _213_HouseRobberII │ ├── Practice.java │ └── Solution.java ├── _215_KthLargestElementInAnArray │ ├── Practice.java │ ├── Solution.java │ ├── SolutionNlgK.java │ └── SolutionNlgN.java ├── _216_CombinationSumIII │ ├── Practice.java │ └── Solution.java ├── _217_ContainsDuplicate │ ├── Practice.java │ └── Solution.java ├── _218_TheSkylineProblem │ ├── Practice.java │ ├── Solution.java │ └── SolutionHeap.java ├── _219_ContainsDuplicateII │ ├── Practice.java │ ├── Solution.java │ └── SolutionSet.java ├── _220_ContainsDuplicateIII │ ├── Practice.java │ └── Solution.java ├── _221_MaximalSquare │ ├── Practice.java │ └── Solution.java ├── _223_RectangleArea │ ├── Practice.java │ └── Solution.java ├── _224_BasicCalculator │ ├── Practice.java │ └── Solution.java ├── _225_ImplementStackUsingQueues │ ├── Practice.java │ ├── Solution.java │ └── SolutionTwoQueues.java ├── _228_SummaryRanges │ ├── Practice.java │ └── Solution.java ├── _229_MajorityElementII │ ├── Practice.java │ └── Solution.java ├── _230_KthSmallestElementInABST │ ├── Practice.java │ └── Solution.java ├── _231_PowerOfTwo │ ├── Practice.java │ └── Solution.java ├── _232_ImplementQueueUsingStacks │ ├── Practice.java │ ├── README.md │ └── Solution.java ├── _235_LowestCommonAncestorOfABinarySearchTree │ ├── Practice.java │ └── Solution.java ├── _236_LowestCommonAncestorOfABinaryTree │ ├── Practice.java │ ├── README.md │ └── Solution.java ├── _237_DeleteNodeInALinkedList │ ├── Practice.java │ └── Solution.java ├── _238_ProductOfArrayExceptSelf │ ├── Practice.java │ ├── Solution.java │ └── SolutionLinearSpace.java ├── _239_SlidingWindowMaximum │ ├── Practice.java │ └── Solution.java ├── _240_SearchA2DMatrixII │ ├── Practice.java │ └── Solution.java ├── _242_ValidAnagram │ ├── Practice.java │ ├── Solution.java │ └── SolutionSort.java ├── _246_StrobogrammaticNumber │ ├── Practice.java │ └── Solution.java ├── _251_Flatten2DVector │ ├── Practice.java │ └── Solution.java ├── _252_MeetingRooms │ ├── Practice.java │ └── Solution.java ├── _253_MeetingRoomsII │ ├── Practice.java │ └── Solution.java ├── _256_PaintHouse │ ├── Practice.java │ └── Solution.java ├── _261_GraphValidTree │ ├── Practice.java │ ├── Solution.java │ └── SolutionBFS.java ├── _265_PaintHouseII │ ├── Practice.java │ ├── Solution.java │ └── SolutionSlow.java ├── _268_MissingNumber │ ├── Practice.java │ ├── Solution.java │ └── SolutionSwap.java ├── _269_AlienDictionary │ ├── Practice.java │ └── Solution.java ├── _273_IntegerToEnglishWords │ ├── Practice.java │ └── Solution.java ├── _274_HIndex │ ├── Practice.java │ └── Solution.java ├── _275_HIndexII │ ├── Practice.java │ ├── Solution.java │ └── SolutionTemplate.java ├── _276_PaintFence │ ├── Solution.java │ └── Solution2.java ├── _278_FirstBadVersion │ ├── Practice.java │ ├── Solution.java │ └── SolutionTwo.java ├── _281_ZigzagIterator │ ├── Practice.java │ └── Solution.java ├── _283_MoveZeroes │ ├── Practice.java │ ├── Solution.java │ └── SolutionCount.java ├── _284_PeekingIterator │ └── Solution.java ├── _285_InorderSuccessorInBST │ ├── Practice.java │ ├── Solution.java │ └── SolutionRecursive.java ├── _286_WallsAndGates │ ├── Practice.java │ ├── Solution.java │ └── SolutionSlow.java ├── com │ └── leetcode │ │ ├── Helper.java │ │ ├── Interval.java │ │ ├── Leetcode.java │ │ ├── ListNode.java │ │ ├── Point.java │ │ ├── Problem.java │ │ ├── Progress.java │ │ ├── Test.java │ │ ├── TreeLinkNode.java │ │ ├── TreeNode.java │ │ └── UndirectedGraphNode.java ├── s01_SubarraySum │ └── Solution.java ├── s02_DiameterOfBinaryTree │ └── Solution.java ├── s03_LongestCommonSequence │ ├── Solution.java │ └── SolutionTest.java ├── s04_LongestIncreasingContinuousSubsequence │ └── Solution.java ├── s05_LongestIncreasingContinuousSubsequenceII │ └── Solution.java ├── s06_LongestIncreasingContinuousSubsequenceIII │ └── Solution.java ├── s07_MedianInDataStream │ └── Solution.java ├── s09_ConvertTernaryExpressionToBinaryTree │ └── Solution.java ├── s10_MostFrequentKNumbers │ └── Solution.java ├── s11_ReplacePermutation │ ├── Solution.java │ └── Solution2.java └── s12_FindArithmeticMissing │ └── Solution.java └── test ├── _001_TwoSum ├── PracticeTest.java └── SolutionTest.java ├── _002_AddTwoNumbers ├── PracticeTest.java └── SolutionTest.java ├── _003_LongestSubstringWithoutRepeatingCharacters ├── PracticeTest.java └── SolutionTest.java ├── _004_MedianOfTwoSortedArrays ├── PracticeTest.java └── SolutionTest.java ├── _005_LongestPalindromicSubstring ├── PracticeTest.java └── SolutionTest.java ├── _006_ZigZagConversion ├── PracticeTest.java └── SolutionTest.java ├── _007_ReverseInteger ├── PracticeTest.java └── SolutionTest.java ├── _009_PalindromeNumber ├── PracticeTest.java └── SolutionTest.java ├── _010_RegularExpressionMatching ├── PracticeTest.java ├── SolutionDFSTest.java ├── SolutionMemoTest.java └── SolutionTest.java ├── _011_ContainerWithMostWater ├── PracticeTest.java └── SolutionTest.java ├── _012_IntegerToRoman ├── PracticeTest.java └── SolutionTest.java ├── _013_RomanToInteger ├── PracticeTest.java └── SolutionTest.java ├── _014_LongestCommonPrefix ├── PracticeTest.java └── SolutionTest.java ├── _015_3Sum ├── PracticeTest.java └── SolutionTest.java ├── _016_3SumClosest ├── PracticeTest.java └── SolutionTest.java ├── _017_LetterCombinationsOfAPhoneNumber ├── PracticeTest.java ├── SolutionBacktrackingTest.java └── SolutionTest.java ├── _018_4Sum ├── PracticeTest.java └── SolutionTest.java ├── _019_RemoveNthNodeFromEndOfList ├── PracticeTest.java └── SolutionTest.java ├── _020_ValidParentheses ├── PracticeTest.java ├── SolutionTest.java └── SolutionVerboseTest.java ├── _021_MergeTwoSortedLists ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _022_GenerateParentheses ├── PracticeTest.java ├── SolutionDPTest.java └── SolutionTest.java ├── _023_MergeKSortedLists ├── PracticeTest.java ├── SolutionHeapTest.java └── SolutionTest.java ├── _024_SwapNodesInPairs ├── PracticeTest.java └── SolutionTest.java ├── _026_RemoveDuplicatesFromSortedArray ├── PracticeTest.java └── SolutionTest.java ├── _027_RemoveElement ├── PracticeTest.java ├── SolutionPartitionTest.java └── SolutionTest.java ├── _028_ImplementStrStr ├── PracticeTest.java └── SolutionTest.java ├── _029_DivideTwoIntegers ├── PracticeTest.java └── SolutionTest.java ├── _030_SubstringWithConcatenationOfAllWords ├── PracticeTest.java └── SolutionTest.java ├── _031_NextPermutation ├── PracticeTest.java └── SolutionTest.java ├── _032_LongestValidParentheses ├── PracticeTest.java └── SolutionTest.java ├── _033_SearchInRotatedSortedArray ├── PracticeTest.java └── SolutionTest.java ├── _034_SearchForARange ├── PracticeTest.java ├── SolutionOneBinarySearchTest.java ├── SolutionSlowTest.java └── SolutionTest.java ├── _035_SearchInsertPosition ├── PracticeTest.java └── SolutionTest.java ├── _036_ValidSudoku ├── PracticeTest.java ├── SolutionBadTest.java └── SolutionTest.java ├── _037_SudokuSolver ├── PracticeTest.java └── SolutionTest.java ├── _038_CountAndSay ├── PracticeTest.java └── SolutionTest.java ├── _039_CombinationSum ├── PracticeTest.java └── SolutionTest.java ├── _040_CombinationSumII ├── PracticeTest.java └── SolutionTest.java ├── _041_FirstMissingPositive ├── PracticeTest.java └── SolutionTest.java ├── _042_TrappingRainWater ├── PracticeTest.java ├── SolutionStackTest.java └── SolutionTest.java ├── _043_MultiplyStrings ├── PracticeTest.java ├── SolutionBigIntegerTest.java └── SolutionTest.java ├── _044_WildcardMatching ├── PracticeTest.java ├── SolutionDP2Test.java ├── SolutionDPTest.java ├── SolutionMemoTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _045_JumpGameII ├── PracticeTest.java ├── SolutionDPTest.java ├── SolutionTest.java └── input3.txt ├── _046_Permutations ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _047_PermutationsII ├── PracticeTest.java ├── SolutionIterativeSetTest.java ├── SolutionIterativeTest.java └── SolutionTest.java ├── _048_RotateImage ├── PracticeTest.java ├── SolutionTest.java └── SolutionTwoStepsTest.java ├── _049_GroupAnagrams ├── PracticeTest.java ├── SolutionTest.java ├── input5.txt └── output5.txt ├── _050_PowxN ├── PracticeTest.java └── SolutionTest.java ├── _051_NQueens ├── PracticeTest.java └── SolutionTest.java ├── _052_NQueensII ├── PracticeTest.java └── SolutionTest.java ├── _053_MaximumSubarray ├── PracticeTest.java └── SolutionTest.java ├── _054_SpiralMatrix ├── PracticeTest.java └── SolutionTest.java ├── _055_JumpGame ├── Input3.txt ├── PracticeTest.java ├── SolutionDPTest.java └── SolutionTest.java ├── _056_MergeIntervals ├── PracticeTest.java └── SolutionTest.java ├── _057_InsertInterval ├── PracticeTest.java └── SolutionTest.java ├── _058_LengthOfLastWord ├── PracticeTest.java ├── SolutionScanTest.java └── SolutionTest.java ├── _059_SpiralMatrixII ├── PracticeTest.java ├── SolutionDirectionTest.java └── SolutionTest.java ├── _060_PermutationSequence ├── PracticeTest.java └── SolutionTest.java ├── _061_RotateList ├── PracticeTest.java └── SolutionTest.java ├── _062_UniquePaths ├── PracticeTest.java ├── Solution1DTest.java ├── SolutionMathTest.java └── SolutionTest.java ├── _063_UniquePathsII ├── PracticeTest.java └── SolutionTest.java ├── _064_MinimumPathSum ├── PracticeTest.java └── SolutionTest.java ├── _065_ValidNumber ├── PracticeTest.java └── SolutionTest.java ├── _066_PlusOne ├── PracticeTest.java └── SolutionTest.java ├── _067_AddBinary ├── PracticeTest.java └── SolutionTest.java ├── _068_TextJustification ├── PracticeTest.java └── SolutionTest.java ├── _069_Sqrtx ├── PracticeTest.java ├── SolutionNewtonTest.java ├── SolutionTemplateTest.java └── SolutionTest.java ├── _070_ClimbingStairs ├── PracticeTest.java ├── SolutionMemoTest.java └── SolutionTest.java ├── _071_SimplifyPath ├── PracticeTest.java ├── SolutionParsingTest.java └── SolutionTest.java ├── _072_EditDistance ├── PracticeTest.java ├── SolutionMemoTest.java ├── SolutionPrefixTest.java ├── SolutionSuffixTest.java └── SolutionTest.java ├── _073_SetMatrixZeroes ├── PracticeTest.java └── SolutionTest.java ├── _074_SearchA2DMatrix ├── PracticeTest.java ├── SolutionConvertTo1DTest.java └── SolutionTest.java ├── _075_SortColors ├── PracticeTest.java └── SolutionTest.java ├── _076_MinimumWindowSubstring ├── PracticeTest.java └── SolutionTest.java ├── _077_Combinations ├── PracticeTest.java └── SolutionTest.java ├── _078_Subsets ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _079_WordSearch ├── PracticeTest.java └── SolutionTest.java ├── _080_RemoveDuplicatesFromSortedArrayII ├── PracticeTest.java ├── SolutionGeneral2Test.java ├── SolutionGeneralTest.java └── SolutionTest.java ├── _081_SearchInRotatedSortedArrayII ├── PracticeTest.java └── SolutionTest.java ├── _082_RemoveDuplicatesFromSortedListII ├── PracticeTest.java ├── SolutionCountTest.java ├── SolutionTailTest.java └── SolutionTest.java ├── _083_RemoveDuplicatesFromSortedList ├── PracticeTest.java └── SolutionTest.java ├── _084_LargestRectangleInHistogram ├── PracticeTest.java ├── SolutionTLETest.java └── SolutionTest.java ├── _085_MaximalRectangle ├── PracticeTest.java ├── SolutionStackTest.java └── SolutionTest.java ├── _086_PartitionList ├── PracticeTest.java └── SolutionTest.java ├── _087_ScrambleString ├── PracticeTest.java └── SolutionTest.java ├── _088_MergeSortedArray ├── PracticeTest.java └── SolutionTest.java ├── _089_GrayCode ├── PracticeTest.java ├── SolutionIterativeTest.java └── SolutionTest.java ├── _090_SubsetsII ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _091_DecodeWays ├── PracticeTest.java ├── SolutionMemoTest.java ├── SolutionSuffixTest.java └── SolutionTest.java ├── _092_ReverseLinkedListII ├── PracticeTest.java └── SolutionTest.java ├── _093_RestoreIPAddresses ├── PracticeTest.java └── SolutionTest.java ├── _094_BinaryTreeInorderTraversal ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _095_UniqueBinarySearchTreesII ├── PracticeTest.java └── SolutionTest.java ├── _096_UniqueBinarySearchTrees ├── PracticeTest.java ├── SolutionDFSTest.java └── SolutionTest.java ├── _097_InterleavingString ├── PracticeTest.java ├── SolutionBottomUpTest.java ├── SolutionDFSTest.java ├── SolutionMemoTest.java └── SolutionTest.java ├── _098_ValidateBinarySearchTree ├── PracticeTest.java ├── SolutionNotGoodest.java ├── SolutionPrevTest.java └── SolutionTest.java ├── _099_RecoverBinarySearchTree ├── PracticeTest.java ├── SolutionMorrisTest.java ├── SolutionNTest.java └── SolutionTest.java ├── _100_SameTree ├── PracticeTest.java └── SolutionTest.java ├── _101_SymmetricTree ├── PracticeTest.java └── SolutionTest.java ├── _102_BinaryTreeLevelOrderTraversal ├── PracticeTest.java └── SolutionTest.java ├── _103_BinaryTreeZigzagLevelOrderTraversal ├── PracticeTest.java └── SolutionTest.java ├── _104_MaximumDepthOfBinaryTree ├── PracticeTest.java ├── SolutionIterativeTest.java └── SolutionTest.java ├── _105_ConstructBinaryTreeFromPreorderAndInorderTraversal ├── PracticeTest.java └── SolutionTest.java ├── _106_ConstructBinaryTreeFromInorderAndPostorderTraversal ├── PracticeTest.java └── SolutionTest.java ├── _107_BinaryTreeLevelOrderTraversalII ├── PracticeTest.java └── SolutionTest.java ├── _108_ConvertSortedArrayToBinarySearchTree ├── PracticeTest.java └── SolutionTest.java ├── _109_ConvertSortedListToBinarySearchTree ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _110_BalancedBinaryTree ├── PracticeTest.java ├── SolutionNlgNTest.java └── SolutionTest.java ├── _111_MinimumDepthOfBinaryTree ├── PracticeTest.java └── SolutionTest.java ├── _112_PathSum ├── PracticeTest.java └── SolutionTest.java ├── _113_PathSumII ├── PracticeTest.java └── SolutionTest.java ├── _114_FlattenBinaryTreeToLinkedList ├── PracticeTest.java └── SolutionTest.java ├── _115_DistinctSubsequences ├── PracticeTest.java ├── SolutionDFSTest.java ├── SolutionMemoTest.java └── SolutionTest.java ├── _116_PopulatingNextRightPointersInEachNode ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _117_PopulatingNextRightPointersInEachNodeII ├── PracticeTest.java └── SolutionTest.java ├── _118_PascalsTriangle ├── PracticeTest.java └── SolutionTest.java ├── _119_PascalsTriangleII ├── PracticeTest.java └── SolutionTest.java ├── _120_Triangle ├── PracticeTest.java ├── Solution2Test.java └── SolutionTest.java ├── _121_BestTimeToBuyAndSellStock ├── PracticeTest.java ├── Solution2Test.java └── SolutionTest.java ├── _122_BestTimeToBuyAndSellStockII ├── PracticeTest.java └── SolutionTest.java ├── _123_BestTimeToBuyAndSellStockIII ├── PracticeTest.java └── SolutionTest.java ├── _124_BinaryTreeMaximumPathSum ├── PracticeTest.java ├── SolutionTLETest.java └── SolutionTest.java ├── _125_ValidPalindrome ├── PracticeTest.java └── SolutionTest.java ├── _126_WordLadderII ├── PracticeTest.java ├── SolutionBottomUpTest.java ├── SolutionMLETest.java ├── SolutionTest.java ├── Test7Result ├── Test8Input └── Test8Result ├── _127_WordLadder ├── PracticeTest.java ├── SolutionOneEndTest.java └── SolutionTest.java ├── _128_LongestConsecutiveSequence ├── PracticeTest.java └── SolutionTest.java ├── _129_SumRootToLeafNumbers ├── PracticeTest.java └── SolutionTest.java ├── _130_SurroundedRegions ├── PracticeTest.java └── SolutionTest.java ├── _131_PalindromePartitioning ├── PracticeTest.java ├── SolutionDPTest.java ├── SolutionTest.java └── Test5Expected ├── _132_PalindromePartitioningII ├── PracticeTest.java └── SolutionTest.java ├── _133_CloneGraph ├── PracticeTest.java ├── SolutionDFSTest.java └── SolutionTest.java ├── _135_Candy ├── PracticeTest.java └── SolutionTest.java ├── _136_SingleNumber ├── PracticeTest.java └── SolutionTest.java ├── _138_CopyListWithRandomPointer ├── PracticeTest.java ├── SolutionBFSTest.java └── SolutionTest.java ├── _139_WordBreak ├── PracticeTest.java ├── SolutionDFSTest.java ├── SolutionMemoTest.java └── SolutionTest.java ├── _140_WordBreakII ├── PracticeTest.java ├── SolutionBottomUpTest.java └── SolutionTest.java ├── _142_LinkedListCycleII ├── PracticeTest.java ├── SolutionSetTest.java └── SolutionTest.java ├── _143_ReorderList ├── PracticeTest.java └── SolutionTest.java ├── _144_BinaryTreePreorderTraversal ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _145_BinaryTreePostorderTraversal ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _146_LRUCache ├── PracticeTest.java └── SolutionTest.java ├── _147_InsertionSortList ├── PracticeTest.java └── SolutionTest.java ├── _148_SortList ├── PracticeTest.java └── SolutionTest.java ├── _149_MaxPointsOnALine ├── PracticeTest.java ├── SolutionSortTest.java ├── SolutionTest.java └── Test9Input ├── _150_EvaluateReversePolishNotation ├── PracticeTest.java └── SolutionTest.java ├── _151_ReverseWordsInAString ├── PracticeTest.java ├── SolutionAdvancedTest.java └── SolutionTest.java ├── _152_MaximumProductSubarray ├── PracticeTest.java ├── SolutionNTest.java └── SolutionTest.java ├── _153_FindMinimumInRotatedSortedArray ├── PracticeTest.java ├── SolutionTemplateTest.java └── SolutionTest.java ├── _154_FindMinimumInRotatedSortedArrayII ├── PracticeTest.java └── SolutionTest.java ├── _155_MinStack ├── PracticeTest.java └── SolutionTest.java ├── _157_ReadNCharactersGivenRead4 ├── PracticeTest.java └── SolutionTest.java ├── _159_LongestSubstringWithAtMostTwoDistinctCharacters ├── PracticeTest.java └── SolutionTest.java ├── _160_IntersectionOfTwoLinkedLists ├── PracticeTest.java ├── SolutionLengthTest.java └── SolutionTest.java ├── _161_OneEditDistance ├── PracticeTest.java ├── SolutionDPTest.java └── SolutionTest.java ├── _162_FindPeakElement ├── PracticeTest.java └── SolutionTest.java ├── _163_MissingRanges ├── PracticeTest.java └── SolutionTest.java ├── _164_MaximumGap ├── PracticeTest.java └── SolutionTest.java ├── _165_CompareVersionNumbers ├── PracticeTest.java └── SolutionTest.java ├── _166_FractionToRecurringDecimal ├── PracticeTest.java └── SolutionTest.java ├── _168_ExcelSheetColumnTitle ├── PracticeTest.java └── SolutionTest.java ├── _171_ExcelSheetColumnNumber ├── PracticeTest.java └── SolutionTest.java ├── _172_FactorialTrailingZeroes ├── PracticeTest.java └── SolutionTest.java ├── _173_BinarySearchTreeIterator ├── PracticeTest.java └── SolutionTest.java ├── _179_LargestNumber ├── PracticeTest.java └── SolutionTest.java ├── _186_ReverseWordsInAStringII ├── PracticeTest.java └── SolutionTest.java ├── _189_RotateArray ├── PracticeTest.java └── SolutionTest.java ├── _199_BinaryTreeRightSideView ├── PracticeTest.java ├── SolutionOneQueueTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _200_NumberOfIslands ├── PracticeTest.java ├── SolutionBFSPointTest.java ├── SolutionBFSTest.java ├── SolutionTest.java └── SolutionWrongTest.java ├── _203_RemoveLinkedListElements ├── PracticeTest.java └── SolutionTest.java ├── _204_CountPrimes ├── PracticeTest.java └── SolutionTest.java ├── _206_ReverseLinkedList ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _207_CourseSchedule ├── PracticeTest.java ├── SolutionDFSTest.java ├── SolutionTest.java └── Test7Input.txt ├── _208_ImplementTriePrefixTree ├── PracticeTest.java └── SolutionTest.java ├── _209_MinimumSizeSubarraySum ├── PracticeTest.java ├── SolutionSlidingWindowTest.java └── SolutionTest.java ├── _210_CourseScheduleII ├── PracticeTest.java └── SolutionTest.java ├── _211_AddAndSearchWordDataStructureDesign ├── PracticeTest.java └── SolutionTest.java ├── _212_WordSearchII ├── PracticeTest.java └── SolutionTest.java ├── _213_HouseRobberII ├── PracticeTest.java └── SolutionTest.java ├── _215_KthLargestElementInAnArray ├── PracticeTest.java ├── SolutionNlgKTest.java ├── SolutionNlgNTest.java └── SolutionTest.java ├── _216_CombinationSumIII ├── PracticeTest.java └── SolutionTest.java ├── _217_ContainsDuplicate ├── PracticeTest.java └── SolutionTest.java ├── _218_TheSkylineProblem ├── PracticeTest.java ├── SolutionHeapTest.java └── SolutionTest.java ├── _219_ContainsDuplicateII ├── PracticeTest.java ├── SolutionSetTest.java └── SolutionTest.java ├── _220_ContainsDuplicateIII ├── PracticeTest.java └── SolutionTest.java ├── _221_MaximalSquare ├── PracticeTest.java └── SolutionTest.java ├── _223_RectangleArea ├── PracticeTest.java └── SolutionTest.java ├── _224_BasicCalculator ├── PracticeTest.java └── SolutionTest.java ├── _225_ImplementStackUsingQueues ├── PracticeTest.java ├── SolutionTest.java └── SolutionTwoQueuesTest.java ├── _228_SummaryRanges ├── PracticeTest.java └── SolutionTest.java ├── _229_MajorityElementII ├── PracticeTest.java └── SolutionTest.java ├── _230_KthSmallestElementInABST ├── PracticeTest.java └── SolutionTest.java ├── _231_PowerOfTwo ├── PracticeTest.java └── SolutionTest.java ├── _232_ImplementQueueUsingStacks ├── PracticeTest.java └── SolutionTest.java ├── _235_LowestCommonAncestorOfABinarySearchTree ├── PracticeTest.java └── SolutionTest.java ├── _236_LowestCommonAncestorOfABinaryTree ├── PracticeTest.java └── SolutionTest.java ├── _237_DeleteNodeInALinkedList ├── PracticeTest.java └── SolutionTest.java ├── _238_ProductOfArrayExceptSelf ├── PracticeTest.java ├── SolutionLinearSpaceTest.java └── SolutionTest.java ├── _239_SlidingWindowMaximum ├── PracticeTest.java └── SolutionTest.java ├── _240_SearchA2DMatrixII ├── PracticeTest.java └── SolutionTest.java ├── _242_ValidAnagram ├── PracticeTest.java ├── SolutionSortTest.java └── SolutionTest.java ├── _246_StrobogrammaticNumber ├── PracticeTest.java └── SolutionTest.java ├── _251_Flatten2DVector ├── PracticeTest.java └── SolutionTest.java ├── _252_MeetingRooms ├── PracticeTest.java └── SolutionTest.java ├── _253_MeetingRoomsII ├── PracticeTest.java └── SolutionTest.java ├── _256_PaintHouse ├── PracticeTest.java └── SolutionTest.java ├── _261_GraphValidTree ├── PracticeTest.java ├── SolutionBFSTest.java └── SolutionTest.java ├── _265_PaintHouseII ├── PracticeTest.java ├── SolutionSlowTest.java └── SolutionTest.java ├── _268_MissingNumber ├── PracticeTest.java ├── SolutionSwapTest.java └── SolutionTest.java ├── _269_AlienDictionary ├── PracticeTest.java └── SolutionTest.java ├── _273_IntegerToEnglishWords ├── PracticeTest.java └── SolutionTest.java ├── _274_HIndex ├── PracticeTest.java └── SolutionTest.java ├── _275_HIndexII ├── PracticeTest.java ├── SolutionTemplateTest.java └── SolutionTest.java ├── _276_PaintFence └── SolutionTest.java ├── _278_FirstBadVersion ├── PracticeTest.java ├── SolutionTest.java └── SolutionTwoTest.java ├── _281_ZigzagIterator ├── PracticeTest.java └── SolutionTest.java ├── _283_MoveZeroes ├── PracticeTest.java ├── SolutionCountTest.java └── SolutionTest.java ├── _285_InorderSuccessorInBST ├── PracticeTest.java ├── SolutionRecursiveTest.java └── SolutionTest.java ├── _286_WallsAndGates ├── PracticeTest.java ├── SolutionSlowTest.java └── SolutionTest.java ├── com └── leetcode │ └── UndirectedGraphNodeTest.java ├── s01_SubarraySum └── SolutionTest.java ├── s02_DiameterOfBinaryTree └── SolutionTest.java ├── s04_LongestIncreasingContinuousSubsequence └── SolutionTest.java ├── s05_LongestIncreasingContinuousSubsequenceII └── SolutionTest.java ├── s07_MedianInDataStream └── SolutionTest.java ├── s09_ConvertTernaryExpressionToBinaryTree └── SolutionTest.java ├── s11_ReplacePermutation └── SolutionTest.java └── s12_FindArithmeticMissing └── SolutionTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .classpath 3 | .settings 4 | bin 5 | *.class 6 | *.swp 7 | scripts 8 | NotComfortable.md 9 | Parse.java 10 | leetcode_java.md 11 | TEST-* 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | 4 | jdk: 5 | - oraclejdk8 6 | 7 | #before_script: 8 | ##cd to root directory for java 9 | #- cd java 10 | 11 | script: 12 | - ant test > rep 13 | 14 | after_script: 15 | - rm -rf report 16 | - rm rep 17 | 18 | 19 | -------------------------------------------------------------------------------- /doc/category_basic.md: -------------------------------------------------------------------------------- 1 | #### Category : Basic 2 | 3 | Basic operations, string manipulation, 4 | 5 | | Type | # | Description | 6 | | ----- |:---:| ------ | 7 | | `String` `Include Last` | 151 | Reverse Words In A String | 8 | -------------------------------------------------------------------------------- /doc/category_bfs.md: -------------------------------------------------------------------------------- 1 | #### Category :: Breadth-first Search (BFS) 2 | | Type | # | Description | 3 | | ---------------------: |:---:| :------------| 4 | | **`pure BFS`** | | | 5 | | Both *`BFS`* and *`DFS`* | | | 6 | | | 017 | [Letter Combinations Of A Phone Number](https://github.com/interviewcoder/leetcode/tree/master/src/_017_LetterCombinationsOfAPhoneNumber) | 7 | | | 133 | [Clone Graph](https://github.com/interviewcoder/leetcode/tree/master/src/_133_CloneGraph) | -------------------------------------------------------------------------------- /doc/category_binarysearch.md: -------------------------------------------------------------------------------- 1 | #### Category : : Binary Search 2 | 3 | | Type | # | Description | 4 | | ---------------------: |:---:| :------------| 5 | | rotated array | 153 | [Find Minimum in Rotated Sorted Array](https://github.com/interviewcoder/blob/master/src/_153_FindMinimumInRotatedSortedArray/) | 6 | | rotated array | 154 | [Find Minimum in Rotated Sorted Array II]() | 7 | | matrix | 240 | [Search A 2D Matrix II](https://github.com/interviewcoder/leetcode/blob/master/src/_240_SearchA2DMatrixII/Solution.java) | -------------------------------------------------------------------------------- /doc/category_largescale.md: -------------------------------------------------------------------------------- 1 | #### Category :: Large Scale Data Stream 2 | | Type | # | Description | 3 | | ---------------------: |:---:| ------------| 4 | | max-heap + min-heap | s07 | Median of Data Stream | -------------------------------------------------------------------------------- /doc/category_linkedlist.md: -------------------------------------------------------------------------------- 1 | #### Category :: Linked List 2 | 3 | | Type | # | Description | 4 | | ---------------------: |:---:| :------------| 5 | | connect | 143 | [Reorder List](https://github.com/interviewcoder/leetcode/tree/master/src/_143_ReorderList) | 6 | | connect | 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | 7 | | dummy head | 021 | Merge Two Sortd Lists | -------------------------------------------------------------------------------- /doc/category_monotonicstack.md: -------------------------------------------------------------------------------- 1 | #### Category :: Monotonic Stack 2 | 3 | keep indices of numbers which are in ascending/descending order 4 | 5 | | Type | # | Description | 6 | | ---------------------: |:---:| ------------| 7 | | descending stack | 042 | [Trapping Rain Water](https://github.com/interviewcoder/leetcode/tree/master/src/_042_TrappingRainWater) | 8 | | ascending stack | 084 | [Largest Rectangle In Histogram](https://github.com/interviewcoder/leetcode/tree/master/src/_084_LargestRectangleInHistogram) | 9 | | descending deque | 239 | [Sliding Window Maximum](https://github.com/interviewcoder/leetcode/tree/master/src/_239_SlidingWindowMaximum) | -------------------------------------------------------------------------------- /doc/category_partition.md: -------------------------------------------------------------------------------- 1 | #### Category : : Partition 2 | 3 | ###### where `partition` technique makes amazing 4 | 5 | | Type | # | Description | 6 | | ---------------------: |:---:| :------------| 7 | | at most 1 duplicate `||` others | 026 | [Remove Duplicates From Sorted Array](https://github.com/interviewcoder/leetcode/blob/master/src/_026_RemoveDuplicatesFromSortedArray/Solution.java) | 8 | | at most k duplicates `||` others | 080 | [Remove Duplicates From Sorted ArrayII](https://github.com/interviewcoder/leetcode/tree/master/src/_080_RemoveDuplicatesFromSortedArrayII) | -------------------------------------------------------------------------------- /doc/category_similar.md: -------------------------------------------------------------------------------- 1 | #### Category :: Some questions sharing similar thinking 2 | | Type | # | Description | 3 | | ---------------------: |:---:| ------------| 4 | | heap V.S. merge | 023 | https://github.com/interviewcoder/leetcode/tree/master/src/_023_MergeKSortedLists | 5 | | heap V.S. merge | 218 | [The Skyline Problem](https://github.com/interviewcoder/leetcode/tree/master/src/_218_TheSkylineProblem) | 6 | | | | | 7 | | comsume cache | 158 | [Read N Characteres Given Read4 II](https://github.com/interviewcoder/leetcode/blob/master/src/_158_ReadNCharactersGivenRead4II/Solution.java) | 8 | | comsume cache | 284 | [Peeking Iterator](https://github.com/interviewcoder/leetcode/blob/master/src/_284_PeekingIterator/Solution.java) | -------------------------------------------------------------------------------- /lib/jsoup-1.8.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/interviewcoder/leetcode/5f86f5fbfb4bf81ea1cfc83f3510893568903542/lib/jsoup-1.8.2.jar -------------------------------------------------------------------------------- /lib/junit.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/interviewcoder/leetcode/5f86f5fbfb4bf81ea1cfc83f3510893568903542/lib/junit.jar -------------------------------------------------------------------------------- /lib/org.hamcrest.core_1.3.0.v201303031735.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/interviewcoder/leetcode/5f86f5fbfb4bf81ea1cfc83f3510893568903542/lib/org.hamcrest.core_1.3.0.v201303031735.jar -------------------------------------------------------------------------------- /src/_001_TwoSum/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * Given an array of integers, find two numbers such that they add up to 5 | * a specific target number. 6 | * The function twoSum should return indices of the two numbers such that 7 | * they add up to the target, where index1 must be less than index2. 8 | * Please note that your returned answers (both index1 and index2) are not 9 | * zero-based. 10 | * 11 | * You may assume that each input would have exactly one solution. 12 | * Input: numbers={2, 7, 11, 15}, target=9 13 | * Output: index1=1, index2=2 14 | * 15 | *************************************************************************** 16 | * @tag : Array; Hash Table 17 | * {@link https://leetcode.com/problems/two-sum/ } 18 | */ 19 | package _001_TwoSum; 20 | 21 | /** see test {@link _001_TwoSum.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int[] twoSum(int[] nums, int target) { 25 | // TODO Auto-generated method stub 26 | return null; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_002_AddTwoNumbers/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * You are given two linked lists representing two non-negative numbers. 6 | * The digits are stored in reverse order and each of their nodes contain 7 | * a single digit. Add the two numbers and return it as a linked list. 8 | * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 9 | * Output: 7 -> 0 -> 8 10 | * 11 | ******************************************************************************* 12 | * @tag : Linked List; Math 13 | * {@link https://leetcode.com/problems/add-two-numbers/ } 14 | */ 15 | package _002_AddTwoNumbers; 16 | 17 | import com.leetcode.ListNode; 18 | 19 | /** see test {@link _002_AddTwoNumbers.PracticeTest } */ 20 | public class Practice { 21 | 22 | public ListNode addTwoNumbers(ListNode a, ListNode b) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_003_LongestSubstringWithoutRepeatingCharacters/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a string, find the length of the longest substring without repeating 6 | * characters. For example, the longest substring without repeating letters 7 | * for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest 8 | * substring is "b", with the length of 1. 9 | * 10 | *************************************************************************** 11 | * @tag : Hash Table; Two Pointers; String 12 | * {@link https://leetcode.com/problems/longest-substring-without-repeating-characters/ } 13 | */ 14 | package _003_LongestSubstringWithoutRepeatingCharacters; 15 | 16 | /** see test {@link _003_LongestSubstringWithoutRepeatingCharacters.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int lengthOfLongestSubstring(String a) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_004_MedianOfTwoSortedArrays/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * There are two sorted arrays nums1 and nums2 of size m and n respectively. 6 | * Find the median of the two sorted arrays. The overall run time complexity 7 | * should be O(log (m+n)). 8 | * 9 | *************************************************************************** 10 | * @tag : Divide and Conquer; Array; Binary Search 11 | * {@link https://leetcode.com/problems/median-of-two-sorted-arrays/ } 12 | */ 13 | package _004_MedianOfTwoSortedArrays; 14 | 15 | /** see test {@link _004_MedianOfTwoSortedArrays.PracticeTest } */ 16 | public class Practice { 17 | 18 | public double findMedianSortedArrays(int[] nums1, int[] nums2) { 19 | // TODO Auto-generated method stub 20 | return 0; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_005_LongestPalindromicSubstring/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a string S, find the longest palindromic substring in S. You may 6 | * assume that the maximum length of S is 1000, and there exists one 7 | * unique longest palindromic substring. 8 | * 9 | ******************************************************************************* 10 | * @tag : String 11 | * {@link https://leetcode.com/problems/longest-palindromic-substring/ } 12 | */ 13 | package _005_LongestPalindromicSubstring; 14 | 15 | /** see test {@link _005_LongestPalindromicSubstring.PracticeTest } */ 16 | public class Practice { 17 | 18 | public String longestPalindrome(String s) { 19 | // TODO Auto-generated method stub 20 | return null; 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /src/_006_ZigZagConversion/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * The string "PAYPALISHIRING" is written in a zigzag pattern on a given 6 | * number of rows like this: (you may want to display this pattern in a 7 | * fixed font for better legibility) 8 | * P A H N 9 | * A P L S I I G 10 | * Y I R 11 | * And then read line by line: "PAHNAPLSIIGYIR" 12 | * 13 | * Write the code that will take a string and make this conversion given a 14 | * number of rows: 15 | * string convert(string text, int nRows); 16 | * convert("PAYPALISHIRING", 3) 17 | * should return "PAHNAPLSIIGYIR". 18 | * 19 | *************************************************************************** 20 | * @tag : String 21 | * {@link https://leetcode.com/problems/zigzag-conversion/ } 22 | */ 23 | package _006_ZigZagConversion; 24 | 25 | /** see test {@link _006_ZigZagConversion.PracticeTest } */ 26 | public class Practice { 27 | 28 | public String convert(String a, int b) { 29 | // TODO Auto-generated method stub 30 | return null; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/_007_ReverseInteger/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Reverse digits of an integer. 6 | * 7 | * Example1: x = 123, return 321 8 | * Example2: x = -123, return -321 9 | * 10 | * Return 0 if the result overflows and does not fit in a 32 bit signed integer 11 | * 12 | ************************************************************************* 13 | * @tag : Math 14 | * {@link https://leetcode.com/problems/reverse-integer/ } 15 | */ 16 | package _007_ReverseInteger; 17 | 18 | /** see test {@link _007_ReverseInteger.PracticeTest } */ 19 | public class Practice { 20 | 21 | public int reverse(int a) { 22 | // TODO Auto-generated method stub 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_007_ReverseInteger/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(); Space : O() 3 | * @tag : Math 4 | * @by : Steven Cooks 5 | * @date: Jul 8, 2015 6 | ************************************************************************* 7 | * Description: 8 | * 9 | * Reverse digits of an integer. 10 | * 11 | * Example1: x = 123, return 321 12 | * Example2: x = -123, return -321 13 | * 14 | * Return 0 if the result overflows and does not fit in a 32 bit signed integer 15 | * 16 | ************************************************************************* 17 | * {@link https://leetcode.com/problems/reverse-integer/ } 18 | */ 19 | package _007_ReverseInteger; 20 | 21 | /** see test {@link _007_ReverseInteger.SolutionTest } */ 22 | public class Solution { 23 | public int reverse(int x) { 24 | long result = 0; 25 | while (x != 0) { 26 | // adding the last digit from original number 27 | result = result * 10 + x % 10; 28 | x /= 10; 29 | } 30 | return (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) ? 0 : (int) result; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/_009_PalindromeNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Determine whether an integer is a palindrome. Do this without extra space. 6 | * 7 | * For this problem, negative numbers are considered not palindrome number. 8 | * 9 | ************************************************************************* 10 | * @tag : Math 11 | * {@link https://leetcode.com/problems/palindrome-number/ } 12 | */ 13 | package _009_PalindromeNumber; 14 | 15 | /** see test {@link _009_PalindromeNumber.PracticeTest } */ 16 | public class Practice { 17 | 18 | public boolean isPalindrome(int x) { 19 | // TODO Auto-generated method stub 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_011_ContainerWithMostWater/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given n non-negative integers a1, a2, ..., an, where each represents a 6 | * point at coordinate (i, ai). n vertical lines are drawn such that the 7 | * two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which 8 | * together with x-axis forms a container, such that the container contains 9 | * the most water. 10 | * 11 | * Note: You may not slant the container. 12 | * 13 | *************************************************************************** 14 | * @tag : Two Pointers 15 | * {@link https://leetcode.com/problems/container-with-most-water/ } 16 | */ 17 | package _011_ContainerWithMostWater; 18 | 19 | /** see test {@link _011_ContainerWithMostWater.PracticeTest } */ 20 | public class Practice { 21 | 22 | public int maxArea(int[] height) { 23 | // TODO Auto-generated method stub 24 | return 0; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_012_IntegerToRoman/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given an integer, convert it to a roman numeral. 6 | * Input is guaranteed to be within the range from 1 to 3999. 7 | * 8 | ******************************************************************************* 9 | * @tag : Math; String 10 | * {@link https://leetcode.com/problems/integer-to-roman/ } 11 | */ 12 | package _012_IntegerToRoman; 13 | 14 | /** see test {@link _012_IntegerToRoman.PracticeTest } */ 15 | public class Practice { 16 | 17 | public String intToRoman(int a) { 18 | // TODO Auto-generated method stub 19 | return null; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_013_RomanToInteger/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a roman numeral, convert it to an integer. 6 | * Input is guaranteed to be within the range from 1 to 3999. 7 | * 8 | ******************************************************************************* 9 | * @tag : Math; String 10 | * {@link https://leetcode.com/problems/roman-to-integer/ } 11 | */ 12 | package _013_RomanToInteger; 13 | 14 | public class Practice { 15 | 16 | public int romanToInt(String a) { 17 | // TODO Auto-generated method stub 18 | return 0; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/_014_LongestCommonPrefix/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space : O(N) 3 | * @tag : String 4 | * @by : Steven Cooks 5 | * @date: Jul 10, 2015 6 | ******************************************************************************* 7 | * Description: 8 | * 9 | * Write a function to find the longest common prefix string amongst an array of strings. 10 | * 11 | ******************************************************************************* 12 | * {@link https://leetcode.com/problems/longest-common-prefix/ } 13 | */ 14 | package _014_LongestCommonPrefix; 15 | 16 | /** see test {@link _014_LongestCommonPrefix.PracticeTest } */ 17 | public class Practice { 18 | 19 | public String longestCommonPrefix(String[] strs) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_015_3Sum/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an array S of n integers, are there elements a, b, c in S such that 6 | * a + b + c = 0? Find all unique triplets in the array which gives the sum 7 | * of zero. 8 | * 9 | * Note: 10 | * Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) 11 | * The solution set must not contain duplicate triplets. 12 | * For example, given array S = {-1 0 1 2 -1 -4}, 13 | * A solution set is: (-1, 0, 1) (-1, -1, 2) 14 | * 15 | *************************************************************************** 16 | * @tag : Array; Two Pointers; 17 | * {@link https://leetcode.com/problems/3sum/ } 18 | */ 19 | package _015_3Sum; 20 | 21 | import java.util.List; 22 | 23 | /** see test {@link _015_3Sum.PracticeTest } */ 24 | public class Practice { 25 | 26 | public List> threeSum(int[] nums) { 27 | // TODO Auto-generated method stub 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_016_3SumClosest/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an array S of n integers, find three integers in S such that the 6 | * sum is closest to a given number, target. Return the sum of the three 7 | * integers. You may assume that each input would have exactly one solution. 8 | * 9 | * For example, given array S = {-1 2 1 -4}, and target = 1. 10 | * The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 11 | * 12 | ************************************************************************* 13 | * @tag : Array; Two Pointers 14 | * {@link https://leetcode.com/problems/3sum-closest/ } 15 | */ 16 | package _016_3SumClosest; 17 | 18 | /** see test {@link _016_3SumClosest.PracticeTest } */ 19 | public class Practice { 20 | 21 | public int threeSumClosest(int[] nums, int target) { 22 | // TODO Auto-generated method stub 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_017_LetterCombinationsOfAPhoneNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a digit string, return all possible letter combinations that the 6 | * number could represent. 7 | * A mapping of digit to letters (just like on the telephone buttons) 8 | * is given below. 9 | * 10 | * Input:Digit string "23" 11 | * Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 12 | * 13 | * Note: 14 | * Although the above answer is in lexicographical order, your answer 15 | * could be in any order you want. 16 | * 17 | ************************************************************************* 18 | * @tag : Backtracking; String 19 | * {@link https://leetcode.com/problems/letter-combinations-of-a-phone-number/ } 20 | */ 21 | package _017_LetterCombinationsOfAPhoneNumber; 22 | 23 | import java.util.List; 24 | 25 | /** see test {@link _017_LetterCombinationsOfAPhoneNumber.PracticeTest } */ 26 | public class Practice { 27 | 28 | public List letterCombinations(String digits) { 29 | // TODO Auto-generated method stub 30 | return null; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/_018_4Sum/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * Given an array S of n integers, are there elements a, b, c, and d in S 5 | * such that a + b + c + d = target? Find all unique quadruplets in the 6 | * array which gives the sum of target. 7 | * 8 | * Note: 9 | * Elements in a quadruplet (a,b,c,d) must be in non-descending order. 10 | * (ie, a ≤ b ≤ c ≤ d) 11 | * The solution set must not contain duplicate quadruplets. 12 | * For example, given array S = {1 0 -1 0 -2 2}, and target = 0. 13 | * A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2) 14 | * 15 | ************************************************************************* 16 | * @tag : Array; Hash Table; Two Pointers; 17 | * {@link https://leetcode.com/problems/4sum/ } 18 | */ 19 | package _018_4Sum; 20 | 21 | import java.util.List; 22 | 23 | /** see test {@link _018_4Sum.PracticeTest } */ 24 | public class Practice { 25 | 26 | public List> fourSum(int[] nums, int target) { 27 | // TODO Auto-generated method stub 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_019_RemoveNthNodeFromEndOfList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a linked list, remove the nth node from the end of list and return 6 | * its head. 7 | * 8 | * For example, 9 | * Given linked list: 1->2->3->4->5, and n = 2. 10 | * After removing the second node from the end, the linked list 11 | * becomes 1->2->3->5. 12 | * 13 | * Note: 14 | * Given n will always be valid. Try to do this in one pass. 15 | * 16 | ************************************************************************* 17 | * @tag : Linked List; Two Pointers 18 | * {@link https://leetcode.com/problems/remove-nth-node-from-end-of-list/ } 19 | */ 20 | package _019_RemoveNthNodeFromEndOfList; 21 | 22 | import com.leetcode.ListNode; 23 | 24 | /** see test {@link _019_RemoveNthNodeFromEndOfList.PracticeTest } */ 25 | public class Practice { 26 | 27 | public ListNode removeNthFromEnd(ListNode head, int n) { 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_020_ValidParentheses/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', 5 | * determine if the input string is valid. 6 | * The brackets must close in the correct order, "()" and "()[]{}" are all 7 | * valid but "(]" and "([)]" are not. 8 | * 9 | ******************************************************************************* 10 | * @tag : Stack; String 11 | * {@link https://leetcode.com/problems/valid-parentheses/ } 12 | */ 13 | package _020_ValidParentheses; 14 | 15 | /** see test {@link _020_ValidParentheses.PracticeTest } */ 16 | public class Practice { 17 | 18 | public boolean isValid(String s) { 19 | // TODO Auto-generated method stub 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_021_MergeTwoSortedLists/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Merge two sorted linked lists and return it as a new list. The new list 6 | * should be made by splicing together the nodes of the first two lists. 7 | * 8 | ************************************************************************* 9 | * @tag : Linked List 10 | * {@link https://leetcode.com/problems/merge-two-sorted-lists/ } 11 | */ 12 | package _021_MergeTwoSortedLists; 13 | 14 | import com.leetcode.ListNode; 15 | 16 | /** see test {@link _021_MergeTwoSortedLists.PracticeTest } */ 17 | public class Practice { 18 | 19 | public ListNode mergeTwoLists(ListNode node11, ListNode node21) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_022_GenerateParentheses/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given n pairs of parentheses, write a function to generate all 6 | * combinations of well-formed parentheses. 7 | * 8 | * For example, given n = 3, a solution set is: 9 | * "((()))", "(()())", "(())()", "()(())", "()()()" 10 | * 11 | ************************************************************************* 12 | * @tag : Backtracking; String 13 | * {@link https://leetcode.com/problems/generate-parentheses/ } 14 | */ 15 | package _022_GenerateParentheses; 16 | 17 | import java.util.List; 18 | 19 | /** see test {@link _022_GenerateParentheses.PracticeTest } */ 20 | public class Practice { 21 | 22 | public List generateParenthesis(int n) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_023_MergeKSortedLists/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Merge k sorted linked lists and return it as one sorted list. Analyze 6 | * and describe its complexity. 7 | * 8 | *************************************************************************** 9 | * @tag : Divide and Conquer; Linked List; Heap 10 | * {@link https://leetcode.com/problems/merge-k-sorted-lists/ } 11 | */ 12 | package _023_MergeKSortedLists; 13 | 14 | import com.leetcode.ListNode; 15 | 16 | /** see test {@link _023_MergeKSortedLists.PracticeTest } */ 17 | public class Practice { 18 | 19 | public ListNode mergeKLists(ListNode[] lists) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_024_SwapNodesInPairs/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a linked list, swap every two adjacent nodes and return its head. 6 | * 7 | * For example, Given 1->2->3->4, you should return the list as 2->1->4->3. 8 | * 9 | * Your algorithm should use only constant space. You may not modify the 10 | * values in the list, only nodes itself can be changed. 11 | * 12 | *************************************************************************** 13 | * @tag : Linked List 14 | * {@link https://leetcode.com/problems/swap-nodes-in-pairs/ } 15 | */ 16 | package _024_SwapNodesInPairs; 17 | 18 | import com.leetcode.ListNode; 19 | 20 | /** see test {@link _024_SwapNodesInPairs.PracticeTest } */ 21 | public class Practice { 22 | 23 | public ListNode swapPairs(ListNode head) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_027_RemoveElement/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an array and a value, remove all instances of that value in place 6 | * and return the new length. 7 | * 8 | * The order of elements can be changed. 9 | * It doesn't matter what you leave beyond the new length. 10 | * 11 | ************************************************************************* 12 | * @tag : Array; Two Pointers 13 | * {@link https://leetcode.com/problems/remove-element/ } 14 | */ 15 | package _027_RemoveElement; 16 | 17 | /** see test {@link _027_RemoveElement.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int removeElement(int[] nums, int val) { 21 | // TODO Auto-generated method stub 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_027_RemoveElement/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space: O(1) 3 | * @tag : Array; Two Pointers 4 | * @by : Steven Cooks 5 | * @date: May 8, 2015 6 | ************************************************************************* 7 | * Description: 8 | * 9 | * Given an array and a value, remove all instances of that value in place 10 | * and return the new length. 11 | * 12 | * The order of elements can be changed. 13 | * It doesn't matter what you leave beyond the new length. 14 | * 15 | ************************************************************************* 16 | * {@link https://leetcode.com/problems/remove-element/ } 17 | */ 18 | package _027_RemoveElement; 19 | 20 | /** see test {@link _027_RemoveElement.SolutionTest } */ 21 | public class Solution { 22 | 23 | // send-to-the-right-place 24 | public int removeElement(int[] nums, int val) { 25 | int len = nums.length; 26 | for (int i = 0; i < len; i++) { 27 | while (nums[i] == val && i < len) { 28 | nums[i] = nums[--len]; 29 | } 30 | } 31 | return len; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/_027_RemoveElement/SolutionPartition.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space: O(1) 3 | * @Tag : Array; Two Pointers 4 | * @Date: May 8, 2015 5 | * @By : Steven Cooks 6 | */ 7 | package _027_RemoveElement; 8 | 9 | public class SolutionPartition { 10 | // partition array so that 11 | // nums[0:lastIndex] != val && nums(lastIndex : end) = val 12 | public int removeElement(int[] nums, int val) { 13 | int lastIndex = -1; 14 | for (int i = 0; i < nums.length; i++) { 15 | if (nums[i] != val) { 16 | nums[++lastIndex] = nums[i]; 17 | } 18 | } 19 | return lastIndex + 1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/_028_ImplementStrStr/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Implement strStr(). 6 | * 7 | * Returns the index of the first occurrence of needle in haystack, 8 | * or -1 if needle is not part of haystack. 9 | * 10 | ************************************************************************* 11 | * @tag : String; Two Pointers 12 | * {@link https://leetcode.com/problems/implement-strstr/ } 13 | */ 14 | package _028_ImplementStrStr; 15 | 16 | /** see test {@link _028_ImplementStrStr.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int strStr(String haystack, String needle) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_029_DivideTwoIntegers/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Divide two integers without using multiplication, division and mod operator. 6 | * If it is overflow, return MAX_INT. 7 | * 8 | ************************************************************************* 9 | * @tag : Math; Binary Search 10 | * {@link https://leetcode.com/problems/divide-two-integers/ } 11 | */ 12 | package _029_DivideTwoIntegers; 13 | 14 | /** see test {@link _029_DivideTwoIntegers.PracticeTest } */ 15 | public class Practice { 16 | 17 | public int divide(int dividend, int divisor) { 18 | // TODO Auto-generated method stub 19 | return 0; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_030_SubstringWithConcatenationOfAllWords/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * You are given a string, s, and a list of words, words, that are all of 6 | * the same length. Find all starting indices of substring(s) in s that 7 | * is a concatenation of each word in words exactly once and without any 8 | * intervening characters. 9 | * 10 | * For example, given: 11 | * s: "barfoothefoobarman" words: ["foo", "bar"] 12 | * You should return the indices: [0,9]. (order does not matter). 13 | * 14 | ************************************************************************* 15 | * @tag : Hash Table; Two Pointers; String 16 | * {@link https://leetcode.com/problems/substring-with-concatenation-of-all-words/ } 17 | */ 18 | package _030_SubstringWithConcatenationOfAllWords; 19 | 20 | import java.util.List; 21 | 22 | /** see test {@link _030_SubstringWithConcatenationOfAllWords.PracticeTest } */ 23 | public class Practice { 24 | 25 | public List findSubstring(String s, String[] words) { 26 | // TODO Auto-generated method stub 27 | return null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_032_LongestValidParentheses/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space: O(N) 3 | * @tag : Dynamic Programming; String 4 | * @by : Steven Cooks 5 | * @date: May 6, 2015 6 | ************************************************************************* 7 | * Description: 8 | * 9 | * Given a string containing just the characters '(' and ')', find the 10 | * length of the longest valid (well-formed) parentheses substring. 11 | * For "(()", the longest valid parentheses substring is "()", 12 | * which has length = 2. 13 | * 14 | * Another example is ")()())", where the longest valid parentheses 15 | * substring is "()()", which has length = 4. 16 | * 17 | ************************************************************************* 18 | * {@link https://leetcode.com/problems/longest-valid-parentheses/ } 19 | */ 20 | package _032_LongestValidParentheses; 21 | 22 | /** see test {@link _032_LongestValidParentheses.PracticeTest } */ 23 | public class Practice { 24 | 25 | public int longestValidParentheses(String s) { 26 | // TODO Auto-generated method stub 27 | return 0; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_033_SearchInRotatedSortedArray/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Suppose a sorted array is rotated at some pivot unknown to you beforehand. 6 | * (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 7 | * You are given a target value to search. If found in the array return 8 | * its index, otherwise return -1. 9 | * You may assume no duplicate exists in the array. 10 | * 11 | ************************************************************************* 12 | * @tag : Array; Two Pointers 13 | * {@link https://leetcode.com/problems/search-in-rotated-sorted-array/ } 14 | */ 15 | package _033_SearchInRotatedSortedArray; 16 | 17 | /** see test {@link _033_SearchInRotatedSortedArray.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int search(int[] nums, int target) { 21 | // TODO Auto-generated method stub 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_033_SearchInRotatedSortedArray/README.md: -------------------------------------------------------------------------------- 1 | ### [033] Search in roated sorted array 2 | 3 | 1. For rotated sorted array, 4 | - case 1: [left : mid] are sorted 5 | - case 2: [mid : right] are sorted 6 | - case 3: both [left : mid] and [mid : right] are sorted 7 | 8 | 2. For sorted part, e.g. [left : mid] are sorted 9 | 10 | if target is within value range of sorted part, then target is in this `sorted` part if exists; otherwise target is in `un-sorted` part 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/_034_SearchForARange/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a sorted array of integers, find the starting and ending position 6 | * of a given target value. 7 | * Your algorithm's runtime complexity must be in the order of O(log n). 8 | * If the target is not found in the array, return [-1, -1]. 9 | * 10 | * For example, 11 | * Given [5, 7, 7, 8, 8, 10] and target value 8, 12 | * return [3, 4]. 13 | * 14 | ************************************************************************* 15 | * @tag : Array; Binary Search 16 | * {@link https://leetcode.com/problems/search-for-a-range/ } 17 | */ 18 | package _034_SearchForARange; 19 | 20 | /** see test {@link _034_SearchForARange.PracticeTest } */ 21 | public class Practice { 22 | 23 | public int[] searchRange(int[] nums, int target) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_035_SearchInsertPosition/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a sorted array and a target value, return the index if the target 6 | * is found. If not, return the index where it would be if it were 7 | * inserted in order. 8 | * 9 | * You may assume no duplicates in the array. 10 | * Here are few examples. 11 | * [1,3,5,6], 5 → 2 12 | * [1,3,5,6], 2 → 1 13 | * [1,3,5,6], 7 → 4 14 | * [1,3,5,6], 0 → 0 15 | * 16 | ************************************************************************* 17 | * @tag : Array; Binary Search 18 | * {@link https://leetcode.com/problems/search-insert-position/ } 19 | */ 20 | package _035_SearchInsertPosition; 21 | 22 | /** see test {@link _035_SearchInsertPosition.PracticeTest } */ 23 | public class Practice { 24 | 25 | public int searchInsert(int[] nums, int target) { 26 | // TODO Auto-generated method stub 27 | return 0; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_035_SearchInsertPosition/README.md: -------------------------------------------------------------------------------- 1 | ### [035] [Search Insert Position](https://github.com/interviewcoder/leetcode/blob/master/src/_035_SearchInsertPosition/Solution.java) 2 | 3 | To make life easier, we can consider the very easy situation, only one number exists. 4 | 5 | ```java 6 | /** 7 | * Before search left & right 8 | | 9 | index 0 10 | number 5 11 | 12 | * case I : (target = 5) => expect = 0 => return left or right 13 | * case II : (target > 5) => expect = 1 => return left 14 | after left = mid + 1; 15 | right left 16 | | | 17 | index 0 1 18 | number 5 (null) 19 | * case III: (target < 5) => expect = 0 => return left 20 | after right = mid - 1; 21 | right left 22 | | | 23 | index -1 0 24 | number (null) 5 25 | * Summary: return left 26 | */ 27 | 28 | ``` -------------------------------------------------------------------------------- /src/_036_ValidSudoku/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 6 | * The Sudoku board could be partially filled, where empty cells are 7 | * filled with the character '.'. 8 | * Note: 9 | * A valid Sudoku board (partially filled) is not necessarily solvable. 10 | * Only the filled cells need to be validated. 11 | * 12 | ************************************************************************* 13 | * @tag : Hash Table 14 | * {@link https://leetcode.com/problems/valid-sudoku/ } 15 | */ 16 | package _036_ValidSudoku; 17 | 18 | /** see test {@link _036_ValidSudoku.PracticeTest } */ 19 | public class Practice { 20 | 21 | public boolean isValidSudoku(char[][] board) { 22 | // TODO Auto-generated method stub 23 | return false; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_037_SudokuSolver/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Write a program to solve a Sudoku puzzle by filling the empty cells. 6 | * Empty cells are indicated by the character '.'. 7 | * You may assume that there will be only one unique solution. 8 | * 9 | ************************************************************************* 10 | * @tag : Backtracking; Hash Table 11 | * {@link https://leetcode.com/problems/sudoku-solver/ } 12 | */ 13 | package _037_SudokuSolver; 14 | 15 | /** see test {@link _037_SudokuSolver.PracticeTest } */ 16 | public class Practice { 17 | 18 | public void solveSudoku(char[][] board) { 19 | // TODO Auto-generated method stub 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_038_CountAndSay/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * The count-and-say sequence is the sequence of integers beginning as 5 | * follows: 1, 11, 21, 1211, 111221, ... 6 | * 7 | * 1 is read off as "one 1" or 11. 8 | * 11 is read off as "two 1s" or 21. 9 | * 21 is read off as "one 2, then one 1" or 1211. 10 | * 11 | * Given an integer n, generate the nth sequence. 12 | * Note: The sequence of integers will be represented as a string. 13 | * 14 | ************************************************************************* 15 | * @tag : String 16 | * {@link https://leetcode.com/problems/count-and-say/ } 17 | */ 18 | package _038_CountAndSay; 19 | 20 | /** see test {@link _038_CountAndSay.PracticeTest } */ 21 | public class Practice { 22 | 23 | public String countAndSay(int i) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_041_FirstMissingPositive/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an unsorted integer array, find the first missing positive integer. 6 | * 7 | * For example, Given [1,2,0] return 3, 8 | * and [3,4,-1,1] return 2. 9 | * 10 | * Your algorithm should run in O(n) time and uses constant space. 11 | * 12 | ************************************************************************* 13 | * @tag : Array 14 | * {@link https://leetcode.com/problems/first-missing-positive/ } 15 | */ 16 | package _041_FirstMissingPositive; 17 | 18 | /** see test {@link _041_FirstMissingPositive.PracticeTest} */ 19 | public class Practice { 20 | 21 | public int firstMissingPositive(int[] nums) { 22 | // TODO Auto-generated method stub 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_042_TrappingRainWater/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given n non-negative integers representing an elevation map where the 6 | * width of each bar is 1, compute how much water it is able to trap after raining. 7 | * 8 | * For example, 9 | * Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 10 | * 11 | ************************************************************************* 12 | * @tag : Array; Stack; Two Pointers 13 | * {@link https://leetcode.com/problems/trapping-rain-water/ } 14 | */ 15 | package _042_TrappingRainWater; 16 | 17 | /** see test {@link _042_TrappingRainWater.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int trap(int[] height) { 21 | // TODO Auto-generated method stub 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_043_MultiplyStrings/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given two numbers represented as strings, return multiplication of 6 | * the numbers as a string. 7 | * 8 | * Note: The numbers can be arbitrarily large and are non-negative. 9 | * 10 | ************************************************************************* 11 | * @tag : Math; Array 12 | * {@link https://leetcode.com/problems/multiply-strings/ } 13 | */ 14 | package _043_MultiplyStrings; 15 | 16 | /** see test {@link _043_MultiplyStrings.PracticeTest } */ 17 | public class Practice { 18 | 19 | public String multiply(String num1, String num2) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_043_MultiplyStrings/SolutionBigInteger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N*M); Space: O() 3 | * @tag : Math; Array 4 | * @by : Steven Cooks 5 | * @date: Jun 4, 2015 6 | ************************************************************************* 7 | * Description: 8 | * 9 | * Given two numbers represented as strings, return multiplication of 10 | * the numbers as a string. 11 | * 12 | * Note: The numbers can be arbitrarily large and are non-negative. 13 | * 14 | ************************************************************************* 15 | * {@link https://leetcode.com/problems/multiply-strings/ } 16 | */ 17 | package _043_MultiplyStrings; 18 | 19 | import java.math.BigInteger; 20 | 21 | /** see test {@link _043_MultiplyStrings.SolutionBigIntegerTest } */ 22 | public class SolutionBigInteger { 23 | 24 | public String multiply(String num1, String num2) { 25 | // cheating way: use BigInteger class in Java 26 | BigInteger b1 = new BigInteger(num1); 27 | BigInteger b2 = new BigInteger(num2); 28 | return b1.multiply(b2).toString(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_045_JumpGameII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an array of non-negative integers, you are initially positioned 6 | * at the first index of the array. 7 | * 8 | * Each element in the array represents your maximum jump length at that position. 9 | * Your goal is to reach the last index in the minimum number of jumps. 10 | * 11 | * For example: Given array A = [2,3,1,1,4] 12 | * The minimum number of jumps to reach the last index is 2. (Jump 1 step 13 | * from index 0 to 1, then 3 steps to the last index.) 14 | * 15 | *************************************************************************** 16 | * @tag : Array; Greedy 17 | * {@link https://leetcode.com/problems/jump-game-ii/ } 18 | */ 19 | package _045_JumpGameII; 20 | 21 | /** see test {@link _045_JumpGameII.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int jump(int[] nums) { 25 | // TODO Auto-generated method stub 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_046_Permutations/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a collection of numbers, return all possible permutations. 6 | * 7 | * For example, [1,2,3] have the following permutations: 8 | * [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 9 | * 10 | ************************************************************************* 11 | * @tag : Backtracking 12 | * {@link https://leetcode.com/problems/permutations/ } 13 | */ 14 | package _046_Permutations; 15 | 16 | import java.util.List; 17 | 18 | /** see test {@link _046_Permutations.PracticeTest } */ 19 | public class Practice { 20 | 21 | public List> permute(int[] nums) { 22 | // TODO Auto-generated method stub 23 | return null; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_047_PermutationsII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a collection of numbers that might contain duplicates, return all 6 | * possible unique permutations. 7 | * 8 | * For example, 9 | * [1,1,2] have the following unique permutations: 10 | * [1,1,2], [1,2,1], and [2,1,1]. 11 | * 12 | *************************************************************************** 13 | * @tag : Backtracking 14 | * {@link https://leetcode.com/problems/permutations-ii/ } 15 | */ 16 | package _047_PermutationsII; 17 | 18 | import java.util.List; 19 | 20 | /** see test {@link _047_PermutationsII.PracticeTest } */ 21 | public class Practice { 22 | 23 | public List> permuteUnique(int[] nums) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_048_RotateImage/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * You are given an n x n 2D matrix representing an image. 6 | * Rotate the image by 90 degrees (clockwise). 7 | * 8 | * Follow up: Could you do this in-place? 9 | * 10 | *************************************************************************** 11 | * @tag : Array 12 | * {@link https://leetcode.com/problems/rotate-image/ } 13 | */ 14 | package _048_RotateImage; 15 | 16 | /** see test {@link _048_RotateImage.PracticeTest } */ 17 | public class Practice { 18 | 19 | public void rotate(int[][] matrix) { 20 | // TODO Auto-generated method stub 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_049_GroupAnagrams/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(); Space: O() 3 | * @tag : Hash Table; String 4 | * @date: Jun 2, 2015 5 | *************************************************************************** 6 | * Description: 7 | * 8 | * Given an array of strings, return all groups of strings that are anagrams. 9 | * 10 | * Note: 11 | * For the return value, each inner list's elements must follow the lexicographic order. 12 | * All inputs will be in lower-case. 13 | * 14 | *************************************************************************** 15 | * {@link https://leetcode.com/problems/anagrams/ } 16 | */ 17 | package _049_GroupAnagrams; 18 | 19 | import java.util.List; 20 | 21 | /** see test {@link _049_GroupAnagrams.PracticeTest } */ 22 | public class Practice { 23 | 24 | public List> groupAnagrams(String[] strs) { 25 | // TODO Auto-generated method stub 26 | return null; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_050_PowxN/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Implement pow(x, n). 6 | * 7 | *************************************************************************** 8 | * @tag : Math; Binary Search 9 | * {@link https://leetcode.com/problems/powx-n/ } 10 | */ 11 | package _050_PowxN; 12 | 13 | /** see test {@link _050_PowxN.PracticeTest } */ 14 | public class Practice { 15 | 16 | public double myPow(double x, int n) { 17 | // TODO Auto-generated method stub 18 | return 0; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/_051_NQueens/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * A queen can attack other queen in the same row, 6 | * same column and diagonal line. 7 | * 8 | *************************************************************************** 9 | * @tag : Backtracking 10 | * {@link https://leetcode.com/problems/n-queens/ } 11 | */ 12 | package _051_NQueens; 13 | 14 | import java.util.List; 15 | 16 | /** see test {@link _051_NQueens.PracticeTest } */ 17 | public class Practice { 18 | 19 | public List solveNQueens(int n) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_052_NQueensII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Follow up for N-Queens problem. 6 | * Now, instead outputting board configurations, return the total number of 7 | * distinct solutions. 8 | * 9 | *************************************************************************** 10 | * @tag : Backtracking 11 | * {@link https://leetcode.com/problems/n-queens-ii/ } 12 | */ 13 | package _052_NQueensII; 14 | 15 | /** see test {@link _052_NQueensII.PracticeTest } */ 16 | public class Practice { 17 | 18 | public int totalNQueens(int n) { 19 | // TODO Auto-generated method stub 20 | return 0; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_053_MaximumSubarray/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Find the contiguous subarray within an array (containing at least one 6 | * number) which has the largest sum. 7 | * 8 | * For example, given the array [−2,1,−3,4,−1,2,1,−5,4], 9 | * the contiguous subarray [4,−1,2,1] has the largest sum = 6. 10 | * 11 | * More practice: 12 | * If you have figured out the O(n) solution, try coding another solution 13 | * using the divide and conquer approach, which is more subtle. 14 | * 15 | *************************************************************************** 16 | * @tag : Divide and Conquer; Array; Dynamic Programming 17 | * {@link https://leetcode.com/problems/maximum-subarray/ } 18 | */ 19 | package _053_MaximumSubarray; 20 | 21 | /** see test {@link _053_MaximumSubarray.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int maxSubArray(int[] nums) { 25 | // TODO Auto-generated method stub 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_054_SpiralMatrix/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a matrix of m x n elements (m rows, n columns), return all 6 | * elements of the matrix in spiral order. 7 | * 8 | * For example, 9 | * Given the following matrix: 10 | * [ 11 | * [ 1, 2, 3 ], 12 | * [ 4, 5, 6 ], 13 | * [ 7, 8, 9 ] 14 | * ] 15 | * 16 | * You should return [1,2,3,6,9,8,7,4,5]. 17 | * 18 | ************************************************************************* 19 | * @tag : Array 20 | * {@link https://leetcode.com/problems/spiral-matrix/ } 21 | */ 22 | package _054_SpiralMatrix; 23 | 24 | import java.util.List; 25 | 26 | /** see test {@link _054_SpiralMatrix.PracticeTest } */ 27 | public class Practice { 28 | 29 | public List spiralOrder(int[][] matrix) { 30 | // TODO Auto-generated method stub 31 | return null; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/_055_JumpGame/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an array of non-negative integers, you are initially positioned at 6 | * the first index of the array. 7 | * Each element in the array represents your maximum jump length at that position. 8 | * 9 | * Determine if you are able to reach the last index. 10 | * For example: 11 | * A = [2,3,1,1,4], return true. 12 | * A = [3,2,1,0,4], return false. 13 | * 14 | *************************************************************************** 15 | * @tag : Array; Greedy 16 | * {@link https://leetcode.com/problems/jump-game/ } 17 | */ 18 | package _055_JumpGame; 19 | 20 | /** see test {@link _055_JumpGame.PracticeTest } */ 21 | public class Practice { 22 | 23 | public boolean canJump(int[] nums) { 24 | // TODO Auto-generated method stub 25 | return false; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_055_JumpGame/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space: O(1) 3 | * @tag : Array; Greedy 4 | * @by : Steven Cooks 5 | * @date: Jun 3, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * Given an array of non-negative integers, you are initially positioned at 10 | * the first index of the array. 11 | * Each element in the array represents your maximum jump length at that position. 12 | * 13 | * Determine if you are able to reach the last index. 14 | * For example: 15 | * A = [2,3,1,1,4], return true. 16 | * A = [3,2,1,0,4], return false. 17 | * 18 | *************************************************************************** 19 | * {@link https://leetcode.com/problems/jump-game/ } 20 | */ 21 | package _055_JumpGame; 22 | 23 | /** see test {@link _055_JumpGame.SolutionTest } */ 24 | public class Solution { 25 | 26 | public boolean canJump(int[] nums) { 27 | int i = 0; 28 | int n = nums.length; 29 | int reach = 0; 30 | for (; i < n && i <= reach; i++) { 31 | reach = Math.max(nums[i] + i, reach); 32 | } 33 | return i == n; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/_056_MergeIntervals/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Description: 6 | * 7 | * Given a collection of intervals, merge all overlapping intervals. 8 | * 9 | * For example, 10 | * Given [1,3],[2,6],[8,10],[15,18], 11 | * return [1,6],[8,10],[15,18]. 12 | * 13 | ************************************************************************* 14 | * @tag : Array; Sort 15 | * {@link https://leetcode.com/problems/merge-intervals/ } 16 | */ 17 | package _056_MergeIntervals; 18 | 19 | import java.util.List; 20 | 21 | import com.leetcode.Interval; 22 | 23 | /** see test {@link _056_MergeIntervals.PracticeTest } */ 24 | public class Practice { 25 | 26 | public List merge(List intervals) { 27 | // TODO Auto-generated method stub 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_058_LengthOfLastWord/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a string s consists of upper/lower-case alphabets and empty space 6 | * characters ' ', return the length of last word in the string. 7 | * If the last word does not exist, return 0. 8 | * 9 | * Note: A word is defined as a character sequence consists of non-space 10 | * characters only. 11 | * 12 | * For example, Given s = "Hello World", return 5. 13 | * 14 | ******************************************************************************* 15 | * @tag : String 16 | * {@link https://leetcode.com/problems/length-of-last-word/ } 17 | */ 18 | package _058_LengthOfLastWord; 19 | 20 | /** see test {@link _058_LengthOfLastWord.PracticeTest } */ 21 | public class Practice { 22 | 23 | public int lengthOfLastWord(String s) { 24 | // TODO Auto-generated method stub 25 | return 0; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_059_SpiralMatrixII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an integer n, generate a square matrix filled with elements 6 | * from 1 to n2 in spiral order. 7 | * 8 | * For example, 9 | * 10 | * Given n = 3, You should return the following matrix: 11 | * [ 12 | * [ 1, 2, 3 ], 13 | * [ 8, 9, 4 ], 14 | * [ 7, 6, 5 ] 15 | * ] 16 | ************************************************************************* 17 | * @Tag : Array 18 | * {@link https://leetcode.com/problems/spiral-matrix-ii/ } 19 | */ 20 | package _059_SpiralMatrixII; 21 | 22 | /** see test {@link _059_SpiralMatrixII.PracticeTest } */ 23 | public class Practice { 24 | 25 | public int[][] generateMatrix(int n) { 26 | // TODO Auto-generated method stub 27 | return null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_060_PermutationSequence/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * The set [1,2,3,…,n] contains a total of n! unique permutations. 6 | * By listing and labeling all of the permutations in order, 7 | * We get the following sequence (ie, for n = 3): 8 | * "123" "132" "213" "231" "312" "321" 9 | * 10 | * Given n and k, return the kth permutation sequence. 11 | * Note: Given n will be between 1 and 9 inclusive. 12 | * 13 | ************************************************************************* 14 | * @tag : Backtracking; Math 15 | * {@link https://leetcode.com/problems/permutation-sequence/ } 16 | */ 17 | package _060_PermutationSequence; 18 | 19 | /** see test {@link _060_PermutationSequence.PracticeTest } */ 20 | public class Practice { 21 | 22 | public String getPermutation(int n, int k) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_062_UniquePaths/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * A robot is located at the top-left corner of a m x n grid (marked 6 | * 'Start' in the diagram below). 7 | * 8 | * The robot can only move either down or right at any point in time. The 9 | * robot is trying to reach the bottom-right corner of the grid (marked 10 | * 'Finish' in the diagram below). 11 | * 12 | * How many possible unique paths are there? 13 | * 14 | ************************************************************************* 15 | * @tag : Array; Dynamic Programming 16 | * {@link https://leetcode.com/problems/unique-paths/ } 17 | */ 18 | package _062_UniquePaths; 19 | 20 | /** see test {@link _062_UniquePaths.PracticeTest } */ 21 | public class Practice { 22 | 23 | public int uniquePaths(int m, int n) { 24 | // TODO Auto-generated method stub 25 | return 0; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_063_UniquePathsII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Follow up for "Unique Paths": 6 | * Now consider if some obstacles are added to the grids. How many unique 7 | * paths would there be? 8 | * An obstacle and empty space is marked as 1 and 0 respectively in the grid. 9 | * 10 | * For example, 11 | * There is one obstacle in the middle of a 3x3 grid as illustrated below. 12 | * [ 13 | * [0,0,0], 14 | * [0,1,0], 15 | * [0,0,0] 16 | * ] 17 | * The total number of unique paths is 2. 18 | * 19 | ************************************************************************* 20 | * @tag : Array; Dynamic Programming 21 | * {@link https://leetcode.com/problems/unique-paths-ii/ } 22 | */ 23 | package _063_UniquePathsII; 24 | 25 | /** see test {@link _063_UniquePathsII.PracticeTest } */ 26 | public class Practice { 27 | 28 | public int uniquePathsWithObstacles(int[][] obstacleGrid) { 29 | // TODO Auto-generated method stub 30 | return 0; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/_064_MinimumPathSum/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a m x n grid filled with non-negative numbers, find a path from top 6 | * left to bottom right which minimizes the sum of all numbers along its path. 7 | * 8 | * Note: You can only move either down or right at any point in time. 9 | * 10 | ************************************************************************* 11 | * @tag : Array; Dynamic Programming 12 | * {@link https://leetcode.com/problems/minimum-path-sum/ } 13 | */ 14 | package _064_MinimumPathSum; 15 | 16 | /** see test {@link _064_MinimumPathSum.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int minPathSum(int[][] grid) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_065_ValidNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Validate if a given string is numeric. 6 | * Some examples: 7 | * "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true 8 | * 9 | * Note: It is intended for the problem statement to be ambiguous. You 10 | * should gather all requirements up front before implementing one. 11 | * 12 | ************************************************************************* 13 | * @tag : Math; String 14 | * {@link https://leetcode.com/problems/valid-number/ } 15 | */ 16 | package _065_ValidNumber; 17 | 18 | /** see test {@link _065_ValidNumber.PracticeTest } */ 19 | public class Practice { 20 | 21 | public boolean isNumber(String s) { 22 | // TODO Auto-generated method stub 23 | return false; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_066_PlusOne/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a non-negative number represented as an array of digits, plus one 6 | * to the number. 7 | * The digits are stored such that the most significant digit is at the 8 | * head of the list. 9 | * 10 | ************************************************************************* 11 | * @tag : Array; Math 12 | * {@link https://leetcode.com/problems/plus-one/ } 13 | */ 14 | package _066_PlusOne; 15 | 16 | /** see test {@link _066_PlusOne.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int[] plusOne(int[] digits) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_067_AddBinary/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a non-negative number represented as an array of digits, plus one 6 | * to the number. 7 | * The digits are stored such that the most significant digit is at the 8 | * head of the list. 9 | * 10 | ************************************************************************* 11 | * @tag : Math; String 12 | * {@link https://leetcode.com/problems/add-binary/ } 13 | */ 14 | package _067_AddBinary; 15 | 16 | /** see test {@link _067_AddBinary.PracticeTest } */ 17 | public class Practice { 18 | 19 | public String addBinary(String a, String b) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/_069_Sqrtx/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description 4 | * 5 | * Implement int sqrt(int x). 6 | * Compute and return the square root of x. 7 | * 8 | ************************************************************************* 9 | * @tag : Math; Binary Search 10 | * {@link https://leetcode.com/problems/sqrtx/ } 11 | */ 12 | package _069_Sqrtx; 13 | 14 | /** see test {@link _069_Sqrtx.PracticeTest } */ 15 | public class Practice { 16 | 17 | public int mySqrt(int x) { 18 | // TODO Auto-generated method stub 19 | return 0; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_069_Sqrtx/SolutionNewton.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(logN); Space: O(1) 3 | * @tag : Math; Binary Search 4 | * @by : Steven Cooks 5 | * @date: Jun 5, 2015 6 | ************************************************************************* 7 | * Description 8 | * 9 | * Implement int sqrt(int x). 10 | * Compute and return the square root of x. 11 | * 12 | ************************************************************************* 13 | * {@link https://leetcode.com/problems/sqrtx/ } 14 | */ 15 | package _069_Sqrtx; 16 | 17 | /** see test {@link _069_Sqrtx.SolutionNewtonTest } */ 18 | public class SolutionNewton { 19 | 20 | public int mySqrt(int x) { 21 | //TODO 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_070_ClimbingStairs/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @tag : Dynamic Programming 3 | ************************************************************************* 4 | * Description 5 | * 6 | * You are climbing a stair case. It takes n steps to reach to the top. 7 | * Each time you can either climb 1 or 2 steps. In how many distinct ways 8 | * can you climb to the top? 9 | * 10 | ************************************************************************* 11 | * {@link https://leetcode.com/problems/climbing-stairs/ } 12 | */ 13 | package _070_ClimbingStairs; 14 | 15 | /** see test {@link _070_ClimbingStairs.PracticeTest } */ 16 | public class Practice { 17 | 18 | public int climbStairs(int n) { 19 | // TODO Auto-generated method stub 20 | return 0; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_070_ClimbingStairs/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(); Space: O() 3 | * @tag : Dynamic Programming 4 | * @by : Steven Cooks 5 | * @date: Jun 5, 2015 6 | ************************************************************************* 7 | * Description 8 | * 9 | * You are climbing a stair case. It takes n steps to reach to the top. 10 | * Each time you can either climb 1 or 2 steps. In how many distinct ways 11 | * can you climb to the top? 12 | * 13 | ************************************************************************* 14 | * {@link https://leetcode.com/problems/climbing-stairs/ } 15 | */ 16 | package _070_ClimbingStairs; 17 | 18 | /** see test {@link _070_ClimbingStairs.SolutionTest } */ 19 | public class Solution { 20 | 21 | public int climbStairs(int n) { 22 | if (n == 1) { 23 | return 1; 24 | } 25 | int n1 = 1; 26 | int n2 = 2; 27 | int result = n2; 28 | for (int i = 2; i < n; i++) { 29 | result = n1 + n2; 30 | n1 = n2; 31 | n2 = result; 32 | } 33 | return result; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/_071_SimplifyPath/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an absolute path for a file (Unix-style), simplify it. 6 | * For example, 7 | * path = "/home/", => "/home" 8 | * path = "/a/./b/../../c/", => "/c" 9 | * 10 | ************************************************************************* 11 | * -------------------------------------------------------------- 12 | * @tag : Stack; String 13 | * {@link https://leetcode.com/problems/simplify-path/ } 14 | */ 15 | package _071_SimplifyPath; 16 | 17 | /** see test {@link _071_SimplifyPath.PracticeTest } */ 18 | public class Practice { 19 | 20 | public String simplifyPath(String path) { 21 | // TODO Auto-generated method stub 22 | return null; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_072_EditDistance/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * Given two words word1 and word2, find the minimum number of steps 5 | * required to convert word1 to word2. (each operation is counted as 1 step.) 6 | * 7 | * You have the following 3 operations permitted on a word: 8 | * a) Insert a character 9 | * b) Delete a character 10 | * c) Replace a character 11 | * 12 | ************************************************************************* 13 | * @tag : Dynamic Programming; String 14 | * {@link https://leetcode.com/problems/edit-distance/ } 15 | */ 16 | package _072_EditDistance; 17 | 18 | /** see test {@link _072_EditDistance.PracticeTest } */ 19 | public class Practice { 20 | 21 | public int minDistance(String word1, String word2) { 22 | // TODO Auto-generated method stub 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_073_SetMatrixZeroes/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a m x n matrix, if an element is 0, set its entire row and column 6 | * to 0. Do it in place. 7 | * 8 | * Follow up: 9 | * Could you devise a constant space solution? 10 | * 11 | ************************************************************************* 12 | * @tag : Array 13 | * {@link https://leetcode.com/problems/set-matrix-zeroes/ } 14 | */ 15 | package _073_SetMatrixZeroes; 16 | 17 | /** see test {@link _073_SetMatrixZeroes.PracticeTest } */ 18 | public class Practice { 19 | 20 | public void setZeroes(int[][] matrix) { 21 | // TODO Auto-generated method stub 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_075_SortColors/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an array with n objects colored red, white or blue, sort them so 6 | * that objects of the same color are adjacent, with the colors in the order 7 | * red, white and blue. 8 | * 9 | * Here, we will use the integers 0, 1, and 2 to represent the color red, 10 | * white, and blue respectively. 11 | * 12 | * Note: 13 | * You are not suppose to use the library's sort function for this problem. 14 | * 15 | * Follow up: 16 | * Could you come up with an one-pass algorithm using only constant space? 17 | * 18 | ************************************************************************* 19 | * @tag : Array; Two Pointers; Sort 20 | * {@link https://leetcode.com/problems/sort-colors/ } 21 | */ 22 | package _075_SortColors; 23 | 24 | /** see test {@link _075_SortColors.PracticeTest } */ 25 | public class Practice { 26 | 27 | public void sortColors(int[] nums) { 28 | // TODO Auto-generated method stub 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/_076_MinimumWindowSubstring/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a string S and a string T, find the minimum window in S which will 6 | * contain all the characters in T in complexity O(n). 7 | * For example, 8 | * S = "ADOBECODEBANC", T = "ABC" 9 | * Minimum window is "BANC". 10 | * Note: 11 | * If there is no such window in S that covers all characters in T, 12 | * return the emtpy string "". 13 | * If there are multiple such windows, you are guaranteed that there will 14 | * always be only one unique minimum window in S. 15 | * 16 | *************************************************************************** 17 | * @tag : Hash Table; Two Pointers; String 18 | * {@link https://leetcode.com/problems/minimum-window-substring/ } 19 | */ 20 | package _076_MinimumWindowSubstring; 21 | 22 | /** see test {@link _076_MinimumWindowSubstring.PracticeTest } */ 23 | public class Practice { 24 | 25 | public String minWindow(String s, String t) { 26 | // TODO Auto-generated method stub 27 | return null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_077_Combinations/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * Given two integers n and k, return all possible combinations of k numbers 5 | * out of 1 ... n. 6 | * For example, 7 | * If n = 4 and k = 2, a solution is: 8 | * [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4] ] 9 | * 10 | ************************************************************************* 11 | * @tag : Backtracking 12 | * {@link https://leetcode.com/problems/combinations/ } 13 | */ 14 | package _077_Combinations; 15 | 16 | import java.util.List; 17 | 18 | /** see test {@link _077_Combinations.PracticeTest } */ 19 | public class Practice { 20 | 21 | public List> combine(int n, int k) { 22 | // TODO Auto-generated method stub 23 | return null; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_078_Subsets/README.md: -------------------------------------------------------------------------------- 1 | ### [078] Subsets 2 | 3 | Recursive Tree for nums = [1, 2, 3, 4] 4 | 5 | ```java 6 | /** foo(index): 7 | [] 8 | / / \ \ 9 | [1] [2] [3] [4] 10 | / \ \ / \ | 11 | [1,2] [1,3] [1,4] [2,3] [2,4] [3,4] 12 | / \ | | 13 | [1,2,3] [1,2,4] [1,3,4] [2,3,4] 14 | | 15 | [1,2,3,4] 16 | */ 17 | 18 | func foo(index): 19 | for (int i = index; i < n; i++) { 20 | foo(i + 1) 21 | } 22 | } 23 | ``` -------------------------------------------------------------------------------- /src/_079_WordSearch/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a 2D board and a word, find if the word exists in the grid. 6 | * The word can be constructed from letters of sequentially adjacent cell, 7 | * where "adjacent" cells are those horizontally or vertically neighboring. 8 | * The same letter cell may not be used more than once. 9 | * 10 | * For example, Given board = 11 | * [ 12 | * ["ABCE"], 13 | * ["SFCS"], 14 | * ["ADEE"] 15 | * ] 16 | * word = "ABCCED", -> returns true, 17 | * word = "SEE", -> returns true, 18 | * word = "ABCB", -> returns false. 19 | * 20 | ************************************************************************* 21 | * @tag : Array; Backtracking 22 | * {@link https://leetcode.com/problems/word-search/ } 23 | */ 24 | package _079_WordSearch; 25 | 26 | /** see test {@link _079_WordSearch.PracticeTest } */ 27 | public class Practice { 28 | 29 | public boolean exist(char[][] board, String word) { 30 | // TODO Auto-generated method stub 31 | return false; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /src/_080_RemoveDuplicatesFromSortedArrayII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Follow up for "Remove Duplicates": 6 | * What if duplicates are allowed at most twice? 7 | * 8 | * For example, Given sorted array nums = [1,1,1,2,2,3], 9 | * Your function should return length = 5, with the first five elements of 10 | * nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond 11 | * the new length. 12 | * 13 | ************************************************************************* 14 | * @tag : Array; Two Pointers 15 | * {@link https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ } 16 | */ 17 | package _080_RemoveDuplicatesFromSortedArrayII; 18 | 19 | /** see test {@link _080_RemoveDuplicatesFromSortedArrayII.PracticeTest } */ 20 | public class Practice { 21 | 22 | public int removeDuplicates(int[] nums) { 23 | // TODO Auto-generated method stub 24 | return 0; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_081_SearchInRotatedSortedArrayII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Follow up for "Search in Rotated Sorted Array" 6 | * 7 | * Duplicates are allowed in array. 8 | * Write a function to determine if a given target is in the array. 9 | * 10 | ************************************************************************* 11 | * @tag : Array; Binary Search 12 | * {@link https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ } 13 | */ 14 | package _081_SearchInRotatedSortedArrayII; 15 | 16 | /** see test {@link _081_SearchInRotatedSortedArrayII.PracticeTest } */ 17 | public class Practice { 18 | 19 | public boolean search(int[] nums, int target) { 20 | // TODO Auto-generated method stub 21 | return false; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_083_RemoveDuplicatesFromSortedList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a sorted linked list, delete all duplicates such that each 6 | * element appear only once. 7 | * 8 | * For example, 9 | * Given 1->1->2, return 1->2. 10 | * Given 1->1->2->3->3, return 1->2->3. 11 | * 12 | ************************************************************************* 13 | * @tag : Linked List 14 | * {@link https://leetcode.com/problems/remove-duplicates-from-sorted-list/ } 15 | */ 16 | package _083_RemoveDuplicatesFromSortedList; 17 | 18 | import com.leetcode.ListNode; 19 | 20 | /** see test {@link _083_RemoveDuplicatesFromSortedList.PracticeTest } */ 21 | public class Practice { 22 | 23 | public ListNode deleteDuplicates(ListNode head) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_084_LargestRectangleInHistogram/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given n non-negative integers representing the histogram's bar height 6 | * where the width of each bar is 1, find the area of largest rectangle in 7 | * the histogram. 8 | ************************************************************************* 9 | * @tag : Array; Stack 10 | * {@link https://leetcode.com/problems/largest-rectangle-in-histogram/ } 11 | */ 12 | package _084_LargestRectangleInHistogram; 13 | 14 | /** see test {@link _084_LargestRectangleInHistogram.PracticeTest } */ 15 | public class Practice { 16 | 17 | public int largestRectangleArea(int[] height) { 18 | // TODO Auto-generated method stub 19 | return 0; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /src/_084_LargestRectangleInHistogram/SolutionTLE.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space: O() 3 | * @Tag : Array; Stack 4 | * @Date: Jun 9, 2015 5 | */ 6 | package _084_LargestRectangleInHistogram; 7 | 8 | public class SolutionTLE { 9 | public int largestRectangleArea(int[] height) { 10 | int result = 0; 11 | int len = height.length; 12 | for (int i = 0; i < len; i++) { 13 | int h = height[i]; 14 | int left = i - 1; 15 | int right = i + 1; 16 | while (left >= 0 && height[left] >= h) { 17 | left--; 18 | } 19 | while (right < len && height[right] >= h) { 20 | right++; 21 | } 22 | int w = right - left - 1; 23 | result = Math.max(result, w * h); 24 | } 25 | return result; 26 | } 27 | } -------------------------------------------------------------------------------- /src/_085_MaximalRectangle/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a 2D binary matrix filled with 0's and 1's, find the largest 6 | * rectangle containing all ones and return its area. 7 | * 8 | ************************************************************************* 9 | * @tag : Array; Hash Table; Stack; Dynamic Programming 10 | * {@link https://leetcode.com/problems/maximal-rectangle/ } 11 | */ 12 | package _085_MaximalRectangle; 13 | 14 | /** see test {@link _085_MaximalRectangle.PracticeTest } */ 15 | public class Practice { 16 | 17 | public int maximalRectangle(char[][] matrix) { 18 | // TODO Auto-generated method stub 19 | return 0; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_086_PartitionList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a linked list and a value x, partition it such that all nodes less 6 | * than x come before nodes greater than or equal to x. 7 | * 8 | * You should preserve the original relative order of the nodes in each of 9 | * the two partitions. 10 | * 11 | * For example, 12 | * Given 1->4->3->2->5->2 and x = 3, return 1->2->2->4->3->5. 13 | * 14 | ************************************************************************* 15 | * @tag : Linked List; Two Pointers 16 | * {@link https://leetcode.com/problems/partition-list/ } 17 | */ 18 | package _086_PartitionList; 19 | 20 | import com.leetcode.ListNode; 21 | 22 | /** see test {@link _086_PartitionList.PracticeTest } */ 23 | public class Practice { 24 | 25 | public ListNode partition(ListNode head, int x) { 26 | // TODO Auto-generated method stub 27 | return null; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/_088_MergeSortedArray/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 6 | * as one sorted array. 7 | * 8 | * Note: 9 | * You may assume that nums1 has enough space (size that is greater or equal 10 | * to m + n) to hold additional elements from nums2. The number of elements 11 | * initialized in nums1 and nums2 are m and n respectively. 12 | * 13 | ************************************************************************* 14 | * @tag : Array; Two Pointers 15 | * {@link https://leetcode.com/problems/merge-sorted-array/ } 16 | */ 17 | package _088_MergeSortedArray; 18 | 19 | /** see test {@link _088_MergeSortedArray.PracticeTest } */ 20 | public class Practice { 21 | 22 | public void merge(int[] nums1, int m, int[] nums2, int n) { 23 | // TODO Auto-generated method stub 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_090_SubsetsII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************ 3 | * Given a collection of integers that might contain duplicates, nums, 4 | * return all possible subsets. 5 | * 6 | * Note: 7 | * Elements in a subset must be in non-descending order. 8 | * The solution set must not contain duplicate subsets. 9 | * 10 | * For example, 11 | * If nums = [1,2,2], a solution is: 12 | * [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 13 | * 14 | ************************************************************************ 15 | * @tag : Array; Backtracking 16 | * {@link https://leetcode.com/problems/subsets-ii/ } 17 | */ 18 | package _090_SubsetsII; 19 | 20 | import java.util.List; 21 | 22 | /** see test {@link _090_SubsetsII.PracticeTest } */ 23 | public class Practice { 24 | 25 | public List> subsetsWithDup(int[] nums) { 26 | // TODO Auto-generated method stub 27 | return null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_091_DecodeWays/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************ 3 | * Description: 4 | * 5 | * A message containing letters from A-Z is being encoded to numbers using 6 | * the following mapping: 7 | * 'A' -> 1 'B' -> 2 ... 'Z' -> 26 8 | * Given an encoded message containing digits, determine the total number 9 | * of ways to decode it. 10 | * 11 | * For example, 12 | * Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). 13 | * The number of ways decoding "12" is 2. 14 | * 15 | * @tag : Dynamic Programming; String 16 | ************************************************************************ 17 | * {@link https://leetcode.com/problems/decode-ways/ } 18 | */ 19 | package _091_DecodeWays; 20 | 21 | /** see test {@link _091_DecodeWays.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int numDecodings(String s) { 25 | // TODO Auto-generated method stub 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_092_ReverseLinkedListII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Reverse a linked list from position m to n. Do it in-place and in one-pass. 6 | * 7 | * For example: 8 | * Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. 9 | * 10 | * Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. 11 | * 12 | ************************************************************************* 13 | * @tag : Linked List 14 | * {@link https://leetcode.com/problems/reverse-linked-list-ii/ } 15 | */ 16 | package _092_ReverseLinkedListII; 17 | 18 | import com.leetcode.ListNode; 19 | 20 | /** see test {@link _092_ReverseLinkedListII.PracticeTest } */ 21 | public class Practice { 22 | 23 | public ListNode reverseBetween(ListNode head, int m, int n) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_093_RestoreIPAddresses/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * Given a string containing only digits, restore it by returning all 5 | * possible valid IP address combinations. 6 | * For example: 7 | * Given "25525511135", 8 | * return ["255.255.11.135", "255.255.111.35"]. 9 | * (Order does not matter) 10 | * 11 | ************************************************************************* 12 | * @tag : Backtracking; String 13 | * {@link https://leetcode.com/problems/restore-ip-addresses/ } 14 | */ 15 | package _093_RestoreIPAddresses; 16 | 17 | import java.util.List; 18 | 19 | /** see test {@link _093_RestoreIPAddresses.PracticeTest } */ 20 | public class Practice { 21 | 22 | public List restoreIpAddresses(String s) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_094_BinaryTreeInorderTraversal/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, return the inorder traversal of its nodes' values. 6 | * For example: 7 | * Given binary tree {1,#,2,3}, 8 | * 1 9 | * \ 10 | * 2 11 | * / 12 | * 3 13 | * return [1,3,2]. 14 | * Note: Recursive solution is trivial, could you do it iteratively? 15 | * 16 | ************************************************************************* 17 | * @tag : Tree; Hash Table; Stack 18 | * {@link https://leetcode.com/problems/binary-tree-inorder-traversal/ } 19 | */ 20 | package _094_BinaryTreeInorderTraversal; 21 | 22 | import java.util.List; 23 | 24 | import com.leetcode.TreeNode; 25 | 26 | /** see test {@link _094_BinaryTreeInorderTraversal.PracticeTest } */ 27 | public class Practice { 28 | 29 | public List inorderTraversal(TreeNode root) { 30 | // TODO Auto-generated method stub 31 | return null; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/_095_UniqueBinarySearchTreesII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given n, generate all structurally unique BST's (binary search trees) 6 | * that store values 1...n. 7 | * 8 | * For example, 9 | * Given n = 3, your program should return all 5 unique BST's shown below. 10 | * 1 3 3 2 1 11 | * \ / / / \ \ 12 | * 3 2 1 1 3 2 13 | * / / \ \ 14 | * 2 1 2 3 15 | * 16 | ************************************************************************* 17 | * @tag : Tree; Dynamic Programming 18 | * {@link https://leetcode.com/problems/unique-binary-search-trees-ii/ } 19 | */ 20 | package _095_UniqueBinarySearchTreesII; 21 | 22 | import java.util.List; 23 | 24 | import com.leetcode.TreeNode; 25 | 26 | /** see test {@link _095_UniqueBinarySearchTreesII.PracticeTest } */ 27 | public class Practice { 28 | 29 | public List generateTrees(int n) { 30 | // TODO Auto-generated method stub 31 | return null; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/_096_UniqueBinarySearchTrees/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given n, how many structurally unique BST's (binary search trees) that 6 | * store values 1...n? 7 | * 8 | ************************************************************************* 9 | * @tag : Tree; Dynamic Programming 10 | * {@link https://leetcode.com/problems/unique-binary-search-trees/ } 11 | */ 12 | package _096_UniqueBinarySearchTrees; 13 | 14 | /** see test {@link _096_UniqueBinarySearchTrees.PracticeTest } */ 15 | public class Practice { 16 | 17 | public int numTrees(int n) { 18 | // TODO Auto-generated method stub 19 | return 0; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_097_InterleavingString/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. 6 | * 7 | * For example, 8 | * 9 | * Given: 10 | * s1 = "aabcc", s2 = "dbbca", 11 | * When s3 = "aadbbcbcac", return true. 12 | * When s3 = "aadbbbaccc", return false. 13 | * 14 | ************************************************************************* 15 | * @Tag : Dynamic Programming; String 16 | * {@link https://leetcode.com/problems/interleaving-string/ } 17 | */ 18 | package _097_InterleavingString; 19 | 20 | /** see test {@link _097_InterleavingString.PracticeTest } */ 21 | public class Practice { 22 | 23 | public boolean isInterleave(String s1, String s2, String s3) { 24 | // TODO Auto-generated method stub 25 | return false; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_097_InterleavingString/README.md: -------------------------------------------------------------------------------- 1 | ### [097] Interleaving String 2 | 3 | 1. Progress 4 | 5 | [DFS](https://github.com/interviewcoder/leetcode/blob/master/src/_097_InterleavingString/SolutionDFS.java) 6 | -> [Memo DPmemorize](https://github.com/interviewcoder/leetcode/blob/master/src/_097_InterleavingString/SolutionMemo.java) 7 | -> [Bottom-up DP](https://github.com/interviewcoder/leetcode/blob/master/src/_097_InterleavingString/SolutionBottomUp.java) 8 | -> [Space efficient Bottom-up DP](https://github.com/interviewcoder/leetcode/blob/master/src/_097_InterleavingString/Solution.java) 9 | 2. 10 | -------------------------------------------------------------------------------- /src/_098_ValidateBinarySearchTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, determine if it is a valid binary search tree (BST). 6 | * Assume a BST is defined as follows: 7 | * 8 | * The left subtree of a node contains only nodes with keys less than the node's key. 9 | * The right subtree of a node contains only nodes with keys greater than the node's key. 10 | * Both the left and right subtrees must also be binary search trees. 11 | * 12 | ************************************************************************* 13 | * @tag : Tree; Depth-first Search 14 | * {@link https://leetcode.com/problems/validate-binary-search-tree/ } 15 | */ 16 | package _098_ValidateBinarySearchTree; 17 | 18 | import com.leetcode.TreeNode; 19 | 20 | /** see test {@link _098_ValidateBinarySearchTree.PracticeTest } */ 21 | public class Practice { 22 | 23 | public boolean isValidBST(TreeNode root) { 24 | // TODO Auto-generated method stub 25 | return false; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_099_RecoverBinarySearchTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Two elements of a binary search tree (BST) are swapped by mistake. 6 | * Recover the tree without changing its structure. 7 | * 8 | * Note: 9 | * A solution using O(n) space is pretty straight forward. Could you devise 10 | * a constant space solution? 11 | * 12 | ************************************************************************* 13 | * @tag : Tree; Depth-first Search 14 | * {@link https://leetcode.com/problems/recover-binary-search-tree/ } 15 | */ 16 | package _099_RecoverBinarySearchTree; 17 | 18 | import com.leetcode.TreeNode; 19 | 20 | /** see test {@link _099_RecoverBinarySearchTree.PracticeTest } */ 21 | public class Practice { 22 | 23 | public void recoverTree(TreeNode root) { 24 | // TODO Auto-generated method stub 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_099_RecoverBinarySearchTree/README.md: -------------------------------------------------------------------------------- 1 | #### [099] Recover binary search tree 2 | 3 | 0. Solution 0: brutal force -- O(N) space 4 | 5 | 1. convert tree to list using in-order traversal 6 | 2. find `first` and `second` nodes: 7 | - `first` is the **former** one in first `former` > `latter` pair 8 | - `second` is the **latter** one in `last` `former` > `latter` pair 9 | 10 | 3. swap values of `first` and `second` 11 | 12 | 1. Solution I: in-order traversal -- O(lgN) space (due to recursive call) 13 | 14 | Try to implement the abobe logic in in-order traversal, to do that, we need know `previous` node before current visiting node 15 | 16 | 2. Solution I with pruning: 17 | 18 | ```java 19 | if (second != null && cur.val > first.val) { 20 | return; 21 | } 22 | foo(root.right); 23 | ``` 24 | 25 | 1) e.g. 1, 3, 2, 4, || 5, 6, 7, 8 ==> 1, 2, 3, 4, 5, 6, 7, 8 26 | 27 | 2) e.g. 1, 5, 3, 4, 2, 6,|| 7, 8 => 1, 2, 3, 4, 5, 6, 7, 8 28 | 29 | 30 | 2. Solution II: Morris Traversal -- O(1) space -------------------------------------------------------------------------------- /src/_100_SameTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given two binary trees, write a function to check if they are equal or not. 6 | * Two binary trees are considered equal if they are structurally identical 7 | * and the nodes have the same value. 8 | * 9 | ************************************************************************* 10 | * @tag : Tree; Depth-First-Search 11 | * {@link https://leetcode.com/problems/same-tree/ } 12 | */ 13 | package _100_SameTree; 14 | 15 | import com.leetcode.TreeNode; 16 | 17 | /** see test {@link _100_SameTree.PracticeTest } */ 18 | public class Practice { 19 | 20 | public boolean isSameTree(TreeNode p, TreeNode q) { 21 | // TODO Auto-generated method stub 22 | return false; 23 | } 24 | 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/_101_SymmetricTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 6 | * For example, this binary tree is symmetric: 7 | * 1 8 | * / \ 9 | * 2 2 10 | * / \ / \ 11 | * 3 4 4 3 12 | * 13 | * But the following is not: 14 | * 1 15 | * / \ 16 | * 2 2 17 | * \ \ 18 | * 3 3 19 | * Note: 20 | * Bonus points if you could solve it both recursively and iteratively. 21 | * 22 | ************************************************************************* 23 | * @tag : Tree; Depth-first Search 24 | * {@link https://leetcode.com/problems/symmetric-tree/ } 25 | */ 26 | package _101_SymmetricTree; 27 | 28 | import com.leetcode.TreeNode; 29 | 30 | /** see test {@link _101_SymmetricTree.PracticeTest } */ 31 | public class Practice { 32 | 33 | public boolean isSymmetric(TreeNode root) { 34 | // TODO Auto-generated method stub 35 | return false; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/_102_BinaryTreeLevelOrderTraversal/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, return the level order traversal of its nodes' values. 6 | * (ie, from left to right, level by level). 7 | * 8 | * For example: Given binary tree {3,9,20,#,#,15,7}, 9 | * 3 10 | * / \ 11 | * 9 20 12 | * / \ 13 | * 15 7 14 | * return its level order traversal as: 15 | * [ [3], [9,20], [15,7] ] 16 | * 17 | ************************************************************************* 18 | * @tag : Tree; Breadth-first Search 19 | * {@link https://leetcode.com/problems/binary-tree-level-order-traversal/ } 20 | */ 21 | package _102_BinaryTreeLevelOrderTraversal; 22 | 23 | import java.util.List; 24 | import com.leetcode.TreeNode; 25 | 26 | /** see test {@link _102_BinaryTreeLevelOrderTraversal.PracticeTest } */ 27 | public class Practice { 28 | 29 | public List> levelOrder(TreeNode root) { 30 | // TODO Auto-generated method stub 31 | return null; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/_104_MaximumDepthOfBinaryTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, find its maximum depth. 6 | * 7 | * The maximum depth is the number of nodes along the longest path from 8 | * the root node down to the farthest leaf node. 9 | * 10 | ******************************************************************************* 11 | * @tag : Tree; Depth-first Search 12 | * {@link https://leetcode.com/problems/maximum-depth-of-binary-tree/ } 13 | */ 14 | package _104_MaximumDepthOfBinaryTree; 15 | 16 | import com.leetcode.TreeNode; 17 | 18 | /** see test {@link _104_MaximumDepthOfBinaryTree.PracticeTest } */ 19 | public class Practice { 20 | 21 | public int maxDepth(TreeNode rootNode) { 22 | // TODO Auto-generated method stub 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_104_MaximumDepthOfBinaryTree/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space : O() 3 | * @tag : Tree; Depth-first Search 4 | * @by : Steven Cooks 5 | * @date: Jun 28, 2015 6 | ******************************************************************************* 7 | * Description: 8 | * 9 | * Given a binary tree, find its maximum depth. 10 | * 11 | * The maximum depth is the number of nodes along the longest path from 12 | * the root node down to the farthest leaf node. 13 | * 14 | ******************************************************************************* 15 | * {@link https://leetcode.com/problems/maximum-depth-of-binary-tree/ } 16 | */ 17 | package _104_MaximumDepthOfBinaryTree; 18 | 19 | import com.leetcode.TreeNode; 20 | 21 | /** see test {@link _104_MaximumDepthOfBinaryTree.SolutionTest } */ 22 | public class Solution { 23 | 24 | // post-order traversal of DFS 25 | public int maxDepth(TreeNode rootNode) { 26 | // base case 27 | if (rootNode == null) { 28 | return 0; 29 | } 30 | // recursive case 31 | return 1 + Math.max(maxDepth(rootNode.left), maxDepth(rootNode.right)); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/_105_ConstructBinaryTreeFromPreorderAndInorderTraversal/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ***************************************************************** 3 | * Description: 4 | * 5 | * Given preorder and inorder traversal of a tree, construct the binary tree. 6 | * 7 | * Note: 8 | * You may assume that duplicates do not exist in the tree. 9 | * 10 | ***************************************************************** 11 | * @tag : Tree; Array; Depth-first Search 12 | * {@link https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/} 13 | */ 14 | package _105_ConstructBinaryTreeFromPreorderAndInorderTraversal; 15 | 16 | import com.leetcode.TreeNode; 17 | 18 | /** 19 | * see test 20 | * {@link _105_ConstructBinaryTreeFromPreorderAndInorderTraversal.PracticeTest } 21 | */ 22 | public class Practice { 23 | 24 | public TreeNode buildTree(int[] preorder, int[] inorder) { 25 | // TODO Auto-generated method stub 26 | return null; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_106_ConstructBinaryTreeFromInorderAndPostorderTraversal/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ***************************************************************** 3 | * Description: 4 | * 5 | * Given inorder and postorder traversal of a tree, construct the binary tree. 6 | * Note: 7 | * You may assume that duplicates do not exist in the tree 8 | * 9 | ***************************************************************** 10 | * @tag : Tree; Array; Depth-first Search 11 | * {@link https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ } 12 | */ 13 | package _106_ConstructBinaryTreeFromInorderAndPostorderTraversal; 14 | 15 | import com.leetcode.TreeNode; 16 | 17 | /** 18 | * see test 19 | * {@link _106_ConstructBinaryTreeFromInorderAndPostorderTraversal.PracticeTest } 20 | */ 21 | public class Practice { 22 | 23 | public TreeNode buildTree(int[] inorder, int[] postorder) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_107_BinaryTreeLevelOrderTraversalII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ***************************************************************** 3 | * Description: 4 | * 5 | * Given a binary tree, return the bottom-up level order traversal of its 6 | * nodes' values. (ie, from left to right, level by level from leaf to root). 7 | * 8 | * For example: 9 | * Given binary tree {3,9,20,#,#,15,7}, 10 | * 3 11 | * / \ 12 | * 9 20 13 | * / \ 14 | * 15 7 15 | * return its bottom-up level order traversal as: 16 | * [ [15,7], [9,20], [3] ] 17 | * 18 | ***************************************************************** 19 | * @tag : Tree; Breadth-first Search 20 | * {@link: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ } 21 | */ 22 | package _107_BinaryTreeLevelOrderTraversalII; 23 | 24 | import java.util.List; 25 | import com.leetcode.TreeNode; 26 | 27 | /** see test {@link _107_BinaryTreeLevelOrderTraversalII.PracticeTest } */ 28 | public class Practice { 29 | 30 | public List> levelOrderBottom(TreeNode root) { 31 | // TODO Auto-generated method stub 32 | return null; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/_108_ConvertSortedArrayToBinarySearchTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ***************************************************************** 3 | * Description: 4 | * 5 | * Given an array where elements are sorted in ascending order, * 6 | * convert it to a height balanced BST. * 7 | ***************************************************************** 8 | * @tag : Tree; Depth-first Search 9 | * {@link https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ } 10 | */ 11 | package _108_ConvertSortedArrayToBinarySearchTree; 12 | 13 | import com.leetcode.TreeNode; 14 | 15 | /** see test {@link _108_ConvertSortedArrayToBinarySearchTree.PracticeTest } */ 16 | public class Practice { 17 | 18 | public TreeNode sortedArrayToBST(int[] nums) { 19 | // TODO Auto-generated method stub 20 | return null; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_110_BalancedBinaryTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, determine if it is height-balanced. 6 | * For this problem, a height-balanced binary tree is defined as a binary 7 | * tree in which the depth of the two subtrees of every node never differ by 8 | * more than 1. 9 | * 10 | ************************************************************************* 11 | * @tag : Tree; Depth-first Search 12 | * {@link: https://leetcode.com/problems/balanced-binary-tree/ } 13 | */ 14 | package _110_BalancedBinaryTree; 15 | 16 | import com.leetcode.TreeNode; 17 | 18 | /** see test {@link _110_BalancedBinaryTree.PracticeTest } */ 19 | public class Practice { 20 | 21 | public boolean isBalanced(TreeNode root) { 22 | // TODO Auto-generated method stub 23 | return false; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_110_BalancedBinaryTree/README.md: -------------------------------------------------------------------------------- 1 | ### [110] Balanced Binary Tree 2 | 3 | - Solution I: visit same node repeated `O(NlgN) ~ O(N^2)` 4 | 5 | - Either *pre*-order or *post*-order can solve the problem. 6 | ```java 7 | // viist left subtree and right subtree 8 | foo(root.left) && foo(root.right) 9 | // visit again to get the depth of each subtree 10 | return | depth(root.left) - depth(root.right) | <= 1 11 | ``` 12 | - Solution II: visit each node only once! `O(N)` 13 | 14 | - only `post`-order can solve the problen in this way 15 | ```java 16 | // return -1 if tree is not balanced 17 | // otherwise return tree depth 18 | public int maxDepth(TreeNode root) { 19 | // visit left subtree and right subtree 20 | int left = maxDepth(root.left); 21 | int right = maxDepth(root.right); 22 | 23 | // visit root 24 | if (left == -1 || right == -1 25 | || Math.max(left - right) > 1) { 26 | return -1; 27 | } else { 28 | return 1 + Math.max(left, right); 29 | } 30 | } 31 | ``` -------------------------------------------------------------------------------- /src/_111_MinimumDepthOfBinaryTree/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, find its minimum depth. 6 | * The minimum depth is the number of nodes along the shortest path from the 7 | * root node down to the nearest leaf node. 8 | * 9 | ************************************************************************* 10 | * @tag : Tree; Depth-first Search 11 | * {@link https://leetcode.com/problems/minimum-depth-of-binary-tree/ } 12 | */ 13 | package _111_MinimumDepthOfBinaryTree; 14 | 15 | import com.leetcode.TreeNode; 16 | 17 | /** see test {@link _111_MinimumDepthOfBinaryTree.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int minDepth(TreeNode root) { 21 | // TODO Auto-generated method stub 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_112_PathSum/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree and a sum, determine if the tree has a root-to-leaf 6 | * path such that adding up all the values along the path equals the given sum. 7 | * 8 | * For example: 9 | * Given the below binary tree and sum = 22, 10 | * 5 11 | * / \ 12 | * 4 8 13 | * / / \ 14 | * 11 13 4 15 | * / \ \ 16 | * 7 2 1 17 | * 18 | * return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22. 19 | * 20 | ************************************************************************* 21 | * @tag : Tree; Depth-first Search 22 | * {@link: https://leetcode.com/problems/path-sum/ } 23 | */ 24 | package _112_PathSum; 25 | 26 | import com.leetcode.TreeNode; 27 | 28 | /** see test {@link _112_PathSum.PracticeTest } */ 29 | public class Practice { 30 | 31 | public boolean hasPathSum(TreeNode root, int sum) { 32 | // TODO Auto-generated method stub 33 | return false; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/_114_FlattenBinaryTreeToLinkedList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, flatten it to a linked list in-place. 6 | * For example, Given 7 | * 1 8 | * / \ 9 | * 2 5 10 | * / \ \ 11 | * 3 4 6 12 | * 13 | * The flattened tree should look like: 14 | * 1 \ 2 \ 3 \ 4 \ 5 \ 6 15 | * 16 | ************************************************************************* 17 | * @tag : Tree; Depth-first Search 18 | * {@link: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ } 19 | */ 20 | package _114_FlattenBinaryTreeToLinkedList; 21 | 22 | import com.leetcode.TreeNode; 23 | 24 | /** see test {@link _114_FlattenBinaryTreeToLinkedList.PracticeTest } */ 25 | public class Practice { 26 | 27 | public void flatten(TreeNode root) { 28 | // TODO Auto-generated method stub 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/_115_DistinctSubsequences/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************** 3 | * Description: 4 | * 5 | * Given a string S and a string T, count the number of distinct subsequences 6 | * of T in S. 7 | * A subsequence of a string is a new string which is formed from the original 8 | * string by deleting some (can be none) of the characters without disturbing 9 | * the relative positions of the remaining characters. (ie, "ACE" is a 10 | * subsequence of "ABCDE" while "AEC" is not). 11 | * 12 | * Here is an example: S = "rabbbit", T = "rabbit" 13 | * Return 3. 14 | * 15 | ******************************************************** 16 | * @tag : Dynamic Programming; String 17 | * {@link https://leetcode.com/problems/distinct-subsequences/ } 18 | */ 19 | package _115_DistinctSubsequences; 20 | 21 | /** see test {@link _115_DistinctSubsequences.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int numDistinct(String s, String t) { 25 | // TODO Auto-generated method stub 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_116_PopulatingNextRightPointersInEachNode/SolutionRecursive.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space: O(logN) 3 | * @tag : Tree; Depth-first Search 4 | * @date: Jun 14, 2015 5 | * @by : Steven Cooks 6 | * {@link https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ } 7 | */ 8 | package _116_PopulatingNextRightPointersInEachNode; 9 | 10 | import com.leetcode.TreeLinkNode; 11 | 12 | /** see test {@link _116_PopulatingNextRightPointersInEachNode.SolutionRecursiveTest } */ 13 | public class SolutionRecursive { 14 | 15 | // recursive version 16 | public void connect(TreeLinkNode root) { 17 | if (root == null) { 18 | return; 19 | } 20 | if (root.left != null && root.right != null) { 21 | root.left.next = root.right; 22 | root.right.next = (root.next == null) ? null : root.next.left; 23 | } 24 | connect(root.left); 25 | connect(root.right); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_117_PopulatingNextRightPointersInEachNodeII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************** 3 | * Description: 4 | * 5 | * Follow up for problem "Populating Next Right Pointers in Each Node". 6 | * What if the given tree could be any binary tree? Would your previous solution still work? 7 | ******************************************************** 8 | * @tag : Tree; Depth-first Search 9 | * {@link https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ } 10 | */ 11 | package _117_PopulatingNextRightPointersInEachNodeII; 12 | 13 | import com.leetcode.TreeLinkNode; 14 | 15 | /** 16 | * {@link https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ } 17 | */ 18 | public class Practice { 19 | 20 | public void connect(TreeLinkNode root) { 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_118_PascalsTriangle/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given numRows, generate the first numRows of Pascal's triangle. 6 | * 7 | * For example, given numRows = 5, 8 | * Return 9 | * [ [1], 10 | * [1,1], 11 | * [1,2,1], 12 | * [1,3,3,1], 13 | * [1,4,6,4,1] 14 | * ] 15 | ******************************************************************************* 16 | * @tag : Array 17 | * {@link https://leetcode.com/problems/pascals-triangle/ } 18 | */ 19 | package _118_PascalsTriangle; 20 | 21 | import java.util.List; 22 | 23 | /** see test {@link _118_PascalsTriangle.PracticeTest } */ 24 | public class Practice { 25 | 26 | public List> generate(int numRows) { 27 | // TODO Auto-generated method stub 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_119_PascalsTriangleII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given an index k, return the kth row of the Pascal's triangle. 6 | * 7 | * For example, given k = 3, 8 | * Return [1,3,3,1]. 9 | * 10 | * Note: 11 | * Could you optimize your algorithm to use only O(k) extra space? 12 | * 13 | ******************************************************************************* 14 | * @tag : Array 15 | * {@link https://leetcode.com/problems/pascals-triangle-ii/ } 16 | */ 17 | package _119_PascalsTriangleII; 18 | 19 | import java.util.List; 20 | 21 | /** see test {@link _119_PascalsTriangleII.SolutionTest } */ 22 | public class Practice { 23 | 24 | public List getRow(int rowIndex) { 25 | // TODO Auto-generated method stub 26 | return null; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_121_BestTimeToBuyAndSellStock/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ***************************************************************************** 3 | * Description: 4 | * 5 | * Say you have an array for which the ith element is the price 6 | * of a given stock on day i. 7 | * 8 | * If you were only permitted to complete at most one transaction 9 | * (ie, buy one and sell one share of the stock), design an algorithm 10 | * to find the maximum profit. 11 | * 12 | ***************************************************************************** 13 | * @tag : Array; Dynamic Programming 14 | * {@link https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ } 15 | */ 16 | package _121_BestTimeToBuyAndSellStock; 17 | 18 | /** see test {@link _121_BestTimeToBuyAndSellStock.PracticeTest } */ 19 | public class Practice { 20 | 21 | public int maxProfit(int[] prices) { 22 | // TODO Auto-generated method stub 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_121_BestTimeToBuyAndSellStock/README.md: -------------------------------------------------------------------------------- 1 | #### [121] Best to Buy and Sell Stock 2 | 3 | 0. Brutal Force: O(N^2) solution 4 | 5 | ```plain 6 | For each stock price, // -> O(M) 7 | we want to find the lowest price beforehands. // O(N) 8 | ``` 9 | 10 | 1. O(N) solution 11 | 12 | Save the lowest price before 13 | -------------------------------------------------------------------------------- /src/_122_BestTimeToBuyAndSellStockII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Say you have an array for which the ith element is the price of 4 | * a given stock on day i. 5 | * 6 | * Design an algorithm to find the maximum profit. You may complete as 7 | * many transactions as you like (ie, buy one and sell one share of the 8 | * stock multiple times). However, you may not engage in multiple 9 | * transactions at the same time (ie, you must sell the stock before 10 | * you buy again). 11 | * 12 | ************************************************************************* 13 | * @tag : Array; Greedy 14 | * {@link https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ } 15 | */ 16 | package _122_BestTimeToBuyAndSellStockII; 17 | 18 | /** see test {@link _122_BestTimeToBuyAndSellStockII.PracticeTest } */ 19 | public class Practice { 20 | 21 | public int maxProfit(int[] prices) { 22 | // TODO Auto-generated method stub 23 | return 0; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_123_BestTimeToBuyAndSellStockIII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *********************************************************************** 3 | * Say you have an array for which the ith element is the price of * 4 | * a given stock on day i. * 5 | * Design an algorithm to find the maximum profit. * 6 | * You may complete at most two transactions. * 7 | * 8 | * Note: * 9 | * You may not engage in multiple transactions at the same time * 10 | * (ie, you must sell the stock before you buy again). * 11 | *********************************************************************** 12 | * @tag : Array; Dynamic Programming 13 | * {@link https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ } 14 | */ 15 | package _123_BestTimeToBuyAndSellStockIII; 16 | 17 | /** see test {@link _123_BestTimeToBuyAndSellStockIII.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int maxProfit(int[] prices) { 21 | // TODO Auto-generated method stub 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_124_BinaryTreeMaximumPathSum/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, find the maximum path sum. 6 | * The path may start and end at any node in the tree. 7 | * For example: Given the below binary tree, 8 | * 1 9 | * / \ 10 | * 2 3 11 | * Return 6. 12 | * 13 | ************************************************************************* 14 | * @tag : Tree; Depth-first Search 15 | * {@link https://leetcode.com/problems/binary-tree-maximum-path-sum/ } 16 | */ 17 | package _124_BinaryTreeMaximumPathSum; 18 | 19 | import com.leetcode.TreeNode; 20 | 21 | /** see test {@link _124_BinaryTreeMaximumPathSum.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int maxPathSum(TreeNode root) { 25 | // TODO Auto-generated method stub 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_125_ValidPalindrome/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Given a string, determine if it is a palindrome, considering only 4 | * alphanumeric characters and ignoring cases. 5 | * 6 | * For example, 7 | * "A man, a plan, a canal: Panama" is a palindrome. 8 | * "race a car" is not a palindrome. 9 | ************************************************************************* 10 | * @tag : Two Pointers; String 11 | * {@link https://leetcode.com/problems/valid-palindrome/ } 12 | */ 13 | package _125_ValidPalindrome; 14 | 15 | /** see test {@link _125_ValidPalindrome.PracticeTest } */ 16 | public class Practice { 17 | 18 | public boolean isPalindrome(String s) { 19 | // TODO Auto-generated method stub 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_128_LongestConsecutiveSequence/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an unsorted array of integers, find the length of the longest 6 | * consecutive elements sequence. 7 | * 8 | * For example, Given [100, 4, 200, 1, 3, 2], 9 | * The longest consecutive elements sequence is [1, 2, 3, 4]. Return its 10 | * length: 4. 11 | * 12 | * Your algorithm should run in O(n) complexity. 13 | * 14 | ************************************************************************* 15 | * @tag : Array 16 | * {@link https://leetcode.com/problems/longest-consecutive-sequence/ } 17 | */ 18 | package _128_LongestConsecutiveSequence; 19 | 20 | /** see test {@link _128_LongestConsecutiveSequence.PracticeTest } */ 21 | public class Practice { 22 | 23 | public int longestConsecutive(int[] nums) { 24 | // TODO Auto-generated method stub 25 | return 0; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_129_SumRootToLeafNumbers/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************** 3 | * Given a binary tree containing digits from 0-9 only, * 4 | * each root-to-leaf path could represent a number. * 5 | * An example is the root-to-leaf path 1->2->3 which represents the number 123.* 6 | * Find the total sum of all root-to-leaf numbers. * 7 | ******************************************************************************** 8 | * @tag : Tree; Depth-first Search 9 | * {@link https://leetcode.com/problems/sum-root-to-leaf-numbers/ } 10 | */ 11 | package _129_SumRootToLeafNumbers; 12 | 13 | import com.leetcode.TreeNode; 14 | 15 | /** see test {@link _129_SumRootToLeafNumbers.PracticeTest } */ 16 | public class Practice { 17 | 18 | public int sumNumbers(TreeNode root) { 19 | // TODO Auto-generated method stub 20 | return 0; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_130_SurroundedRegions/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Given a 2D board containing 'X' and 'O', capture all regions * 4 | * surrounded by 'X'. * 5 | * A region is captured by flipping all 'O's into 'X's in * 6 | * that surrounded region. * 7 | ************************************************************************* 8 | * @tag : Breadth-first Search 9 | * {@link https://leetcode.com/problems/surrounded-regions/ } 10 | */ 11 | package _130_SurroundedRegions; 12 | 13 | /** see test {@link _130_SurroundedRegions.PracticeTest } */ 14 | public class Practice { 15 | 16 | public void solve(char[][] board) { 17 | // TODO Auto-generated method stub 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/_131_PalindromePartitioning/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Given a string s, partition s such that every substring of the 4 | * partition is a palindrome. 5 | * Return all possible palindrome partitioning of s. 6 | * 7 | * For example, given s = "aab", 8 | * Return 9 | * [ 10 | * ["aa","b"], 11 | * ["a","a","b"] 12 | * ] 13 | * 14 | ************************************************************************* 15 | * @tag : Backtracking 16 | * {@link https://leetcode.com/problems/palindrome-partitioning/ } 17 | */ 18 | package _131_PalindromePartitioning; 19 | 20 | import java.util.List; 21 | 22 | /** see test {@link _131_PalindromePartitioning.PracticeTest } */ 23 | public class Practice { 24 | 25 | public List> partition(String s) { 26 | // TODO Auto-generated method stub 27 | return null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_131_PalindromePartitioning/SolutionDP.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N^2); Space: O() 3 | * @tag : Backtracking 4 | * @by : Steven Cooks 5 | * @date: Jun 17, 2015 6 | ************************************************************************* 7 | * Description: 8 | * 9 | ************************************************************************* 10 | * {@link https://leetcode.com/problems/palindrome-partitioning/ } 11 | */ 12 | package _131_PalindromePartitioning; 13 | 14 | import java.util.List; 15 | 16 | /** see test {@link _131_PalindromePartitioning.SolutionDPTest } */ 17 | public class SolutionDP { 18 | 19 | public List> partition(String s) { 20 | return null; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_132_PalindromePartitioningII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a string s, partition s such that every substring of the partition 6 | * is a palindrome. 7 | * 8 | * Return the minimum cuts needed for a palindrome partitioning of s. 9 | * 10 | * For example, given s = "aab", 11 | * Return 1 since the palindrome partitioning ["aa","b"] could be produced 12 | * using 1 cut. 13 | * 14 | ************************************************************************* 15 | * @tag : Dynamic Programming 16 | * {@link https://leetcode.com/problems/palindrome-partitioning-ii/ } 17 | */ 18 | package _132_PalindromePartitioningII; 19 | 20 | /** see test {@link _132_PalindromePartitioningII.PracticeTest } */ 21 | public class Practice { 22 | 23 | public int minCut(String s) { 24 | // TODO Auto-generated method stub 25 | return 0; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_133_CloneGraph/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Clone an undirected graph. Each node in the graph contains a label and 6 | * a list of its neighbors. 7 | * 8 | * Visually, the graph looks like the following: 9 | * 10 | * 1 11 | * / \ 12 | * / \ 13 | * 0 --- 2 14 | * / \ 15 | * \_/ 16 | ******************************************************************************* 17 | * @tag : Depth-first Search; Breadth-first Search; Graph 18 | * {@link https://leetcode.com/problems/clone-graph/ } 19 | */ 20 | package _133_CloneGraph; 21 | 22 | import com.leetcode.UndirectedGraphNode; 23 | 24 | /** {@link _133_CloneGraph.PracticeTest } */ 25 | public class Practice { 26 | 27 | public UndirectedGraphNode cloneGraph(UndirectedGraphNode n1) { 28 | // TODO Auto-generated method stub 29 | return null; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/_135_Candy/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * There are N children standing in a line. Each child is assigned 6 | * a rating value. You are giving candies to these children subjected 7 | * to the following requirements: 8 | * 9 | * Each child must have at least one candy. Children with a higher 10 | * rating get more candies than their neighbors. 11 | * 12 | * What is the minimum candies you must give? 13 | ************************************************************************* 14 | * @tag : Greedy 15 | * {@link https://leetcode.com/problems/candy/ } 16 | */ 17 | package _135_Candy; 18 | 19 | /** see test {@link _135_Candy.PracticeTest } */ 20 | public class Practice { 21 | 22 | public int candy(int[] ratings) { 23 | // TODO Auto-generated method stub 24 | return 0; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_135_Candy/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(); Space: O() 3 | * @tag : Greedy 4 | * @by : Steven Cooks 5 | * @date: Jun 17, 2015 6 | ************************************************************************* 7 | * Description: 8 | * 9 | * There are N children standing in a line. Each child is assigned 10 | * a rating value. You are giving candies to these children subjected 11 | * to the following requirements: 12 | * 13 | * Each child must have at least one candy. Children with a higher 14 | * rating get more candies than their neighbors. 15 | * 16 | * What is the minimum candies you must give? 17 | ************************************************************************* 18 | * {@link https://leetcode.com/problems/candy/ } 19 | */ 20 | package _135_Candy; 21 | 22 | /** see test {@link _135_Candy.SolutionTest } */ 23 | public class Solution { 24 | 25 | public int candy(int[] ratings) { 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_136_SingleNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an array of integers, every element appears twice except for one. 6 | * Find that single one. 7 | * Note: 8 | * Your algorithm should have a linear runtime complexity. Could you 9 | * implement it without using extra memory? 10 | * 11 | ************************************************************************* 12 | * @tag : Bit Manipulation 13 | * {@link https://leetcode.com/problems/single-number/ } 14 | */ 15 | package _136_SingleNumber; 16 | 17 | /** see test {@link _136_SingleNumber.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int singleNumber(int[] nums) { 21 | // TODO Auto-generated method stub 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_136_SingleNumber/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N) ; Space: O(1) 3 | * @tag : Bit Manipulation 4 | * @by : Steven Cooks 5 | * @date: 04.27.2015 6 | ************************************************************************* 7 | * Description: 8 | * 9 | * Given an array of integers, every element appears twice except for one. 10 | * Find that single one. 11 | * Note: 12 | * Your algorithm should have a linear runtime complexity. Could you 13 | * implement it without using extra memory? 14 | * 15 | ************************************************************************* 16 | * {@link https://leetcode.com/problems/single-number/ } 17 | */ 18 | package _136_SingleNumber; 19 | 20 | /** see test {@link _136_SingleNumber.SolutionTest } */ 21 | public class Solution { 22 | 23 | public int singleNumber(int[] nums) { 24 | if (nums.length <= 0) { 25 | return 0; 26 | } 27 | int result = nums[0]; 28 | for (int i = 1; i < nums.length; i++) { 29 | // xor operation: using the trick 30 | // same number ^ same number = 0 31 | result ^= nums[i]; 32 | } 33 | return result; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/_138_CopyListWithRandomPointer/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * A linked list is given such that each node contains an additional random 6 | * pointer which could point to any node in the list or null. 7 | * 8 | * Return a deep copy of the list. 9 | * 10 | ************************************************************************* 11 | * @tag : Hash Table; Linked List 12 | * {@link https://leetcode.com/problems/copy-list-with-random-pointer/ } 13 | */ 14 | package _138_CopyListWithRandomPointer; 15 | 16 | /** see test {@link _138_CopyListWithRandomPointer.PracticeTest } */ 17 | public class Practice { 18 | 19 | public RandomListNode copyRandomList(RandomListNode head) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_138_CopyListWithRandomPointer/RandomListNode.java: -------------------------------------------------------------------------------- 1 | package _138_CopyListWithRandomPointer; 2 | 3 | class RandomListNode { 4 | int label; 5 | RandomListNode next; 6 | RandomListNode random; 7 | RandomListNode(int x) { 8 | this.label = x; 9 | } 10 | 11 | @Override 12 | public String toString() { 13 | return "" + label; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /src/_139_WordBreak/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a string s and a dictionary of words dict, determine if s can be 6 | * segmented into a space-separated sequence of one or more dictionary words. 7 | * 8 | * For example, given s = "leetcode", dict = ["leet", "code"]. 9 | * Return true because "leetcode" can be segmented as "leet code". 10 | * 11 | ************************************************************************* 12 | * @tag : Dynamic Programming 13 | * {@link: https://leetcode.com/problems/word-break/ } 14 | */ 15 | package _139_WordBreak; 16 | 17 | import java.util.Set; 18 | 19 | /** see test {@link _139_WordBreak.PracticeTest } */ 20 | public class Practice { 21 | 22 | public boolean wordBreak(String s, Set wordDict) { 23 | // TODO Auto-generated method stub 24 | return false; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_140_WordBreakII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a string s and a dictionary of words dict, add spaces in s to 6 | * construct a sentence where each word is a valid dictionary word. 7 | * Return all such possible sentences. 8 | * 9 | * For example, given s = "catsanddog", 10 | * dict = ["cat", "cats", "and", "sand", "dog"]. 11 | * 12 | * A solution is ["cats and dog", "cat sand dog"]. 13 | * 14 | ************************************************************************* 15 | * @tag : Dynamic Programming; Backtracking 16 | * {@link https://leetcode.com/problems/word-break-ii/ } 17 | */ 18 | package _140_WordBreakII; 19 | 20 | import java.util.List; 21 | import java.util.Set; 22 | 23 | /** see test {@link _140_WordBreakII.PracticeTest } */ 24 | public class Practice { 25 | 26 | public List wordBreak(String s, Set wordDict) { 27 | // TODO Auto-generated method stub 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_141_LinkedListCycle/Practice.java: -------------------------------------------------------------------------------- 1 | package _141_LinkedListCycle; 2 | 3 | import com.leetcode.ListNode; 4 | 5 | /** 6 | * {@link https://leetcode.com/problems/linked-list-cycle/ } 7 | */ 8 | public class Practice { 9 | 10 | public boolean hasCycle(ListNode head) { 11 | // TODO Auto-generated method stub 12 | return false; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/_141_LinkedListCycle/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space: O(1) 3 | * @tag : Linked List; Two Pointers 4 | * @date: Jun 18, 2015 5 | * @by : Steven Cooks 6 | * {@link https://leetcode.com/problems/linked-list-cycle/ } 7 | */ 8 | package _141_LinkedListCycle; 9 | 10 | import com.leetcode.ListNode; 11 | 12 | /** see test {@link _141_LinkedListCycle.SolutionTest } */ 13 | public class Solution { 14 | public boolean hasCycle(ListNode head) { 15 | if (head == null || head.next == null) { 16 | return false; 17 | } 18 | ListNode fast = head.next; 19 | ListNode slow = head; 20 | while (fast != null && fast.next != null) { 21 | if (fast == slow || fast.next == slow) { 22 | return true; 23 | } 24 | fast = fast.next.next; 25 | slow = slow.next; 26 | } 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/_142_LinkedListCycleII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a linked list, return the node where the cycle begins. 6 | * If there is no cycle, return null. 7 | * 8 | * Follow up: 9 | * 10 | * Can you solve it without using extra space? 11 | * 12 | *************************************************************************** 13 | * @tag : Linked List; Two Pointers 14 | * {@link https://leetcode.com/problems/linked-list-cycle-ii/ } 15 | */ 16 | package _142_LinkedListCycleII; 17 | 18 | import com.leetcode.ListNode; 19 | 20 | /** see test {@link _142_LinkedListCycleII.SolutionTest } */ 21 | public class Practice { 22 | 23 | public ListNode detectCycle(ListNode head) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_143_ReorderList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a singly linked list L: L0→L1→…→Ln-1→Ln, 6 | * reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… 7 | * 8 | * You must do this in-place without altering the nodes' values. 9 | * 10 | * For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 11 | * 12 | *************************************************************************** 13 | * @tag : Linked List 14 | * {@link https://leetcode.com/problems/reorder-list/ } 15 | */ 16 | package _143_ReorderList; 17 | 18 | import com.leetcode.ListNode; 19 | 20 | /** see test {@link _143_ReorderList.PracticeTest } */ 21 | public class Practice { 22 | 23 | public void reorderList(ListNode head) { 24 | // TODO Auto-generated method stub 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_144_BinaryTreePreorderTraversal/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, return the preorder traversal of its nodes' values. 6 | * 7 | ************************************************************************* 8 | * @tag : Tree; Stack 9 | * {@link https://leetcode.com/problems/binary-tree-preorder-traversal/ } 10 | */ 11 | package _144_BinaryTreePreorderTraversal; 12 | 13 | import java.util.List; 14 | 15 | import com.leetcode.TreeNode; 16 | 17 | /** see test {@link _144_BinaryTreePreorderTraversal.PracticeTest } */ 18 | public class Practice { 19 | 20 | public List preorderTraversal(TreeNode root) { 21 | // TODO Auto-generated method stub 22 | return null; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_145_BinaryTreePostorderTraversal/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary tree, return the postorder traversal of its nodes' values. 6 | * 7 | ************************************************************************* 8 | * @tag : Tree; Stack 9 | * {@link https://leetcode.com/problems/binary-tree-postorder-traversal/ } 10 | */ 11 | package _145_BinaryTreePostorderTraversal; 12 | 13 | import java.util.List; 14 | 15 | import com.leetcode.TreeNode; 16 | 17 | /** see test {@link _145_BinaryTreePostorderTraversal.PracticeTest } */ 18 | public class Practice { 19 | 20 | public List postorderTraversal(TreeNode root) { 21 | // TODO Auto-generated method stub 22 | return null; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_147_InsertionSortList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************** 3 | * Description: 4 | * 5 | * Sort a linked list using insertion sort. 6 | * 7 | **************************************************************** 8 | * @tag : Linked List; Sort 9 | * {@link https://leetcode.com/problems/insertion-sort-list/ } 10 | */ 11 | package _147_InsertionSortList; 12 | 13 | import com.leetcode.ListNode; 14 | 15 | /** see test {@link _147_InsertionSortList.PracticeTest } */ 16 | public class Practice { 17 | 18 | public ListNode insertionSortList(ListNode head) { 19 | // TODO Auto-generated method stub 20 | return null; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_148_SortList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Sort a linked list in O(n log n) time using constant space complexity. 6 | * 7 | *************************************************************************** 8 | * @tag : Linked List; Sort 9 | * {@link https://leetcode.com/problems/sort-list/ } 10 | */ 11 | package _148_SortList; 12 | 13 | import com.leetcode.ListNode; 14 | 15 | /** see test {@link _148_SortList.PracticeTest } */ 16 | public class Practice { 17 | 18 | public ListNode sortList(ListNode head) { 19 | // TODO Auto-generated method stub 20 | return null; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_149_MaxPointsOnALine/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given n points on a 2D plane, find the maximum number of points that 6 | * lie on the same straight line. 7 | * 8 | ************************************************************************* 9 | * @tag : Hash Table; Math 10 | * {@link https://leetcode.com/problems/max-points-on-a-line/ } 11 | */ 12 | package _149_MaxPointsOnALine; 13 | 14 | import com.leetcode.Point; 15 | 16 | /** see test {@link _149_MaxPointsOnALine.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int maxPoints(Point[] points) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_150_EvaluateReversePolishNotation/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an input string, reverse the string word by word. 6 | * For example, 7 | * Given s = "the sky is blue", 8 | * return "blue is sky the". 9 | * 10 | ************************************************************************* 11 | * @tag : String 12 | * {@link https://leetcode.com/problems/reverse-words-in-a-string/ } 13 | */ 14 | package _150_EvaluateReversePolishNotation; 15 | 16 | /** see test {@link _150_EvaluateReversePolishNotation.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int evalRPN(String[] tokens) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_151_ReverseWordsInAString/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an input string, reverse the string word by word. 6 | * For example, 7 | * Given s = "the sky is blue", 8 | * return "blue is sky the". 9 | * 10 | ************************************************************************* 11 | * @tag : String 12 | * {@link https://leetcode.com/problems/reverse-words-in-a-string/ } 13 | */ 14 | package _151_ReverseWordsInAString; 15 | 16 | /** see test {@link _151_ReverseWordsInAString.PracticeTest } */ 17 | public class Practice { 18 | 19 | public String reverseWords(String a) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_152_MaximumProductSubarray/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Find the contiguous subarray within an array (containing at least one 6 | * number) which has the largest product. 7 | * 8 | * For example, given the array [2,3,-2,4], 9 | * the contiguous subarray [2,3] has the largest product = 6. 10 | * 11 | ************************************************************************* 12 | * @tag : Array; Dynamic Programming 13 | * {@link https://leetcode.com/problems/maximum-product-subarray/ } 14 | */ 15 | package _152_MaximumProductSubarray; 16 | 17 | /** see test {@link _152_MaximumProductSubarray.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int maxProduct(int[] nums) { 21 | // TODO Auto-generated method stub 22 | return 0; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/_153_FindMinimumInRotatedSortedArray/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Suppose a sorted array is rotated at some pivot unknown to you beforehand. 6 | * (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 7 | * 8 | * Find the minimum element. You may assume no duplicate exists in the array. 9 | * 10 | *************************************************************************** 11 | * @tag : Array; Binary Search 12 | * {@link https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ } 13 | */ 14 | package _153_FindMinimumInRotatedSortedArray; 15 | 16 | /** see test {@link _153_FindMinimumInRotatedSortedArray.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int findMin(int[] nums) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_154_FindMinimumInRotatedSortedArrayII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Follow up for "Find Minimum in Rotated Sorted Array": 6 | * What if duplicates are allowed? 7 | * 8 | * Would this affect the run-time complexity? How and why? 9 | * Suppose a sorted array is rotated at some pivot unknown to you beforehand. 10 | * (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 11 | * 12 | * Find the minimum element. The array may contain duplicates. 13 | * 14 | *************************************************************************** 15 | * @tag : Array; Binary Search 16 | * {@link https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ } 17 | */ 18 | package _154_FindMinimumInRotatedSortedArrayII; 19 | 20 | /** see test {@link _154_FindMinimumInRotatedSortedArrayII.PracticeTest } */ 21 | public class Practice { 22 | 23 | public int findMin(int[] nums) { 24 | // TODO Auto-generated method stub 25 | return 0; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_157_ReadNCharactersGivenRead4/Reader4.java: -------------------------------------------------------------------------------- 1 | package _157_ReadNCharactersGivenRead4; 2 | 3 | public class Reader4 { 4 | 5 | private char[] _contents; 6 | 7 | private int _offset; 8 | 9 | private static final int READ_SIZE = 4; 10 | 11 | public Reader4() { 12 | _contents = null; 13 | _offset = 0; 14 | } 15 | 16 | public void setContents(char[] contents) { 17 | this._contents = contents; 18 | } 19 | 20 | public int read4(char[] buf) { 21 | int sz = Math.min(_contents.length - _offset, READ_SIZE); 22 | for (int i = 0; i < sz; i++) { 23 | buf[i] = _contents[_offset + i]; 24 | } 25 | _offset += sz; 26 | return sz; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_159_LongestSubstringWithAtMostTwoDistinctCharacters/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a string, find the length of the longest substring T that contains 6 | * at most 2 distinct characters. 7 | * 8 | * For example, Given s = “eceba”, T is "ece" which its length is 3. 9 | * 10 | *************************************************************************** 11 | * @tag : Hash Table; Two Pointers; String 12 | * {@link https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ } 13 | */ 14 | package _159_LongestSubstringWithAtMostTwoDistinctCharacters; 15 | 16 | /** see test {@link _159_LongestSubstringWithAtMostTwoDistinctCharacters.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int lengthOfLongestSubstringTwoDistinct(String s) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_161_OneEditDistance/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given two strings S and T, determine if they are both one edit distance apart. 6 | * 7 | *************************************************************************** 8 | * @tag : String 9 | * {@link https://leetcode.com/problems/one-edit-distance/ } 10 | */ 11 | package _161_OneEditDistance; 12 | 13 | /** see test {@link _161_OneEditDistance.PracticeTest } */ 14 | public class Practice { 15 | 16 | public boolean isOneEditDistance(String s, String t) { 17 | // TODO Auto-generated method stub 18 | return false; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/_162_FindPeakElement/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * A peak element is an element that is greater than its neighbors. 6 | * Given an input array where num[i] ≠ num[i+1], find a peak element and 7 | * return its index. 8 | * The array may contain multiple peaks, in that case return the index to 9 | * any one of the peaks is fine. 10 | * 11 | * You may imagine that num[-1] = num[n] = -∞. 12 | * 13 | * For example, in array [1, 2, 3, 1], 3 is a peak element and your 14 | * function should return the index number 2. 15 | * 16 | *************************************************************************** 17 | * @tag : Array; Binary Search 18 | * {@link https://leetcode.com/problems/find-peak-element/ } 19 | */ 20 | package _162_FindPeakElement; 21 | 22 | /** see test {@link _162_FindPeakElement.PracticeTest } */ 23 | public class Practice { 24 | 25 | public int findPeakElement(int[] nums) { 26 | // TODO Auto-generated method stub 27 | return 0; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_163_MissingRanges/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a sorted integer array where the range of elements are [lower, upper] 6 | * inclusive, return its missing ranges. 7 | * 8 | * For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, 9 | * return ["2", "4->49", "51->74", "76->99"]. 10 | * 11 | *************************************************************************** 12 | * @tag : Array 13 | * {@link https://leetcode.com/problems/missing-ranges/ } 14 | */ 15 | package _163_MissingRanges; 16 | 17 | import java.util.List; 18 | 19 | /** see test {@link _163_MissingRanges.PracticeTest } */ 20 | public class Practice { 21 | 22 | public List findMissingRanges(int[] nums, int lower, int upper) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_164_MaximumGap/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given an unsorted array, find the maximum difference between the 6 | * successive elements in its sorted form. 7 | * 8 | * Try to solve it in linear time/space. 9 | * Return 0 if the array contains less than 2 elements. 10 | * 11 | * You may assume all elements in the array are non-negative integers and 12 | * fit in the 32-bit signed integer range. 13 | * 14 | ******************************************************************************* 15 | * @tag : Sort 16 | * {@link https://leetcode.com/problems/maximum-gap/ } 17 | */ 18 | package _164_MaximumGap; 19 | 20 | public class Practice { 21 | 22 | public int maximumGap(int[] nums) { 23 | // TODO Auto-generated method stub 24 | return 0; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_164_MaximumGap/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(); Space : O() 3 | * @tag : Sort 4 | * @by : Steven Cooks 5 | * @date: Jul 6, 2015 6 | ******************************************************************************* 7 | * Description: 8 | * 9 | * Given an unsorted array, find the maximum difference between the 10 | * successive elements in its sorted form. 11 | * 12 | * Try to solve it in linear time/space. 13 | * Return 0 if the array contains less than 2 elements. 14 | * 15 | * You may assume all elements in the array are non-negative integers and 16 | * fit in the 32-bit signed integer range. 17 | * 18 | ******************************************************************************* 19 | * {@link https://leetcode.com/problems/maximum-gap/ } 20 | */ 21 | package _164_MaximumGap; 22 | 23 | /** see test {@link _164_MaximumGap.SolutionTest } */ 24 | public class Solution { 25 | 26 | public int maximumGap(int[] nums) { 27 | return 0; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_166_FractionToRecurringDecimal/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given two integers representing the numerator and denominator of a fraction, 6 | * return the fraction in string format. 7 | * 8 | * If the fractional part is repeating, enclose the repeating part in parentheses. 9 | * 10 | * For example, 11 | * 12 | * Given numerator = 1, denominator = 2, return "0.5". 13 | * Given numerator = 2, denominator = 1, return "2". 14 | * Given numerator = 2, denominator = 3, return "0.(6)". 15 | * 16 | *************************************************************************** 17 | * @tag : Hash Table; Math 18 | * {@link https://leetcode.com/problems/fraction-to-recurring-decimal/ } 19 | */ 20 | package _166_FractionToRecurringDecimal; 21 | 22 | /** see test {@link _166_FractionToRecurringDecimal.PracticeTest } */ 23 | public class Practice { 24 | 25 | public String fractionToDecimal(int numerator, int denominator) { 26 | // TODO Auto-generated method stub 27 | return null; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_168_ExcelSheetColumnTitle/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given a positive integer, return its corresponding column title as 6 | * appear in an Excel sheet. 7 | * 8 | * For example: 9 | * 1 -> A 10 | * 2 -> B 11 | * 3 -> C 12 | * ... 13 | * 26 -> Z 14 | * 27 -> AA 15 | * 28 -> AB 16 | * 17 | ************************************************************************* 18 | * @tag : Math 19 | * {@link https://leetcode.com/problems/excel-sheet-column-title/ } 20 | */ 21 | package _168_ExcelSheetColumnTitle; 22 | 23 | /** see test {@link _168_ExcelSheetColumnTitle.PracticeTest } */ 24 | public class Practice { 25 | 26 | public String convertToTitle(int a) { 27 | // TODO Auto-generated method stub 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_171_ExcelSheetColumnNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a column title as appear in an Excel sheet, return its 6 | * corresponding column number. 7 | * 8 | * For example: 9 | * A -> 1 10 | * B -> 2 11 | * C -> 3 12 | * ... 13 | * Z -> 26 14 | * AA -> 27 15 | * AB -> 28 16 | * 17 | ******************************************************************************* 18 | * @tag : Math 19 | * {@link https://leetcode.com/problems/excel-sheet-column-number/ } 20 | */ 21 | package _171_ExcelSheetColumnNumber; 22 | 23 | /** see test {@link _171_ExcelSheetColumnNumber.PracticeTest } */ 24 | public class Practice { 25 | 26 | public int titleToNumber(String a) { 27 | // TODO Auto-generated method stub 28 | return 0; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_171_ExcelSheetColumnNumber/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space : O(1) 3 | * @tag : Math 4 | * @by : Steven Cooks 5 | * @date: Jul 9, 2015 6 | ******************************************************************************* 7 | * Description: 8 | * 9 | * Given a column title as appear in an Excel sheet, return its 10 | * corresponding column number. 11 | * 12 | * For example: 13 | * A -> 1 14 | * B -> 2 15 | * C -> 3 16 | * ... 17 | * Z -> 26 18 | * AA -> 27 19 | * AB -> 28 20 | * 21 | ******************************************************************************* 22 | * {@link https://leetcode.com/problems/excel-sheet-column-number/ } 23 | */ 24 | package _171_ExcelSheetColumnNumber; 25 | 26 | /** see test {@link _171_ExcelSheetColumnNumber.SolutionTest } */ 27 | public class Solution { 28 | 29 | private int BASE = 26; 30 | 31 | public int titleToNumber(String s) { 32 | int result = 0; 33 | for (char ch : s.toCharArray()) { 34 | result = result * BASE + (ch - 'A' + 1); 35 | } 36 | return result; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/_172_FactorialTrailingZeroes/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given an integer n, return the number of trailing zeroes in n!. 6 | * Note: Your solution should be in logarithmic time complexity. 7 | * 8 | ******************************************************************************* 9 | * @tag : Math 10 | * {@link https://leetcode.com/problems/factorial-trailing-zeroes/ } 11 | */ 12 | package _172_FactorialTrailingZeroes; 13 | 14 | /** see test {@link _172_FactorialTrailingZeroes.PracticeTest } */ 15 | public class Practice { 16 | 17 | public int trailingZeroes(int n) { 18 | // TODO Auto-generated method stub 19 | return 0; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_179_LargestNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a list of non negative integers, arrange them such that they form 6 | * the largest number. 7 | * 8 | * For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. 9 | * 10 | * Note: The result may be very large, so you need to return a string instead of an integer. 11 | * 12 | ******************************************************************************* 13 | * @tag : Sort 14 | * {@link https://leetcode.com/problems/largest-number/ } 15 | */ 16 | package _179_LargestNumber; 17 | 18 | /** see test {@link _179_LargestNumber.PracticeTest } */ 19 | public class Practice { 20 | 21 | public String largestNumber(int[] a) { 22 | // TODO Auto-generated method stub 23 | return null; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_186_ReverseWordsInAStringII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an input string, reverse the string word by word. 6 | * For example, 7 | * Given s = "the sky is blue", 8 | * return "blue is sky the". 9 | * 10 | * Note: 1) no leading and trailing spaces 11 | * 2) only one space between words 12 | * 3) do it in-place 13 | * 14 | *************************************************************************** 15 | * @tag : Array 16 | */ 17 | package _186_ReverseWordsInAStringII; 18 | 19 | /** see test {@link _186_ReverseWordsInAStringII.PracticeTest } */ 20 | public class Practice { 21 | 22 | public void reverseWords(char[] s) { 23 | // TODO Auto-generated method stub 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/_189_RotateArray/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(); Space : O() 3 | * @tag : 4 | * @by : Steven Cooks 5 | * @date: Jul 4, 2015 6 | ******************************************************************************* 7 | * Description: 8 | * 9 | * Rotate an array of n elements to the right by k steps. 10 | * 11 | * For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 12 | * 13 | * Note: 14 | * Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. 15 | * 16 | ******************************************************************************* 17 | * {@link https://leetcode.com/problems/rotate-array/ } 18 | */ 19 | package _189_RotateArray; 20 | 21 | /** see test {@link _189_RotateArray.PracticeTest } */ 22 | public class Practice { 23 | 24 | public void rotate(int[] nums, int k) { 25 | // TODO Auto-generated method stub 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_199_BinaryTreeRightSideView/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a binary tree, imagine yourself standing on the right side of it, 6 | * return the values of the nodes you can see ordered from top to bottom. 7 | * 8 | * For example: 9 | * 10 | * Given the following binary tree, 11 | * 1 <--- 12 | * / \ 13 | * 2 3 <--- 14 | * \ \ 15 | * 5 4 <--- 16 | * You should return [1, 3, 4]. 17 | * 18 | *************************************************************************** 19 | * @tag : Tree; Depth-first Search; Breadth-first Search 20 | * {@link https://leetcode.com/problems/binary-tree-right-side-view/ } 21 | */ 22 | package _199_BinaryTreeRightSideView; 23 | 24 | import java.util.List; 25 | 26 | import com.leetcode.TreeNode; 27 | 28 | /** see test {@link _199_BinaryTreeRightSideView.PracticeTest } */ 29 | public class Practice { 30 | 31 | public List rightSideView(TreeNode root) { 32 | // TODO Auto-generated method stub 33 | return null; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/_200_NumberOfIslands/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a 2d grid map of '1's (land) and '0's (water), count the number of 6 | * islands. An island is surrounded by water and is formed by connecting 7 | * adjacent lands horizontally or vertically. You may assume all four edges 8 | * of the grid are all surrounded by water. 9 | * 10 | * Example 1: 11 | * 11110 12 | * 11010 13 | * 11000 14 | * 00000 15 | * Answer: 1 16 | * 17 | * Example 2: 18 | * 11000 19 | * 11000 20 | * 00100 21 | * 00011 22 | * Answer: 3 23 | * 24 | *************************************************************************** 25 | * @tag : Depth-first Search; Breadth-first Search 26 | * {@link https://leetcode.com/problems/number-of-islands/ } 27 | */ 28 | package _200_NumberOfIslands; 29 | 30 | /** see test {@link _200_NumberOfIslands.PracticeTest } */ 31 | public class Practice { 32 | 33 | public int numIslands(char[][] grid) { 34 | // TODO Auto-generated method stub 35 | return 0; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/_203_RemoveLinkedListElements/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Remove all elements from a linked list of integers that have value val. 6 | * 7 | * Example 8 | * Given : 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 9 | * Return: 1 --> 2 --> 3 --> 4 --> 5 10 | * 11 | ******************************************************************************* 12 | * @tag : Linked List 13 | * {@link https://leetcode.com/problems/remove-linked-list-elements/ } 14 | */ 15 | package _203_RemoveLinkedListElements; 16 | 17 | import com.leetcode.ListNode; 18 | 19 | public class Practice { 20 | public ListNode removeElements(ListNode head, int val) { 21 | return head; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/_203_RemoveLinkedListElements/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N); Space : O(1) 3 | * @tag : Linked List 4 | * @by : Steven Cooks 5 | * @date: Jun 25, 2015 6 | * {@link https://leetcode.com/problems/remove-linked-list-elements/ } 7 | */ 8 | package _203_RemoveLinkedListElements; 9 | 10 | import com.leetcode.ListNode; 11 | 12 | public class Solution { 13 | public ListNode removeElements(ListNode head, int val) { 14 | ListNode dummy = new ListNode(-1); 15 | dummy.next = head; 16 | ListNode pre = dummy; 17 | ListNode node = dummy.next; 18 | while (node != null) { 19 | if (node.val == val) { 20 | // delete current node 21 | pre.next = node.next; 22 | } else { 23 | pre = pre.next; 24 | } 25 | // go to next node 26 | node = node.next; 27 | } 28 | return dummy.next; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/_204_CountPrimes/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Count the number of prime numbers less than a non-negative number, n. 6 | * 7 | ******************************************************************************* 8 | * @tag : Hash Table; Math 9 | * {@link https://leetcode.com/problems/count-primes/ } 10 | */ 11 | package _204_CountPrimes; 12 | 13 | /** see test {@link _204_CountPrimes.PracticeTest } */ 14 | public class Practice { 15 | 16 | public int countPrimes(int n) { 17 | // TODO Auto-generated method stub 18 | return 0; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/_206_ReverseLinkedList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Reverse a singly linked list. 6 | * 7 | * Hint: 8 | * A linked list can be reversed either iteratively or recursively. 9 | * Could you implement both? 10 | * 11 | ******************************************************************************* 12 | * @tag : Linked List 13 | * {@link https://leetcode.com/problems/reverse-linked-list/ } 14 | */ 15 | package _206_ReverseLinkedList; 16 | 17 | import com.leetcode.ListNode; 18 | 19 | /** see test {@link _206_ReverseLinkedList.PracticeTest } */ 20 | public class Practice { 21 | 22 | public ListNode reverseList(ListNode head) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_208_ImplementTriePrefixTree/README.md: -------------------------------------------------------------------------------- 1 | ### [208] Implement Trie (Prefix Tree) 2 | 3 | ```java 4 | class Node { 5 | // since input is a-z, each slot represents one character 6 | Node[] children = new Node[26]; 7 | boolean end = false; // whether a word ends at this node 8 | } 9 | /** 10 | empty slot means there is no character following that path 11 | e.g. in level 1, it means no words starting with 'b', 'd' exist 12 | Trie: root 13 | / / / | \ \ \ 14 | [a],[],[c],[],... ,[x],[],[z] ---- level 1 15 | | 16 | [a],[ ],[ ],[d],[e] ---- level 2 17 | */ 18 | 19 | ``` 20 | -------------------------------------------------------------------------------- /src/_209_MinimumSizeSubarraySum/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an array of n positive integers and a positive integer s, find 6 | * the minimal length of a subarray of which the sum ≥ s. If there isn't 7 | * one, return 0 instead. 8 | * 9 | * For example, given the array [2,3,1,2,4,3] and s = 7, 10 | * the subarray [4,3] has the minimal length under the problem constraint. 11 | * 12 | * If you have figured out the O(n) solution, try coding another solution 13 | * of which the time complexity is O(n log n). 14 | * 15 | ************************************************************************* 16 | * @tag : Array; Two Pointers; Binary Search 17 | * {@link https://leetcode.com/problems/minimum-size-subarray-sum/ } 18 | */ 19 | package _209_MinimumSizeSubarraySum; 20 | 21 | /** see test {@link _209_MinimumSizeSubarraySum.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int minSubArrayLen(int s, int[] nums) { 25 | // TODO Auto-generated method stub 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_215_KthLargestElementInAnArray/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Find the kth largest element in an unsorted array. Note that it is the 6 | * kth largest element in the sorted order, not the kth distinct element. 7 | * 8 | * For example, Given [3,2,1,5,6,4] and k = 2, return 5. 9 | * Note: You may assume k is always valid, 1 ≤ k ≤ array's length. 10 | * 11 | ******************************************************************************* 12 | * @tag : Divide and Conquer; Heap 13 | * {@link https://leetcode.com/problems/kth-largest-element-in-an-array/ } 14 | */ 15 | package _215_KthLargestElementInAnArray; 16 | 17 | /** see test {@link _215_KthLargestElementInAnArray.PracticeTest } */ 18 | public class Practice { 19 | 20 | public int findKthLargest(int[] nums, int k) { 21 | return k; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_215_KthLargestElementInAnArray/SolutionNlgN.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(NlgN); Space : O(1) 3 | * @tag : Divide and Conquer; Heap 4 | * @by : Steven Cooks 5 | * @date: Jul 11, 2015 6 | ******************************************************************************* 7 | * Description: 8 | * 9 | * Find the kth largest element in an unsorted array. Note that it is the 10 | * kth largest element in the sorted order, not the kth distinct element. 11 | * 12 | * For example, Given [3,2,1,5,6,4] and k = 2, return 5. 13 | * Note: You may assume k is always valid, 1 ≤ k ≤ array's length. 14 | * 15 | ******************************************************************************* 16 | * {@link https://leetcode.com/problems/kth-largest-element-in-an-array/ } 17 | */ 18 | package _215_KthLargestElementInAnArray; 19 | 20 | import java.util.Arrays; 21 | 22 | /** see test {@link _215_KthLargestElementInAnArray.SolutionNlgNTest } */ 23 | public class SolutionNlgN { 24 | 25 | // sort array and then find the kth largest number 26 | public int findKthLargest(int[] nums, int k) { 27 | Arrays.sort(nums); 28 | return nums[nums.length - k]; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_216_CombinationSumIII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Find all possible combinations of k numbers that add up to a number n, given 6 | * that only numbers from 1 to 9 can be used and each combination should be a 7 | * unique set of numbers. 8 | * 9 | * Ensure that numbers within the set are sorted in ascending order. 10 | * Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] 11 | * Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]] 12 | * 13 | *************************************************************************** 14 | * @tag : Array; Backtracking 15 | * {@link https://leetcode.com/problems/combination-sum-iii/ } 16 | */ 17 | package _216_CombinationSumIII; 18 | 19 | import java.util.List; 20 | 21 | /** see test {@link _216_CombinationSumIII.PracticeTest } */ 22 | public class Practice { 23 | 24 | public List> combinationSum3(int k, int n) { 25 | // TODO Auto-generated method stub 26 | return null; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_217_ContainsDuplicate/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ************************************************************************* 3 | * Description: 4 | * 5 | * Given an array of integers, find if the array contains any duplicates. 6 | * Your function should return true if any value appears at least twice 7 | * in the array, and it should return false if every element is distinct. 8 | * 9 | ************************************************************************* 10 | * @tag : Array; Hash Table 11 | * {@link https://leetcode.com/problems/contains-duplicate/ } 12 | */ 13 | package _217_ContainsDuplicate; 14 | 15 | /** see test {@link _217_ContainsDuplicate.PracticeTest } */ 16 | public class Practice { 17 | 18 | public boolean containsDuplicate(int[] nums) { 19 | // TODO Auto-generated method stub 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_218_TheSkylineProblem/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * 6 | *************************************************************************** 7 | * @tag : Divide and Conquer; Heap 8 | * {@link https://leetcode.com/problems/the-skyline-problem/ } 9 | */ 10 | package _218_TheSkylineProblem; 11 | 12 | import java.util.List; 13 | 14 | /** see test {@link _218_TheSkylineProblem.PraticeTest } */ 15 | public class Practice { 16 | 17 | public List getSkyline(int[][] buildings) { 18 | // TODO Auto-generated method stub 19 | return null; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_219_ContainsDuplicateII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | **************************************************************************** 3 | * Given an array of integers and an integer k, find out whether there there 4 | * are two distinct indices i and j in the array such that nums[i] = nums[j] 5 | * and the difference between i and j is at most k. 6 | * 7 | **************************************************************************** 8 | * @tag : Array; Hash Table 9 | * {@link https://leetcode.com/problems/contains-duplicate-ii/ } 10 | */ 11 | package _219_ContainsDuplicateII; 12 | 13 | /** see test {@link _219_ContainsDuplicateII.PracticeTest } */ 14 | public class Practice { 15 | 16 | public boolean containsNearbyDuplicate(int[] nums, int k) { 17 | // TODO Auto-generated method stub 18 | return false; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/_220_ContainsDuplicateIII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given an array of integers, find out whether there are two distinct 6 | * indices i and j in the array such that the difference between nums[i] 7 | * and nums[j] is at most t and the difference between i and j is at most k. 8 | * 9 | ******************************************************************************* 10 | * @tag : Binary Search Tree 11 | * {@link https://leetcode.com/problems/contains-duplicate-iii/ } 12 | */ 13 | package _220_ContainsDuplicateIII; 14 | 15 | /** see test {@link _220_ContainsDuplicateIII.PracticeTest } */ 16 | public class Practice { 17 | 18 | public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { 19 | // TODO Auto-generated method stub 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_221_MaximalSquare/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a 2D binary matrix filled with 0's and 1's, find the largest square 6 | * containing all 1's and return its area. 7 | * 8 | * For example, given the following matrix: 9 | * 1 0 1 0 0 10 | * 1 0 1 1 1 11 | * 1 1 1 1 1 12 | * 1 0 0 1 0 13 | * 14 | * Return 4. 15 | * 16 | *************************************************************************** 17 | * @tag : Dynamic Programming 18 | * {@link https://leetcode.com/problems/maximal-square/ } 19 | */ 20 | package _221_MaximalSquare; 21 | 22 | /** see test {@link _221_MaximalSquare.SolutionTest } */ 23 | public class Practice { 24 | 25 | public int maximalSquare(char[][] matrix) { 26 | // TODO Auto-generated method stub 27 | return 0; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_223_RectangleArea/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Find the total area covered by two rectilinear rectangles in a 2D plane. 6 | * Each rectangle is defined by its bottom left corner and top right corner as 7 | * shown in the figure. 8 | * 9 | *************************************************************************** 10 | * @tag : Math 11 | * {@link https://leetcode.com/problems/rectangle-area/ } 12 | */ 13 | package _223_RectangleArea; 14 | 15 | /** see test {@link _223_RectangleArea.PracticeTest } */ 16 | public class Practice { 17 | 18 | public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 19 | return 0; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/_223_RectangleArea/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(1) ; Space: O(1) 3 | * @tag : Math 4 | * @by : Steven Cooks 5 | * @date: Aug 21, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * Find the total area covered by two rectilinear rectangles in a 2D plane. 10 | * Each rectangle is defined by its bottom left corner and top right corner as 11 | * shown in the figure. 12 | * 13 | *************************************************************************** 14 | * {@link https://leetcode.com/problems/rectangle-area/ } 15 | */ 16 | package _223_RectangleArea; 17 | 18 | /** see test {@link _223_RectangleArea.SolutionTest } */ 19 | public class Solution { 20 | 21 | // two areas - overlapping area 22 | public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 23 | int x1 = Math.max(A, E); 24 | int x2 = Math.min(C, G); 25 | int y1 = Math.max(B, F); 26 | int y2 = Math.min(D, H); 27 | int overlap = (x2 > x1 && y2 > y1) ? (x2 - x1) * (y2 - y1) : 0; 28 | return (C - A) * (D - B) + (G - E) * (H - F) - overlap; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_228_SummaryRanges/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a sorted integer array without duplicates, return the summary of its ranges. 6 | * 7 | * For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 8 | * 9 | ******************************************************************************* 10 | * @tag : Array 11 | * {@link https://leetcode.com/problems/summary-ranges/ } 12 | */ 13 | package _228_SummaryRanges; 14 | 15 | import java.util.List; 16 | 17 | public class Practice { 18 | 19 | public List summaryRanges(int[] nums) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_229_MajorityElementII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given an integer array of size n, find all elements that appear more 6 | * than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 7 | * 8 | ******************************************************************************* 9 | * @tag : Array 10 | * {@link https://leetcode.com/problems/majority-element-ii/ } 11 | */ 12 | package _229_MajorityElementII; 13 | 14 | import java.util.List; 15 | 16 | /** see test {@link _229_MajorityElementII.PracticeTest} */ 17 | public class Practice { 18 | 19 | public List majorityElement(int[] nums) { 20 | // TODO Auto-generated method stub 21 | return null; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_230_KthSmallestElementInABST/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | ******************************************************************************* 3 | * Description: 4 | * 5 | * Given a binary search tree, write a function kthSmallest to find the 6 | * kth smallest element in it. 7 | * 8 | * Note: 9 | * You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 10 | * 11 | * Follow up: 12 | * What if the BST is modified (insert/delete operations) often and you 13 | * need to find the kth smallest frequently? How would you optimize the 14 | * kthSmallest routine? 15 | * 16 | ******************************************************************************* 17 | * @tag : Tree 18 | * {@link https://leetcode.com/problems/kth-smallest-element-in-a-bst/ } 19 | */ 20 | package _230_KthSmallestElementInABST; 21 | 22 | import com.leetcode.TreeNode; 23 | 24 | /** see test {@link _230_KthSmallestElementInABST.PracticeTest } */ 25 | public class Practice { 26 | 27 | public int kthSmallest(TreeNode root, int k) { 28 | // TODO Auto-generated method stub 29 | return 0; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/_231_PowerOfTwo/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an integer, write a function to determine if it is a power of two. 6 | * 7 | *************************************************************************** 8 | * @tag : Math; Bit Manipulation 9 | * {@link https://leetcode.com/problems/power-of-two/ } 10 | */ 11 | package _231_PowerOfTwo; 12 | 13 | /** see test {@link _231_PowerOfTwo.PracticeTest } */ 14 | public class Practice { 15 | 16 | public boolean isPowerOfTwo(int i) { 17 | // TODO Auto-generated method stub 18 | return false; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/_231_PowerOfTwo/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O() ; Space: O() 3 | * @tag : Math; Bit Manipulation 4 | * @by : Steven Cooks 5 | * @date: Sep 24, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * Given an integer, write a function to determine if it is a power of two. 10 | * 11 | *************************************************************************** 12 | * {@link https://leetcode.com/problems/power-of-two/ } 13 | */ 14 | package _231_PowerOfTwo; 15 | 16 | /** see test {@link _231_PowerOfTwo.SolutionTest } */ 17 | public class Solution { 18 | 19 | public boolean isPowerOfTwo(int n) { 20 | return n > 0 && (n & (n - 1)) == 0; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/_236_LowestCommonAncestorOfABinaryTree/README.md: -------------------------------------------------------------------------------- 1 | ### [236] lowest common ancestor in binary tree 2 | 3 | Divide and Conquer + post-order traversal 4 | 5 | 0. foo(root, left, right) => return whatever target node we can find in this subtree 6 | 7 | 1. if (root == left || root == right) => return root; 8 | 9 | 2. foo(root.left, left, right) & foo(root.right, left, right) 10 | ```java 11 | TreeNode left = foo(root.left, left, right); 12 | TreeNode right = foo(root.right, left, right); 13 | if (left != null && right != null) => return root; 14 | if (left != null) => return left; 15 | else => return right; 16 | ``` -------------------------------------------------------------------------------- /src/_237_DeleteNodeInALinkedList/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Write a function to delete a node (except the tail) in a singly linked list, 6 | * given only access to that node. 7 | * 8 | * Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node 9 | * with value 3, the linked list should become 1 -> 2 -> 4 after calling your function. 10 | * 11 | *************************************************************************** 12 | * @tag : Linked List 13 | * {@link https://leetcode.com/problems/delete-node-in-a-linked-list/ } 14 | */ 15 | package _237_DeleteNodeInALinkedList; 16 | 17 | import com.leetcode.ListNode; 18 | 19 | /** see test {@link _237_DeleteNodeInALinkedList.PracticeTest } */ 20 | public class Practice { 21 | 22 | public void deleteNode(ListNode node) { 23 | // TODO Auto-generated method stub 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_238_ProductOfArrayExceptSelf/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an array of n integers where n > 1, nums, return an array output 6 | * such that output[i] is equal to the product of all the elements of nums 7 | * except nums[i]. 8 | * 9 | * Solve it without division and in O(n). 10 | * 11 | * For example, given [1,2,3,4], return [24,12,8,6]. 12 | * 13 | * Follow up: 14 | * Could you solve it with constant space complexity? (Note: The output array 15 | * does not count as extra space for the purpose of space complexity analysis.) 16 | * 17 | *************************************************************************** 18 | * @tag : Array 19 | * {@link https://leetcode.com/problems/product-of-array-except-self/ } 20 | */ 21 | package _238_ProductOfArrayExceptSelf; 22 | 23 | /** see test {@link _238_ProductOfArrayExceptSelf.PracticeTest } */ 24 | public class Practice { 25 | 26 | public int[] productExceptSelf(int[] nums) { 27 | // TODO Auto-generated method stub 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/_242_ValidAnagram/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given two strings s and t, write a function to determine if t is an anagram of s. 6 | * For example, 7 | * s = "anagram", t = "nagaram", return true. 8 | * s = "rat", t = "car", return false. 9 | * Note: You may assume the string contains only lowercase alphabets. 10 | * 11 | *************************************************************************** 12 | * @tag : Hash Table; Sort 13 | * {@link https://leetcode.com/problems/valid-anagram/ } 14 | */ 15 | package _242_ValidAnagram; 16 | 17 | /** see test {@link _242_ValidAnagram.PracticeTest } */ 18 | public class Practice { 19 | 20 | public boolean isAnagram(String s, String t) { 21 | return false; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_246_StrobogrammaticNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * A strobogrammatic number is a number that looks the same when rotated 6 | * 180 degrees (looked at upside down). 7 | * 8 | * Write a function to determine if a number is strobogrammatic. 9 | * The number is represented as a string. 10 | * 11 | * For example, the numbers "69", "88", and "818" are all strobogrammatic. 12 | * 13 | *************************************************************************** 14 | * @tag : Hash Table; Math 15 | * {@link https://leetcode.com/problems/strobogrammatic-number/ } 16 | */ 17 | package _246_StrobogrammaticNumber; 18 | 19 | /** see test {@link _246_StrobogrammaticNumber.PracticeTest } */ 20 | public class Practice { 21 | 22 | public boolean isStrobogrammatic(String num) { 23 | // TODO Auto-generated method stub 24 | return false; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/_251_Flatten2DVector/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Implement an iterator to flatten a 2d vector. 6 | * For example, 7 | * Given 2d vector = 8 | * [ [1,2], 9 | * [3], 10 | * [4,5,6] 11 | * ] 12 | * 13 | * By calling next repeatedly until hasNext returns false, the order of 14 | * elements returned by next should be: [1,2,3,4,5,6]. 15 | * 16 | *************************************************************************** 17 | * @tag : Design 18 | * {@link https://leetcode.com/problems/flatten-2d-vector/ } 19 | */ 20 | package _251_Flatten2DVector; 21 | 22 | import java.util.List; 23 | 24 | /** see test {@link _251_Flatten2DVector.SolutionTest } */ 25 | public class Practice { 26 | 27 | public Practice(List> vec2d) { 28 | } 29 | 30 | public boolean hasNext() { 31 | return false; 32 | } 33 | 34 | public int next() { 35 | return 0; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/_252_MeetingRooms/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an array of meeting time intervals consisting of start and end times 6 | * [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. 7 | * 8 | * For example, 9 | * Given [[0, 30],[5, 10],[15, 20]], 10 | * return false. 11 | * 12 | *************************************************************************** 13 | * @tag : Sort 14 | * {@link https://leetcode.com/problems/meeting-rooms/ } 15 | */ 16 | package _252_MeetingRooms; 17 | 18 | import com.leetcode.Interval; 19 | 20 | /** see test {@link _252_MeetingRooms.PracticeTest } */ 21 | public class Practice { 22 | 23 | public boolean canAttendMeetings(Interval[] intervals) { 24 | // TODO Auto-generated method stub 25 | return false; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_253_MeetingRoomsII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an array of meeting time intervals consisting of start and end 6 | * times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference 7 | * rooms required. 8 | * 9 | * For example, 10 | * 11 | * Given [[0, 30],[5, 10],[15, 20]], return 2. 12 | * 13 | *************************************************************************** 14 | * @tag : Heap; Greedy; Sort 15 | * {@link https://leetcode.com/problems/meeting-rooms-ii/ } 16 | */ 17 | package _253_MeetingRoomsII; 18 | 19 | import com.leetcode.Interval; 20 | 21 | /** see test {@link _253_MeetingRoomsII.PracticeTest } */ 22 | public class Practice { 23 | 24 | public int minMeetingRooms(Interval[] intervals) { 25 | // TODO Auto-generated method stub 26 | return 0; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/_268_MissingNumber/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, 6 | * find the one that is missing from the array. 7 | * 8 | * For example, Given nums = [0, 1, 3] return 2. 9 | * 10 | *************************************************************************** 11 | * @tag : Array; Math; Bit Manipulation 12 | * {@link https://leetcode.com/problems/missing-number/ } 13 | */ 14 | package _268_MissingNumber; 15 | 16 | /** see test {@link _268_MissingNumber.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int missingNumber(int[] nums) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_268_MissingNumber/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N) ; Space: O(1) 3 | * @tag : Array; Math; Bit Manipulation 4 | * @by : Steven Cooks 5 | * @date: Sep 14, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, 10 | * find the one that is missing from the array. 11 | * 12 | * For example, Given nums = [0, 1, 3] return 2. 13 | * 14 | *************************************************************************** 15 | * {@link https://leetcode.com/problems/missing-number/ } 16 | */ 17 | package _268_MissingNumber; 18 | 19 | /** see test {@link _268_MissingNumber.SolutionTest } */ 20 | public class Solution { 21 | 22 | // non-missing numbers will have two numbers for xor 23 | public int missingNumber(int[] nums) { 24 | int xor = 0; 25 | int n = nums.length; 26 | for (int i = 0; i < nums.length; i++) { 27 | xor = xor ^ nums[i]; 28 | xor = xor ^ n--; 29 | } 30 | return xor; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/_269_AlienDictionary/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * are sorted lexicographically by the rules of this new language. 6 | * Derive the order of letters in this language. 7 | * 8 | * For example, Given the following words in dictionary, 9 | * [ "wrt", 10 | * "wrf", 11 | * "er", 12 | * "ett", 13 | * "rftt" 14 | * ] 15 | * 16 | * The correct order is: "wertf". 17 | * 18 | * Note: 19 | * You may assume all letters are in lowercase. 20 | * If the order is invalid, return an empty string. 21 | * There may be multiple valid order of letters, return any one of them is fine. 22 | * 23 | *************************************************************************** 24 | * @tag : Graph; Togological Sort 25 | * {@link https://leetcode.com/problems/alien-dictionary/ } 26 | */ 27 | package _269_AlienDictionary; 28 | 29 | /** see test {@link _269_AlienDictionary.PracticeTest } */ 30 | public class Practice { 31 | 32 | public String alienOrder(String[] words) { 33 | // TODO Auto-generated method stub 34 | return null; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/_273_IntegerToEnglishWords/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Convert a non-negative integer to its english words representation. 6 | * Given input is guaranteed to be less than 231 - 1. 7 | * 8 | * For example, 9 | * 123 -> "One Hundred Twenty Three" 10 | * 12345 -> "Twelve Thousand Three Hundred Forty Five" 11 | * 12 | *************************************************************************** 13 | * @tag : Math; String 14 | * {@link https://leetcode.com/problems/integer-to-english-words/ } 15 | */ 16 | package _273_IntegerToEnglishWords; 17 | 18 | /** see test {@link _273_IntegerToEnglishWords.PracticeTest } */ 19 | 20 | /** see test {@link _273_IntegerToEnglishWords.PracticeTest } */ 21 | public class Practice { 22 | 23 | public String numberToWords(int num) { 24 | // TODO Auto-generated method stub 25 | return null; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/_275_HIndexII/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Follow up for H-Index: What if the citations array is sorted in ascending 6 | * order? Could you optimize your algorithm? 7 | * 8 | * Hint: Expected runtime complexity is in O(log n) and the input is sorted. 9 | * 10 | *************************************************************************** 11 | * @tag : Binary Search 12 | * {@link https://leetcode.com/problems/h-index-ii/ } 13 | */ 14 | package _275_HIndexII; 15 | 16 | /** see test {@link _275_HIndexII.PracticeTest } */ 17 | public class Practice { 18 | 19 | public int hIndex(int[] citations) { 20 | // TODO Auto-generated method stub 21 | return 0; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/_283_MoveZeroes/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(N) ; Space: O(1) 3 | * @tag : Array; Two Pointers 4 | * @by : Steven Cooks 5 | * @date: Sep 20, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * Given an array nums, write a function to move all 0's to the end of it 10 | * while maintaining the relative order of the non-zero elements. 11 | * For example, given nums = [0, 1, 0, 3, 12], after calling your function, 12 | * nums should be [1, 3, 12, 0, 0]. 13 | * 14 | * Note: 15 | * You must do this in-place without making a copy of the array. 16 | * Minimize the total number of operations. 17 | * 18 | *************************************************************************** 19 | * {@link https://leetcode.com/problems/move-zeroes/ } 20 | */ 21 | package _283_MoveZeroes; 22 | 23 | /** see test {@link _283_MoveZeroes.SolutionTest } */ 24 | public class Practice { 25 | 26 | public void moveZeroes(int[] nums) { 27 | // TODO Auto-generated method stub 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/_285_InorderSuccessorInBST/Practice.java: -------------------------------------------------------------------------------- 1 | /** 2 | *************************************************************************** 3 | * Description: 4 | * 5 | * Given a binary search tree and a node in it, find the in-order successor 6 | * of that node in the BST. 7 | * 8 | * Note: If the given node has no in-order successor in the tree, return null. 9 | * 10 | *************************************************************************** 11 | * @tag : Tree 12 | * {@link https://leetcode.com/problems/inorder-successor-in-bst/ } 13 | */ 14 | package _285_InorderSuccessorInBST; 15 | 16 | import com.leetcode.TreeNode; 17 | 18 | /** see test {@link _285_InorderSuccessorInBST.SolutionTest } */ 19 | public class Practice { 20 | 21 | public TreeNode inorderSuccessor(TreeNode root, TreeNode node) { 22 | // TODO Auto-generated method stub 23 | return null; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/com/leetcode/Point.java: -------------------------------------------------------------------------------- 1 | package com.leetcode; 2 | 3 | public class Point { 4 | public int x; 5 | public int y; 6 | 7 | public Point() { 8 | x = 0; 9 | y = 0; 10 | } 11 | 12 | public Point(int a, int b) { 13 | x = a; 14 | y = b; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "(" + x + ", " + y + ")"; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/s01_SubarraySum/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O(); Space : O() 3 | * @tag : 4 | * @by : Steven Cooks 5 | * @date: Jul 12, 2015 6 | ******************************************************************************* 7 | * Description: 8 | * 9 | * Find all contiguous sub arrays in the given list that sum to given 10 | * target value. 11 | * 12 | ******************************************************************************* 13 | */ 14 | package s01_SubarraySum; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | public class Solution { 20 | 21 | public List> subarraySumToTarget(int[] nums, int target) { 22 | List> result = new ArrayList<>(); 23 | return result; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/s03_LongestCommonSequence/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O() ; Space: O() 3 | * @tag : 4 | * @by : Steven Cooks 5 | * @date: Aug 6, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * 10 | *************************************************************************** 11 | * {@link } 12 | */ 13 | package s03_LongestCommonSequence; 14 | 15 | public class Solution { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/s03_LongestCommonSequence/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package s03_LongestCommonSequence; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | public class SolutionTest { 8 | 9 | @Test 10 | public void test() { 11 | fail("Not yet implemented"); // TODO 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/s06_LongestIncreasingContinuousSubsequenceIII/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O() ; Space: O() 3 | * @tag : 4 | * @by : Steven Cooks 5 | * @date: Aug 9, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * 10 | *************************************************************************** 11 | * {@link } 12 | */ 13 | package s06_LongestIncreasingContinuousSubsequenceIII; 14 | 15 | public class Solution { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/s10_MostFrequentKNumbers/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Time : O() ; Space: O() 3 | * @tag : 4 | * @by : Steven Cooks 5 | * @date: Aug 14, 2015 6 | *************************************************************************** 7 | * Description: 8 | * 9 | * 10 | *************************************************************************** 11 | * {@link } 12 | */ 13 | package s10_MostFrequentKNumbers; 14 | 15 | public class Solution { 16 | 17 | class Item { 18 | 19 | } 20 | 21 | // O(N) 22 | // read into map, 23 | // find kth highest number, then find all numbers that are smaller than this number 24 | // private List mostFrequentKNumbers(int[] nums) { 25 | // 26 | // List res = new ArrayList<>(); 27 | //// PriorityQueue minHeap = new PriorityQueue<>(); 28 | // return res; 29 | // } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /test/_126_WordLadderII/Test8Result: -------------------------------------------------------------------------------- 1 | [sand, sans, sins, sims, aims, arms, arts, ants, ante, anne, acne] 2 | [sand, sans, sins, sirs, airs, aids, ands, ants, ante, anne, acne] 3 | [sand, sans, kans, kins, kids, aids, ands, ants, ante, anne, acne] 4 | [sand, sans, sins, kins, kids, aids, ands, ants, ante, anne, acne] 5 | [sand, band, bind, bins, bids, aids, ands, ants, ante, anne, acne] 6 | [sand, sans, bans, bins, bids, aids, ands, ants, ante, anne, acne] 7 | [sand, band, bans, bins, bids, aids, ands, ants, ante, anne, acne] 8 | [sand, sans, sins, bins, bids, aids, ands, ants, ante, anne, acne] 9 | [sand, sane, sade, side, aide, aids, ands, ants, ante, anne, acne] 10 | [sand, sane, sine, side, aide, aids, ands, ants, ante, anne, acne] 11 | [sand, sans, sins, sims, aims, aids, ands, ants, ante, anne, acne] 12 | -------------------------------------------------------------------------------- /test/_149_MaxPointsOnALine/Test9Input: -------------------------------------------------------------------------------- 1 | [[40,-23],[9,138],[429,115],[50,-17],[-3,80],[-10,33],[5,-21],[-3,80],[-6,-65],[-18,26],[-6,-65],[5,72],[0,77],[-9,86],[10,-2],[-8,85],[21,130],[18,-6],[-18,26],[-1,-15],[10,-2],[8,69],[-4,63],[0,3],[-4,40],[-7,84],[-8,7],[30,154],[16,-5],[6,90],[18,-6],[5,77],[-4,77],[7,-13],[-1,-45],[16,-5],[-9,86],[-16,11],[-7,84],[1,76],[3,77],[10,67],[1,-37],[-10,-81],[4,-11],[-20,13],[-10,77],[6,-17],[-27,2],[-10,-81],[10,-1],[-9,1],[-8,43],[2,2],[2,-21],[3,82],[8,-1],[10,-1],[-9,1],[-12,42],[16,-5],[-5,-61],[20,-7],[9,-35],[10,6],[12,106],[5,-21],[-5,82],[6,71],[-15,34],[-10,87],[-14,-12],[12,106],[-5,82],[-46,-45],[-4,63],[16,-5],[4,1],[-3,-53],[0,-17],[9,98],[-18,26],[-9,86],[2,77],[-2,-49],[1,76],[-3,-38],[-8,7],[-17,-37],[5,72],[10,-37],[-4,-57],[-3,-53],[3,74],[-3,-11],[-8,7],[1,88],[-12,42],[1,-37],[2,77],[-6,77],[5,72],[-4,-57],[-18,-33],[-12,42],[-9,86],[2,77],[-8,77],[-3,77],[9,-42],[16,41],[-29,-37],[0,-41],[-21,18],[-27,-34],[0,77],[3,74],[-7,-69],[-21,18],[27,146],[-20,13],[21,130],[-6,-65],[14,-4],[0,3],[9,-5],[6,-29],[-2,73],[-1,-15],[1,76],[-4,77],[6,-29]] -------------------------------------------------------------------------------- /test/_164_MaximumGap/PracticeTest.java: -------------------------------------------------------------------------------- 1 | package _164_MaximumGap; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.Timeout; 10 | 11 | public class PracticeTest { 12 | 13 | /** Test method for {@link _164_MaximumGap.Practice } */ 14 | Practice solution; 15 | 16 | @Rule 17 | public Timeout globalTimeout = new Timeout(200); 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | solution = new Practice(); 22 | } 23 | 24 | @After 25 | public void tearDown() throws Exception { 26 | solution = null; 27 | } 28 | 29 | @Test 30 | public void Test1() { 31 | int[] nums = {1, 10000000}; 32 | int actual = solution.maximumGap(nums); 33 | int expected = 9999999; 34 | assertEquals(expected, actual); 35 | } 36 | 37 | @Test 38 | public void Test2() { 39 | int[] nums = {3, 6, 9, 1}; 40 | int actual = solution.maximumGap(nums); 41 | int expected = 3; 42 | assertEquals(expected, actual); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /test/_164_MaximumGap/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package _164_MaximumGap; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.Timeout; 10 | 11 | public class SolutionTest { 12 | 13 | /** Test method for {@link _164_MaximumGap.Solution } */ 14 | Solution solution; 15 | 16 | @Rule 17 | public Timeout globalTimeout = new Timeout(200); 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | solution = new Solution(); 22 | } 23 | 24 | @After 25 | public void tearDown() throws Exception { 26 | solution = null; 27 | } 28 | 29 | @Test 30 | public void Test1() { 31 | int[] nums = {1, 10000000}; 32 | int actual = solution.maximumGap(nums); 33 | int expected = 9999999; 34 | assertEquals(expected, actual); 35 | } 36 | 37 | @Test 38 | public void Test2() { 39 | int[] nums = {3, 6, 9, 1}; 40 | int actual = solution.maximumGap(nums); 41 | int expected = 3; 42 | assertEquals(expected, actual); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /test/_274_HIndex/PracticeTest.java: -------------------------------------------------------------------------------- 1 | package _274_HIndex; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.Timeout; 10 | 11 | public class PracticeTest { 12 | 13 | /** Test method for {@link _274_HIndex.Practice } */ 14 | Practice solution; 15 | 16 | @Rule 17 | public Timeout globalTimeout = new Timeout(200); 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | solution = new Practice(); 22 | } 23 | 24 | @After 25 | public void tearDown() throws Exception { 26 | solution = null; 27 | } 28 | 29 | @Test 30 | public void Test1() { 31 | int[] nums = { 0 }; 32 | int actual = solution.hIndex(nums); 33 | int expected = 0; 34 | assertEquals(expected, actual); 35 | } 36 | 37 | @Test 38 | public void Test2() { 39 | int[] nums = { 3, 1, 0, 6, 5 }; 40 | int actual = solution.hIndex(nums); 41 | int expected = 3; 42 | assertEquals(expected, actual); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /test/_274_HIndex/SolutionTest.java: -------------------------------------------------------------------------------- 1 | package _274_HIndex; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.rules.Timeout; 10 | 11 | public class SolutionTest { 12 | 13 | /** Test method for {@link _274_HIndex.Solution } */ 14 | Solution solution; 15 | 16 | @Rule 17 | public Timeout globalTimeout = new Timeout(200); 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | solution = new Solution(); 22 | } 23 | 24 | @After 25 | public void tearDown() throws Exception { 26 | solution = null; 27 | } 28 | 29 | @Test 30 | public void Test1() { 31 | int[] nums = { 0 }; 32 | int actual = solution.hIndex(nums); 33 | int expected = 0; 34 | assertEquals(expected, actual); 35 | } 36 | 37 | @Test 38 | public void Test2() { 39 | int[] nums = { 3, 1, 0, 6, 5 }; 40 | int actual = solution.hIndex(nums); 41 | int expected = 3; 42 | assertEquals(expected, actual); 43 | } 44 | 45 | } 46 | --------------------------------------------------------------------------------