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<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(); |
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 binary tree | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(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
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++ ) { |
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: | |
bool isValid(string s) { | |
stack<char> myS; | |
for(int i=0; i<s.size(); i++) { | |
switch (s[i]) { | |
case '(': | |
myS.push('('); | |
break; | |
case ')': |
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: | |
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) |
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 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; |
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: | |
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; |
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 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. |
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 binary tree | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |