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 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--]; |
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 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 |
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 with a random pointer. | |
* struct RandomListNode { | |
* int label; | |
* RandomListNode *next, *random; | |
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {} | |
* }; | |
*/ | |
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
/*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]; |
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
/** | |
* 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
/* | |
Given a binary tree, return the inorder traversal of its nodes' values. | |
For example: | |
Given binary tree {1,#,2,3}, | |
1 | |
\ | |
2 | |
/ |
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 undirected graph. | |
* struct UndirectedGraphNode { | |
* int label; | |
* vector<UndirectedGraphNode *> neighbors; | |
* UndirectedGraphNode(int x) : label(x) {}; | |
* }; | |
*/ | |
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) {} | |
* }; | |
*/ | |
/** | |
* Definition for binary tree |
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 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; |
OlderNewer