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
class Queue(): | |
def __init__(self): | |
self.inbox = [] | |
self.outbox = [] | |
def enqueue(self, item): | |
self.inbox.append(item) | |
def dequeue(self): | |
if not self.outbox: |
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
/* | |
* Credits: http://stackoverflow.com/questions/8599374/sub-sequence-occurrence-in-a-string | |
*/ | |
bool checkSubSequence(string A, string B) { | |
int pos = -1; | |
bool ok = true; | |
for (int i=0; i!=A.size() && ok; i++) | |
ok = (pos = B.find(A[i], pos+1)) != string::npos; |
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
/* | |
Credit to Leetcode: http://leetcode.com/2010/04/reversing-linked-list-iteratively-and.html | |
*/ | |
struct Node { | |
int val; | |
Node *next; | |
Node(int value):val(value), next(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
struct Node { | |
Node *left; | |
Node *right; | |
int _data; | |
Node(int data) : _data(data), left(NULL), right(NULL) {} | |
}; | |
bool checkBST(Node *root) { | |
return help(root, INT_MIN, INT_MAX); | |
} |
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
void insertNode(LinkedList *newIp, LinkedList *list) { | |
LinkedList **lpp; | |
LInkedList *lp; | |
for (lpp = &list; *lpp != NULL; lpp = &(*lpp)->next) { | |
lp = *lpp; | |
if (newIp->val < lp->val) { | |
newIp->next = lp; | |
*lpp = newIp; |
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
/* | |
* Given a sorted array with duplicate elements, find the first index where its element equals to target | |
* invariant: if key is in A[], then key's index in (low, high), and low + 1 < high | |
*/ | |
int binary_search_first_position(int *A, int n, int target) { | |
int low = -1, high = n; | |
while (low + 1 < high) { | |
int mid = low + (high - low) / 2; | |
if (A[mid] < target) |
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
// #46 Fibonacci closure | |
package main | |
import "fmt" | |
func fibonacci() func() int { | |
x, y := 0, 1 | |
return func() int { | |
x, y = y, x+y |