This file contains hidden or 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 hidden or 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: | |
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; | |
} |
This file contains hidden or 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: | |
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]; |
This file contains hidden or 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 binary tree | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
This file contains hidden or 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: | |
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; |