add more solutions and explanation
This commit is contained in:
commit
7a13e40df4
230
README.md
230
README.md
@ -8,6 +8,7 @@ LeetCode
|
||||
|
||||
| # | Title | Solution | Difficulty |
|
||||
|---| ----- | -------- | ---------- |
|
||||
<<<<<<< HEAD
|
||||
|285|[Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst/) ♥ | [Java](./algorithms/java/src/inorderSuccessorInBST/inorderSuccessorInBST.java)|Medium|
|
||||
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)| [Java](./algorithms/java/src/firstBadVersion/firstBadVersion.java)|Easy|
|
||||
|226|[Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [C++](./algorithms/cpp/anagrams/ValidAnagram.cpp)|Easy|
|
||||
@ -236,6 +237,235 @@ LeetCode
|
||||
|3|[Longest Substring Without Repeating Characters](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/)| [C++](./algorithms/cpp/longestSubstringWithoutRepeatingCharacters/longestSubstringWithoutRepeatingCharacters.cpp)|Medium|
|
||||
|2|[Median of Two Sorted Arrays](https://oj.leetcode.com/problems/median-of-two-sorted-arrays/)| [C++](./algorithms/cpp/medianOfTwoSortedArrays/medianOfTwoSortedArrays.cpp)|Hard|
|
||||
|1|[Two Sum](https://oj.leetcode.com/problems/two-sum/)| [C++](./algorithms/cpp/twoSum/twoSum.cpp)|Medium|
|
||||
=======
|
||||
|258|[Add Digits](https://leetcode.com/problems/add-digits/)| [C++](./algorithms/addDigits/addDigits.cpp)|Easy|
|
||||
|226|[Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [C++](./algorithms/anagrams/ValidAnagram.cpp)|Easy|
|
||||
|225|[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)|[C++](./algorithms/differentWaysToAddParentheses/DifferentWaysToAddParentheses.cpp)|Medium|
|
||||
|224|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)|[C++](./algorithms/search2DMatrix/search2DMatrix.II.cpp)|Medium|
|
||||
|223|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)| [C++](./algorithms/slidingWindowMaximum/SlidingWindowMaximum.cpp)|Hard|
|
||||
|222|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| [C++](./algorithms/productOfArrayExceptSelf/ProductOfArrayExceptSelf.cpp)|Medium|
|
||||
|221|[Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)| [C++](./algorithms/deleteNodeInALinkedList/DeleteNodeInALinkedList.cpp)|Easy|
|
||||
|220|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| [C++](./algorithms/lowestCommonAncestorOfABinaryTree/LowestCommonAncestorOfABinaryTree.cpp)|Medium|
|
||||
|219|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| [C++](./algorithms/lowestCommonAncestorOfABinarySearchTree/LowestCommonAncestorOfABinarySearchTree.cpp)|Easy|
|
||||
|218|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| [C++](./algorithms/palindromeLinkedList/PalindromeLinkedList.cpp)|Easy|
|
||||
|217|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)| [C++](./algorithms/numberOfDigitOne/NumberOfDigitOne.cpp)|Medium|
|
||||
|216|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)| [C++](./algorithms/implementQueueUsingStacks/ImplementQueueUsingStacks.cpp)|Easy|
|
||||
|215|[Power of Two](https://leetcode.com/problems/power-of-two/)| [C++](./algorithms/powerOfTwo/PowerOfTwo.cpp)|Easy|
|
||||
|214|[Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| [C++](./algorithms/kthSmallestElementInaBST/KthSmallestElementInABst.cpp)|Medium|
|
||||
|213|[Majority Element II](https://oj.leetcode.com/problems/majority-element-ii/) | [C++](./algorithms/majorityElement/majorityElement.II.cpp)|Medium|
|
||||
|212|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [C++](./algorithms/summaryRanges/SummaryRanges.cpp)|Easy|
|
||||
|211|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)| [C++](./algorithms/basicCalculator/BasicCalculator.II.cpp)|Medium|
|
||||
|210|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [C++](./algorithms/invertBinaryTree/InvertBinaryTree.cpp)|Easy|
|
||||
|209|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)| [C++](./algorithms/implementStackUsingQueues/ImplementStackUsingQueues.cpp)|Medium|
|
||||
|208|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)| [C++](./algorithms/basicCalculator/BasicCalculator.cpp)|Medium|
|
||||
|207|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| [C++](./algorithms/rectangleArea/RectangleArea.cpp)|Easy|
|
||||
|206|[Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)| [C++](./algorithms/countCompleteTreeNodes/CountCompleteTreeNodes.cpp)|Medium|
|
||||
|205|[Maximal Square](https://leetcode.com/problems/maximal-square/)| [C++](./algorithms/maximalSquare/MaximalSquare.cpp)|Medium|
|
||||
|204|[Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)| [C++](./algorithms/containsDuplicate/ContainsDuplicate.III.cpp)|Medium|
|
||||
|203|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)| [C++](./algorithms/containsDuplicate/ContainsDuplicate.II.cpp)|Easy|
|
||||
|202|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)| [C++](./algorithms/theSkylineProblem/TheSkylineProblem.cpp)|Hard|
|
||||
|201|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)| [C++](./algorithms/containsDuplicate/ContainsDuplicate.cpp)|Easy|
|
||||
|200|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [C++](./algorithms/combinationSum/combinationSum.III.cpp)|Medium|
|
||||
|199|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)| [C++](./algorithms/kthLargestElementInAnArray/KthLargestElementInAnArray.cpp)|Medium|
|
||||
|198|[Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/)| [C++](./algorithms/shortestPalindrome/ShortestPalindrome.cpp)|Hard|
|
||||
|197|[House Robber II](https://leetcode.com/problems/house-robber-ii/)| [C++](./algorithms/houseRobber/houseRobber.II.cpp)|Medium|
|
||||
|196|[Word Search II](https://leetcode.com/problems/word-search-ii/)| [C++](./algorithms/wordSearch/wordSearch.II.cpp)|Hard|
|
||||
|195|[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)| [C++](./algorithms/addAndSearchWord/AddAndSearchWord.cpp)|Medium|
|
||||
|194|[Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)| [C++](./algorithms/courseSchedule/CourseSchedule.II.cpp)|Medium|
|
||||
|193|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| [C++](./algorithms/minimumSizeSubarraySum/MinimumSizeSubarraySum.cpp)|Medium|
|
||||
|192|[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)| [C++](./algorithms/implementTriePrefixTree/ImplementTriePrefixTree.cpp)|Medium|
|
||||
|191|[Course Schedule](https://leetcode.com/problems/course-schedule/)| [C++](./algorithms/courseSchedule/CourseSchedule.cpp)|Medium|
|
||||
|190|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [C++](./algorithms/reverseLinkedList/reverseLinkedList.cpp)|Easy|
|
||||
|189|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)| [C++](./algorithms/isomorphicStrings/IsomorphicStrings.cpp)|Easy|
|
||||
|188|[Count Primes](https://leetcode.com/problems/count-primes/)| [C++](./algorithms/countPrimes/CountPrimes.cpp)|Easy|
|
||||
|187|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/)| [C++](./algorithms/removeLinkedListElements/RemoveLinkedListElements.cpp)|Easy|
|
||||
|186|[Happy Number](https://leetcode.com/problems/happy-number/)| [C++](./algorithms/happyNumber/HappyNumber.cpp)|Easy|
|
||||
|185|[Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/)| [C++](./algorithms/bitwiseANDOfNumbersRange/BitwiseAndOfNumbersRange.cpp)|Medium|
|
||||
|184|[Number of Islands](https://leetcode.com/problems/number-of-islands/)| [C++](./algorithms/numberOfIslands/NumberOfIslands.cpp)|Medium|
|
||||
|183|[Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/)| [C++](./algorithms/binaryTreeRightSideView/binaryTreeRightSideView.cpp)|Medium|
|
||||
|182|[House Robber](https://leetcode.com/problems/house-robber/)| [C++](./algorithms/houseRobber/houseRobber.cpp)|Easy|
|
||||
|181|[Number of 1 Bits](https://oj.leetcode.com/problems/number-of-1-bits/)| [C++](./algorithms/numberOf1Bits/numberOf1Bits.cpp)|Easy|
|
||||
|180|[Reverse Bits](https://oj.leetcode.com/problems/reverse-bits/)| [C++](./algorithms/reverseBits/reverseBits.cpp)|Easy|
|
||||
|179|[Rotate Array](https://oj.leetcode.com/problems/rotate-array/)| [C++](./algorithms/rotateArray/rotateArray.cpp)|Easy|
|
||||
|178|[Best Time to Buy and Sell Stock IV](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| [C++](./algorithms/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.IV.cpp)|Hard|
|
||||
|177|[Repeated DNA Sequences](https://oj.leetcode.com/problems/repeated-dna-sequences/)| [C++](./algorithms/repeatedDNASequences/repeatedDNASequences.cpp)|Medium|
|
||||
|176|[Reverse Words in a String II](https://oj.leetcode.com/problems/reverse-words-in-a-string-ii/) ♥ | [C++](./algorithms/reverseWordsInAString/reverseWordsInAString.II.cpp)|Medium|
|
||||
|175|[Largest Number](https://oj.leetcode.com/problems/largest-number/) | [C++](./algorithms/largestNumber/largestNumber.cpp)|Medium|
|
||||
|174|[Dungeon Game](https://oj.leetcode.com/problems/dungeon-game/) | [C++](./algorithms/dungeonGame/dungeonGame.cpp)|Hard|
|
||||
|173|[Binary Search Tree Iterator](https://oj.leetcode.com/problems/binary-search-tree-iterator/) | [C++](./algorithms/binarySearchTreeIterator/binarySearchTreeIterator.cpp)|Medium|
|
||||
|172|[Factorial Trailing Zeroes](https://oj.leetcode.com/problems/factorial-trailing-zeroes/) | [C++](./algorithms/factorialTrailingZeroes/factorialTrailingZeroes.cpp)|Easy|
|
||||
|171|[Excel Sheet Column Number](https://oj.leetcode.com/problems/excel-sheet-column-number/) | [C++](./algorithms/excelSheetColumnNumber/excelSheetColumnNumber.cpp)|Easy|
|
||||
|170|[Two Sum III - Data structure design](https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/) ♥ | [C++](./algorithms/twoSum/twoSum.III.cpp)|Easy|
|
||||
|169|[Majority Element](https://oj.leetcode.com/problems/majority-element/) | [C++](./algorithms/majorityElement/majorityElement.cpp)|Easy|
|
||||
|168|[Excel Sheet Column Title](https://oj.leetcode.com/problems/excel-sheet-column-title/) | [C++](./algorithms/excelSheetColumnTitle/excelSheetColumnTitle.cpp)|Easy|
|
||||
|167|[Two Sum II - Input array is sorted](https://oj.leetcode.com/problems/two-sum-ii-input-array-is-sorted/) ♥ | [C++](./algorithms/twoSum/twoSum.II.cpp)|Medium|
|
||||
|166|[Fraction to Recurring Decimal](https://oj.leetcode.com/problems/fraction-to-recurring-decimal/) | [C++](./algorithms/fractionToRecurringDecimal/fractionToRecurringDecimal.cpp)|Medium|
|
||||
|165|[Compare Version Numbers](https://oj.leetcode.com/problems/compare-version-numbers/) | [C++](./algorithms/compareVersionNumbers/compareVersionNumbers.cpp)|Easy|
|
||||
|164|[Maximum Gap](https://oj.leetcode.com/problems/maximum-gap/) | [C++](./algorithms/maximumGap/maximumGap.cpp)|Hard|
|
||||
|163|[Missing Ranges](https://oj.leetcode.com/problems/missing-ranges/) ♥ | [C++](./algorithms/missingRanges/missingRanges.cpp)|Medium|
|
||||
|162|[Find Peak Element](https://oj.leetcode.com/problems/find-peak-element/) | [C++](./algorithms/findPeakElement/findPeakElement.cpp)|Medium|
|
||||
|161|[One Edit Distance](https://oj.leetcode.com/problems/one-edit-distance/)♥ | [C++](./algorithms/oneEditDistance/oneEditDistance.cpp)|Medium|
|
||||
|160|[Intersection of Two Linked Lists](https://oj.leetcode.com/problems/intersection-of-two-linked-lists/) | [C++](./algorithms/intersectionOfTwoLinkedLists/intersectionOfTwoLinkedLists.cpp)|Easy|
|
||||
|159|[Longest Substring with At Most Two Distinct Characters](https://oj.leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) ♥ | [C++](./algorithms/longestSubstringWithAtMostTwoDistinctCharacters/longestSubstringWithAtMostTwoDistinctCharacters.cpp)|Hard|
|
||||
|158|[Read N Characters Given Read4 II - Call multiple times](https://oj.leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) ♥ | [C++](./algorithms/readNCharactersGivenRead4/readNCharactersGivenRead4.II.cpp)|Hard|
|
||||
|157|[Read N Characters Given Read4](https://oj.leetcode.com/problems/read-n-characters-given-read4/) ♥ | [C++](./algorithms/readNCharactersGivenRead4/readNCharactersGivenRead4.cpp)|Easy|
|
||||
|156|[Binary Tree Upside Down](https://oj.leetcode.com/problems/binary-tree-upside-down/) ♥ | [C++](./algorithms/binaryTreeUpsideDown/binaryTreeUpsideDown.cpp)|Medium|
|
||||
|155|[Min Stack](https://oj.leetcode.com/problems/min-stack/)| [C++](./algorithms/minStack/minStack.cpp)|Easy|
|
||||
|154|[Find Minimum in Rotated Sorted Array II](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| [C++](./algorithms/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.II.cpp)|Hard|
|
||||
|153|[Find Minimum in Rotated Sorted Array](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| [C++](./algorithms/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.cpp)|Medium|
|
||||
|152|[Maximum Product Subarray](https://oj.leetcode.com/problems/maximum-product-subarray/)| [C++](./algorithms/maximumProductSubarray/maximumProductSubarray.cpp)|Medium|
|
||||
|151|[Reverse Words in a String](https://oj.leetcode.com/problems/reverse-words-in-a-string/)| [C++](./algorithms/reverseWordsInAString/reverseWordsInAString.cpp)|Medium|
|
||||
|150|[Evaluate Reverse Polish Notation](https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/)| [C++](./algorithms/evaluateReversePolishNotation/evaluateReversePolishNotation.cpp)|Medium|
|
||||
|149|[Max Points on a Line](https://oj.leetcode.com/problems/max-points-on-a-line/)| [C++](./algorithms/maxPointsOnALine/maxPointsOnALine.cpp)|Hard|
|
||||
|148|[Sort List](https://oj.leetcode.com/problems/sort-list/)| [C++](./algorithms/sortList/sortList.cpp)|Medium|
|
||||
|147|[Insertion Sort List](https://oj.leetcode.com/problems/insertion-sort-list/)| [C++](./algorithms/insertionSortList/insertionSortList.cpp)|Medium|
|
||||
|146|[LRU Cache](https://oj.leetcode.com/problems/lru-cache/)| [C++](./algorithms/LRUCache/LRUCache.cpp)|Hard|
|
||||
|145|[Binary Tree Postorder Traversal](https://oj.leetcode.com/problems/binary-tree-postorder-traversal/)| [C++](./algorithms/binaryTreePostorderTraversal/binaryTreePostorderTraversal.cpp)|Hard|
|
||||
|144|[Binary Tree Preorder Traversal](https://oj.leetcode.com/problems/binary-tree-preorder-traversal/)| [C++](./algorithms/binaryTreePreorderTraversal/binaryTreePreorderTraversal.cpp)|Medium|
|
||||
|143|[Reorder List](https://oj.leetcode.com/problems/reorder-list/)| [C++](./algorithms/reorderList/reorderList.cpp)|Medium|
|
||||
|142|[Linked List Cycle II](https://oj.leetcode.com/problems/linked-list-cycle-ii/)| [C++](./algorithms/linkedListCycle/linkedListCycle.II.cpp)|Medium|
|
||||
|141|[Linked List Cycle](https://oj.leetcode.com/problems/linked-list-cycle/)| [C++](./algorithms/linkedListCycle/linkedListCycle.cpp)|Medium|
|
||||
|140|[Word Break II](https://oj.leetcode.com/problems/word-break-ii/)| [C++](./algorithms/wordBreak/wordBreak.II.cpp)|Hard|
|
||||
|139|[Word Break](https://oj.leetcode.com/problems/word-break/)| [C++](./algorithms/wordBreak/wordBreak.cpp)|Medium|
|
||||
|138|[Copy List with Random Pointer](https://oj.leetcode.com/problems/copy-list-with-random-pointer/)| [C++](./algorithms/copyListWithRandomPointer/copyListWithRandomPointer.cpp)|Hard|
|
||||
|137|[Single Number II](https://oj.leetcode.com/problems/single-number-ii/)| [C++](./algorithms/singleNumber/singleNumber.II.cpp)|Medium|
|
||||
|136|[Single Number](https://oj.leetcode.com/problems/single-number/)| [C++](./algorithms/singleNumber/singleNumber.cpp)|Medium|
|
||||
|135|[Candy](https://oj.leetcode.com/problems/candy/)| [C++](./algorithms/candy/candy.cpp)|Hard|
|
||||
|134|[Gas Station](https://oj.leetcode.com/problems/gas-station/)| [C++](./algorithms/gasStation/gasStation.cpp)|Medium|
|
||||
|133|[Clone Graph](https://oj.leetcode.com/problems/clone-graph/)| [C++](./algorithms/cloneGraph/cloneGraph.cpp)|Medium|
|
||||
|132|[Palindrome Partitioning II](https://oj.leetcode.com/problems/palindrome-partitioning-ii/)| [C++](./algorithms/palindromePartitioning/palindromePartitioning.II.cpp)|Hard|
|
||||
|131|[Palindrome Partitioning](https://oj.leetcode.com/problems/palindrome-partitioning/)| [C++](./algorithms/palindromePartitioning/palindromePartitioning.cpp)|Medium|
|
||||
|130|[Surrounded Regions](https://oj.leetcode.com/problems/surrounded-regions/)| [C++](./algorithms/surroundedRegions/surroundedRegions.cpp)|Medium|
|
||||
|129|[Sum Root to Leaf Numbers](https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/)| [C++](./algorithms/sumRootToLeafNumber/sumRootToLeafNumber.cpp)|Medium|
|
||||
|128|[Longest Consecutive Sequence](https://oj.leetcode.com/problems/longest-consecutive-sequence/)| [C++](./algorithms/longestConsecutiveSequence/longestConsecutiveSequence.cpp)|Hard|
|
||||
|127|[Word Ladder II](https://oj.leetcode.com/problems/word-ladder-ii/)| [C++](./algorithms/wordLadder/wordLadder.II.cpp)|Hard|
|
||||
|126|[Word Ladder](https://oj.leetcode.com/problems/word-ladder/)| [C++](./algorithms/wordLadder/wordLadder.cpp)|Medium|
|
||||
|125|[Valid Palindrome](https://oj.leetcode.com/problems/valid-palindrome/)| [C++](./algorithms/validPalindrome/validPalindrome.cpp)|Easy|
|
||||
|124|[Binary Tree Maximum Path Sum](https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/)| [C++](./algorithms/binaryTreeMaximumPathSum/binaryTreeMaximumPathSum.cpp)|Hard|
|
||||
|123|[Best Time to Buy and Sell Stock III](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [C++](./algorithms/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.III.cpp)|Hard|
|
||||
|122|[Best Time to Buy and Sell Stock II](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [C++](./algorithms/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.II.cpp)|Medium|
|
||||
|121|[Best Time to Buy and Sell Stock](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [C++](./algorithms/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.cpp)|Medium|
|
||||
|120|[Triangle](https://oj.leetcode.com/problems/triangle/)| [C++](./algorithms/triangle/triangle.cpp)|Medium|
|
||||
|119|[Pascal's Triangle II](https://oj.leetcode.com/problems/pascals-triangle-ii/)| [C++](./algorithms/pascalTriangle/pascalTriangle.II.cpp)|Easy|
|
||||
|118|[Pascal's Triangle](https://oj.leetcode.com/problems/pascals-triangle/)| [C++](./algorithms/pascalTriangle/pascalTriangle.cpp)|Easy|
|
||||
|117|[Populating Next Right Pointers in Each Node II](https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| [C++](./algorithms/populatingNextRightPointersInEachNode/populatingNextRightPointersInEachNode.II.cpp)|Hard|
|
||||
|116|[Populating Next Right Pointers in Each Node](https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/)| [C++](./algorithms/populatingNextRightPointersInEachNode/populatingNextRightPointersInEachNode.cpp)|Medium|
|
||||
|115|[Distinct Subsequences](https://oj.leetcode.com/problems/distinct-subsequences/)| [C++](./algorithms/distinctSubsequences/distinctSubsequences.cpp)|Hard|
|
||||
|114|[Flatten Binary Tree to Linked List](https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/)| [C++](./algorithms/flattenBinaryTreeToLinkedList/flattenBinaryTreeToLinkedList.cpp)|Medium|
|
||||
|113|[Path Sum II](https://oj.leetcode.com/problems/path-sum-ii/)| [C++](./algorithms/pathSum/pathSum.II.cpp)|Medium|
|
||||
|112|[Path Sum](https://oj.leetcode.com/problems/path-sum/)| [C++](./algorithms/pathSum/pathSum.cpp)|Easy|
|
||||
|111|[Minimum Depth of Binary Tree](https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/)| [C++](./algorithms/minimumDepthOfBinaryTree/minimumDepthOfBinaryTree.cpp)|Easy|
|
||||
|110|[Balanced Binary Tree](https://oj.leetcode.com/problems/balanced-binary-tree/)| [C++](./algorithms/balancedBinaryTree/balancedBinaryTree.cpp)|Easy|
|
||||
|109|[Convert Sorted List to Binary Search Tree](https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| [C++](./algorithms/convertSortedListToBinarySearchTree/convertSortedListToBinarySearchTree.cpp)|Medium|
|
||||
|108|[Convert Sorted Array to Binary Search Tree](https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| [C++](./algorithms/convertSortedArrayToBinarySearchTree/convertSortedArrayToBinarySearchTree.cpp)|Medium|
|
||||
|107|[Binary Tree Level Order Traversal II](https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/)| [C++](./algorithms/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.II.cpp)|Easy|
|
||||
|106|[Construct Binary Tree from Inorder and Postorder Traversal](https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| [C++](./algorithms/constructBinaryTreeFromInorderAndPostorderTraversal/constructBinaryTreeFromInorderAndPostorderTraversal.cpp)|Medium|
|
||||
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| [C++](./algorithms/constructBinaryTreeFromPreorderAndInorderTraversal/constructBinaryTreeFromPreorderAndInorderTraversal.cpp)|Medium|
|
||||
|104|[Maximum Depth of Binary Tree](https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/)| [C++](./algorithms/maximumDepthOfBinaryTree/maximumDepthOfBinaryTree.cpp)|Easy|
|
||||
|103|[Binary Tree Zigzag Level Order Traversal](https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| [C++](./algorithms/binaryTreeZigzagLevelOrderTraversal/binaryTreeZigzagLevelOrderTraversal.cpp)|Medium|
|
||||
|102|[Binary Tree Level Order Traversal](https://oj.leetcode.com/problems/binary-tree-level-order-traversal/)| [C++](./algorithms/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.cpp)|Easy|
|
||||
|101|[Symmetric Tree](https://oj.leetcode.com/problems/symmetric-tree/)| [C++](./algorithms/symmetricTree/symmetricTree.cpp)|Easy|
|
||||
|100|[Same Tree](https://oj.leetcode.com/problems/same-tree/)| [C++](./algorithms/sameTree/sameTree.cpp)|Easy|
|
||||
|99|[Recover Binary Search Tree](https://oj.leetcode.com/problems/recover-binary-search-tree/)| [C++](./algorithms/recoverBinarySearchTree/recoverBinarySearchTree.cpp)|Hard|
|
||||
|98|[Validate Binary Search Tree](https://oj.leetcode.com/problems/validate-binary-search-tree/)| [C++](./algorithms/validateBinarySearchTree/validateBinarySearchTree.cpp)|Medium|
|
||||
|97|[Interleaving String](https://oj.leetcode.com/problems/interleaving-string/)| [C++](./algorithms/interleavingString/interleavingString.cpp)|Hard|
|
||||
|96|[Unique Binary Search Trees II](https://oj.leetcode.com/problems/unique-binary-search-trees-ii/)| [C++](./algorithms/uniqueBinarySearchTrees/uniqueBinarySearchTrees.II.cpp)|Medium|
|
||||
|95|[Unique Binary Search Trees](https://oj.leetcode.com/problems/unique-binary-search-trees/)| [C++](./algorithms/uniqueBinarySearchTrees/uniqueBinarySearchTrees.cpp)|Medium|
|
||||
|94|[Binary Tree Inorder Traversal](https://oj.leetcode.com/problems/binary-tree-inorder-traversal/)| [C++](./algorithms/binaryTreeInorderTraversal/binaryTreeInorderTraversal.cpp)|Medium|
|
||||
|93|[Restore IP Addresses](https://oj.leetcode.com/problems/restore-ip-addresses/)| [C++](./algorithms/restoreIPAddresses/restoreIPAddresses.cpp)|Medium|
|
||||
|92|[Reverse Linked List II](https://oj.leetcode.com/problems/reverse-linked-list-ii/)| [C++](./algorithms/reverseLinkedList/reverseLinkedList.II.cpp)|Medium|
|
||||
|91|[Subsets II](https://oj.leetcode.com/problems/subsets-ii/)| [C++](./algorithms/subsets/subsets.II.cpp)|Medium|
|
||||
|90|[Decode Ways](https://oj.leetcode.com/problems/decode-ways/)| [C++](./algorithms/decodeWays/decodeWays.cpp)|Medium|
|
||||
|89|[Gray Code](https://oj.leetcode.com/problems/gray-code/)| [C++](./algorithms/grayCode/grayCode.cpp)|Medium|
|
||||
|88|[Merge Sorted Array](https://oj.leetcode.com/problems/merge-sorted-array/)| [C++](./algorithms/mergeTwoSortedArray/mergeTwoSortedArray.cpp)|Easy|
|
||||
|87|[Scramble String](https://oj.leetcode.com/problems/scramble-string/)| [C++](./algorithms/scrambleString/scrambleString.cpp)|Hard|
|
||||
|86|[Partition List](https://oj.leetcode.com/problems/partition-list/)| [C++](./algorithms/partitionList/partitionList.cpp)|Medium|
|
||||
|85|[Maximal Rectangle](https://oj.leetcode.com/problems/maximal-rectangle/)| [C++](./algorithms/maximalRectangle/maximalRectangle.cpp)|Hard|
|
||||
|84|[Largest Rectangle in Histogram](https://oj.leetcode.com/problems/largest-rectangle-in-histogram/)| [C++](./algorithms/largestRectangleInHistogram/largestRectangleInHistogram.cpp)|Hard|
|
||||
|83|[Remove Duplicates from Sorted List II](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| [C++](./algorithms/removeDuplicatesFromSortedList/removeDuplicatesFromSortedList.II.cpp)|Medium|
|
||||
|82|[Remove Duplicates from Sorted List](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/)| [C++](./algorithms/removeDuplicatesFromSortedList/removeDuplicatesFromSortedList.cpp)|Easy|
|
||||
|81|[Search in Rotated Sorted Array II](https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/)| [C++](./algorithms/searchInRotatedSortedArray/searchInRotatedSortedArray.II.cpp)|Medium|
|
||||
|80|[Remove Duplicates from Sorted Array II](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| [C++](./algorithms/removeDuplicatesFromSortedArray/removeDuplicatesFromSortedArray.II.cpp)|Medium|
|
||||
|79|[Word Search](https://oj.leetcode.com/problems/word-search/)| [C++](./algorithms/wordSearch/wordSearch.cpp)|Medium|
|
||||
|78|[Subsets](https://oj.leetcode.com/problems/subsets/)| [C++](./algorithms/subsets/subsets.cpp)|Medium|
|
||||
|77|[Combinations](https://oj.leetcode.com/problems/combinations/)| [C++](./algorithms/combinations/combinations.cpp)|Medium|
|
||||
|76|[Minimum Window Substring](https://oj.leetcode.com/problems/minimum-window-substring/)| [C++](./algorithms/minimumWindowSubstring/minimumWindowSubstring.cpp)|Hard|
|
||||
|75|[Sort Colors](https://oj.leetcode.com/problems/sort-colors/)| [C++](./algorithms/sortColors/sortColors.cpp)|Medium|
|
||||
|74|[Search a 2D Matrix](https://oj.leetcode.com/problems/search-a-2d-matrix/)| [C++](./algorithms/search2DMatrix/search2DMatrix.cpp)|Medium|
|
||||
|73|[Set Matrix Zeroes](https://oj.leetcode.com/problems/set-matrix-zeroes/)| [C++](./algorithms/setMatrixZeroes/setMatrixZeroes.cpp)|Medium|
|
||||
|72|[Edit Distance](https://oj.leetcode.com/problems/edit-distance/)| [C++](./algorithms/editDistance/editDistance.cpp)|Hard|
|
||||
|71|[Simplify Path](https://oj.leetcode.com/problems/simplify-path/)| [C++](./algorithms/simplifyPath/simplifyPath.cpp)|Medium|
|
||||
|70|[Climbing Stairs](https://oj.leetcode.com/problems/climbing-stairs/)| [C++](./algorithms/climbStairs/climbStairs.cpp)|Easy|
|
||||
|69|[Sqrt(x)](https://oj.leetcode.com/problems/sqrtx/)| [C++](./algorithms/sqrt/sqrt.cpp)|Medium|
|
||||
|68|[Text Justification](https://oj.leetcode.com/problems/text-justification/)| [C++](./algorithms/textJustification/textJustification.cpp)|Hard|
|
||||
|67|[Plus One](https://oj.leetcode.com/problems/plus-one/)| [C++](./algorithms/plusOne/plusOne.cpp)|Easy|
|
||||
|66|[Valid Number](https://oj.leetcode.com/problems/valid-number/)| [C++](./algorithms/validNumber/validNumber.cpp)|Easy|
|
||||
|65|[Add Binary](https://oj.leetcode.com/problems/add-binary/)| [C++](./algorithms/addBinary/addBinary.cpp)|Easy|
|
||||
|64|[Merge Two Sorted Lists](https://oj.leetcode.com/problems/merge-two-sorted-lists/)| [C++](./algorithms/mergeTwoSortedList/mergeTwoSortedList.cpp)|Easy|
|
||||
|63|[Minimum Path Sum](https://oj.leetcode.com/problems/minimum-path-sum/)| [C++](./algorithms/minimumPathSum/minimumPathSum.cpp)|Medium|
|
||||
|62|[Unique Paths II](https://oj.leetcode.com/problems/unique-paths-ii/)| [C++](./algorithms/uniquePaths/uniquePaths.II.cpp)|Medium|
|
||||
|61|[Unique Paths](https://oj.leetcode.com/problems/unique-paths/)| [C++](./algorithms/uniquePaths/uniquePaths.cpp)|Medium|
|
||||
|60|[Rotate List](https://oj.leetcode.com/problems/rotate-list/)| [C++](./algorithms/rotateList/rotateList.cpp)|Medium|
|
||||
|59|[Permutation Sequence](https://oj.leetcode.com/problems/permutation-sequence/)| [C++](./algorithms/permutationSequence/permutationSequence.cpp)|Medium|
|
||||
|58|[Spiral Matrix II](https://oj.leetcode.com/problems/spiral-matrix-ii/)| [C++](./algorithms/spiralMatrix/spiralMatrix.II.cpp)|Medium|
|
||||
|57|[Length of Last Word](https://oj.leetcode.com/problems/length-of-last-word/)| [C++](./algorithms/lengthOfLastWord/lengthOfLastWord.cpp)|Easy|
|
||||
|56|[Insert Interval](https://oj.leetcode.com/problems/insert-interval/)| [C++](./algorithms/insertInterval/insertInterval.cpp)|Hard|
|
||||
|55|[Merge Intervals](https://oj.leetcode.com/problems/merge-intervals/)| [C++](./algorithms/mergeIntervals/mergeIntervals.cpp)|Hard|
|
||||
|54|[Jump Game](https://oj.leetcode.com/problems/jump-game/)| [C++](./algorithms/jumpGame/jumpGame.cpp)|Medium|
|
||||
|53|[Spiral Matrix](https://oj.leetcode.com/problems/spiral-matrix/)| [C++](./algorithms/spiralMatrix/spiralMatrix.cpp)|Medium|
|
||||
|52|[Maximum Subarray](https://oj.leetcode.com/problems/maximum-subarray/)| [C++](./algorithms/maximumSubArray/maximumSubArray.cpp)|Medium|
|
||||
|51|[N-Queens II](https://oj.leetcode.com/problems/n-queens-ii/)| [C++](./algorithms/nQueens/nQueuens.II.cpp)|Hard|
|
||||
|50|[N-Queens](https://oj.leetcode.com/problems/n-queens/)| [C++](./algorithms/nQueens/nQueuens.cpp)|Hard|
|
||||
|49|["Pow(x, n)"](https://oj.leetcode.com/problems/powx-n/)| [C++](./algorithms/pow/pow.cpp)|Medium|
|
||||
|48|[Group Anagrams](https://oj.leetcode.com/problems/anagrams/)| [C++](./algorithms/anagrams/GroupAnagrams.cpp)|Medium|
|
||||
|47|[Rotate Image](https://oj.leetcode.com/problems/rotate-image/)| [C++](./algorithms/rotateImage/rotateImage.cpp)|Medium|
|
||||
|46|[Permutations II](https://oj.leetcode.com/problems/permutations-ii/)| [C++](./algorithms/permutations/permutations.II.cpp)|Hard|
|
||||
|45|[Permutations](https://oj.leetcode.com/problems/permutations/)| [C++](./algorithms/permutations/permutations.cpp)|Medium|
|
||||
|44|[Jump Game II](https://oj.leetcode.com/problems/jump-game-ii/)| [C++](./algorithms/jumpGame/jumpGame.II.cpp)|Hard|
|
||||
|43|[Wildcard Matching](https://oj.leetcode.com/problems/wildcard-matching/)| [C++](./algorithms/wildcardMatching/wildcardMatching.cpp)|Hard|
|
||||
|42|[Multiply Strings](https://oj.leetcode.com/problems/multiply-strings/)| [C++](./algorithms/multiplyStrings/multiplyStrings.cpp)|Medium|
|
||||
|41|[Trapping Rain Water](https://oj.leetcode.com/problems/trapping-rain-water/)| [C++](./algorithms/trappingRainWater/trappingRainWater.cpp)|Hard|
|
||||
|40|[First Missing Positive](https://oj.leetcode.com/problems/first-missing-positive/)| [C++](./algorithms/firstMissingPositive/firstMissingPositive.cpp)|Hard|
|
||||
|39|[Combination Sum II](https://oj.leetcode.com/problems/combination-sum-ii/)| [C++](./algorithms/combinationSum/combinationSum.II.cpp)|Medium|
|
||||
|38|[Combination Sum](https://oj.leetcode.com/problems/combination-sum/)| [C++](./algorithms/combinationSum/combinationSum.cpp)|Medium|
|
||||
|37|[Count and Say](https://oj.leetcode.com/problems/count-and-say/)| [C++](./algorithms/countAndSay/countAndSay.cpp)|Easy|
|
||||
|36|[Sudoku Solver](https://oj.leetcode.com/problems/sudoku-solver/)| [C++](./algorithms/sudokuSolver/sudokuSolver.cpp)|Hard|
|
||||
|35|[Valid Sudoku](https://oj.leetcode.com/problems/valid-sudoku/)| [C++](./algorithms/validSudoku/validSudoku.cpp)|Easy|
|
||||
|34|[Search Insert Position](https://oj.leetcode.com/problems/search-insert-position/)| [C++](./algorithms/searchInsertPosition/searchInsertPosition.cpp)|Medium|
|
||||
|33|[Search for a Range](https://oj.leetcode.com/problems/search-for-a-range/)| [C++](./algorithms/searchForRange/searchForRange.cpp)|Medium|
|
||||
|32|[Search in Rotated Sorted Array](https://oj.leetcode.com/problems/search-in-rotated-sorted-array/)| [C++](./algorithms/searchInRotatedSortedArray/searchInRotatedSortedArray.cpp)|Hard|
|
||||
|31|[Longest Valid Parentheses](https://oj.leetcode.com/problems/longest-valid-parentheses/)| [C++](./algorithms/longestValidParentheses/longestValidParentheses.cpp)|Hard|
|
||||
|30|[Next Permutation](https://oj.leetcode.com/problems/next-permutation/)| [C++](./algorithms/nextPermutation/nextPermutation.cpp)|Medium|
|
||||
|29|[Substring with Concatenation of All Words](https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/)| [C++](./algorithms/substringWithConcatenationOfAllWords/substringWithConcatenationOfAllWords.cpp)|Hard|
|
||||
|28|[Divide Two Integers](https://oj.leetcode.com/problems/divide-two-integers/)| [C++](./algorithms/divideTwoInt/divideTwoInt.cpp)|Medium|
|
||||
|27|[Implement strStr()](https://oj.leetcode.com/problems/implement-strstr/)| [C++](./algorithms/strStr/strStr.cpp)|Easy|
|
||||
|26|[Remove Element](https://oj.leetcode.com/problems/remove-element/)| [C++](./algorithms/removeElement/removeElement.cpp)|Easy|
|
||||
|25|[Remove Duplicates from Sorted Array](https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./algorithms/removeDuplicatesFromSortedArray/removeDuplicatesFromSortedArray.cpp)|Easy|
|
||||
|24|[Reverse Nodes in k-Group](https://oj.leetcode.com/problems/reverse-nodes-in-k-group/)| [C++](./algorithms/reverseNodesInKGroup/reverseNodesInKGroup.cpp)|Hard|
|
||||
|23|[Swap Nodes in Pairs](https://oj.leetcode.com/problems/swap-nodes-in-pairs/)| [C++](./algorithms/swapNodesInPairs/swapNodesInPairs.cpp)|Medium|
|
||||
|22|[Merge k Sorted Lists](https://oj.leetcode.com/problems/merge-k-sorted-lists/)| [C++](./algorithms/mergeKSortedLists/mergeKSortedLists.cpp)|Hard|
|
||||
|21|[Generate Parentheses](https://oj.leetcode.com/problems/generate-parentheses/)| [C++](./algorithms/generateParentheses/generateParentheses.cpp)|Medium|
|
||||
|20|[Valid Parentheses](https://oj.leetcode.com/problems/valid-parentheses/)| [C++](./algorithms/validParentheses/validParentheses.cpp)|Easy|
|
||||
|19|[Remove Nth Node From End of List](https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/)| [C++](./algorithms/removeNthNodeFromEndOfList/removeNthNodeFromEndOfList.cpp)|Easy|
|
||||
|18|[Letter Combinations of a Phone Number](https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/)| [C++](./algorithms/letterCombinationsOfAPhoneNumber/letterCombinationsOfAPhoneNumber.cpp)|Medium|
|
||||
|17|[4Sum](https://oj.leetcode.com/problems/4sum/)| [C++](./algorithms/4Sum/4Sum.cpp)|Medium|
|
||||
|16|[3Sum Closest](https://oj.leetcode.com/problems/3sum-closest/)| [C++](./algorithms/3SumClosest/3SumClosest.cpp)|Medium|
|
||||
|15|[3Sum](https://oj.leetcode.com/problems/3sum/)| [C++](./algorithms/3Sum/3Sum.cpp)|Medium|
|
||||
|14|[Longest Common Prefix](https://oj.leetcode.com/problems/longest-common-prefix/)| [C++](./algorithms/longestCommonPrefix/longestCommonPrefix.cpp)|Easy|
|
||||
|13|[Roman to Integer](https://oj.leetcode.com/problems/roman-to-integer/)| [C++](./algorithms/romanToInteger/romanToInteger.cpp)|Easy|
|
||||
|12|[Integer to Roman](https://oj.leetcode.com/problems/integer-to-roman/)| [C++](./algorithms/integerToRoman/integerToRoman.cpp)|Medium|
|
||||
|11|[Container With Most Water](https://oj.leetcode.com/problems/container-with-most-water/)| [C++](./algorithms/containerWithMostWater/containerWithMostWater.cpp)|Medium|
|
||||
|10|[Regular Expression Matching](https://oj.leetcode.com/problems/regular-expression-matching/)| [C++](./algorithms/regularExpressionMatching/regularExpressionMatching.cpp)|Hard|
|
||||
|9|[Palindrome Number](https://oj.leetcode.com/problems/palindrome-number/)| [C++](./algorithms/palindromeNumber/palindromeNumber.cpp)|Easy|
|
||||
|8|[String to Integer (atoi)](https://oj.leetcode.com/problems/string-to-integer-atoi/)| [C++](./algorithms/stringToIntegerAtoi/stringToIntegerAtoi.cpp)|Easy|
|
||||
|7|[Reverse Integer](https://oj.leetcode.com/problems/reverse-integer/)| [C++](./algorithms/reverseInteger/reverseInteger.cpp)|Easy|
|
||||
|6|[ZigZag Conversion](https://oj.leetcode.com/problems/zigzag-conversion/)| [C++](./algorithms/zigZagConversion/zigZagConversion.cpp)|Easy|
|
||||
|5|[Longest Palindromic Substring](https://oj.leetcode.com/problems/longest-palindromic-substring/)| [C++](./algorithms/longestPalindromicSubstring/longestPalindromicSubstring.cpp)|Medium|
|
||||
|4|[Add Two Numbers](https://oj.leetcode.com/problems/add-two-numbers/)| [C++](./algorithms/addTwoNumbers/addTwoNumbers.cpp)|Medium|
|
||||
|3|[Longest Substring Without Repeating Characters](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/)| [C++](./algorithms/longestSubstringWithoutRepeatingCharacters/longestSubstringWithoutRepeatingCharacters.cpp)|Medium|
|
||||
|2|[Median of Two Sorted Arrays](https://oj.leetcode.com/problems/median-of-two-sorted-arrays/)| [C++](./algorithms/medianOfTwoSortedArrays/medianOfTwoSortedArrays.cpp)|Hard|
|
||||
|1|[Two Sum](https://oj.leetcode.com/problems/two-sum/)| [C++](./algorithms/twoSum/twoSum.cpp)|Medium|
|
||||
>>>>>>> 3dbf34bf685c42ae6d46c19e7cc7d9d0ab415a92
|
||||
|
||||
|
||||
|
||||
|
88
algorithms/cpp/addDigits/addDigits.cpp
Normal file
88
algorithms/cpp/addDigits/addDigits.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
// Source : https://leetcode.com/problems/add-digits/
|
||||
// Author : Timothy Lim, Hao Chen
|
||||
// Date : 2015-10-1
|
||||
|
||||
/**********************************************************************************
|
||||
* Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
|
||||
*
|
||||
* For example:
|
||||
* Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
|
||||
*
|
||||
* Follow up:
|
||||
* Could you do it without any loop/recursion in O(1) runtime?
|
||||
*
|
||||
**********************************************************************************/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
int addDigits(int num) {
|
||||
switch(random()%5+1){
|
||||
case 1: return addDigits01(num);
|
||||
case 2: return addDigits02(num);
|
||||
case 3: return addDigits03(num);
|
||||
case 4: return addDigits04(num);
|
||||
default: return addDigits05(num);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//regualr way
|
||||
int addDigits01(int num) {
|
||||
while(num > 9) {
|
||||
int sum;
|
||||
for(sum=0; num > 0; sum += num%10 , num/=10);
|
||||
num = sum;
|
||||
}
|
||||
return num;
|
||||
|
||||
}
|
||||
|
||||
//This solution looks is very tricky, but acutally it is easy to understand.
|
||||
//it just keep adding the last digital until the num < 10
|
||||
int addDigits02(int num) {
|
||||
while(num > 9) {
|
||||
num = num / 10 + num % 10;
|
||||
}
|
||||
return num;
|
||||
|
||||
}
|
||||
|
||||
// Let's observe the pattern
|
||||
// 1 1
|
||||
// 2 2
|
||||
// ... ...
|
||||
// 8 8
|
||||
// 9 9
|
||||
// 10 1
|
||||
// 11 2
|
||||
// 12 3
|
||||
// ... ...
|
||||
// 17 8
|
||||
// 18 9
|
||||
// 19 1
|
||||
// 20 2
|
||||
// ... ...
|
||||
// It looks most of number just simply %9 is the answer,
|
||||
// but there are some edge cases.
|
||||
// 9%9=0 but we need 9.
|
||||
// 18%9=0 but we need 9
|
||||
// so we can find the solution is:
|
||||
// 1) num <=9, return num
|
||||
// 2) num > 9, reutrn num%9 if num%9>0
|
||||
// return 9 if num%9 ==0
|
||||
int addDigits03(int num) {
|
||||
return num >9 ? ((num %9)==0 ? 9:num%9) : num;
|
||||
}
|
||||
|
||||
//But actually, we can use (num-1)%9 + 1 to make all cases right.
|
||||
int addDigits04(int num){
|
||||
return (num - 1) % 9 + 1;
|
||||
}
|
||||
|
||||
//This solution is similar with pervious solution.
|
||||
int addDigits05(int num){
|
||||
return num - 9 * ((num - 1)/9);
|
||||
}
|
||||
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user