Skip to content

Instantly share code, notes, and snippets.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
#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 {
@junjiah
junjiah / subarray_sum.cc
Last active August 29, 2015 14:25
solved 'Minimum Size Subarray Sum' on leetcode https://leetcode.com/problems/minimum-size-subarray-sum/
#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 {
@junjiah
junjiah / trie.cc
Created July 19, 2015 21:18
solved 'Implement Trie (Prefix Tree)' on leetcode https://leetcode.com/problems/implement-trie-prefix-tree/
struct TrieNode {
TrieNode *next = nullptr;
TrieNode *child = nullptr;
char val = 0;
// Indicating end of word.
bool terminal = false;
};
class Trie {
public:
@junjiah
junjiah / lca_compare_path.cc
Last active August 29, 2015 14:25
solved 'Lowest Common Ancestor of a Binary Tree' on leetcode https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
/*
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;
import collections
import itertools
# Definition for a point.
class Point:
def __init__(self, a=0, b=0):
self.x = a
self.y = b
@junjiah
junjiah / min_window_substr.c
Last active August 29, 2015 14:24
solved 'Minimum Window Substring' on leetcode https://leetcode.com/problems/minimum-window-substring/
/**
* 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>
@junjiah
junjiah / inline_calc.cpp
Last active August 29, 2015 14:24
solved 'Basic Calculator II' on leetcode https://leetcode.com/problems/basic-calculator-ii/
/**
* 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'.
@junjiah
junjiah / word_dictionary.cpp
Created July 11, 2015 00:48
solved 'Add and Search Word - Data structure design' on leetcode https://leetcode.com/problems/add-and-search-word-data-structure-design/
// 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(