This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iostream> | |
#include <set> | |
#include <vector> | |
using namespace std; | |
// Inspired by | |
// https://leetcode.com/discuss/45120/c-using-set-less-10-lines-with-simple-explanation | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <climits> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
// Inspired from | |
// https://leetcode.com/discuss/42143/4ms-solution-and-nlogn-solution-with-detailed-explanations | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct TrieNode { | |
TrieNode *next = nullptr; | |
TrieNode *child = nullptr; | |
char val = 0; | |
// Indicating end of word. | |
bool terminal = false; | |
}; | |
class Trie { | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Run time: 28 ms. | |
Used two vectors to record the path (left or right) to p & q. | |
So O(log n) space. | |
*/ | |
class Solution { | |
public: | |
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { | |
this->p_ = p; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import itertools | |
# Definition for a point. | |
class Point: | |
def __init__(self, a=0, b=0): | |
self.x = a | |
self.y = b | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Algorithm inspired from | |
* https://leetcode.com/discuss/44100/16ms-simple-solution-only-using-vector-detailed-explanation | |
**/ | |
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Inspired from https://leetcode.com/discuss/42643/my-16-ms-no-stack-one-pass-short-c-solution | |
**/ | |
class Solution { | |
public: | |
// Regard expression series of *,/ as blocks, | |
// and try to 'inline' them as a single number. | |
int calculate(string s) { | |
// Expression result of current 'block'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Trie + DFS for searching. | |
class WordDictionary { | |
public: | |
// Add a word into the data structure. | |
void addWord(string word) { | |
int curr_level = 0, len = word.length(); | |
TrieNode *curr_level_node = &root; | |
while (curr_level < len) { | |
TrieNode *next_level_node = findChildNode( |