Letter Combinations of a Phone Number

Description https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ Difficulty: 1.5/5.0 Solution The DFS solution would be more clear, but a worse solution. It is common that the length of the string is more than several thousands. class Solution { public: vector<string> letterCombinations(string digits) { string str[]...

Clone Graph

Description https://oj.leetcode.com/problems/clone-graph/ Difficulty: 1.5/5.0 Solution class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>ht; UndirectedGraphNode * newNode = traverse_copy(node, ht); return newNode; } UndirectedGraphNode* traverse_copy(UndirectedGraphNode * node, unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> &ht){ if (node == NULL) return NULL; if (ht.find(node) !=...

Validate Binary Search Tree

Description https://oj.leetcode.com/problems/validate-binary-search-tree/ Difficulty: 1.5/2.0 Solution class Solution { public: bool isValidBST(TreeNode *pt) { return validate(pt, INT_MAX, INT_MIN); } bool validate(TreeNode * pt, int upperBound, int lowerBound){ if (!pt) return true; return (pt->val < upperBound && pt->val > lowerBound) && \...

Reorder List

Description https://oj.leetcode.com/problems/reorder-list/ Difficulty: 2.0/5.0 Solution We can solve this problem in three steps. First, locate the head of the second half Second, reverse the second list Third, Merge the two lists class Solution { public: void reorderList(ListNode *head) { //count...

Find Minimum in Rotated Sorted Array I and II

Description https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Difficulty: 1.5/5.0 Related: Search in Rotated Sorted Array I Search in Rotated Sorted Array II Find Minimum in Rotated Sorted Array II Solution It is a simplified version of Search in Rotated Sorted Array I. We are only...