Spiral Matrix I and II

Description https://oj.leetcode.com/problems/spiral-matrix/ https://oj.leetcode.com/problems/spiral-matrix-ii/ Difficulty: 1.5/5.0 star Analysis The solution to these two problems are almost the same. class Solution { public: vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int> result; if (!matrix.size()) return result; int boundary[4]; //x boundary boundary[0] = 0; boundary[2]...

Single Number ii

Description https://oj.leetcode.com/problems/single-number-ii/ Difficulty: 2.5/5.0 Related: https://oj.leetcode.com/problems/single-number/ Analysis and solution class Solution { public: int singleNumber(int A[], int n) { int count[32] = {0}; for (int i = 0; i < n; ++ i){ int tmp = A[i]; for (int j...

Remove Duplicates from Sorted Array ii

Description https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ Difficulty: 2.0/5.0 Related: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Analysis and solution This problem is not hard at all, but coming up with a neat solution is not that easy. This solution is almost the same with the one for the related problem....

Path Sum

Description https://oj.leetcode.com/problems/path-sum/ Difficulty: 2.0/5.0 Analysis and solution I am so happy that I came up with such a concise solution… Note: sum can be negative… class Solution { public: bool hasPathSum(TreeNode *root, int sum){ if (!root) return false; sum -=...

Generate Parentheses

Description As long as you have gotten more left Parentheses in your string, you can put a right parenthesis in it. Analysis and solution class Solution { public: vector<string> generateParenthesis(int n) { vector<string>result; string tmpStr; generateParenthesis(n, 0, 0, tmpStr, result);...