├── .gitattributes ├── .gitignore ├── appTemplates ├── ConsoleApp │ └── Main.java ├── CsvParser │ ├── Main.java │ ├── Solution.java │ └── data.csv ├── FileIO │ ├── Main.java │ └── temp.txt ├── General │ ├── Main.java │ └── Solution.java ├── Html │ ├── index.html │ └── main.js ├── HttpReader │ └── Main.java └── Regexp │ └── Main.java ├── array ├── ArrayPartition1_561_ │ ├── Main.java │ └── Solution.java ├── BattleshipsInABoard_419_ │ ├── Main.java │ └── Solution.java ├── BestTimeToBuyAndSellStock2_122 │ ├── Main.java │ └── Solution.java ├── CombinationSum3_216 │ ├── Main.java │ └── Solution.java ├── ContainerWithMostWater_11 │ ├── Main.java │ └── Solution.java ├── CountPrimes_204 │ ├── Main.java │ └── Solution.java ├── DegreeOfAnArray_697 │ ├── Main.java │ └── Solution.java ├── DiagonalTraverse_498 │ ├── Main.java │ └── Solution.java ├── FibonacciNumber_509 │ ├── Main.java │ └── Solution.java ├── FindAllDuplicatesInAnArray_442 │ ├── Main.java │ └── Solution.java ├── FindAllNumbersDisappearedInAnArray_448 │ ├── Main.java │ └── Solution.java ├── FindMinimumInRotatedSortedArray_153 │ ├── Main.java │ └── Solution.java ├── FindPeakElement_162 │ ├── Main.java │ └── Solution.java ├── FindTheCelebrity_277 │ ├── Main.java │ ├── Relation.java │ └── Solution.java ├── FindTheDuplicateNumber_287_ │ ├── Main.java │ └── Solution.java ├── FourSum_18 │ ├── Main.java │ └── Solution.java ├── GameOfLife_289 │ ├── Main.java │ └── Solution.java ├── HIndex_274 │ ├── Main.java │ └── Solution.java ├── IntersectionOfTwoArrays2_350 │ ├── Main.java │ └── Solution.java ├── IslandPerimeter_463_ │ ├── Main.java │ └── Solution.java ├── JumpGame_55 │ ├── Main.java │ └── Solution.java ├── KthLargestElementInAnArray_215 │ ├── Main.java │ └── Solution.java ├── LargestNumber_179 │ ├── Main.java │ └── Solution.java ├── LongestConsecutiveSequence_128 │ ├── Main.java │ └── Solution.java ├── MaxConsecutiveOnes_485 │ ├── Main.java │ └── Solution.java ├── MaxNonNegativeSubArray │ ├── Main.java │ └── Solution.java ├── MaximumDistanceInArrays_624 │ ├── Main.java │ └── Solution.java ├── MaximumProductOfThreeNumbers_628 │ ├── Main.java │ └── Solution.java ├── MergeIntervals_56 │ ├── Interval.java │ ├── Main.java │ └── Solution.java ├── MergeSortedArray_88 │ ├── Main.java │ └── Solution.java ├── MinimumSizeSubarraySum_209 │ ├── Main.java │ └── Solution.java ├── MissingNumber_268 │ ├── Main.java │ └── Solution.java ├── MissingRanges_163 │ ├── Main.java │ └── Solution.java ├── NextPermutation_31 │ ├── Main.java │ └── Solution.java ├── PartitionToKEqualSumSubsets_698 │ ├── Main.java │ └── Solution.java ├── PascalTriangle_118 │ ├── Main.java │ └── Solution.java ├── PlusOne_66 │ ├── Main.java │ └── Solution.java ├── ProductOfArrayExceptSelf_238 │ ├── Main.java │ └── Solution.java ├── QueueReconstructionByHeight_406 │ ├── Main.java │ └── Solution.java ├── RangeModule_715 │ ├── Main.java │ └── RangeModule.java ├── RemoveDuplicatesFromSortedArray_26 │ ├── Main.java │ └── Solution.java ├── RemoveElement_27 │ ├── Main.java │ └── Solution.java ├── ReshapeTheMatrix_566_ │ ├── Main.java │ └── Solution.java ├── RotateArray_189 │ ├── Main.java │ └── Solution.java ├── RotateImage_48 │ ├── Main.java │ └── Solution.java ├── SearchForARange_34 │ ├── Main.java │ └── Solution.java ├── SearchInRotatedSortedArray2_81 │ ├── Main.java │ └── Solution.java ├── SearchInRotatedSortedArray_33 │ ├── Main.java │ └── Solution.java ├── SearchInsertPosition_35 │ ├── Main.java │ └── Solution.java ├── SetMatrixZeroes_73 │ ├── Main.java │ └── Solution.java ├── ShortestDistanceFromAllBuildings_317 │ ├── Main.java │ └── Solution.java ├── ShortestUnsortedContinuousSubarray_581 │ ├── Main.java │ └── Solution.java ├── ShortestWordDistance_243 │ ├── Main.java │ └── Solution.java ├── ShuffleAnArray_384_ │ ├── Main.java │ └── Solution.java ├── SortColors_75 │ ├── Main.java │ └── Solution.java ├── SparseMatrixMultiplication_311 │ ├── Main.java │ └── Solution.java ├── SpiralMatrix2_59 │ ├── Main.java │ └── Solution.java ├── SpiralMatrix_54 │ ├── Main.java │ └── Solution.java ├── Subsets_78 │ ├── Main.java │ └── Solution.java ├── SummaryRanges_228 │ ├── Main.java │ └── Solution.java ├── ThirdMaximumNumber_414 │ ├── Main.java │ └── Solution.java ├── ThreeSumClosest_16 │ ├── Main.java │ └── Solution.java ├── ThreeSumSmaller_259 │ ├── Main.java │ └── Solution.java ├── ThreeSum_15 │ ├── Main.java │ ├── Solution.java │ └── Solution1.java ├── Triangle_120 │ ├── Main.java │ └── Solution.java ├── TwoSum2_InputArrayIsSorted_167 │ ├── Main.java │ └── Solution.java ├── WiggleSort2_324 │ ├── Main.java │ └── Solution.java └── WiggleSort_280 │ ├── Main.java │ └── Solution.java ├── backTracking ├── CombinationSum2_40 │ ├── Main.java │ └── Solution.java ├── CombinationSum_39 │ ├── Main.java │ └── Solution.java ├── Combinations_77 │ ├── Main.java │ └── Solution.java ├── LetterCombinationsOfAPhoneNumber_17 │ ├── Main.java │ └── Solution.java ├── Permutations2_47 │ ├── Main.java │ └── Solution.java ├── Permutations_46 │ ├── Main.java │ └── Solution.java ├── RestoreIPAddresses_93_ │ ├── Main.java │ └── Solution.java ├── Subsets2_90 │ ├── Main.java │ └── Solution.java ├── SudokuSolver_37 │ ├── Main.java │ ├── Solution.java │ └── Solution1.java ├── WordBreak2_140 │ ├── Main.java │ └── Solution.java ├── WordSearch_79 │ ├── Main.java │ └── Solution.java └── WordSquares_425 │ ├── Main.java │ ├── Solution.java │ └── Trie.java ├── binarySearch ├── FirstBadVersion_278 │ ├── Main.java │ └── Solution.java ├── GuessNumberHigherOrLower_374 │ ├── Main.java │ └── Solution.java ├── MedianOfTwoSortedArrays_4 │ ├── Main.java │ └── Solution.java ├── SearchA2DMatrix2_240 │ ├── Main.java │ └── Solution.java ├── SearchA2DMatrix_74 │ ├── Main.java │ └── Solution.java └── Sqrt_69 │ ├── Main.java │ └── Solution.java ├── bitManipulation ├── FindTheDifference_389 │ ├── Main.java │ └── Solution.java ├── HammingDistance_461 │ ├── Main.java │ └── Solution.java ├── MajorityElement_169 │ ├── Main.java │ └── Solution.java ├── NumberComplement_476 │ ├── Main.java │ └── Solution.java ├── NumberOfOneBits_191 │ ├── Main.java │ └── Solution.java ├── PowerOfFour_342 │ ├── Main.java │ └── Solution.java ├── PowerOfTwo_231 │ ├── Main.java │ └── Solution.java ├── ReverseBits_190 │ ├── Main.java │ └── Solution.java ├── SingleNumber2_137 │ ├── Main.java │ └── Solution.java ├── SingleNumber3_260 │ ├── Main.java │ └── Solution.java ├── SingleNumber_136 │ ├── Main.java │ └── Solution.java └── SumOfTwoIntegers_371 │ ├── Main.java │ └── Solution.java ├── conclusions ├── design ├── DesignPhoneDirectory_379_ │ ├── Main.java │ └── PhoneDirectory.java ├── DesignTwitter_355 │ ├── Main.java │ └── Twitter.java ├── ImplementConnectedComponents │ ├── ConnectedComponents.java │ ├── Graph.java │ └── Main.java ├── ImplementDigraphShortestPath │ ├── Digraph.java │ ├── Main.java │ └── ShortestPathInDigraph.java ├── ImplementGraphBfs │ ├── BreadthFirstSearch.java │ ├── Graph.java │ └── Main.java ├── ImplementGraphDfs │ ├── DepthFirstSearch.java │ ├── Graph.java │ └── Main.java ├── ImplementHashTable_SeparateChaining │ ├── Main.java │ └── SeparateChainingHashTable.java ├── ImplementPriorityQueue │ ├── Main.java │ └── MyPriorityQueue.java ├── ImplementRedBlackTree │ ├── LeanLeftRedBlackTree.java │ ├── Main.java │ └── Node.java ├── ImplementShortestPathTreeViaDijkstra │ ├── DirectedEdge.java │ ├── EdgeWeightedDigraph.java │ ├── IndexMinPQ.java │ ├── Main.java │ └── ShortestPathTreeViaDijkstra.java ├── ImplementSymbolTableViaBinarySearchTree │ ├── Main.java │ ├── Node.java │ └── SymbolTable.java ├── ImplementTopologicalSortInDigraph │ ├── DepthFirstOrder.java │ ├── Digraph.java │ └── Main.java ├── ImplementTrie_208 │ ├── Main.java │ ├── Trie.java │ └── TrieNode.java ├── LRUCache_146 │ ├── CacheItem.java │ ├── LRUCache.java │ └── Main.java ├── LoggerRateLimiter_359 │ ├── Logger.java │ └── Main.java ├── Mocks │ ├── Main.java │ ├── Slot.java │ ├── SlotState.java │ ├── Solution.java │ ├── Timetable.java │ ├── User.java │ └── UserDaySchedule.java ├── MovingAverageFromDataStream_346 │ ├── Main.java │ └── MovingAverage.java ├── QuickSort │ ├── Main.java │ └── QuickSort.java ├── RemoveItemFromMapWhileIterating │ └── Main.java ├── SnakeGame_353 │ ├── Main.java │ └── SnakeGame.java ├── UniqueWordAbbreviation_288 │ ├── Main.java │ └── ValidWordAbbr.java ├── WebCrawler │ ├── Main.java │ ├── WebCrawler.java │ └── resources.txt └── ZigzagIterator_281 │ ├── Main.java │ └── ZigzagIterator.java ├── dynamic ├── BombEnemy_361 │ ├── Main.java │ ├── Solution.java │ └── Spot.java ├── CoinChange2_518_ │ ├── Main.java │ └── Solution.java ├── CoinChange_322 │ ├── Main.java │ └── Solution.java ├── CountingBits_338 │ ├── Main.java │ └── Solution.java ├── DecodeWays_91_ │ ├── Main.java │ └── Solution.java ├── EditDistance_72 │ ├── Main.java │ └── Solution.java ├── ExpressionAddOperators_282 │ ├── Main.java │ └── Solution.java ├── HouseRobber_198 │ ├── Main.java │ └── Solution.java ├── IsSubsequence_392 │ ├── Main.java │ └── Solution.java ├── LevenshteinDistance │ ├── Main.java │ └── Solution.java ├── LongestIncreasingSubsequence_300 │ ├── Main.java │ └── Solution.java ├── MaximumProductSubarray_152 │ ├── Main.java │ └── Solution.java ├── MaximumSubarray_53 │ ├── Main.java │ └── Solution.java ├── MinimumPathSum_64 │ ├── Main.java │ └── Solution.java ├── PaintHouse_256 │ ├── Main.java │ └── Solution.java ├── PartitionEqualSubsetSum_416 │ ├── Main.java │ └── Solution.java ├── PerfectSquares_279 │ ├── Main.java │ └── Solution.java ├── RangeSumQuery2DImmutable_304 │ ├── Main.java │ └── NumMatrix.java ├── RangeSumQueryImmutable_303 │ ├── Main.java │ └── NumArray.java ├── RegularExpressionMatching_10 │ ├── Main.java │ └── Solution.java ├── SentenceScreenFitting_418 │ ├── Main.java │ └── Solution.java ├── SerializeAndDeserializeBinaryTree_297 │ ├── Codec.java │ ├── Main.java │ └── TreeNode.java ├── UniqueBinarySearchTrees2_95 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── UniqueBinarySearchTrees_96 │ ├── Main.java │ └── Solution.java ├── UniquePaths2_63 │ ├── Main.java │ └── Solution.java ├── UniquePaths_62 │ ├── Main.java │ └── Solution.java ├── WiggleSubsequence_376 │ ├── Main.java │ └── Solution.java ├── WildcardMatching_44 │ ├── Main.java │ └── Solution.java └── WordBreak_139 │ ├── Main.java │ └── Solution.java ├── graph ├── AlienDictionary_269 │ ├── Main.java │ └── Solution.java ├── CloneGraph_133 │ ├── Main.java │ ├── Solution.java │ └── UndirectedGraphNode.java ├── CommutableIslands_intbit │ ├── Main.java │ └── Solution.java ├── CourseSchedule2_210 │ ├── Main.java │ └── Solution.java ├── CourseSchedule_207 │ ├── Main.java │ └── Solution.java ├── DoctorsRating_real │ ├── Doctor.java │ ├── Main.java │ └── Solution.java ├── GenerateMaze_vstar │ ├── Direction.java │ ├── Main.java │ ├── Maze.java │ └── StdDraw.java ├── GraphTraversal_BFS_DFS_iterativeDFS │ ├── Graph.java │ └── Main.java ├── GraphValidTree_261 │ ├── Main.java │ └── Solution.java ├── MinimumHeightTrees_310 │ ├── Main.java │ └── Solution.java ├── NumberOfConnectedComponentsInAnUndirectedGraph_323 │ ├── Main.java │ └── Solution.java ├── ReconstructItinerary_332 │ ├── Main.java │ └── Solution.java ├── WallsAndGates_286 │ ├── Main.java │ └── Solution.java └── WordLadder_127 │ ├── Main.java │ └── Solution.java ├── hashtable ├── ContainsDuplicate2_219 │ ├── Main.java │ └── Solution.java ├── ContainsDuplicate_217 │ ├── Main.java │ └── Solution.java ├── EncodeAndDecodeTinyURL_535_ │ ├── Codec.java │ └── Main.java ├── GroupAnagrams_49 │ ├── Main.java │ └── Solution.java ├── HappyNumber_202 │ ├── Main.java │ └── Solution.java ├── IntersectionOfTwoArrays_349 │ ├── Main.java │ └── Solution.java ├── IsomorphicStrings_205 │ ├── Main.java │ └── Solution.java ├── KeyboardRow_500 │ ├── Main.java │ └── Solution.java ├── LongestPalindrome_409 │ ├── Main.java │ └── Solution.java ├── LongestSubstringWithAtMostKDistinctCharacters_340 │ ├── Main.java │ └── Solution.java ├── LongestSubstringWithoutRepeatingCharacters_3 │ ├── Main.java │ └── Solution.java ├── MaximumSizeSubarraySumEqualsK_325 │ ├── Main.java │ └── Solution.java ├── MinimumWindowSubstring_76 │ ├── Main.java │ └── Solution.java ├── PalindromePermutation_266 │ ├── Main.java │ └── Solution.java ├── RepeatedDNASequences_187 │ ├── Main.java │ └── Solution.java ├── SortCharactersByFrequency_451_ │ ├── Main.java │ ├── Pair.java │ └── Solution.java ├── StrobogrammaticNumber_246 │ ├── Main.java │ └── Solution.java ├── SubstringWithConcatenationOfAllWords_30 │ ├── Main.java │ └── Solution.java ├── TopKFrequentElements_347 │ ├── Main.java │ ├── MyComparator.java │ ├── MyEntry.java │ └── Solution.java ├── TwoSum_1 │ ├── Main.java │ └── Solution.java ├── ValidAnagram_242 │ ├── Main.java │ └── Solution.java ├── ValidSudoku_36 │ ├── Main.java │ ├── Solution.java │ ├── Solution1.java │ └── Solution2.java └── WordPattern_290 │ ├── Main.java │ └── Solution.java ├── heap ├── KthSmallestElementInASortedMatrix_378 │ └── description.txt ├── TheSkylineProblem_218 │ ├── Main.java │ └── Solution.java └── WaysToFormMaxHeap_intbit │ ├── Main.java │ └── Solution.java ├── info ├── linkedList ├── AddTwoNumbers_2 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── ConvertSortedListToBinarySearchTree_109 │ ├── ListNode.java │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── CopyListWithRandomPointer_138 │ ├── Main.java │ ├── RandomListNode.java │ └── Solution.java ├── DeleteNodeInALinkedList_237 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── InsertionSortList_147 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── IntersectionOfTwoLinkedLists_160 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── LinkedListCycle2_142 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── LinkedListRandomNode_382 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── MergeKSortedLists_23 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── MergeTwoSortedLists_21 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── OddEvenLinkedList_328 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── PalindromeLinkedList_234 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── PartitionList_86_ │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── RemoveDuplicatesFromSortedList2_82_ │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── RemoveLinkedListElements_203 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── RemoveNthNodeFromEndOfList_19 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── ReorderList_143 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── ReverseLinkedList2_92 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── ReverseLinkedList_206 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── RotateList_61 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── SortList_148 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java └── SwapNodesInPairs_24 │ ├── ListNode.java │ ├── Main.java │ └── Solution.java ├── math ├── AddStrings_415 │ ├── Main.java │ └── Solution.java ├── Base7_504 │ ├── Main.java │ └── Solution.java ├── BulbSwitcher_319 │ ├── Main.java │ └── Solution.java ├── CountNumbersWithUniqueDigits_357 │ ├── Main.java │ └── Solution.java ├── ExcelSheetColumnTitle_168 │ ├── Main.java │ └── Solution.java ├── FactorialTrailingZeroes_172 │ ├── Main.java │ └── Solution.java ├── FractionToRecurringDecimal_166 │ ├── Main.java │ └── Solution.java ├── IntegerBreak_343 │ ├── Main.java │ └── Solution.java ├── IntegerToRoman_12 │ ├── Main.java │ └── Solution.java ├── LineReflection_356 │ ├── Main.java │ └── Solution.java ├── MaxPointsOnALine_149 │ ├── Main.java │ ├── Point.java │ ├── Solution.java │ └── Tuple.java ├── MultiplyStrings_43 │ ├── Main.java │ └── Solution.java ├── NumberOfBoomerangs_447 │ ├── Main.java │ └── Solution.java ├── PalindromeNumber_9 │ ├── Main.java │ └── Solution.java ├── PowXN_50 │ ├── Main.java │ └── Solution.java ├── RandomPickIndex_398 │ ├── Main.java │ └── Solution.java ├── RectangleArea_223 │ ├── Main.java │ └── Solution.java ├── ReverseInteger_7 │ ├── Main.java │ └── Solution.java ├── SelfCrossing_335 │ ├── Main.java │ └── Solution.java ├── StringToIntegerAtoi_8 │ ├── Main.java │ └── Solution.java └── ValidPerfectSquare_367 │ ├── Main.java │ └── Solution.java ├── sorting ├── HeapSort │ ├── HeapSort.java │ └── Main.java └── QuickSort │ ├── Main.java │ ├── QuickSelection.java │ ├── QuickSort.java │ └── ThreeWayPartitioning.java ├── stack ├── BasicCalculator_224 │ ├── Main.java │ └── Solution.java ├── BinaryTreeInorderTraversal_94 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── DecodeString_394 │ ├── Main.java │ └── Solution.java ├── EvaluateReversePolishNotation_150 │ ├── Main.java │ └── Solution.java ├── FlattenNestedListIterator_341 │ ├── Main.java │ ├── NestedInteger.java │ └── NestedIterator.java ├── ImplementQueueUsingStacks_232 │ ├── Main.java │ └── MyQueue.java ├── ImplementStackUsingQueues_225 │ ├── Main.java │ └── MyStack.java ├── MinStack_155 │ ├── Main.java │ └── MinStack.java ├── MiniParser_385 │ ├── Main.java │ ├── NestedInteger.java │ └── Solution.java ├── SimplifyPath_71 │ ├── Main.java │ └── Solution.java └── TrappingRainWater_42 │ ├── Main.java │ └── Solution.java ├── statistics ├── string ├── AddBinary_67 │ ├── Main.java │ └── Solution.java ├── BasicCalculator2_227 │ ├── Main.java │ └── Solution.java ├── CompareVersionNumbers_165 │ ├── Main.java │ └── Solution.java ├── CountAndSay_38 │ ├── Main.java │ └── Solution.java ├── CountBinarySubstrings_696 │ ├── Main.java │ └── Solution.java ├── EncodeAndDecodeStrings_271 │ ├── Codec.java │ └── Main.java ├── FirstUniqueCharacterInAString_387 │ ├── Main.java │ └── Solution.java ├── FizzBuzz_412 │ ├── Main.java │ └── Solution.java ├── GenerateParentheses_22 │ ├── Main.java │ └── Solution.java ├── ImplementStrStr_28 │ ├── Main.java │ └── Solution.java ├── IntegerToEnglishWords_273 │ ├── Main.java │ └── Solution.java ├── JudgeRouteCircle_657 │ ├── Main.java │ └── Solution.java ├── LengthOfLastWord_58 │ ├── Main.java │ └── Solution.java ├── LicenseKeyFormatting_482 │ ├── Main.java │ └── Solution.java ├── LongestAbsoluteFilePath_388 │ ├── Main.java │ └── Solution.java ├── LongestCommonPrefix_14 │ ├── Main.java │ └── Solution.java ├── LongestPalindromicSubstring_5 │ ├── Main.java │ └── Solution.java ├── LongestSubstringWithAtMostTwoDistinctCharacters_159 │ ├── Main.java │ └── Solution.java ├── OneEditDistance_161 │ ├── Main.java │ └── Solution.java ├── RansomNote_383 │ ├── Main.java │ └── Solution.java ├── ReadNCharactersGivenRead4_157 │ ├── Main.java │ └── Solution.java ├── ReadNCharactersGivenRead4_2CallMultipleTimes_158 │ ├── Main.java │ └── Solution.java ├── RegularExpressionMatching_10 │ ├── Main.java │ └── Solution.java ├── RemoveInvalidParentheses_301 │ ├── Main.java │ └── Solution.java ├── ReverseWordsInAString_151 │ ├── Main.java │ └── Solution.java ├── ValidPalindrome_125 │ ├── Main.java │ └── Solution.java ├── ValidParentheses_20 │ ├── Main.java │ └── Solution.java └── ZigZagConversion_6 │ ├── Main.java │ └── Solution.java ├── tree ├── BalancedBinaryTree_110 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinarySearchTreeIterator_173 │ ├── BSTIterator.java │ ├── Main.java │ └── TreeNode.java ├── BinaryTreeLevelOrderTraversal2_107 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreeLevelOrderTraversal_102 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreeLongestConsecutiveSequence_298 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreePaths_257 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreePreorderTraversal_144 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreeRightSideView_199 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreeUpsideDown_156_ │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreeVerticalOrderTraversal_314 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── BinaryTreeZigzagLevelOrderTraversal_103 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── ClosestBinarySearchTreeValue_270 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── ConstructBinaryTreeFromInorderAndPostorderTraversal_106 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── ConstructBinaryTreeFromPreorderAndInorderTraversal_105 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── ConvertSortedArrayToBinarySearchTree_108 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── CountCompleteTreeNodes_222 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── FlattenBinaryTreeToLinkedList_114 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── InorderSuccessorInBST_285_ │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── LowestCommonAncestorOfABinarySearchTree_235 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── LowestCommonAncestorOfABinaryTree_236 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── MergeTwoBinaryTrees_617 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── MinimumDepthOfBinaryTree_111 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── PathSum_112 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── PathSum_113 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── PopulatingNextRightPointersInEachNode_116 │ ├── Main.java │ ├── Solution.java │ └── TreeLinkNode.java ├── SumOfNodesWithEvenValuedGrandparent_1315 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── SumRootToLeafNumbers_129 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── SumofLeftLeaves_404 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── SymmetricTree_101 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java ├── TwoSum4InputIsBST_653 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java └── ValidateBinarySearchTree_98 │ ├── Main.java │ ├── Solution.java │ └── TreeNode.java └── unionFind └── NumberOfIslands_200 ├── Main.java └── Solution.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /appTemplates/ConsoleApp/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.appTemplates.ConsoleApp; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | try { 10 | BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 11 | System.out.println("What is your name?"); 12 | String line = reader.readLine(); 13 | System.out.println(String.format("Hello %s", line)); 14 | } catch (IOException ex) { 15 | System.out.println("IOException"); 16 | } 17 | } 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /appTemplates/CsvParser/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.appTemplates.CsvParser; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | s.parseData(); 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /appTemplates/CsvParser/data.csv: -------------------------------------------------------------------------------- 1 | 1,Title1,SomeData, 2 | 1,Title2,SomeOtherData, 3 | -------------------------------------------------------------------------------- /appTemplates/FileIO/temp.txt: -------------------------------------------------------------------------------- 1 | Test data appended data -------------------------------------------------------------------------------- /appTemplates/General/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.appTemplates.General; 2 | 3 | import Templates.General.Solution; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | s.run(); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /appTemplates/General/Solution.java: -------------------------------------------------------------------------------- 1 | package LeetCode.appTemplates.General; 2 | 3 | /** 4 | Template 5 | */ 6 | public class Solution { 7 | public void run() { 8 | System.out.println("Hello world"); 9 | } 10 | } -------------------------------------------------------------------------------- /appTemplates/Html/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | 17 | 18 |
19 |
20 | 21 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /appTemplates/Html/main.js: -------------------------------------------------------------------------------- 1 | class Main { 2 | constructor() { 3 | // do preparation 4 | } 5 | 6 | render() { 7 | const appContainer = document.querySelector("#app"); 8 | const textElement = document.createElement("div"); 9 | textElement.className = "text"; 10 | textElement.innerHTML = "Hello World"; 11 | appContainer.appendChild(textElement); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /array/ArrayPartition1_561_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ArrayPartition1_561_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] arr = {7,3,1,0,0,6}; 7 | int result = s.arrayPairSum(arr); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/BattleshipsInABoard_419_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.BattleshipsInABoard_419_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | char[][] board = {{'X', 'X', 'X', '.', 'X'}}; 7 | int result = s.countBattleships(board); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/BestTimeToBuyAndSellStock2_122/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.BestTimeToBuyAndSellStock2_122; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | //int[] nums = {6,5,5,7,9,9,6,4,3,5,7,6}; 7 | int[] nums = {2,1,2,0,1}; 8 | int profit = s.maxProfit(nums); 9 | System.out.print(profit); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /array/CombinationSum3_216/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.CombinationSum3_216; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List> result = s.combinationSum3(3,9); 9 | for (List l : result) { 10 | System.out.print("[ "); 11 | for (Integer i : l) { 12 | System.out.print(i + ", "); 13 | } 14 | System.out.print(" ]"); 15 | } 16 | 17 | } 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /array/ContainerWithMostWater_11/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ContainerWithMostWater_11; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | System.out.print(s.maxArea(new int[]{2,3,10,5,7,8,9})); 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /array/CountPrimes_204/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.CountPrimes_204; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | System.out.print(s.countPrimes(7)); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/DegreeOfAnArray_697/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.DegreeOfAnArray_697; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int res = s.findShortestSubArray(new int[]{1, 2, 2, 3, 1}); 7 | System.out.print(res); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /array/DiagonalTraverse_498/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.DiagonalTraverse_498; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[][] matrix = new int[][] { 9 | {1,2,6}, 10 | {3,5,7}, 11 | {4,8,9} 12 | }; 13 | int[] res = s.findDiagonalOrder(matrix); 14 | System.out.print(Arrays.toString(res)); 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /array/FibonacciNumber_509/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FibonacciNumber_509; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | s.fib(10); 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /array/FibonacciNumber_509/Solution.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FibonacciNumber_509; 2 | 3 | /** 4 | 509. Fibonacci Number 5 | The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, 6 | such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, 7 | 8 | F(0) = 0, F(1) = 1 9 | F(N) = F(N - 1) + F(N - 2), for N > 1. 10 | Given N, calculate F(N). 11 | */ 12 | class Solution { 13 | public int fib(int N) { 14 | if (N <= 1) return N; 15 | int pprev = 0; 16 | int prev = 1; 17 | int res = 1; 18 | for (int i = 2; i<=N; i++) { 19 | res = prev + pprev; 20 | pprev = prev; 21 | prev = res; 22 | } 23 | return res; 24 | } 25 | } -------------------------------------------------------------------------------- /array/FindAllDuplicatesInAnArray_442/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FindAllDuplicatesInAnArray_442; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | int[] arr = new int[]{1,2,3,4,4}; 10 | List res = s.findDuplicates(arr); 11 | System.out.print(res); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /array/FindAllNumbersDisappearedInAnArray_448/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FindAllNumbersDisappearedInAnArray_448; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] arr = new int[]{4,3,2,7,8,2,3,1}; 9 | List res = s.findDisappearedNumbers(arr); 10 | System.out.print(res); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/FindMinimumInRotatedSortedArray_153/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FindMinimumInRotatedSortedArray_153; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {4, 5, 6, 7, 0, 1, 2}; 7 | int min = s.findMin(nums); 8 | System.out.print(min); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/FindPeakElement_162/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FindPeakElement_162; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {1, 2, 3, 1}; 7 | int peakIndex = s.findPeakElement(nums); 8 | System.out.print(peakIndex); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/FindTheCelebrity_277/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FindTheCelebrity_277; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int res = s.findCelebrity(5); 7 | System.out.print(res); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /array/FindTheCelebrity_277/Relation.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FindTheCelebrity_277; 2 | 3 | /** 4 | * Created by Sergii on 03.12.2016. 5 | */ 6 | public class Relation { 7 | boolean knows(int a, int b) { 8 | return false; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /array/FindTheDuplicateNumber_287_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FindTheDuplicateNumber_287_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {1,3,4,2,2}; 7 | int d = s.findDuplicate(nums); 8 | System.out.print(d); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/FourSum_18/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.FourSum_18; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List> res = s.fourSum(new int[]{1,2,3,4,5}, 10); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/GameOfLife_289/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.GameOfLife_289; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[][] nums = {{1,1},{1,0}}; 7 | s.gameOfLife(nums); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /array/HIndex_274/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.HIndex_274; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {3,0,6,1,5}; 7 | int hIndex = s.hIndex(nums); 8 | System.out.print(hIndex); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/IntersectionOfTwoArrays2_350/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.IntersectionOfTwoArrays2_350; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | int[] nums1 = {1, 2, 3, 4, 5, 5, 6, 6}; 10 | int[] nums2 = {5, 6, 6, 0, 30, 76}; 11 | int[] res = s.intersect(nums1, nums2); 12 | System.out.print(Arrays.toString(res)); 13 | } 14 | } 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /array/IslandPerimeter_463_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.IslandPerimeter_463_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[][] nums = {{0,1,0,0}, 7 | {1,1,1,0}, 8 | {0,1,0,0}, 9 | {1,1,0,0}}; 10 | int res = s.islandPerimeter(nums); 11 | System.out.print(res); 12 | } 13 | } 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /array/JumpGame_55/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.JumpGame_55; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {2,3,1,4}; 7 | int[] nums1 = {3,2,1,0,4}; 8 | boolean res = s.canJump(nums1); 9 | System.out.print(res); 10 | } 11 | } 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/KthLargestElementInAnArray_215/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.KthLargestElementInAnArray_215; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {-1,0,2}; 7 | int res = s.findKthLargest(nums, 2); 8 | System.out.print(res); 9 | } 10 | } 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /array/LargestNumber_179/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.LargestNumber_179; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {3, 30, 34, 5, 9}; 7 | String res = s.largestNumber(nums); 8 | System.out.print(res); 9 | } 10 | } 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /array/LongestConsecutiveSequence_128/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.LongestConsecutiveSequence_128; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {100,4,200,1,3,2}; 7 | int min = s.longestConsecutive(nums); 8 | System.out.print(min); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/MaxConsecutiveOnes_485/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MaxConsecutiveOnes_485; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {1,0,1,1}; 7 | int max = s.findMaxConsecutiveOnes(nums); 8 | System.out.print(max); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/MaxNonNegativeSubArray/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MaxNonNegativeSubArray; 2 | 3 | import LeetCode.array.MergeIntervals_56.Interval; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | public class Main { 10 | public static void main(String[] args) { 11 | Solution s = new Solution(); 12 | ArrayList list = new ArrayList<>(Arrays.asList(1967513926, 1540383426, -1303455736, -521595368)); 13 | ArrayList res = s.maxset(list); 14 | System.out.println(res); 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /array/MaximumDistanceInArrays_624/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MaximumDistanceInArrays_624; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | Solution s = new Solution(); 10 | List> list = new ArrayList<>(); 11 | list.add(Arrays.asList(new Integer[]{1,4})); 12 | list.add(Arrays.asList(new Integer[]{0,5})); 13 | //list.add(Arrays.asList(new Integer[]{1,2,3})); 14 | int max = s.maxDistance(list); 15 | System.out.print(max); 16 | } 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /array/MaximumProductOfThreeNumbers_628/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MaximumProductOfThreeNumbers_628; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {1,2,3}; 7 | int max = s.maximumProduct(nums); 8 | System.out.print(max); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/MergeIntervals_56/Interval.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MergeIntervals_56; 2 | 3 | public class Interval { 4 | int start; 5 | int end; 6 | Interval() { start = 0; end = 0; } 7 | Interval(int s, int e) { start = s; end = e; } 8 | 9 | @Override 10 | public String toString() { 11 | return String.format("{%1$d, %2$d}", start, end); 12 | } 13 | } -------------------------------------------------------------------------------- /array/MergeIntervals_56/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MergeIntervals_56; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | List list = new ArrayList<>(); 10 | list.add(new Interval(1,3)); 11 | list.add(new Interval(2,6)); 12 | List res = s.merge(list); 13 | for(Interval i : res) { 14 | System.out.println(i); 15 | } 16 | } 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /array/MergeSortedArray_88/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MergeSortedArray_88; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] a = {1, 0}; 9 | int[] b = {2}; 10 | s.merge(a, 1, b, 1); 11 | System.out.print(Arrays.toString(a)); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /array/MissingNumber_268/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MissingNumber_268; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {0,1,9,2,8,3,7,4,6}; // should return 5; 7 | int res = s.missingNumber(nums); 8 | System.out.print(res); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /array/MissingRanges_163/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.MissingRanges_163; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] nums = {0, 1, 3, 50, 75}; // should return ["2", "4->49", "51->74", "76->99"] 9 | List res = s.findMissingRanges(nums, 0, 99); 10 | System.out.print(res); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/NextPermutation_31/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.NextPermutation_31; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] nums = {0,1,2,5,3,3,0}; 9 | s.nextPermutation(nums); 10 | System.out.print(Arrays.toString(nums));// 0,1,3,0,2,3,5 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/PartitionToKEqualSumSubsets_698/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.PartitionToKEqualSumSubsets_698; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean res = s.canPartitionKSubsets(new int[]{4,15,1,1,1,1,3,11,1,10}, 3); 7 | System.out.print(res); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /array/PascalTriangle_118/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.PascalTriangle_118; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution tr = new Solution(); 8 | List> result = tr.generate(10); 9 | System.out.print(result); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /array/PlusOne_66/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.PlusOne_66; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] a = {9}; 9 | int[] result = s.plusOne(a); 10 | System.out.print(Arrays.toString(result)); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/PlusOne_66/Solution.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.PlusOne_66; 2 | 3 | /** 4 | * Created by Sergii on 25.07.2016. 5 | 6 | 66. Plus One 7 | https://leetcode.com/problems/plus-one/ 8 | Given a non-negative number represented as an array of digits, plus one to the number. 9 | 10 | The digits are stored such that the most significant digit is at the head of the list. 11 | */ 12 | public class Solution { 13 | public int[] plusOne(int[] digits) { 14 | int i = digits.length-1; 15 | while(i>=0) { 16 | if(digits[i]==9) { 17 | digits[i]=0; 18 | i--; 19 | } else { 20 | digits[i]++; 21 | return digits; 22 | } 23 | } 24 | int[] result = new int[digits.length+1]; 25 | result[0] = 1; 26 | return result; 27 | } 28 | } -------------------------------------------------------------------------------- /array/ProductOfArrayExceptSelf_238/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ProductOfArrayExceptSelf_238; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] result = s.productExceptSelf(new int[]{1,2,3,4}); 9 | System.out.print(Arrays.toString(result)); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /array/QueueReconstructionByHeight_406/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.QueueReconstructionByHeight_406; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[][] a = {{7,0}, {4,4}, {7,1}, {5,0}, {6,1}, {5,2}}; 9 | int[][] r = s.reconstructQueue(a); 10 | for (int[] p : r) { 11 | System.out.print(p[0] + "-" + p[1] + "; "); 12 | } 13 | } 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /array/RemoveDuplicatesFromSortedArray_26/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.RemoveDuplicatesFromSortedArray_26; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] a = {1,1,2,2,3,3}; 9 | int len = s.removeDuplicates(a); 10 | System.out.print(len); 11 | System.out.print(Arrays.toString(a)); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /array/RemoveElement_27/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.RemoveElement_27; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] a = {2,3,3,2}; 9 | int result = s.removeElement(a, 3); 10 | System.out.print(result); 11 | System.out.print(Arrays.toString(a)); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /array/ReshapeTheMatrix_566_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ReshapeTheMatrix_566_; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[][] a = {{1,2}, {3,4}}; 9 | int[][] result = s.matrixReshape(a, 1, 4); 10 | for(int[] row : result) { 11 | System.out.print(Arrays.toString(row)); 12 | } 13 | } 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /array/RotateArray_189/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.RotateArray_189; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] array = {1,2,3}; 9 | s.rotate(array, 2); 10 | System.out.print(Arrays.toString(array)); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/RotateImage_48/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.RotateImage_48; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[][] matrix = {{1,2,3,4,0,0}, {5,6,7,8,1,1}, {9,10,11,12,2,2}, {13,14,15,16,3,3}, {7,2,5,4,7,5}, {7,3,6,4,5,6}}; 9 | s.rotate(matrix); 10 | for (int i=0; i resuilt = s.spiralOrder(matrix); 11 | System.out.print(resuilt); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /array/Subsets_78/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.Subsets_78; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | List> resuilt = s.subsets(new int[]{1,2,3,4}); 10 | System.out.print(resuilt); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/SummaryRanges_228/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.SummaryRanges_228; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List resuilt = s.summaryRanges(new int[]{1,2,4,5,7}); 9 | System.out.print(resuilt); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /array/ThirdMaximumNumber_414/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ThirdMaximumNumber_414; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int resuilt = s.thirdMax(new int[]{2,1}); 7 | System.out.print(resuilt); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /array/ThreeSumClosest_16/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ThreeSumClosest_16; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] a = {0,2,1,-3}; 7 | int res = s.threeSumClosest(a, 1); 8 | System.out.print(res); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /array/ThreeSumSmaller_259/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ThreeSumSmaller_259; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | 10 | int res = s.threeSumSmaller(new int[] {-2, 0, 3, 1}, 2); 11 | System.out.print(res); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /array/ThreeSum_15/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.ThreeSum_15; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution1 s = new Solution1(); 8 | int[] a = {-1, 0, 1, 2, -1, -4}; 9 | List> res = s.threeSum(a); 10 | for (List l: res) { 11 | System.out.println(l.toString()); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /array/TwoSum2_InputArrayIsSorted_167/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.TwoSum2_InputArrayIsSorted_167; 2 | 3 | import LeetCode.array.Triangle_120.*; 4 | import LeetCode.dynamic.UniquePaths_62.*; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | public class Main { 10 | public static void main(String[] args) { 11 | Solution s = new Solution(); 12 | int[] res = s.twoSum(new int[]{2,7,11,15}, 9); 13 | System.out.print(res[0] + " " + res[1]); 14 | } 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /array/WiggleSort2_324/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.WiggleSort2_324; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] res = new int[]{1,5,1,1,6,4}; 9 | s.wiggleSort(res); 10 | System.out.print(Arrays.toString(res)); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /array/WiggleSort_280/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.array.WiggleSort_280; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] res = new int[]{2,4,1,3,7,5,6,9,0,8}; 9 | s.wiggleSort(res); 10 | System.out.print(res); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /backTracking/CombinationSum2_40/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.CombinationSum2_40; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List> result = s.combinationSum2(new int[] {10, 1, 2, 7, 6, 1, 5}, 8); 9 | for (List l : result) { 10 | System.out.println(l); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /backTracking/CombinationSum_39/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.CombinationSum_39; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List> result = s.combinationSum(new int[]{2,3,6,7}, 7) ; 9 | System.out.println(result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /backTracking/Combinations_77/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.Combinations_77; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List> result = s.combine(4, 2); 9 | for (List l : result) { 10 | System.out.println(l); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /backTracking/LetterCombinationsOfAPhoneNumber_17/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.LetterCombinationsOfAPhoneNumber_17; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List result = s.letterCombinations("23"); 9 | System.out.println(result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /backTracking/Permutations2_47/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.Permutations2_47; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List> result = s.permuteUnique(new int[]{1,2,3,2,3}); 9 | for(List l : result) { 10 | System.out.println(l); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /backTracking/Permutations_46/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.Permutations_46; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List> result = s.permute(new int[]{1,2,3}); 9 | for(List l : result) { 10 | System.out.println(l); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /backTracking/RestoreIPAddresses_93_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.RestoreIPAddresses_93_; 2 | 3 | import LeetCode.backTracking.SudokuSolver_37.Solution1; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | public class Main { 9 | public static void main(String[] args) { 10 | Solution s = new Solution(); 11 | List res = s.restoreIpAddresses("127001"); 12 | 13 | for (String ip : res) { 14 | System.out.println(ip); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /backTracking/Subsets2_90/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.Subsets2_90; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | //List> resuilt = s.subsetsWithDup(new int[]{5,5,5,5,5}); 9 | List> resuilt = s.subsetsWithDup(new int[]{1,2,2}); 10 | System.out.print(resuilt); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /backTracking/SudokuSolver_37/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.SudokuSolver_37; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | Solution1 s = new Solution1(); 10 | char [][] sudoku = new char[][]{ 11 | {'1','.','.','4'}, 12 | {'4','.','.','1'}, 13 | {'.','1','.','.'}, 14 | {'.','.','.','.'} 15 | }; 16 | s.solveSudoku(sudoku); 17 | 18 | for (int i=0; i result = s.wordBreak("catsanddog", new ArrayList<>(Arrays.asList("cat", "cats", "and", "sand", "dog"))); 11 | System.out.println(result); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /backTracking/WordSearch_79/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.backTracking.WordSearch_79; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | char[][] board = {// ["ABCE","SFES","ADEE"] 7 | {'A','B','C','E'}, 8 | {'S','F','E','S'}, 9 | {'A','D','E','E'} 10 | }; 11 | // char[][] board = {{'A', 'A'}}; 12 | boolean result = s.exist(board, "ABCESEEEFS") ; 13 | System.out.println(result); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /binarySearch/FirstBadVersion_278/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.binarySearch.FirstBadVersion_278; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.firstBadVersion(27) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /binarySearch/GuessNumberHigherOrLower_374/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.binarySearch.GuessNumberHigherOrLower_374; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.guessNumber(100) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /binarySearch/MedianOfTwoSortedArrays_4/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.binarySearch.MedianOfTwoSortedArrays_4; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int[] a = new int[]{1,3}; 6 | int[] b = new int[]{2}; 7 | Solution s = new Solution(); 8 | double result = s.findMedianSortedArrays(a, b); 9 | System.out.println(result); 10 | } 11 | } -------------------------------------------------------------------------------- /binarySearch/SearchA2DMatrix2_240/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.binarySearch.SearchA2DMatrix2_240; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[][] matrix = new int[][] 7 | {{3, 3, 8, 13,13,18}, 8 | {4, 5, 11,13,18,20}, 9 | {9, 9, 14,15,23,23}, 10 | {13,18,22,22,25,27}, 11 | {18,22,23,28,30,33}, 12 | {21,25,28,30,35,35}, 13 | {24,25,33,36,37,40}}; 14 | boolean result = s.searchMatrix(matrix, 21); 15 | System.out.println(result); 16 | } 17 | } -------------------------------------------------------------------------------- /binarySearch/SearchA2DMatrix_74/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.binarySearch.SearchA2DMatrix_74; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | //boolean result = s.searchMatrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}}, 5); 7 | boolean result = s.searchMatrix(new int[][]{{1}}, 1); 8 | System.out.println(result); 9 | } 10 | } -------------------------------------------------------------------------------- /binarySearch/Sqrt_69/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.binarySearch.Sqrt_69; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.mySqrt(2147395599); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/FindTheDifference_389/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.FindTheDifference_389; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | char result = s.findTheDifference("abcd", "abcde") ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/HammingDistance_461/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.HammingDistance_461; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.hammingDistance(1,4) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/MajorityElement_169/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.MajorityElement_169; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.majorityElement(new int[]{1,4,2,4,3,4,4,5}) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/NumberComplement_476/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.NumberComplement_476; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.findComplement(5) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/NumberOfOneBits_191/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.NumberOfOneBits_191; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.hammingWeight(1) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/PowerOfFour_342/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.PowerOfFour_342; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isPowerOfFour(16) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/PowerOfTwo_231/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.PowerOfTwo_231; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isPowerOfTwo(256); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/ReverseBits_190/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.ReverseBits_190; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.reverseBits(43261596); // 964176192 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/SingleNumber2_137/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.SingleNumber2_137; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.singleNumber(new int[]{1,2,1,2,3,4,5,4,5,9}); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/SingleNumber3_260/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.SingleNumber3_260; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] result = s.singleNumber(new int[]{1,2,1,2,3,4,5,4,5,9}); 7 | System.out.println(String.format("[ %1s, %2s ]", result[0], result[1])); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/SingleNumber_136/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.SingleNumber_136; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.singleNumber(new int[]{1,2,1,2,3,4,5,4,5}); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /bitManipulation/SingleNumber_136/Solution.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.SingleNumber_136; 2 | 3 | /* 4 | 136. Single Number 5 | https://leetcode.com/problems/single-number/ 6 | 7 | Given an array of integers, every element appears twice except for one. Find that single one. 8 | */ 9 | public class Solution { 10 | // A XOR A = 0 and the XOR operator is commutative. 11 | // Time complexity O(n) space complexity O(1) 12 | public int singleNumber(int[] nums) { 13 | int temp = 0; 14 | for(int i : nums) { 15 | temp ^= i; 16 | } 17 | return temp; 18 | } 19 | } -------------------------------------------------------------------------------- /bitManipulation/SumOfTwoIntegers_371/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.bitManipulation.SumOfTwoIntegers_371; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.getSum(20, 30); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /conclusions: -------------------------------------------------------------------------------- 1 | My Conclusions: 2 | 1. If you trying to solve the issue more than 1 hour, look the best solutions. 3 | Mark this problem as "TODO tomorrow", and do it tomorrow. 4 | 2. Write good examples on paper. 5 | 3. Write good readable solution on paper. Debug it manually with tricky input. 6 | (for instance: list with only one item). Fix the bugs, make optimizations. 7 | 4. Write you code in IDE, it should work from the first time!!! 8 | 5. Write your explanation on comments, to make your solution obvious when you read it next time. -------------------------------------------------------------------------------- /design/DesignPhoneDirectory_379_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.DesignPhoneDirectory_379_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | PhoneDirectory s = new PhoneDirectory(3); 6 | int first = s.get(); 7 | System.out.println(first); 8 | System.out.println(s.check(first)); 9 | s.release(first); 10 | System.out.println(s.check(first)); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /design/ImplementConnectedComponents/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementConnectedComponents; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Graph g = new Graph(6); 6 | g.addEdge(0,1); 7 | g.addEdge(1,2); 8 | g.addEdge(3,4); 9 | g.addEdge(4,2); 10 | g.addEdge(2,3); 11 | g.addEdge(0,3); 12 | g.addEdge(0,4); 13 | g.addEdge(2,1); 14 | 15 | ConnectedComponents d = new ConnectedComponents(g); 16 | System.out.println(d.isConnected(0, 4)); 17 | System.out.println(d.isConnected(0, 5)); 18 | } 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /design/ImplementGraphBfs/Graph.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementGraphBfs; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * Graph implementation 8 | */ 9 | public class Graph { 10 | // graph size, so vertices names should be in [0 .. V-1] diapason. 11 | // we can use hashmap to connecg real vertes name with this index. 12 | private int V; 13 | private Set[] adj; 14 | 15 | public Graph(int v) { 16 | this.V = v; 17 | this.adj = new Set[v]; 18 | for (int i=0; i(); 20 | } 21 | } 22 | 23 | public void addEdge(int v, int w) { 24 | this.adj[v].add(w); 25 | this.adj[w].add(v); 26 | } 27 | 28 | public Iterable adj(int v) { 29 | return this.adj[v]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /design/ImplementGraphBfs/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementGraphBfs; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Graph g = new Graph(5); 6 | g.addEdge(0,1); 7 | g.addEdge(1,2); 8 | g.addEdge(3,4); 9 | g.addEdge(4,2); 10 | g.addEdge(2,3); 11 | g.addEdge(0,3); 12 | g.addEdge(0,4); 13 | g.addEdge(2,1); 14 | 15 | BreadthFirstSearch d = new BreadthFirstSearch(g, 0, 5); 16 | Iterable all = d.getItems(); 17 | Iterable path = d.getPath(2); 18 | for(Integer i : all) { 19 | System.out.print(i + ", "); 20 | } 21 | System.out.println("from 0 to 2"); 22 | for(Integer i : path) { 23 | System.out.print(i + "->"); 24 | } 25 | } 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /design/ImplementGraphDfs/Graph.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementGraphDfs; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * Graph implementation 8 | */ 9 | public class Graph { 10 | // graph size, so vertices names should be in [0 .. V-1] diapason. 11 | // we can use hashmap to connecg real vertes name with this index. 12 | private int V; 13 | private Set[] adj; 14 | 15 | public Graph(int v) { 16 | this.V = v; 17 | this.adj = new Set[v]; 18 | for (int i=0; i(); 20 | } 21 | } 22 | 23 | public void addEdge(int v, int w) { 24 | this.adj[v].add(w); 25 | this.adj[w].add(v); 26 | } 27 | 28 | public Iterable adj(int v) { 29 | return this.adj[v]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /design/ImplementGraphDfs/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementGraphDfs; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Graph g = new Graph(5); 6 | g.addEdge(0,1); 7 | g.addEdge(1,2); 8 | g.addEdge(3,4); 9 | g.addEdge(4,2); 10 | g.addEdge(2,3); 11 | g.addEdge(0,3); 12 | g.addEdge(0,4); 13 | g.addEdge(2,1); 14 | 15 | DepthFirstSearch d = new DepthFirstSearch(g, 0, 5); 16 | Iterable all = d.getItems(); 17 | Iterable path = d.getPath(2); 18 | for(Integer i : all) { 19 | System.out.print(i + ", "); 20 | } 21 | System.out.println("from 0 to 2"); 22 | for(Integer i : path) { 23 | System.out.print(i + "->"); 24 | } 25 | } 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /design/ImplementHashTable_SeparateChaining/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementHashTable_SeparateChaining; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | SeparateChainingHashTable map = new SeparateChainingHashTable<>(); 6 | map.put(1, "a"); 7 | map.put(2, "b"); 8 | map.put(3, "c"); 9 | 10 | System.out.println(map.get(1)); 11 | System.out.println(map.get(2)); 12 | System.out.println(map.get(3)); 13 | 14 | map.remove(3); 15 | System.out.println(map.get(3)); 16 | } 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /design/ImplementPriorityQueue/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementPriorityQueue; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | MyPriorityQueue q = new MyPriorityQueue(5); 6 | q.insert(10); 7 | q.insert(2); 8 | q.insert(101); 9 | q.insert(6); 10 | q.insert(4); 11 | System.out.println(q.delMax()); 12 | System.out.println(q.delMax()); 13 | System.out.println(q.delMax()); 14 | System.out.println(q.delMax()); 15 | System.out.println(q.delMax()); 16 | } 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /design/ImplementRedBlackTree/Node.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementRedBlackTree; 2 | 3 | /** 4 | * Node implementation 5 | */ 6 | public class Node, Value> { 7 | public static final boolean RED = true; 8 | public static final boolean BLACK = false; 9 | 10 | public Node(Key key, Value value, boolean color) { 11 | this.key = key; 12 | this.value = value; 13 | this.count = 1; 14 | this.color = color; 15 | } 16 | 17 | public Key key; 18 | public Value value; 19 | public Node left; 20 | public Node right; 21 | public boolean color; 22 | public int count; 23 | 24 | @Override 25 | public String toString() { 26 | return key + " " + value + " " + count; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /design/ImplementShortestPathTreeViaDijkstra/DirectedEdge.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementShortestPathTreeViaDijkstra; 2 | 3 | /** 4 | * Directed edge implementation 5 | */ 6 | public class DirectedEdge { 7 | private final int _from, _to; 8 | private final double _weight; 9 | 10 | public DirectedEdge(int from, int to, double weight) { 11 | this._from = from; 12 | this._to = to; 13 | this._weight = weight; 14 | } 15 | 16 | public int from() { return this._from; } 17 | public int to() { return this._to; } 18 | public double weight() { return this._weight; } 19 | 20 | @Override 21 | public String toString() { 22 | return this._from + "->" + this._to + "(" + this.weight() + "); "; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /design/ImplementSymbolTableViaBinarySearchTree/Node.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementSymbolTableViaBinarySearchTree; 2 | 3 | /** 4 | * BST node class. 5 | */ 6 | public class Node, Value> { 7 | public Node(Key key, Value value) { 8 | this.key = key; 9 | this.value = value; 10 | this.count = 1; 11 | } 12 | 13 | public Key key; 14 | public Value value; 15 | public Node left; 16 | public Node right; 17 | public int count; 18 | 19 | @Override 20 | public String toString() { 21 | return key + " " + value + count; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /design/ImplementTopologicalSortInDigraph/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementTopologicalSortInDigraph; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Digraph g = new Digraph(7); 6 | g.addEdge(0, 5); 7 | g.addEdge(0, 1); 8 | g.addEdge(3, 5); 9 | g.addEdge(5, 2); 10 | g.addEdge(6, 0); 11 | g.addEdge(1, 4); 12 | g.addEdge(0, 2); 13 | g.addEdge(3, 6); 14 | g.addEdge(3, 4); 15 | g.addEdge(6, 4); 16 | g.addEdge(3, 2); 17 | 18 | DepthFirstOrder d = new DepthFirstOrder(g); 19 | Iterable path = d.getTopologicalOrder(); 20 | 21 | System.out.println("Topological order: "); 22 | for(Integer i : path) { 23 | System.out.print(i + "->"); 24 | } 25 | } 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /design/ImplementTrie_208/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementTrie_208; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Trie trie = new Trie(); 6 | trie.insert("car"); 7 | trie.insert("cap"); 8 | trie.insert("capitan"); 9 | trie.insert("america"); 10 | System.out.println(trie.search("cap")); 11 | System.out.println(trie.startsWith("capi")); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /design/ImplementTrie_208/TrieNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ImplementTrie_208; 2 | 3 | import java.util.Hashtable; 4 | 5 | class TrieNode { 6 | public char key; 7 | public boolean hasInfo = false; 8 | public Hashtable children = new Hashtable<>(); 9 | public TrieNode() { 10 | } 11 | public TrieNode(char key) { 12 | this.key = key; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /design/LRUCache_146/CacheItem.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.LRUCache_146; 2 | 3 | public class CacheItem { 4 | public CacheItem prev; 5 | public CacheItem next; 6 | public int value; 7 | public int key; 8 | 9 | public CacheItem(int key, int value) { 10 | this.value = value; 11 | this.key = key; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /design/LRUCache_146/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.LRUCache_146; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | LRUCache cache = new LRUCache(5); 6 | cache.put(1,1); 7 | cache.put(2,2); 8 | cache.put(3,3); 9 | cache.put(4,4); 10 | cache.put(2,10); 11 | cache.remove(3); 12 | 13 | System.out.println(cache.toString()); 14 | } 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /design/Mocks/Slot.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.Mocks; 2 | 3 | public class Slot { 4 | int time; //0-23 in PST 5 | SlotState state; 6 | User opponent; // TODO: make a test to check that that opponent has this user as opponent. 7 | boolean isPrimary; 8 | 9 | Slot(int time, SlotState state) { 10 | this.state = state; 11 | this.time = time; 12 | } 13 | 14 | void updateState(SlotState state) { 15 | this.state = state; 16 | } 17 | 18 | /** User and isPrimary is not copied */ 19 | Slot freshCopy() { 20 | return new Slot(this.time, this.state); 21 | } 22 | 23 | public String toString() { 24 | return String.format("SLOT: time: %d state: %s", time, state); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /design/Mocks/SlotState.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.Mocks; 2 | 3 | public enum SlotState { 4 | BLOCKED, 5 | ONHOLD, 6 | FREE, 7 | SCHEDULED 8 | } 9 | -------------------------------------------------------------------------------- /design/MovingAverageFromDataStream_346/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.MovingAverageFromDataStream_346; 2 | 3 | import LeetCode.design.ZigzagIterator_281.ZigzagIterator; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | public class Main { 10 | public static void main(String[] args) { 11 | MovingAverage obj = new MovingAverage(3); 12 | obj.next(1); 13 | obj.next(10); 14 | obj.next(3); 15 | double param_1 = obj.next(3); 16 | System.out.println(param_1); // to be 3 17 | } 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /design/QuickSort/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.QuickSort; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Integer[] arr = new Integer[]{0,1,9,2,8,3,7,4,4,4,4,4,5,6}; 8 | //Integer[] arr = new Integer[]{}; 9 | System.out.println(QuickSort.select(arr, 6)); 10 | QuickSort.sort(arr); 11 | System.out.println(Arrays.toString(arr)); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /design/SnakeGame_353/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.SnakeGame_353; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | SnakeGame game = new SnakeGame(3,3,new int[][]{{1,1}, {2,2}, {2,0}}); 6 | 7 | System.out.println(game.move("R")); 8 | System.out.println(game.move("D")); 9 | System.out.println(game.move("R")); 10 | System.out.println(game.move("D")); 11 | System.out.println(game.move("L")); 12 | System.out.println(game.move("L")); 13 | System.out.println(game.move("L")); 14 | System.out.println(game.move("L")); 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /design/UniqueWordAbbreviation_288/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.UniqueWordAbbreviation_288; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ValidWordAbbr vwa = new ValidWordAbbr(new String[] {"deer", "door", "cake", "card" }); 6 | System.out.println(vwa.isUnique("dppr")); 7 | System.out.println(vwa.isUnique("anotherWord")); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /design/WebCrawler/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.WebCrawler; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | WebCrawler crawler = new WebCrawler(); 6 | crawler.run(); 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /design/ZigzagIterator_281/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.design.ZigzagIterator_281; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | List l1 = new ArrayList<>(Arrays.asList(1,3,5,7,9)); 10 | List l2 = new ArrayList<>(Arrays.asList(2,4,6,8)); 11 | ZigzagIterator iterator = new ZigzagIterator(l1, l2); 12 | while (iterator.hasNext()) { 13 | System.out.println(iterator.next()); 14 | } 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /dynamic/BombEnemy_361/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.BombEnemy_361; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | char[][] grid = new char[][] { 7 | {'0', 'E', '0', '0'}, 8 | {'E', '0', 'W', 'E'}, 9 | {'0', 'E', '0', '0'} 10 | }; 11 | int result = s.maxKilledEnemies(grid); 12 | System.out.println(result); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /dynamic/BombEnemy_361/Spot.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.BombEnemy_361; 2 | 3 | class Spot { 4 | public int up; // enemies that can be destroyed up + current cell enemy; 5 | public int bottom; // enemies that can be destroyed to the bottom; 6 | public int left; // enemies that can be destroyed to the left + current cell enemy; 7 | public int right; // enemies that can be destroyed to the right; 8 | } -------------------------------------------------------------------------------- /dynamic/CoinChange2_518_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.CoinChange2_518_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.change(5, new int[]{1,2,5}); 7 | System.out.println(result); 8 | //System.out.println(s.change(500, new int[]{3,5,7,8,9,10,11})); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /dynamic/CoinChange_322/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.CoinChange_322; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] coins = {2,5,1}; 7 | int result = s.coinChange(coins, 11); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/CountingBits_338/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.CountingBits_338; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] result = s.countBits(5); 9 | System.out.print(Arrays.toString(result)); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /dynamic/DecodeWays_91_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.DecodeWays_91_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.numDecodings("0"); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/EditDistance_72/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.EditDistance_72; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.minDistance("intention", "execution"); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/ExpressionAddOperators_282/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.ExpressionAddOperators_282; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List result = s.addOperators("123", 6); 9 | System.out.print(result); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /dynamic/HouseRobber_198/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.HouseRobber_198; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {10,1,2,12}; 7 | int result = s.rob(nums); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/IsSubsequence_392/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.IsSubsequence_392; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {10,1,2,12}; 7 | boolean result = s.isSubsequence("ace", "abcde"); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/LevenshteinDistance/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.LevenshteinDistance; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.findMinEditDistance("intention", "execution") ; 7 | System.out.println(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/LongestIncreasingSubsequence_300/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.LongestIncreasingSubsequence_300; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {10, 9, 2, 5, 3, 7, 101, 18}; 7 | int result = s.lengthOfLIS(nums) ; 8 | System.out.println(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/MaximumProductSubarray_152/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.MaximumProductSubarray_152; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {2,3,-2,4}; 7 | int result = s.maxProduct(nums); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/MaximumSubarray_53/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.MaximumSubarray_53; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {-2,1,-3,4,-1,2,1,-5,4}; 7 | int result = s.maxSubArray(nums); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/MinimumPathSum_64/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.MinimumPathSum_64; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | int result = n.minPathSum(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}}); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/PaintHouse_256/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.PaintHouse_256; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int[][] costs = new int[][] {{1,2,3}, {5,2,3}, {7,2,3}}; 6 | Solution n = new Solution(); 7 | int result = n.minCost(costs); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/PartitionEqualSubsetSum_416/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.PartitionEqualSubsetSum_416; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | boolean result = n.canPartition(new int[]{1, 5, 11, 5}); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/PerfectSquares_279/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.PerfectSquares_279; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | int result = n.numSquares(51); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/RangeSumQuery2DImmutable_304/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.RangeSumQuery2DImmutable_304; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | NumMatrix n = new NumMatrix(new int[][] {{3,0,1}, {2,0,1}, {1,1,1}}); 6 | int result = n.sumRegion(0,0,2,2); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/RangeSumQueryImmutable_303/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.RangeSumQueryImmutable_303; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | NumArray n = new NumArray(new int[] {5,10,2,12}); 6 | int result = n.sumRange(1,3); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/RegularExpressionMatching_10/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.RegularExpressionMatching_10; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | //int result = n.wordsTyping(new String[] {"hello", "world"}, 2, 8); // to be 1 7 | //int result = n.wordsTyping1(new String[] {"f", "p", "a"}, 8, 7); // to be 10 8 | //int result = n.wordsTyping1(new String[] {"I", "had", "apple", "pie"}, 4, 5); // to be 1 9 | //boolean result = n.isMatch("aab", "c*a*b"); // to be 1 10 | boolean result = n.isMatch("aaa", "a.a"); // to be 1 11 | System.out.println(result); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dynamic/SentenceScreenFitting_418/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.SentenceScreenFitting_418; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | //int result = n.wordsTyping(new String[] {"hello", "world"}, 2, 8); // to be 1 7 | //int result = n.wordsTyping1(new String[] {"f", "p", "a"}, 8, 7); // to be 10 8 | //int result = n.wordsTyping1(new String[] {"I", "had", "apple", "pie"}, 4, 5); // to be 1 9 | int result = n.wordsTyping1(new String[] {"a", "bcd", "e"}, 3, 6); // to be 1 10 | System.out.println(result); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /dynamic/SerializeAndDeserializeBinaryTree_297/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.SerializeAndDeserializeBinaryTree_297; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Codec codec = new Codec(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | root.left.right.left = new TreeNode(10); 12 | 13 | String encoded = codec.serialize(root); 14 | TreeNode res = codec.deserialize(encoded); 15 | String encoded1 = codec.serialize(res); 16 | System.out.println(encoded); 17 | System.out.println(encoded1); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /dynamic/SerializeAndDeserializeBinaryTree_297/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.SerializeAndDeserializeBinaryTree_297; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | } -------------------------------------------------------------------------------- /dynamic/UniqueBinarySearchTrees2_95/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.UniqueBinarySearchTrees2_95; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution n = new Solution(); 8 | List result = n.generateTrees(3); 9 | for (TreeNode root : result) { 10 | System.out.println(root); 11 | } 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /dynamic/UniqueBinarySearchTrees2_95/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.UniqueBinarySearchTrees2_95; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /dynamic/UniqueBinarySearchTrees_96/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.UniqueBinarySearchTrees_96; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | int result = n.numTrees(4); 7 | System.out.println(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/UniquePaths2_63/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.UniquePaths2_63; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | int result = n.uniquePathsWithObstacles(new int [][] {{0,0,0}, {0,1,0}, {0,0,0}}); 7 | System.out.println(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/UniquePaths_62/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.UniquePaths_62; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution n = new Solution(); 6 | int result = n.uniquePaths(3,3); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /dynamic/WiggleSubsequence_376/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.WiggleSubsequence_376; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {1,7,4,9,2,5}; 7 | int result = s.wiggleMaxLength(nums); 8 | System.out.println(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /dynamic/WildcardMatching_44/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.WildcardMatching_44; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | boolean result = s.isMatch("baaabab", "ba*****ab"); 10 | System.out.println(result); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /dynamic/WordBreak_139/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.dynamic.WordBreak_139; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | boolean result = s.wordBreak("leetcode", new ArrayList<>(Arrays.asList("le", "leet", "code"))); 10 | System.out.println(result); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /graph/AlienDictionary_269/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.AlienDictionary_269; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String[] words = new String[]{"wrt", "wrf", "er", "ett", "rftt"}; // "wertf" 7 | //String[] words = new String[]{"wrtkj","wrt"}; 8 | String result = s.alienOrder(words); 9 | System.out.print(result); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /graph/CloneGraph_133/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.CloneGraph_133; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | UndirectedGraphNode node = new UndirectedGraphNode(1); 6 | Solution s = new Solution(); 7 | UndirectedGraphNode res = s.cloneGraph(node); 8 | System.out.print(res.label); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /graph/CloneGraph_133/UndirectedGraphNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.CloneGraph_133; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | class UndirectedGraphNode { 7 | int label; 8 | List neighbors = new ArrayList<>(); 9 | UndirectedGraphNode(int x) { 10 | label = x; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /graph/CommutableIslands_intbit/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.CommutableIslands_intbit; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | ArrayList> list = new ArrayList<>(); 9 | list.add(new ArrayList<>(Arrays.asList(1, 2, 10))); 10 | list.add(new ArrayList<>(Arrays.asList(2, 3, 5))); 11 | list.add(new ArrayList<>(Arrays.asList(1, 3, 9))); 12 | 13 | Solution s = new Solution(); 14 | int res = s.solve(3, list); 15 | System.out.print(res); 16 | } 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /graph/CourseSchedule2_210/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.CourseSchedule2_210; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[][] pre = new int[][]{{1,0},{2,0},{3,1},{3,2}}; 7 | int[] result = s.findOrder(4, pre); 8 | for (int i : result) System.out.print(i + ", "); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /graph/CourseSchedule_207/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.CourseSchedule_207; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | //int[][] pre = new int[][]{{1,2}, {2,1}}; 7 | //boolean result = s.canFinish2(3, pre); 8 | boolean result = s.canFinish2(1, new int[][]{}); 9 | System.out.println(result); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /graph/DoctorsRating_real/Doctor.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.DoctorsRating_real; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Doctor { 7 | public String name; 8 | public float rating; 9 | public List relatedDoctors; 10 | 11 | Doctor(String name, float rating, Doctor[] doctors) { 12 | this.name = name; 13 | this.rating = rating; 14 | this.relatedDoctors = new ArrayList<>(); 15 | for (Doctor doc : doctors) { 16 | this.relatedDoctors.add(doc); 17 | } 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return String.format("%1$s %2$f", this.name, this.rating); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /graph/GenerateMaze_vstar/Direction.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.GenerateMaze_vstar; 2 | 3 | public enum Direction { 4 | NORTH, EAST, SOUTH, WEST; 5 | } 6 | -------------------------------------------------------------------------------- /graph/GenerateMaze_vstar/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.GenerateMaze_vstar; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Maze s = new Maze(10); 6 | s.generate(); 7 | s.draw(); 8 | s.solve(); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /graph/GraphValidTree_261/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.GraphValidTree_261; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | //int[][] edges = new int[][]{{0, 1}, {0, 2}, {0, 3}, {3, 4}}; 7 | int[][] edges = new int[][]{}; 8 | boolean result = s.validTree(1, edges); 9 | System.out.println(result); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /graph/MinimumHeightTrees_310/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.MinimumHeightTrees_310; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[][] pre = new int[][]{{0,1}, {1,2}, {1,3}}; 9 | List result = s.findMinHeightTrees(4, pre); 10 | System.out.println(result); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /graph/NumberOfConnectedComponentsInAnUndirectedGraph_323/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.NumberOfConnectedComponentsInAnUndirectedGraph_323; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[][] edges = new int[][]{{0,1}, {1,2}, {2,3}, {3,4}}; 7 | int result = s.countComponents(5, edges); 8 | System.out.println(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /graph/ReconstructItinerary_332/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.ReconstructItinerary_332; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List result = s.findItinerary(new String[][] {{"JFK","KUL"},{"JFK","NRT"},{"NRT","JFK"}}); 9 | System.out.println(result); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /graph/WordLadder_127/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.graph.WordLadder_127; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | Solution s = new Solution(); 10 | int res = s.ladderLength("hit", "cog", 11 | new ArrayList<>(Arrays.asList("hot","dot","dog","lot","log","cog"))); 12 | System.out.println(res); 13 | 14 | List path = s.ladderPath("hit", "cog", 15 | new ArrayList<>(Arrays.asList("hot","dot","dog","lot","log","cog"))); 16 | System.out.println(String.join(", ", path)); 17 | } 18 | } 19 | 20 | 21 | //INF -1 0 INF 22 | // INF INF INF -1 23 | // INF -1 INF -1 24 | // 0 -1 INF INF -------------------------------------------------------------------------------- /hashtable/ContainsDuplicate2_219/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.ContainsDuplicate2_219; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] nums = {1,2,3,4,5,3,6,7}; 7 | boolean result = s.containsNearbyDuplicate(nums, 3); 8 | System.out.print(result); 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /hashtable/ContainsDuplicate_217/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.ContainsDuplicate_217; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] nums = {1,2,3,4,5,6,7}; 9 | boolean result = s.containsDuplicate(nums); 10 | System.out.print(result); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /hashtable/EncodeAndDecodeTinyURL_535_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.EncodeAndDecodeTinyURL_535_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Codec s = new Codec(); 6 | String result = s.decode(s.encode("https://leetcode.com/problems/design-tinyurl")); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/GroupAnagrams_49/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.GroupAnagrams_49; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"}; 9 | List> result = s.groupAnagrams(strs); 10 | System.out.print(result); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /hashtable/HappyNumber_202/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.HappyNumber_202; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | boolean result = s.isHappy(7); 9 | System.out.print(result); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /hashtable/IntersectionOfTwoArrays_349/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.IntersectionOfTwoArrays_349; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] nums1 = {1,2,3,4,5,6,7}; 9 | int[] nums2 = {6,7,8,9}; 10 | int[] result = s.intersection(nums1, nums2); 11 | System.out.print(Arrays.toString(result)); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /hashtable/IsomorphicStrings_205/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.IsomorphicStrings_205; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isIsomorphic("add", "egg"); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/KeyboardRow_500/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.KeyboardRow_500; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String[] result = s.findWords(new String[] {"Hello", "Alaska", "Dad", "Peace"}); 7 | for(String str : result) { 8 | System.out.println(str); 9 | } 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /hashtable/LongestPalindrome_409/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.LongestPalindrome_409; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.longestPalindrome("addthse"); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/LongestSubstringWithAtMostKDistinctCharacters_340/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.LongestSubstringWithAtMostKDistinctCharacters_340; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.lengthOfLongestSubstringKDistinct("ececb", 2); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/LongestSubstringWithoutRepeatingCharacters_3/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.LongestSubstringWithoutRepeatingCharacters_3; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.lengthOfLongestSubstring("abba"); 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/MaximumSizeSubarraySumEqualsK_325/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.MaximumSizeSubarraySumEqualsK_325; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.maxSubArrayLen(new int[] {1, -1, 5, -2, 3}, 3); // res==4 7 | System.out.println(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/MinimumWindowSubstring_76/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.MinimumWindowSubstring_76; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.minWindow("AOBECOBANC", "ABC"); 7 | System.out.println(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/PalindromePermutation_266/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.PalindromePermutation_266; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.canPermutePalindrome("aab"); 7 | System.out.println(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /hashtable/RepeatedDNASequences_187/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.RepeatedDNASequences_187; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List result = s.findRepeatedDnaSequences("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"); 9 | System.out.println(result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hashtable/SortCharactersByFrequency_451_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.SortCharactersByFrequency_451_; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | String result = s.frequencySort1("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"); 9 | System.out.println(result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hashtable/SortCharactersByFrequency_451_/Pair.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.SortCharactersByFrequency_451_; 2 | 3 | public class Pair { 4 | public char character; 5 | public int count; 6 | public Pair(char character, int count) { 7 | this.character = character; 8 | this.count = count; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /hashtable/StrobogrammaticNumber_246/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.StrobogrammaticNumber_246; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isStrobogrammatic("818"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /hashtable/SubstringWithConcatenationOfAllWords_30/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.SubstringWithConcatenationOfAllWords_30; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List result = s.findSubstring("wordgoodgoodgoodbestword", new String[]{"word","good","best","good"}); 9 | System.out.println(result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hashtable/TopKFrequentElements_347/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.TopKFrequentElements_347; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] nums = {1,1,1,2,2,3,4,4,5,6,6,6,6,6,6,7}; 9 | List result = s.topKFrequent(nums, 4); 10 | System.out.print(result.toString()); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /hashtable/TopKFrequentElements_347/MyComparator.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.TopKFrequentElements_347; 2 | 3 | import java.util.Comparator; 4 | 5 | /** 6 | * Created by Sergii on 01.09.2016. 7 | */ 8 | public class MyComparator implements Comparator { 9 | 10 | @Override 11 | public int compare(MyEntry arg0, MyEntry arg1) { 12 | // if (arg0.count == arg1.count) return 0; 13 | // if (arg0.count > arg1.count) return -1; 14 | // return 1; 15 | return arg1.count - arg0.count; 16 | } 17 | } -------------------------------------------------------------------------------- /hashtable/TopKFrequentElements_347/MyEntry.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.TopKFrequentElements_347; 2 | 3 | /** 4 | * Created by Sergii on 01.09.2016. 5 | */ 6 | public class MyEntry { 7 | public int key; 8 | public int count; 9 | 10 | public MyEntry(int key, int count) { 11 | this.key = key; 12 | this.count = count; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hashtable/TwoSum_1/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.TwoSum_1; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | int[] nums = {1,2,3,4,5,6,7}; 9 | int[] result = s.twoSum(nums, 6); 10 | System.out.print(Arrays.toString(result)); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /hashtable/ValidAnagram_242/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.ValidAnagram_242; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | String a = "cat"; 9 | String b = "tca"; 10 | boolean result = s.isAnagram(a,b); 11 | System.out.print(result); 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /hashtable/ValidSudoku_36/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.ValidSudoku_36; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | char[][] sudoku = new char[][]{ 7 | {'1', '4','.', '2'}, 8 | {'2', '3','.', '.'}, 9 | {'3', '2','.', '.'}, 10 | {'4', '1','.', '.'}}; 11 | boolean result = s.isValidSudoku(sudoku); 12 | System.out.println(result); 13 | 14 | Solution1 s1 = new Solution1(); 15 | boolean result1 = s1.isValidSudoku(sudoku); 16 | System.out.println(result1); 17 | 18 | Solution2 s2 = new Solution2(); 19 | boolean result2 = s2.isValidSudoku(sudoku); 20 | System.out.println(result2); 21 | 22 | } 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /hashtable/WordPattern_290/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.hashtable.WordPattern_290; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | boolean isMatch = s.wordPattern("abba", "dog cat cat dog"); 9 | System.out.print(isMatch); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /heap/KthSmallestElementInASortedMatrix_378/description.txt: -------------------------------------------------------------------------------- 1 | https://discuss.leetcode.com/topic/52948/share-my-thoughts-and-clean-java-code 2 | 3 | great java solution with heap 4 | I didn't catch the binary search solution -------------------------------------------------------------------------------- /heap/TheSkylineProblem_218/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.heap.TheSkylineProblem_218; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List res = s.getSkyline(new int[][] { 9 | {2, 9, 10}, {3, 7, 15}, {5, 12, 12}, {15, 20, 10}, {19, 24, 8} 10 | }); 11 | // [ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ] 12 | for(int[] i : res){ 13 | System.out.print(String.format("%s %s; ", i[0], i[1])); 14 | } 15 | 16 | } 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /heap/WaysToFormMaxHeap_intbit/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.heap.WaysToFormMaxHeap_intbit; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | int res = s.solve(2); 10 | System.out.print(res); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /info: -------------------------------------------------------------------------------- 1 | Binary Search 2 | http://algs4.cs.princeton.edu/11model/BinarySearch.java.html - binary search, java implementation. 3 | 4 | Binary Tree Traversal 5 | https://en.wikipedia.org/wiki/Tree_traversal - tree traversal 6 | https://www.youtube.com/watch?v=86g8jAQug04 - breadth-first search 7 | https://www.youtube.com/watch?v=9RHO6jU--GU - depth-first search 8 | -------------------------------------------------------------------------------- /linkedList/AddTwoNumbers_2/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.AddTwoNumbers_2; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/AddTwoNumbers_2/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.AddTwoNumbers_2; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(); 17 | ListNode res = s.addTwoNumbers(a, c); 18 | System.out.print(res); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/ConvertSortedListToBinarySearchTree_109/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ConvertSortedListToBinarySearchTree_109; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/ConvertSortedListToBinarySearchTree_109/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ConvertSortedListToBinarySearchTree_109; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(); 17 | TreeNode res = s.sortedListToBST(a); 18 | System.out.print(res); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/ConvertSortedListToBinarySearchTree_109/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ConvertSortedListToBinarySearchTree_109; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /linkedList/CopyListWithRandomPointer_138/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.CopyListWithRandomPointer_138; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | RandomListNode a = new RandomListNode(1); 6 | RandomListNode b = new RandomListNode(2); 7 | RandomListNode c = new RandomListNode(3); 8 | RandomListNode d = new RandomListNode(4); 9 | RandomListNode e = new RandomListNode(5); 10 | 11 | a.next = b; 12 | a.random = e; 13 | 14 | b.next = c; 15 | c.next = d; 16 | d.next = e; 17 | e.random = a; 18 | 19 | Solution s = new Solution(); 20 | RandomListNode clone = s.copyRandomList(a); 21 | 22 | System.out.print(clone); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /linkedList/CopyListWithRandomPointer_138/RandomListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.CopyListWithRandomPointer_138; 2 | 3 | public class RandomListNode { 4 | int label; 5 | RandomListNode next; 6 | RandomListNode random; 7 | RandomListNode(int x) { this.label = x; } 8 | 9 | @Override 10 | public String toString() { 11 | StringBuilder sb = new StringBuilder(); 12 | RandomListNode current = this; 13 | while (current != null) { 14 | sb.append(current.label + "->"); 15 | current = current.next; 16 | } 17 | return sb.toString(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /linkedList/DeleteNodeInALinkedList_237/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.DeleteNodeInALinkedList_237; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/DeleteNodeInALinkedList_237/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.DeleteNodeInALinkedList_237; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(); 17 | s.deleteNode(c); 18 | System.out.print(a); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/InsertionSortList_147/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.InsertionSortList_147; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/InsertionSortList_147/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.InsertionSortList_147; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(3); 6 | ListNode b = new ListNode(4); 7 | ListNode c = new ListNode(1); 8 | ListNode d = new ListNode(1); 9 | ListNode e = new ListNode(2); 10 | 11 | a.next = b; 12 | b.next = c; 13 | // c.next = d; 14 | // d.next = e; 15 | 16 | Solution s = new Solution(); 17 | ListNode head = s.insertionSortList(a); 18 | System.out.print(head); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/IntersectionOfTwoLinkedLists_160/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.IntersectionOfTwoLinkedLists_160; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/LinkedListCycle2_142/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.LinkedListCycle2_142; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/LinkedListCycle2_142/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.LinkedListCycle2_142; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | e.next = b; 16 | 17 | Solution s = new Solution(); 18 | ListNode n = s.detectCycle(a); 19 | System.out.print(n.val); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linkedList/LinkedListRandomNode_382/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.LinkedListRandomNode_382; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/LinkedListRandomNode_382/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.LinkedListRandomNode_382; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(a); 17 | for(int i = 0; i<10; i++) { 18 | System.out.print(s.getRandom()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linkedList/MergeKSortedLists_23/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.MergeKSortedLists_23; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/MergeTwoSortedLists_21/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.MergeTwoSortedLists_21; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/MergeTwoSortedLists_21/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.MergeTwoSortedLists_21; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(3); 7 | ListNode c = new ListNode(5); 8 | ListNode d = new ListNode(2); 9 | ListNode e = new ListNode(4); 10 | 11 | // first list 12 | a.next = b; 13 | b.next = c; 14 | 15 | // second list 16 | d.next = e; 17 | Solution s = new Solution(); 18 | ListNode result = s.mergeTwoLists(a,d); 19 | System.out.print(result); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /linkedList/OddEvenLinkedList_328/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.OddEvenLinkedList_328; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/OddEvenLinkedList_328/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.OddEvenLinkedList_328; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(); 17 | ListNode result = s.oddEvenList(a); 18 | System.out.print(result); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/PalindromeLinkedList_234/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.PalindromeLinkedList_234; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | } 8 | -------------------------------------------------------------------------------- /linkedList/PalindromeLinkedList_234/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.PalindromeLinkedList_234; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(2); 9 | ListNode e = new ListNode(1); 10 | a.next = b; 11 | b.next = c; 12 | c.next = d; 13 | d.next = e; 14 | Solution s = new Solution(); 15 | boolean result = s.isPalindrome(a); 16 | System.out.print(result); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/PartitionList_86_/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.PartitionList_86_; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | } 8 | -------------------------------------------------------------------------------- /linkedList/PartitionList_86_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.PartitionList_86_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(2); 6 | ListNode b = new ListNode(1); 7 | a.next = b; 8 | Solution s = new Solution(); 9 | ListNode result = s.partition(a, 2); 10 | System.out.print(result); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /linkedList/RemoveDuplicatesFromSortedList2_82_/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RemoveDuplicatesFromSortedList2_82_; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | } 8 | -------------------------------------------------------------------------------- /linkedList/RemoveDuplicatesFromSortedList2_82_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RemoveDuplicatesFromSortedList2_82_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(1); 7 | a.next = b; 8 | Solution s = new Solution(); 9 | ListNode result = s.deleteDuplicates(a); 10 | System.out.print(result); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /linkedList/RemoveLinkedListElements_203/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RemoveLinkedListElements_203; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/RemoveLinkedListElements_203/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RemoveLinkedListElements_203; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(3); 7 | ListNode c = new ListNode(5); 8 | 9 | a.next = b; 10 | b.next = c; 11 | 12 | Solution s = new Solution(); 13 | ListNode result = s.removeElements(a,3); 14 | System.out.print(result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /linkedList/RemoveNthNodeFromEndOfList_19/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RemoveNthNodeFromEndOfList_19; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | } 8 | -------------------------------------------------------------------------------- /linkedList/RemoveNthNodeFromEndOfList_19/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RemoveNthNodeFromEndOfList_19; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(2); 9 | ListNode e = new ListNode(1); 10 | a.next = b; 11 | b.next = c; 12 | c.next = d; 13 | d.next = e; 14 | Solution s = new Solution(); 15 | ListNode result = s.removeNthFromEnd(e,1); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /linkedList/ReorderList_143/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ReorderList_143; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/ReorderList_143/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ReorderList_143; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(); 17 | s.reorderList(a); 18 | System.out.print(a); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/ReverseLinkedList2_92/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ReverseLinkedList2_92; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/ReverseLinkedList2_92/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ReverseLinkedList2_92; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(); 17 | ListNode res = s.reverseBetween(a,2,4); 18 | System.out.print(res); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/ReverseLinkedList_206/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ReverseLinkedList_206; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/ReverseLinkedList_206/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.ReverseLinkedList_206; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(3); 7 | ListNode c = new ListNode(5); 8 | a.next = b; 9 | b.next = c; 10 | Solution s = new Solution(); 11 | ListNode result = s.reverseList(a); 12 | System.out.print(result); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /linkedList/RotateList_61/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RotateList_61; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/RotateList_61/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.RotateList_61; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | // ListNode c = new ListNode(3); 8 | // ListNode d = new ListNode(4); 9 | // ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | // b.next = c; 13 | // c.next = d; 14 | // d.next = e; 15 | 16 | Solution s = new Solution(); 17 | ListNode result = s.rotateRight(a, 2); 18 | System.out.print(result); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/SortList_148/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.SortList_148; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/SortList_148/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.SortList_148; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(5); 6 | ListNode b = new ListNode(3); 7 | ListNode c = new ListNode(4); 8 | // ListNode d = new ListNode(2); 9 | // ListNode e = new ListNode(1); 10 | 11 | a.next = b; 12 | b.next = c; 13 | // c.next = d; 14 | // d.next = e; 15 | 16 | Solution s = new Solution(); 17 | ListNode result = s.sortList(a); 18 | System.out.print(result); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /linkedList/SwapNodesInPairs_24/ListNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.SwapNodesInPairs_24; 2 | 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode(int x) { val = x; } 7 | 8 | @Override 9 | public String toString() { 10 | StringBuilder sb = new StringBuilder(); 11 | ListNode current = this; 12 | while (current != null) { 13 | sb.append(current.val + "->"); 14 | current = current.next; 15 | } 16 | return sb.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /linkedList/SwapNodesInPairs_24/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.linkedList.SwapNodesInPairs_24; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | ListNode a = new ListNode(1); 6 | ListNode b = new ListNode(2); 7 | ListNode c = new ListNode(3); 8 | ListNode d = new ListNode(4); 9 | ListNode e = new ListNode(5); 10 | 11 | a.next = b; 12 | b.next = c; 13 | c.next = d; 14 | d.next = e; 15 | 16 | Solution s = new Solution(); 17 | ListNode result = s.swapPairs(a); 18 | System.out.print(result); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /math/AddStrings_415/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.AddStrings_415; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.addStrings("99", "955"); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/Base7_504/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.Base7_504; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.convertToBase7(7); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/BulbSwitcher_319/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.BulbSwitcher_319; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.bulbSwitch(8) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/CountNumbersWithUniqueDigits_357/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.CountNumbersWithUniqueDigits_357; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.countNumbersWithUniqueDigits(3) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/ExcelSheetColumnTitle_168/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.ExcelSheetColumnTitle_168; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.convertToTitle(27) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/FactorialTrailingZeroes_172/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.FactorialTrailingZeroes_172; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.trailingZeroes(10) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/FractionToRecurringDecimal_166/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.FractionToRecurringDecimal_166; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.fractionToDecimal(-1, -2147483648) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/IntegerBreak_343/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.IntegerBreak_343; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.integerBreak(10) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/IntegerToRoman_12/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.IntegerToRoman_12; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.intToRoman(1964) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/LineReflection_356/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.LineReflection_356; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[][] points = new int[][]{ 7 | {1,1}, {-1,1} 8 | }; 9 | boolean result = s.isReflected(points) ; 10 | System.out.println(result); 11 | } 12 | } -------------------------------------------------------------------------------- /math/MaxPointsOnALine_149/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.MaxPointsOnALine_149; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Point[] points = new Point[]{new Point(4,0), new Point(4,-1), new Point(4,5)}; 8 | Solution s = new Solution(); 9 | int res = s.maxPoints(points); 10 | System.out.println(res); 11 | PriorityQueue pq = new PriorityQueue<>(10, (a,b)-> a.second-b.second); 12 | } 13 | } -------------------------------------------------------------------------------- /math/MaxPointsOnALine_149/Point.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.MaxPointsOnALine_149; 2 | 3 | /** 4 | * Created by Sergii on 10.03.2017. 5 | */ 6 | class Point { 7 | int x; 8 | int y; 9 | Point() { x = 0; y = 0; } 10 | Point(int a, int b) { 11 | x = a; y = b; 12 | } 13 | } -------------------------------------------------------------------------------- /math/MaxPointsOnALine_149/Tuple.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.MaxPointsOnALine_149; 2 | 3 | /** 4 | * Created by Sergii on 10.03.2017. 5 | */ 6 | public class Tuple { 7 | int first = 0; 8 | int second = 0; 9 | 10 | public Tuple(int first, int second) { 11 | this.first = first; 12 | this.second = second; 13 | } 14 | 15 | @Override 16 | public int hashCode() { 17 | return (this.first + "-" + this.second).hashCode(); 18 | } 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | boolean isTuple = o instanceof Tuple; 23 | if (!isTuple) return false; 24 | Tuple t = (Tuple)o; 25 | return this.first == t.first && this.second == t.second; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /math/MultiplyStrings_43/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.MultiplyStrings_43; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.multiply("999", "0") ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/NumberOfBoomerangs_447/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.NumberOfBoomerangs_447; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.numberOfBoomerangs(new int[][]{{0,1}, {1,1}, {1,0}}) ; 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /math/PalindromeNumber_9/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.PalindromeNumber_9; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isPalindrome(12345) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/PowXN_50/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.PowXN_50; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | double result = s.myPow(2.0, -2147483648); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/RandomPickIndex_398/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.RandomPickIndex_398; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(new int[] {1,2,2,3,3,3}); 6 | System.out.println(s.pick(3)); 7 | System.out.println(s.pick(3)); 8 | System.out.println(s.pick(3)); 9 | System.out.println(s.pick(3)); 10 | System.out.println(s.pick(3)); 11 | System.out.println(s.pick(3)); 12 | System.out.println(s.pick(3)); 13 | } 14 | } -------------------------------------------------------------------------------- /math/RectangleArea_223/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.RectangleArea_223; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.computeArea(-2,-2,2,2,-3,-3,3,-1); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/ReverseInteger_7/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.ReverseInteger_7; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.reverse(-12345) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/SelfCrossing_335/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.SelfCrossing_335; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isSelfCrossing(new int[]{2,1,4,4,3,3,2,1,1}) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /math/StringToIntegerAtoi_8/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.StringToIntegerAtoi_8; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.myAtoi("-2147483647") ; 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /math/ValidPerfectSquare_367/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.math.ValidPerfectSquare_367; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | //boolean result = s.isPerfectSquare(2147483647) ; 7 | boolean result = s.isPerfectSquare(16); 8 | System.out.println(result); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /sorting/HeapSort/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.sorting.HeapSort; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int[] array = new int[]{1,4,2,5,6,4,0,7,8,9,1,2,3,7,6,8}; 6 | HeapSort.sort(array); 7 | for(int i : array) System.out.println(i); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /sorting/QuickSort/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.sorting.QuickSort; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by Sergii on 01.01.2017. 8 | */ 9 | public class Main { 10 | public static void main(String[] args) { 11 | Integer[] array = new Integer[]{1,4,2,5,6,4,0,7,8,9,1,2,3,7,6,8}; 12 | List list = Arrays.asList(array); 13 | QuickSort.sort(list); 14 | System.out.println(list); 15 | 16 | List list1 = Arrays.asList(array); 17 | int el = QuickSelection.getKthElement(list1, 3); 18 | System.out.println(el); 19 | 20 | List list2 = Arrays.asList(array); 21 | ThreeWayPartitioning.sort(list2); 22 | System.out.println(list2); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stack/BasicCalculator_224/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.BasicCalculator_224; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int res = s.calculate("2147483647"); 7 | System.out.print(res); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /stack/BinaryTreeInorderTraversal_94/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.BinaryTreeInorderTraversal_94; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | TreeNode root = new TreeNode(1); 8 | root.right = new TreeNode(2); 9 | root.right.left = new TreeNode(3); 10 | Solution s = new Solution(); 11 | List result = s.inorderTraversal(root); 12 | System.out.print(result); 13 | } 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /stack/BinaryTreeInorderTraversal_94/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.BinaryTreeInorderTraversal_94; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stack/DecodeString_394/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.DecodeString_394; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.decodeString("10[a]20[b]"); 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /stack/EvaluateReversePolishNotation_150/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.EvaluateReversePolishNotation_150; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String[] n = new String[] {"2", "1", "+", "3", "*"}; 7 | int result = s.evalRPN(n) ; 8 | System.out.println(result); 9 | } 10 | } -------------------------------------------------------------------------------- /stack/FlattenNestedListIterator_341/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.FlattenNestedListIterator_341; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | List nestedList = new ArrayList<>(); 9 | NestedIterator i = new NestedIterator(nestedList); 10 | while (i.hasNext()) { 11 | System.out.print(i.next()); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stack/ImplementQueueUsingStacks_232/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.ImplementQueueUsingStacks_232; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | MyQueue q = new MyQueue(); 6 | q.push(1); 7 | q.push(2); 8 | q.push(3); 9 | System.out.print(q.pop() + " " + q.pop() + " " + q.pop()); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /stack/ImplementStackUsingQueues_225/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.ImplementStackUsingQueues_225; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | MyStack stack = new MyStack(); 6 | stack.push(1); 7 | stack.push(2); 8 | stack.push(3); 9 | System.out.print(stack.top()); 10 | stack.pop(); 11 | System.out.print(stack.top()); 12 | stack.pop(); 13 | System.out.print(stack.top()); 14 | stack.pop(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stack/MinStack_155/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.MinStack_155; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | MinStack stack = new MinStack(); 6 | stack.push(-2); 7 | stack.push(0); 8 | stack.push(-3); 9 | System.out.print(stack.getMin()); 10 | stack.pop(); 11 | stack.top(); 12 | System.out.print(stack.getMin()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stack/MiniParser_385/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.MiniParser_385; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | NestedInteger ni = s.deserialize("345"); 7 | System.out.print(ni); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /stack/SimplifyPath_71/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.SimplifyPath_71; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.simplifyPath("/a/./b/../../c/") ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /stack/TrappingRainWater_42/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.stack.TrappingRainWater_42; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.trap(new int[] {0,1,0,2,1,0,1,3,2,1,2,1}) ; 7 | System.out.println(result); 8 | } 9 | } -------------------------------------------------------------------------------- /string/AddBinary_67/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.AddBinary_67; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.addBinary("0", "0"); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/BasicCalculator2_227/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.BasicCalculator2_227; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.calculate(" 3+5 / 2 "); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/CompareVersionNumbers_165/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.CompareVersionNumbers_165; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.compareVersion1("2.101", "2.12"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/CountAndSay_38/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.CountAndSay_38; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.countAndSay(10); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/CountBinarySubstrings_696/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.CountBinarySubstrings_696; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.countBinarySubstrings("asdfasd00110011adfasdf"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/EncodeAndDecodeStrings_271/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.EncodeAndDecodeStrings_271; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Codec s = new Codec(); 9 | List source = new ArrayList<>(); 10 | source.add("abc"); 11 | source.add("def"); 12 | String result = s.encode(source); 13 | source = s.decode(result); 14 | System.out.print(source); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /string/FirstUniqueCharacterInAString_387/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.FirstUniqueCharacterInAString_387; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.firstUniqChar("loveleetcode"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/FizzBuzz_412/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.FizzBuzz_412; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List result = s.fizzBuzz(15); 9 | System.out.println(result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /string/GenerateParentheses_22/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.GenerateParentheses_22; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Solution s = new Solution(); 9 | List result = s.generateParenthesis(2); 10 | System.out.println(result); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /string/ImplementStrStr_28/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.ImplementStrStr_28; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.strStr("mississippi", "ssip"); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/IntegerToEnglishWords_273/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.IntegerToEnglishWords_273; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.numberToWords(1000000); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/JudgeRouteCircle_657/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.JudgeRouteCircle_657; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.judgeCircle("UD"); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/LengthOfLastWord_58/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.LengthOfLastWord_58; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.lengthOfLastWord("Hello World"); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/LicenseKeyFormatting_482/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.LicenseKeyFormatting_482; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.licenseKeyFormatting("2-4A0r7-4k", 3); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/LongestAbsoluteFilePath_388/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.LongestAbsoluteFilePath_388; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.lengthLongestPath("dir\n file.txt"); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/LongestCommonPrefix_14/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.LongestCommonPrefix_14; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String[] strs = {"aab1", "aab", "aa"}; 7 | String result = s.longestCommonPrefix(strs); 8 | System.out.println(result); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /string/LongestPalindromicSubstring_5/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.LongestPalindromicSubstring_5; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.longestPalindrome("bb"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/LongestSubstringWithAtMostTwoDistinctCharacters_159/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.LongestSubstringWithAtMostTwoDistinctCharacters_159; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.lengthOfLongestSubstringTwoDistinct("abaccc"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/OneEditDistance_161/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.OneEditDistance_161; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isOneEditDistance("aba", "aca"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/RansomNote_383/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.RansomNote_383; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.canConstruct("bb", "ababa"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/ReadNCharactersGivenRead4_157/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.ReadNCharactersGivenRead4_157; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.read(new char[]{}, 10); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/ReadNCharactersGivenRead4_2CallMultipleTimes_158/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.ReadNCharactersGivenRead4_2CallMultipleTimes_158; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int result = s.read(new char[]{}, 10); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/RegularExpressionMatching_10/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.RegularExpressionMatching_10; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isMatch("bbbba", ".*a*a"); 7 | System.out.println(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/RemoveInvalidParentheses_301/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.RemoveInvalidParentheses_301; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | List result = s.removeInvalidParentheses("()())()"); 9 | System.out.println(result); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /string/ReverseWordsInAString_151/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.ReverseWordsInAString_151; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.reverseWords(" "); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/ValidPalindrome_125/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.ValidPalindrome_125; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isPalindrome("0P"); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/ValidParentheses_20/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.ValidParentheses_20; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | boolean result = s.isValid("}"); 7 | System.out.print(result); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /string/ZigZagConversion_6/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.string.ZigZagConversion_6; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | String result = s.convert("PAYPALISHIRING", 4);// should return "PINALSIGYAHRPI" 7 | System.out.print(result); 8 | } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /tree/BalancedBinaryTree_110/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BalancedBinaryTree_110; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | root.left.right.left = new TreeNode(10); 12 | 13 | boolean result = s.isBalanced(root); 14 | System.out.print(result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BalancedBinaryTree_110/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BalancedBinaryTree_110; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinarySearchTreeIterator_173/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinarySearchTreeIterator_173; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | TreeNode root = new TreeNode(5); 6 | root.left = new TreeNode(4); 7 | root.right = new TreeNode(6); 8 | root.left.left = new TreeNode(2); 9 | root.left.right = new TreeNode(3); 10 | root.right.right = new TreeNode(9); 11 | 12 | BSTIterator i = new BSTIterator(root); 13 | while (i.hasNext()) { 14 | System.out.println(i.next()); 15 | }; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/BinarySearchTreeIterator_173/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinarySearchTreeIterator_173; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeLevelOrderTraversal2_107/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeLevelOrderTraversal2_107; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(4); 10 | root.right = new TreeNode(6); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(3); 13 | root.right.right = new TreeNode(9); 14 | 15 | List> count = s.levelOrderBottom(root); 16 | System.out.print(count); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tree/BinaryTreeLevelOrderTraversal2_107/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeLevelOrderTraversal2_107; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeLevelOrderTraversal_102/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeLevelOrderTraversal_102; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(4); 10 | root.right = new TreeNode(6); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(3); 13 | root.left.right.left = new TreeNode(10); 14 | 15 | List> result = s.levelOrder(root); 16 | System.out.print(result); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tree/BinaryTreeLevelOrderTraversal_102/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeLevelOrderTraversal_102; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeLongestConsecutiveSequence_298/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeLongestConsecutiveSequence_298; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(1); 7 | root.left = new TreeNode(3); 8 | root.right = new TreeNode(2); 9 | root.left.left = new TreeNode(7); 10 | root.left.right = new TreeNode(4); 11 | root.left.right.left = new TreeNode(5); 12 | 13 | int result = s.longestConsecutive(root); 14 | System.out.print(result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeLongestConsecutiveSequence_298/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeLongestConsecutiveSequence_298; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreePaths_257/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreePaths_257; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(4); 10 | root.right = new TreeNode(6); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(3); 13 | root.right.right = new TreeNode(9); 14 | 15 | List result = s.binaryTreePaths(root); 16 | System.out.print(result); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tree/BinaryTreePaths_257/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreePaths_257; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreePreorderTraversal_144/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreePreorderTraversal_144; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(4); 10 | root.right = new TreeNode(6); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(3); 13 | 14 | List count = s.preorderTraversal(root); 15 | System.out.print(count); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/BinaryTreePreorderTraversal_144/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreePreorderTraversal_144; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeRightSideView_199/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeRightSideView_199; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(3); 10 | root.right = new TreeNode(7); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(4); 13 | root.right.right = new TreeNode(9); 14 | 15 | List result = s.rightSideView(root); 16 | System.out.print(result); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tree/BinaryTreeRightSideView_199/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeRightSideView_199; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeUpsideDown_156_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeUpsideDown_156_; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(3); 10 | root.right = new TreeNode(7); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(4); 13 | 14 | TreeNode result = s.upsideDownBinaryTree(root); 15 | System.out.println(result); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/BinaryTreeUpsideDown_156_/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeUpsideDown_156_; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeVerticalOrderTraversal_314/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeVerticalOrderTraversal_314; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(3); 10 | root.right = new TreeNode(7); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(4); 13 | root.right.right = new TreeNode(9); 14 | 15 | List> result = s.verticalOrder(root); 16 | for (List l : result) { 17 | System.out.println(l); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tree/BinaryTreeVerticalOrderTraversal_314/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeVerticalOrderTraversal_314; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/BinaryTreeZigzagLevelOrderTraversal_103/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeZigzagLevelOrderTraversal_103; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(5); 9 | root.left = new TreeNode(3); 10 | root.right = new TreeNode(7); 11 | root.left.left = new TreeNode(2); 12 | root.left.right = new TreeNode(4); 13 | root.right.right = new TreeNode(9); 14 | 15 | List> result = s.zigzagLevelOrder(root); 16 | for (List l : result) { 17 | System.out.println(l); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tree/BinaryTreeZigzagLevelOrderTraversal_103/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.BinaryTreeZigzagLevelOrderTraversal_103; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/ClosestBinarySearchTreeValue_270/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ClosestBinarySearchTreeValue_270; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(7); 9 | root.left = new TreeNode(3); 10 | root.right = new TreeNode(12); 11 | root.left.left = new TreeNode(1); 12 | root.left.right = new TreeNode(5); 13 | root.right.right = new TreeNode(15); 14 | 15 | int result = s.closestValue(root, 11.5); 16 | System.out.println(result); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tree/ClosestBinarySearchTreeValue_270/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ClosestBinarySearchTreeValue_270; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/ConstructBinaryTreeFromInorderAndPostorderTraversal_106/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ConstructBinaryTreeFromInorderAndPostorderTraversal_106; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] inorder = {3,2,1}; 7 | int[] postorder = {3,2,1}; 8 | TreeNode root = s.buildTree(inorder, postorder); 9 | System.out.print(root); 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /tree/ConstructBinaryTreeFromInorderAndPostorderTraversal_106/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ConstructBinaryTreeFromInorderAndPostorderTraversal_106; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/ConstructBinaryTreeFromPreorderAndInorderTraversal_105/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ConstructBinaryTreeFromPreorderAndInorderTraversal_105; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | int[] inorder = {2,5,1,3,6,4}; 7 | int[] preorder = {1,5,3,2,6,4}; 8 | TreeNode root = s.buildTree(inorder, preorder); 9 | System.out.print(root); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tree/ConstructBinaryTreeFromPreorderAndInorderTraversal_105/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ConstructBinaryTreeFromPreorderAndInorderTraversal_105; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/ConvertSortedArrayToBinarySearchTree_108/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ConvertSortedArrayToBinarySearchTree_108; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = s.sortedArrayToBST(new int[]{1,2,3,4,5}); 7 | System.out.print(root); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tree/ConvertSortedArrayToBinarySearchTree_108/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ConvertSortedArrayToBinarySearchTree_108; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/CountCompleteTreeNodes_222/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.CountCompleteTreeNodes_222; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | 12 | int count = s.countNodes(root); 13 | System.out.print(count); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tree/CountCompleteTreeNodes_222/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.CountCompleteTreeNodes_222; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/FlattenBinaryTreeToLinkedList_114/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.FlattenBinaryTreeToLinkedList_114; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | 12 | s.flatten(root); 13 | System.out.print(root); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tree/FlattenBinaryTreeToLinkedList_114/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.FlattenBinaryTreeToLinkedList_114; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/InorderSuccessorInBST_285_/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.InorderSuccessorInBST_285_; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | 12 | TreeNode t = s.inorderSuccessor(root, root); 13 | System.out.print(t); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tree/InorderSuccessorInBST_285_/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.InorderSuccessorInBST_285_; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/LowestCommonAncestorOfABinarySearchTree_235/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.LowestCommonAncestorOfABinarySearchTree_235; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | root.left.right.left = new TreeNode(10); 12 | 13 | TreeNode result = s.lowestCommonAncestor(root, root.left, root.right); 14 | System.out.print(result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/LowestCommonAncestorOfABinarySearchTree_235/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.LowestCommonAncestorOfABinarySearchTree_235; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/LowestCommonAncestorOfABinaryTree_236/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.LowestCommonAncestorOfABinaryTree_236; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | root.left.right.left = new TreeNode(10); 12 | 13 | TreeNode result = s.lowestCommonAncestor(root, root.left, root.right); 14 | System.out.print(result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/LowestCommonAncestorOfABinaryTree_236/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.LowestCommonAncestorOfABinaryTree_236; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/MergeTwoBinaryTrees_617/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.MergeTwoBinaryTrees_617; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | root.left.right.left = new TreeNode(10); 12 | 13 | TreeNode root1 = new TreeNode(1); 14 | root1.left = new TreeNode(1); 15 | root1.right = new TreeNode(1); 16 | 17 | TreeNode result = s.mergeTrees(root, root1); 18 | System.out.print(result); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tree/MergeTwoBinaryTrees_617/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.MergeTwoBinaryTrees_617; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/MinimumDepthOfBinaryTree_111/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.MinimumDepthOfBinaryTree_111; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(4); 8 | root.right = new TreeNode(6); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(3); 11 | root.left.right.left = new TreeNode(10); 12 | 13 | int result = s.minDepth(root); 14 | System.out.print(result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/MinimumDepthOfBinaryTree_111/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.MinimumDepthOfBinaryTree_111; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/PathSum_112/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.PathSum_112; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(1); 7 | root.left = new TreeNode(2); 8 | root.right = new TreeNode(2); 9 | root.left.left = new TreeNode(3); 10 | root.left.right = new TreeNode(4); 11 | root.right.left = new TreeNode(4); 12 | root.right.right = new TreeNode(3); 13 | 14 | boolean res = s.hasPathSum(root, 7); 15 | System.out.print(res); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/PathSum_112/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.PathSum_112; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/PathSum_113/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.PathSum_113; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Solution s = new Solution(); 8 | TreeNode root = new TreeNode(1); 9 | root.left = new TreeNode(2); 10 | root.right = new TreeNode(2); 11 | root.left.left = new TreeNode(3); 12 | root.left.right = new TreeNode(4); 13 | root.right.left = new TreeNode(4); 14 | root.right.right = new TreeNode(3); 15 | 16 | List> res = s.pathSum(root, 7); 17 | System.out.print(res); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tree/PathSum_113/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.PathSum_113; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/PopulatingNextRightPointersInEachNode_116/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.PopulatingNextRightPointersInEachNode_116; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeLinkNode root = new TreeLinkNode(1); 7 | root.left = new TreeLinkNode(2); 8 | root.right = new TreeLinkNode(2); 9 | root.left.left = new TreeLinkNode(3); 10 | root.left.right = new TreeLinkNode(4); 11 | root.right.left = new TreeLinkNode(4); 12 | root.right.right = new TreeLinkNode(3); 13 | 14 | s.connect(root); 15 | System.out.print(root); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/PopulatingNextRightPointersInEachNode_116/TreeLinkNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.PopulatingNextRightPointersInEachNode_116; 2 | 3 | public class TreeLinkNode { 4 | int val; 5 | TreeLinkNode left; 6 | TreeLinkNode right; 7 | TreeLinkNode next; 8 | TreeLinkNode(int x) { val = x; } 9 | 10 | @Override 11 | public String toString() { 12 | if (this == null) return "NULL"; 13 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 14 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 15 | String nextStr = this.next != null ? this.next.toString() : "NULL"; 16 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr + " NEXT: " + nextStr; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tree/SumOfNodesWithEvenValuedGrandparent_1315/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SumOfNodesWithEvenValuedGrandparent_1315; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(1); 7 | root.left = new TreeNode(2); 8 | root.right = new TreeNode(2); 9 | root.left.left = new TreeNode(3); 10 | root.left.right = new TreeNode(4); 11 | root.right.left = new TreeNode(4); 12 | root.right.right = new TreeNode(3); 13 | 14 | int res = s.sumEvenGrandparent(root); 15 | System.out.print(res); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/SumOfNodesWithEvenValuedGrandparent_1315/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SumOfNodesWithEvenValuedGrandparent_1315; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/SumRootToLeafNumbers_129/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SumRootToLeafNumbers_129; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(1); 7 | root.left = new TreeNode(2); 8 | root.right = new TreeNode(2); 9 | root.left.left = new TreeNode(3); 10 | root.left.right = new TreeNode(4); 11 | root.right.left = new TreeNode(4); 12 | root.right.right = new TreeNode(3); 13 | 14 | int res = s.sumNumbers(root); 15 | System.out.print(res); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/SumRootToLeafNumbers_129/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SumRootToLeafNumbers_129; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/SumofLeftLeaves_404/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SumofLeftLeaves_404; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(1); 7 | root.left = new TreeNode(2); 8 | root.right = new TreeNode(2); 9 | root.left.left = new TreeNode(3); 10 | root.left.right = new TreeNode(4); 11 | root.right.left = new TreeNode(4); 12 | root.right.right = new TreeNode(3); 13 | 14 | int res = s.sumOfLeftLeaves(root); 15 | System.out.print(res); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/SumofLeftLeaves_404/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SumofLeftLeaves_404; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/SymmetricTree_101/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SymmetricTree_101; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(1); 7 | root.left = new TreeNode(2); 8 | root.right = new TreeNode(2); 9 | root.left.left = new TreeNode(3); 10 | root.left.right = new TreeNode(4); 11 | root.right.left = new TreeNode(4); 12 | root.right.right = new TreeNode(3); 13 | 14 | boolean res = s.isSymmetric(root); 15 | System.out.print(res); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/SymmetricTree_101/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.SymmetricTree_101; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/TwoSum4InputIsBST_653/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.TwoSum4InputIsBST_653; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(1); 7 | root.left = new TreeNode(2); 8 | root.right = new TreeNode(2); 9 | root.left.left = new TreeNode(3); 10 | root.left.right = new TreeNode(4); 11 | root.right.left = new TreeNode(4); 12 | root.right.right = new TreeNode(3); 13 | 14 | boolean res = s.findTarget(root, 7); 15 | System.out.print(res); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tree/TwoSum4InputIsBST_653/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.TwoSum4InputIsBST_653; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/ValidateBinarySearchTree_98/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ValidateBinarySearchTree_98; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | TreeNode root = new TreeNode(5); 7 | root.left = new TreeNode(3); 8 | root.right = new TreeNode(7); 9 | root.left.left = new TreeNode(2); 10 | root.left.right = new TreeNode(4); 11 | root.right.right = new TreeNode(9); 12 | 13 | boolean result = s.isValidBST(root); 14 | System.out.print(result); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tree/ValidateBinarySearchTree_98/TreeNode.java: -------------------------------------------------------------------------------- 1 | package LeetCode.tree.ValidateBinarySearchTree_98; 2 | 3 | public class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | TreeNode(int x) { val = x; } 8 | 9 | @Override 10 | public String toString() { 11 | if (this == null) return "NULL"; 12 | String leftStr = this.left != null ? this.left.toString() : "NULL"; 13 | String rightStr = this.right != null ? this.right.toString() : "NULL"; 14 | return "ROOT: " + val + " LEFT: " + leftStr + " RIGHT: " + rightStr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /unionFind/NumberOfIslands_200/Main.java: -------------------------------------------------------------------------------- 1 | package LeetCode.unionFind.NumberOfIslands_200; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Solution s = new Solution(); 6 | char[][] grid = new char[][]{{'0','0','1'}, {'0', '1', '1'}, {'1', '0', '0'}}; 7 | int result = s.numIslands(grid); 8 | System.out.print(result); 9 | } 10 | } 11 | --------------------------------------------------------------------------------