Skip to content

Instantly share code, notes, and snippets.

@xiren-wang
xiren-wang / gist:18ee485e64b884053d2e
Last active August 29, 2015 14:04
merge two sorted arrays and find minimum distance between them
// Given two sorted Array A and B, merge all elements to A (suppose A is large enough)
// Algorithm: scan from end of A and B, moving larger one to it, then move forward untial B is empty
// Later we will find that this code can be slightly modified to find the minimum distance between sorted arrays
void mergeSortedArrays(int *A, int m, int *B, int n) {
//idea 1:
//merge from backward of A
int endA = m-1, endB = n -1, tail = m+n -1;
while(endB >= 0 && endA >=0 ){
if (B[endB] >= A[endA] ) {
A[tail--] = B[endB--];
@xiren-wang
xiren-wang / gist:00705240b62959689d97
Last active August 29, 2015 14:04
same binary tree
/*
struct TreeNode {
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
*/
bool isSameTree(TreeNode *p, TreeNode *q) {
//both empty, OK
@xiren-wang
xiren-wang / gist:2d427ffb8a76938be020
Created July 29, 2014 07:01
deep copy random list
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
@xiren-wang
xiren-wang / singleNumber.cpp
Created July 31, 2014 05:29
single number inside an array
/*Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/
int singleNumber(int A[], int n) {
if (n <= 0)
return -1;
int a0 = A[0];
@xiren-wang
xiren-wang / reverse_linked_listII.cpp
Created August 11, 2014 04:56
Reverse Linked List II. Reverse a linked list from position m to n. Do it in-place and in one-pass. Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@xiren-wang
xiren-wang / insertion_sort_linked_list.cpp
Created August 13, 2014 05:15
Insertion Sort List. Sort a linked list using insertion sort..
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@xiren-wang
xiren-wang / inorder_traversal_iterative.cpp
Created August 13, 2014 06:32
Binary Tree Inorder Traversal, iterative solution
/*
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
@xiren-wang
xiren-wang / clone_graph.cpp
Created August 13, 2014 07:06
Clone graphy. Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
@xiren-wang
xiren-wang / linked_list_to_BST.cpp
Created August 14, 2014 06:26
Convert Sorted List to Binary Search Tree. Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
@xiren-wang
xiren-wang / spiral_matrix_II.cpp
Created August 15, 2014 05:27
Spiral Matrix II. Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > vvInt(n, vector<int>(n,0));
//fill two strips consecutively,
// First row, last col, last row, first col
//Note that if n is odd (n >=3), the central one is NOT filled (it's not in four-strip structure)
int rowStart = 0, colStart = 0;
int scan = 1;