Skip to content

Instantly share code, notes, and snippets.

@xiren-wang
xiren-wang / remove_duplicates_in_sorted_array_II.cpp
Created September 1, 2014 07:17
remove duplicates in sorted array II: keep two copies only, but remove others.
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n <= 2)
return n;
int slow = 0;
int fast = slow + 1;
while (fast < n) {
// keep two copies
if (A[fast] == A[slow] ) {
@xiren-wang
xiren-wang / remove_duplicates_in_sorted_array.cpp
Last active August 29, 2015 14:05
remove duplicates in sorted array. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A = [1,1,2], Your function should return length = 2, and A is now …
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n <= 0) //necessary, since slow is baed on 0 as in next
return n;
//two pointers, one slow, one fast
// sweep fast if A[fast ] == A[slow]
// if new element found, keep it, set as new base element
int moveToLeft = 0;
int slow = 0;
@xiren-wang
xiren-wang / remove_element.cpp
Created September 1, 2014 06:40
remove element. Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length.
class Solution {
public:
int removeElement(int A[], int n, int elem) {
//scan from beginning,
// if find an instance of value, copy an element from tail, and shorten by one
// Note: the new element from tail can NOT be equal to value.
int tail = n-1;
for(int i=0; i<=tail; i++) {
if (A[i] == elem) {
//select one non-element number
@xiren-wang
xiren-wang / subset_II.cpp
Created September 1, 2014 06:12
subset II.
class Solution {
public:
/*
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
@xiren-wang
xiren-wang / binary_tree_zigzag_level_order_traversal.cpp
Created August 31, 2014 07:42
binary tree zigzag level order traversal.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@xiren-wang
xiren-wang / balanced_binary_tree.cpp
Created August 31, 2014 07:14
balanced binary tree.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
* Given a binary tree, determine if it is height-balanced.
@xiren-wang
xiren-wang / flatten_binary_tree_to_linked_list.cpp
Created August 31, 2014 07:02
Flatten Binary Tree to Linked List
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}\ Given a binary tree, flatten it to a linked list in-place.
For example,
Given
@xiren-wang
xiren-wang / symmetric_tree.cpp
Created August 30, 2014 06:51
symmetric tree
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
@xiren-wang
xiren-wang / max_depth_of_binary_tree.cpp
Created August 30, 2014 05:41
max depth of binary tree
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
@xiren-wang
xiren-wang / binary_tree_level_order_traversal_II.cpp
Created August 30, 2014 05:20
binary tree level order traversal II.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
* Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).