Skip to content

Instantly share code, notes, and snippets.

@ywei89
ywei89 / sort_list.cpp
Last active August 29, 2015 13:56 — forked from anonymous/sort_list.cpp
Sort a linked list in O(n log n) time using constant space complexity.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int numRows = obstacleGrid.size();
int numCols = obstacleGrid[0].size();
if (numRows == 0 || numCols == 0 ||
obstacleGrid[numRows - 1][numCols - 1] == 1) { // ignoring this will lead to an erroneous solution
return 0;
}
class Solution {
public:
int uniquePaths(int m, int n) {
int pathCount[m][n];
memset(pathCount, 0, sizeof(pathCount));
pathCount[m - 1][n - 1] = 1;
for (int i = n - 1; i >= 0; i--) { // col
for (int j = m - 1; j >= 0; j--) { // row
if (j + 1 < m) { // to the right
pathCount[j][i] += pathCount[j + 1][i];
@ywei89
ywei89 / level_order_traversal.cpp
Created February 14, 2014 03:08 — forked from anonymous/level_order_traversal.cpp
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).
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n == 0) return 0; // to prevent the function from outputing 1 for an empty list
int i = 0;
int j = 1;
for (; j < n; j++) {
if (A[j] != A[i]) A[++i] = A[j];
}
return i + 1;