Skip to content

Instantly share code, notes, and snippets.

@xiren-wang
xiren-wang / spiral_matrix.cpp
Created August 15, 2014 06:31
spiral matrix I. Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> vInt;
// move row downward, and column right-ward
// first row --> last col --> last row --> first col
// Then special case: if more rows than cols and colNum is odd, add this col
// more columns than row and rowNum is odd, add this row
// equal rows and cols, but rowNum is odd, add this central elements
int m = matrix.size();
@xiren-wang
xiren-wang / preorder_Binary_tree.cpp
Created August 15, 2014 06:39
preorder traversal of binary tree. Given a binary tree, return the preorder traversal of its nodes' values.
/**
* 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 / set_matrix_zero.cpp
Created August 17, 2014 04:06
set matrix zero. Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. With constant extra memory space.
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
//get a row which involves at least one 0
int oneRow = -1;
int m = matrix.size();
if (m <= 0 )
return;
int n = matrix[0].size();
for (int i=0; i<m; i++ ) {
@xiren-wang
xiren-wang / valid_parentheses.cpp
Created August 17, 2014 04:19
Valid Parentheses. Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid..The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
class Solution {
public:
bool isValid(string s) {
stack<char> myS;
for(int i=0; i<s.size(); i++) {
switch (s[i]) {
case '(':
myS.push('(');
break;
case ')':
@xiren-wang
xiren-wang / palindrome_number.cpp
Created August 19, 2014 07:04
palindrome number;Determine whether an integer is a palindrome. Do this without extra space.
class Solution {
public:
bool isPalindrome(int x) {
// strip the first and last digit, compare, until nothing left
if (x < 0 )
return false;
if (x < 10 )
return true;
//now x > 0, get the max divider (i.e 12345 maxDividor = 10^4)
@xiren-wang
xiren-wang / remove_duplicate_in_linked_list.cpp
Created August 20, 2014 06:23
Remove Duplicates from Sorted List. Given a sorted linked list, delete all duplicates such that each element appear only once.
/**
* 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 / remove_duplicates_2.cpp
Created August 20, 2014 06:51
Remove Duplicates from Sorted List II.
/**
* Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
*
* Definition for singly-linked list.
* struct ListNode {
* int val;
@xiren-wang
xiren-wang / sqrt_of_int.cpp
Created August 20, 2014 07:30
sqrt of int
class Solution {
public:
int sqrt(int x) {
//binary search
//Note: must pay attention to overflow, like (int) * (int) easily overflow
// to avoid dead loop, use (low + 1 < high), instead of lwo < high
if (x < 0)
return -1;
if (x == 0)
return 0;
@xiren-wang
xiren-wang / sum_root_to_leaf_numbers.cpp
Created August 21, 2014 04:54
sum root to leaf numbers.
/**
* 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 containing digits from 0-9 only, each root-to-leaf path could represent a number.
@xiren-wang
xiren-wang / path_sum.cpp
Created August 21, 2014 05:41
path sum. Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {