View binary_search_first_position.c
/*
* 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)
View 46.go
// #46 Fibonacci closure
package main
import "fmt"
func fibonacci() func() int {
x, y := 0, 1
return func() int {
x, y = y, x+y
View queue.py
class Queue():
def __init__(self):
self.inbox = []
self.outbox = []
def enqueue(self, item):
self.inbox.append(item)
def dequeue(self):
if not self.outbox:
View subsequence.cc
/*
* 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;
View reverse.cpp
/*
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) {};
};
View checkBST.cc
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);
}
View double_pointer_1.c
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;