Top Google LeetCode Questions
Top Google LeetCode Questions
Given an array of integers, return indices of the two numbers such that they add
up to a specific target.
You may assume that each input would have exactly one solution, and you may not
use the same element twice.
Example:
01/02/2020:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i) {
if (m.find(target - nums[i]) == m.end()) {
m[nums[i]] = i;
} else {
return {m[target - nums[i]], i};
}
}
return {-1, -1};
}
};
Given a string, find the length of the longest substring without repeating
characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence
and not a substring.
Solution
05/21/2020:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> seen;
int ret = 0, slow = 0, n = s.size();
for (int fast = 0; fast < n; ++fast) {
if (seen.count(s[fast]) != 0) slow = max(slow, seen[s[fast]] + 1);
seen[s[fast]] = fast;
ret = max(ret, fast - slow + 1);
}
return ret;
}
};
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should
be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
nums1 = [1, 2]
nums2 = [3, 4]
Solution
01/02/2020:
end
end end
Given a string s, find the longest palindromic substring in s. You may assume
that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
Solution
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size(), start = 0, max_len = n > 0 ? 1 : 0;
for(int i = 0; i < n; ++i) {
for (int l = i - 1, r = i; l >= 0 && r < n && s[l] == s[r]; --l, ++r) {
if (r - l + 1 > max_len) {
max_len = r - l + 1;
start = l;
}
}
for (int l = i - 1, r = i + 1; l >= 0 && r < n && s[l] == s[r]; --l, ++r)
{
if (r - l + 1 > max_len) {
max_len = r - l + 1;
start = l;
}
}
}
return max_len == 0 ? "" : s.substr(start, max_len);
}
};
6. ZigZag Conversion
Description
P A H NA
P L S I I GY I
R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number
of rows:
P I N
A L S I G
Y A H R
P I
Solution
05/24/2020:
8. String to Integer (atoi)
Description
The function first discards as many whitespace characters as necessary until the
first non-whitespace character is found. Then, starting from this character,
takes an optional initial plus or minus sign followed by as many numerical
digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral
number, which are ignored and have no effect on the behavior of this function.
Note:
Input: "42"
Output: 42
Example 2:
Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed
integer.
Thefore INT_MIN (−231) is returned.
Solution
05/24/2020:
class Solution {
public:
int myAtoi(string str) {
long long ret = atol(str.c_str());
ret = ret > INT_MAX ? INT_MAX : ret;
ret = ret < INT_MIN ? INT_MIN : ret;
return ret;
}
};
class Solution {
public:
int myAtoi(string str) {
if (str.empty()) return 0;
long long ret = 0;
istringstream iss(str);
iss >> ret;
ret = ret < INT_MIN ? INT_MIN : ret; ret
= ret > INT_MAX ? INT_MAX : ret; return
ret;
}
};
Given n non-negative integers a1, a2, ..., an , where each represents a point at
coordinate (i, ai). n vertical lines are drawn such that the two endpoints of
line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis
forms a container, such that the container contains the most water.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
Solution
05/24/2020:
class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0, right = height.size() - 1, ret = 0;
while (left < right) {
ret = max(ret, min(height[left], height[right]) * (right - left));
height[left] < height[right] ? left += 1 : right -= 1;
}
return ret;
}
};
Write a function to find the longest common prefix string amongst an array of
strings.
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
Solution
05/24/2020:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty() || strs[0].empty()) return "";
sort(strs.begin(), strs.end(), [](const string& s1, const string& s2) {
if (s1.size() == s2.size()) return s1 < s2;
return s1.size() < s2.size();
});
string ret;
for (int k = (int)strs[0].size(); k >= 0; --k) {
bool isCommonPrefix = true;
string prefix = strs[0].substr(0, k);
for (int i = 1; i < (int)strs.size(); ++i) {
if (prefix != strs[i].substr(0, k)) {
isCommonPrefix = false;
break;
}
}
if (isCommonPrefix) {
ret = prefix;
break;
}
}
return ret;
}
};
class Trie {
private:
Node* root;
public:
Trie() { root = new Node(); }
string commonPrefix() {
string ret;
Node* cur = root;
while (true) {
int cnt = 0;
char ch = 'a';
bool isUnique = false;
for (int i = 0; i < 26; ++i) {
if (cur->children[i] != nullptr) {
isUnique = true;
19. Remove Nth Node From End of List
Description
Solution
05/23/2020:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *pre = new ListNode(0, head), *slow = pre, *fast = pre;
while (fast->next != nullptr && n-- > 0) fast = fast->next;
while (fast->next != nullptr) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return pre->next;
}
};
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
Solution
01/02/2020:
end
Mimicking merge_sort 01/02/2020:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int n = lists.size();
if (n == 0) return nullptr;
if (n == 1) return lists[0];
vector<ListNode*> lists1(lists.begin(), lists.begin() + n / 2);
vector<ListNode*> lists2(lists.begin() + n / 2, lists.end());
ListNode* l1 = mergeKLists(lists1);
ListNode* l2 = mergeKLists(lists2);
if (l1 == nullptr) return l2;
ListNode* ret = l1;
while (l2 != nullptr) {
if (l1->val > l2->val) swap(l1->val, l2->val);
while(l1->next && l1->next->val < l2->val) l1 = l1->next;
ListNode* tmp2 = l2->next;
l2->next = l1->next;
l1->next = l2;
l2 = tmp2;
}
return ret;
}
};
You may not modify the values in the list's nodes, only nodes itself may be
changed.
Example:
Solution
05/22/2020:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
// pre -> A -> B -> C -> D -> null
// pre -> B (pre->next = pre->next->next)
// A -> C (A->next = A->next->next)
// B -> A (B->next = A):
// (B -> A -> C -> D -> null)
ListNode* pre = new ListNode(0, head);
ListNode* cur = pre;
while (cur->next && cur->next->next) {
ListNode* tmp = cur->next;
cur->next = cur->next->next;
tmp->next = tmp->next->next;
cur->next->next = tmp;
cur = tmp;
}
return pre->next;
}
};
26. Remove Duplicates from Sorted Array
Description
Given a sorted array nums, remove the duplicates in-place such that each element
appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the
input array in-place with O(1) extra memory.
Example 1:
Your function should return length = 2, with the first two elements of nums
being 1 and 2 respectively.
Your function should return length = 5, with the first five elements of nums
being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to
the input array will be known to the caller as well.
Solution
05/27/2020:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int slow = 0, fast = 1, n = nums.size();
for (; fast < n; ++fast) {
while (nums[fast] == nums[fast - 1]) if (++fast >= n) break;
if (fast < n) nums[++slow] = nums[fast];
}
return slow + 1;
}
};
Given an array nums and a value val, remove all instances of that value in-place
and return the new length.
Do not allocate extra space for another array, you must do this by modifying the
input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond
the new length.
Example 1:
Your function should return length = 2, with the first two elements of nums
being 2.
Your function should return length = 5, with the first five elements of nums
containing 0, 1, 3, 0, and 4.
It doesn't matter what values are set beyond the returned length.
Clarification:
Solution
05/23/2020:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int right = nums.size() - 1, n = nums.size(), cnt = n;
for (int i = 0; i < n && i <= right; ++i) {
while (right >= 0 && nums[right] == val) {
--right;
--cnt;
}
if (nums[i] == val && i <= right) {
swap(nums[i], nums[right--]);
--cnt;
}
}
return cnt;
}
};
04/28/2020:
<=
= mid -
= mid +
= mid -
= mid +
34. Find First and Last Position of Element in Sorted Array
Description
Given an array of integers nums sorted in ascending order, find the starting and
ending position of a given target value.
Example 1:
Solution
04/28/2020:
empty
= mid +
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if (binary_search(nums.begin(), nums.end(), target)) {
auto p = equal_range(nums.begin(), nums.end(), target);
return {int(p.first - nums.begin()), int(p.second - nums.begin() - 1)};
} else {
return {-1, -1};
}
}
};
04/23/2020:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int lo = 0, hi = nums.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return lo;
}
};
as following:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say
sequence. You can do so recursively, in other words from the previous member
read off the digits, counting the number of digits in groups of the same digit.
Example 1:
Input: 1
Output: "1"
Explanation: This is the base case.
Example 2:
Input: 4
Output: "1211"
Explanation: For n = 3 the term was "21" in which we have two groups "2" and
"1", "2" can be read as "12" which means frequency = 1 and value = 2, the same
way "1" is read as "11", so the answer is the concatenation of "12" and "11"
which is "1211".
Solution
05/24/2020:
41. First Missing Positive
Description
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
Solution
05/24/2020:
46. Permutations
Description
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
Solution
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ret{nums};
while (next_permutation(nums.begin(), nums.end())) ret.push_back(nums);
return ret;
}
};
05/26/2020 (Backtracking):
int n = nums.size(),numOfChosen = 0;
vector<bool> chosen(n, false);
vector<vector<int>> ret;
vector<int> permutation;
backtrack(nums, chosen, numOfChosen, permutation, ret);
return ret;
}
05/05/2020:
50. Pow(x, n)
Description
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:
Solution
04/23/2020:
Discussion
Binary Exponentiation: given a positive power n, e.g., n = 22 (binary: 10110) = 16 (binary:
10000) + 4 (binary: 100)+ 2 (binary: 10), then pow(x, n) = x^n = x^22 = x^(16 + 4 +
2) = x^16 * x^4 * x^2. For negative powers, we just flip the sign first, and once we calculate the
value and return its reciprocal.
Time complexity: O(n);
Space complexity: O(1).
class Solution {
public:
double myPow(double x, int n) {
long double ret = 1.0, pow_x = x;
for (long m = n >= 0 ? (long)n : -1 * (long)n; m != 0; m >>= 1) {
if (m & 1) ret *= pow_x;
pow_x = pow_x * pow_x;
}
return n >= 0 ? ret : 1.0L / ret;
}
};
51. N-Queens
Description
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such
that no two queens attack each other.
Example:
Input: 4
Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown
above.
Solution
05/27/2020:
class Solution {
public:
unordered_set<string> seen;
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> ret;
vector<string> board(n, string(n, '.'));
vector<bool> col(n, false);
vector<bool> diag1(2 * n - 1, false);
vector<bool> diag2(2 * n - 1, false);
backtrack(0, n, board, col, diag1, diag2, ret);
return ret;
}
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the
divide and conquer approach, which is more subtle.
Solution
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size(), max_sum = nums[0];
for (int i = 1; i < n; ++i) {
if (nums[i - 1] > 0) nums[i] += nums[i - 1];
max_sum = max(max_sum, nums[i]);
}
return max_sum;
}
};
1984
526
m
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
Solution
04/23/2020:
m =
N = m d = = =
d = + %
55. Jump Game
Description
Each element in the array represents your maximum jump length at that position.
Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
jump length is 0, which makes it impossible to reach the last
index.
Solution
04/24/2020:
class Solution {
public:
bool canJump(vector<int>& nums) {
int ret = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
if (i <= ret) {
ret = max(i + nums[i], ret);
} else {
break;
}
}
return ret >= n - 1;
}
};
59. Spiral Matrix II
Description
Given a positive integer n, generate a square matrix filled with elements from 1
to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
Solution
Discussion:
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if (n <= 0) return {};
vector<vector<int>> dir{ {0, 1}, {1, 0}, {0, -1}, {-1, 0} };
vector<vector<int>> matrix(n, vector<int>(n, 0));
int i = 0, j = -1, d = 0, N = n * n, cnt = 0;
while (cnt < N) {
int ni = i + dir[d][0], nj = j + dir[d][1];
while (ni < 0 || ni >= n || nj < 0 || nj >= n || matrix[ni][nj] != 0) {
d = (d + 1) % 4;
ni = i + dir[d][0];
nj = j + dir[d][1];
}
i = ni;
j = nj;
matrix[i][j] = ++cnt;
}
return matrix;
}
};
By listing and labeling all of the permutations in order, we get the following
sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
Input: n = 3, k = 3
Output: "213"
Example 2:
Input: n = 4, k = 9
Output: "2314"
Solution
05/26/2020:
class Solution {
public:
string getPermutation(int n, int k) {
string s;
for (int i = 0; i < n; ++i) s += to_string(i + 1);
while (--k > 0 && next_permutation(s.begin(), s.end()));
return s;
}
};
The digits are stored such that the most significant digit is at the head of the
list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number
0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Solution
05/23/2020:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
if (digits.empty()) return {1};
int carry = 1, n = digits.size();;
for (int i = n - 1; i >= 0; --i) {
carry += digits[i];
digits[i] = carry % 10;
carry /= 10;
}
if (carry > 0) digits.insert(digits.begin(), carry);
return digits;
}
};
05/12/2020:
class Solution {
public:
string addBinary(string a, string b) {
int i = a.size() - 1, j = b.size() - 1;
int carry = 0;
string ret;
for (; i >= 0 || j >= 0; --i, --j) {
if (i >= 0) carry += a[i] - '0';
if (j >= 0) carry += b[j] - '0';
ret += (carry & 1) + '0';
carry >>= 1;
}
if (carry > 0) ret += carry + '0';
reverse(ret.begin(), ret.end());
return ret;
}
};
69. Sqrt(x)
Description
Solution
04/28/2020:
class Solution {
public:
int mySqrt(int x) {
int lo = 0, hi = x;
while (lo <= hi) {
long long mid = lo + (hi - lo) / 2;
long long sq = mid * mid;
if (sq == x) {
return mid;
} else if (sq > x) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return hi;
}
};
Solution
class Solution {
public:
int climbStairs(int n) {
int s1 = 1, s2 = 2;
for (int i = 2; i < n; ++i) {
s1 = s1 + s2;
swap(s1, s2);
}
return n >= 2 ? s2 : s1;
}
};
05/31/2020:
m= n=
an an m x
has
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Solution
05/23/2020:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty() || matrix[0].empty()) return false;
int m = matrix.size(), n = matrix[0].size(); int
lo = 0, hi = m * n - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int i = mid / n, j = mid % n;
if (matrix[i][j] == target) {
return true;
} else if (matrix[i][j] < target) { lo
= mid + 1;
} else {
hi = mid - 1;
}
}
return false;
}
};
75. Sort Colors
Description
Given an array with n objects colored red, white or blue, sort them in-place so
that objects of the same color are adjacent, with the colors in the order red,
white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white,
and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Follow up:
Solution
05/12/2020:
class Solution {
public:
void sortColors(vector<int>& nums) {
sort(nums.begin(), nums.end());
}
};
77. Combinations
Description
Solution
05/27/2020:
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<int> combination;
vector<vector<int>> ret;
vector<bool> chosen(n + 1, false);
backtrack(1, n, k, chosen, combination, ret);
return ret;
}
78. Subsets
Description
Given a set of distinct integers, nums, return all possible subsets (the power
set).
Example:
Solution
05/26/2020:
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> ret;
vector<int> subset;
backtrack(0, nums, subset, ret);
return ret;
}
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> ret{ {} };
for (auto& n : nums) {
int sz = ret.size();
for (int i = 0; i < sz; ++i) {
ret.push_back(ret[i]);
ret.back().push_back(n);
}
}
return ret;
}
};
79. Word Search
Description
Solution
05/27/2020:
class Solution {
public:
int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
bool exist(vector<vector<char>>& board, string word) {
if (word.empty()) return true;
if (board.empty() || board[0].empty()) return false;
int m = board.size(), n = board[0].size(), k = word.size();
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (backtrack(board, word, i, j, 0))
return true;
return false;
}
Example 1:
Your function should return length = 5, with the first five elements of nums
being 1, 1, 2, 2 and 3 respectively.
Your function should return length = 7, with the first seven elements of nums
being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to
the input array will be known to the caller as well.
Solution
05/27/2020:
=
You are given a target value to search. If found in the array return true,
otherwise return false.
Example 1:
This is a follow up problem to Search in Rotated Sorted Array, where nums may
contain duplicates.
Would this affect the run-time complexity? How and why?
Solution
05/27/2020:
class Solution {
public:
bool search(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
return binary_search(nums.begin(), nums.end(), target);
}
};
Given a sorted linked list, delete all duplicates such that each element appear
only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
Solution
05/27/2020:
== ==
84. Largest Rectangle in Histogram
Description
Given n non-negative integers representing the histogram's bar height where the
width of each bar is 1, find the area of largest rectangle in the histogram.
The largest rectangle is shown in the shaded area, which has area = 10 unit.
Solution
01/14/2020 (Stack):
// for (int i = 0; i < heights.size(); ++i) {
// int l = i, r = i;
// for (; l >= 0 && heights[l] >= heights[i]; --l);
// for (; r < heights.size() && heights[r] >= heights[i]; ++r);
// max_area = max(max_area, heights[i] * (--r - ++l + 1));
// }
// return max_area;
// }
// Approach 4: Stack
int largestRectangleArea(vector<int>& heights) {
stack<int> s;
s.push(-1);
int max_area = 0;
for (int i = 0; i < heights.size(); ++i) {
while (s.top() != -1 && heights[s.top()] >= heights[i]) {
int k = s.top();
s.pop();
max_area = max(max_area, heights[k] * (i - s.top() - 1));
}
s.push(i);
}
while (s.top() != -1) {
int k = s.top();
s.pop();
max_area = max(max_area, heights[k] * ((int)heights.size() - s.top() -
1));
}
return max_area;
}
};
88. Merge Sorted Array
Description
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one
sorted array.
Note:
The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m +
n) to hold additional elements from nums2.
Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Solution
05/23/2020:
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
copy(nums2.begin(), nums2.end(), nums1.begin() + m);
inplace_merge(nums1.begin(), nums1.begin() + m, nums1.end());
}
};
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution
05/20/2020:
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if (!root) return {};
vector<int> ret = inorderTraversal(root->left);
ret.push_back(root->val);
vector<int> right = inorderTraversal(root->right);
ret.insert(ret.end(), right.begin(), right.end());
return ret;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if (!root) return {};
vector<int> ret;
TreeNode* cur = root;
stack<TreeNode*> st;
while (cur || !st.empty()) {
while (cur) {
st.push(cur);
cur = cur->left;
}
cur = st.top(); st.pop();
ret.push_back(cur->val);
cur = cur->right;
}
return ret;
}
};
2 1 2 3
Solution
06/24/2020:
class Solution {
public:
int numTrees(int n) {
long C = 1;
for (int i = 0; i < n; ++i) C = C * 2 * (2 * i + 1) / (i + 2);
return C;
}
};
2 2
3 4 4 3
2 2
3 3
Solution
class Solution {
public:
bool isSymmetric(TreeNode* root) {
stack<pair<TreeNode*, TreeNode*>> st;
st.emplace(root, root);
while (!st.empty()) {
pair<TreeNode*, TreeNode*> cur = st.top(); st.pop();
if (cur.first == nullptr && cur.second == nullptr) continue;
if (cur.first != nullptr && cur.second == nullptr) return false;
if (cur.first == nullptr && cur.second != nullptr) return false;
if (cur.first->val != cur.second->val) return false;
st.emplace(cur.first->left, cur.second->right);
st.emplace(cur.first->right, cur.second->left);
}
return true;
}
};
Recursive DFS:
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isSubtreeSymmetric(root->left, root->right);
}
Given a binary tree, return the level order traversal of its nodes' values. (ie,
from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
Solution
04/26/2020:
103. Binary Tree Zigzag Level Order Traversal
Description
9 20
15 7
Solution
Discussion 04/26/2020:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ret;
if (root == nullptr) return ret;
queue<pair<TreeNode*, int>> q;
q.emplace(root, 0);
while (!q.empty()) {
int n = q.size();
vector<int> level;
int depth = q.front().second;
for (int i = 0; i < n; ++i) {
pair<TreeNode*, int> cur = q.front(); q.pop();
level.push_back(cur.first->val);
if (cur.first->left) q.emplace(cur.first->left, cur.second + 1);
if (cur.first->right) q.emplace(cur.first->right, cur.second + 1);
}
if (depth % 2 != 0) reverse(level.begin(), level.end());
ret.push_back(level);
}
return ret;
}
};
Better:
empty
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
Solution
05/20/2020:
-10 5
Solution
05/27/2020:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if (head == nullptr) return nullptr;
if (head->next == nullptr) return new TreeNode(head->val);
ListNode *slow = head, *fast = head, *preSlow = head;
while (fast && fast->next) {
preSlow = slow;
slow = slow->next;
fast = fast->next->next;
}
110. Balanced Binary Tree
Description
a binary tree in which the left and right subtrees of every node differ in
height by no more than 1.
Example 1:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
Solution
05/27/2020:
2 5
3 4 6
Solution
05/27/2020:
118. Pascal's Triangle
Description
In Pascal's triangle, each number is the sum of the two numbers directly above
it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Solution
05/27/2020:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
if (numRows < 0) return {};
vector<vector<int>> ret(numRows);
for (int i = 0; i < numRows; ++i) {
ret[i].resize(i + 1, 1);
for (int j = 1; j < i; ++j)
ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1];
}
return ret;
}
};
Given a non-negative index k where k ≤ 33, return the kth index row of the
Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above
it.
Example:
Input: 3
Output: [1,3,3,1]
Follow up:
Could you optimize your algorithm to use only O(k) extra space?
Solution
05/27/2020:
class Solution {
public:
vector<int> getRow(int rowIndex) {
int n = rowIndex + 1;
vector<vector<int>> ret(n);
for (int i = 0; i < n; ++i) {
ret[i].resize(i + 1, 1);
for (int j = 1; j < i; ++j) {
ret[i][j] = ret[i - 1][j - 1] + ret[i - 1][j];
}
}
return ret.back();
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1) return 0;
vector<int> diff(n - 1, 0);
for (int i = 0; i < n - 1; ++i) {
diff[i] = prices[i + 1] - prices[i];
}
int max_sum = max(0, diff[0]);
for (int i = 1; i < n - 1; ++i) {
if (diff[i - 1] > 0) diff[i] += diff[i - 1];
max_sum = max(diff[i], max_sum);
}
return max_sum;
}
};
9 0
5 1
Solution
06/26/2020:
num /= 10;
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions
surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
Example:
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
Explanation:
Surrounded regions shouldn’t be on the border, which means that any 'O' on the
border of the board are not flipped to 'X'. Any 'O' that is not on the border
and it is not connected to an 'O' on the border will be flipped to 'X'. Two
cells are connected if they are adjacent cells connected horizontally or
vertically.
Solution
05/08/2020 Discussion:
The idea is borrowed from one of the assignments of the course Algorithms (Part 1):
1. We assume there is a virtual cell (labeled as m * n) that the node on the boundary of the board is
automatically connected to it.
2. We only need to deal with the cells that are 'O': merge these neighboring 'O' cells.
3. Traverse 'O' cells which are stored in visited: if the cell is connected to the virtual cell do nothing;
otherwise, change it to 'X'.
class UnionFind {
private:
vector<int> id;
vector<int> sz;
public:
UnionFind (int n) {
id.resize(n);
iota(id.begin(), id.end(), 0);
sz.resize(n, 1);
}
int find(int x) {
if (x == id[x]) return x;
return id[x] = find(id[x]);
}
class Solution {
public:
void solve(vector<vector<char>>& board) {
if (board.empty() || board[0].empty()) return;
int m = board.size(), n = board[0].size(), bound = m * n;
UnionFind uf(m * n + 1);
int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
unordered_set<int> visited;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'X' || visited.count(i * n + j)) continue;
Update: Inspired by the above, we can actually use a set uncaptured to store all the cells that areconnected
to the bounds. We can always use BFS which starts from the 'O' cells on the boundary to find the inner
cells which are connected to the boundary.
class Solution {
public:
void solve(vector<vector<char>>& board) {
if (board.empty() || board[0].empty()) return;
int m = board.size(), n = board[0].size();
unordered_set<int> uncaptured;
int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
if (board[i][j] == 'X') continue;
queue<int> q;
q.push(i * n + j);
while (!q.empty()) {
int sz = q.size();
for (int s = 0; s < sz; ++s) {
int cur = q.front(); q.pop();
if (uncaptured.count(cur) > 0) continue;
for (int d = 0; d < 4; ++d) {
int ni = cur / n + dir[d][0], nj = cur % n + dir[d][1];
if (ni >= 0 && ni < m && nj >= 0 && nj < n && board[ni][nj] ==
'O') {
q.push(ni * n + nj);
136. Single Number
Description
Given a non-empty array of integers, every element appears twice except for one.
Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it
without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
Solution
05/27/2020:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ret = 0;
for (auto& i : nums) ret ^= i;
return ret;
}
};
Given a non-empty array of integers, every element appears three times except
for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it
without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99
Solution
05/27/2020:
class Solution {
public:
int singleNumber(vector<int>& nums) {
unordered_map<int, int> cnt;
for (auto& i : nums) ++cnt[i];
for (auto& [k, v] : cnt)
if (v == 1)
return k;
return 0;
}
};
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,2,3]
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution
05/20/2020:
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
if (root == nullptr) return {};
vector<int> ret;
ret.push_back(root->val);
vector<int> l = preorderTraversal(root->left);
vector<int> r = preorderTraversal(root->right);
ret.insert(ret.end(), l.begin(), l.end());
ret.insert(ret.end(), r.begin(), r.end());
return ret;
}
};
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution
05/20/2020:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
if (root == nullptr) return ret;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode* cur = st.top(); st.pop();
ret.push_back(cur->val);
if (cur->left != nullptr) st.push(cur->left);
if (cur->right != nullptr) st.push(cur->right);
}
reverse(ret.begin(), ret.end());
return ret;
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
if (!root) return {};
vector<int> ret = postorderTraversal(root->left);
vector<int> right = postorderTraversal(root->right);
ret.insert(ret.end(), right.begin(), right.end());
ret.push_back(root->val);
return ret;
}
};
Follow up:
Could you do both operations in O(1) time complexity?
Example:
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
Solution
Discussion:
04/28/2020:
class Solution {
public:
int findMin(vector<int>& nums) {
if (nums.empty()) return -1;
int n = nums.size();
int lo = 0, hi = n - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) {
lo = mid + 1;
} else {
hi = mid;
}
}
return nums[hi];
}
};
Solution
04/28/2020:
class Solution {
public:
int findPeakElement(vector<int>& nums) {
if (nums.empty()) return -1;
int lo = 0, hi = nums.size() - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (mid - 1 >= lo && nums[mid] < nums[mid - 1]) {
hi = mid - 1;
} else if (mid + 1 <= hi && nums[mid] < nums[mid + 1]) {
lo = mid + 1;
} else {
return mid;
}
}
return lo;
}
};
Given a sorted integer array nums, where the range of elements are in the
inclusive range [lower, upper], return its missing ranges.
Example:
Solution
05/24/2020:
class Solution {
public:
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
if (nums.empty()) {
string s = lower == upper ? to_string(lower) : to_string(lower) + "->" +
to_string(upper);
return {s};
}
long long cur = nums.back(); nums.pop_back();
vector<string> ranges;
string s = cur + 1 < upper ? to_string(cur + 1) + "->" + to_string(upper) :
to_string(cur + 1);
if (cur - 1 >= lower) ranges = findMissingRanges(nums, lower, cur - 1);
if (cur + 1 <= upper) ranges.push_back(s);
return ranges;
}
};
empty
169. Majority Element
Description
Given an array of size n, find the majority element. The majority element is the
element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist
in the array.
Example 1:
Input: [3,2,3]
Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2
Solution
05/06/2020:
173. Binary Search Tree Iterator
Description
Implement an iterator over a binary search tree (BST). Your iterator will be
initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Example:
Note:
next() and hasNext() should run in average O(1) time and uses O(h) memory, where
h is the height of the tree.
You may assume that next() call will always be valid, that is, there will be at
least a next smallest number in the BST when next() is called.
Solution
05/24/2020:
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class BSTIterator {
private:
stack<TreeNode*> st;
TreeNode* cur;
public:
BSTIterator(TreeNode* root) {
cur = root;
}
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
vector<TreeNode*> inorderTraversal(TreeNode* root) {
if (root == nullptr) return {};
vector<TreeNode*> leftTraversal = inorderTraversal(root->left);
vector<TreeNode*> rightTraversal = inorderTraversal(root->right);
leftTraversal.push_back(root);
leftTraversal.insert(leftTraversal.end(), rightTraversal.begin(),
rightTraversal.end());
return leftTraversal;
}
public:
BSTIterator(TreeNode* root) {
inorder = inorderTraversal(root);
reverse(inorder.begin(), inorder.end());
}
demons
M N 2D
room
In order to reach the princess as quickly as possible, the knight decides to
move only rightward or downward in each step.
For example, given the dungeon below, the initial health of the knight must be
at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
-2 (K) -3 3
-5 -10 1
10 30 -5 (P)
Note:
Solution
06/21/2020:
class Solution {
public:
const int inf = numeric_limits<int>::max();
vector<vector<int>> dp;
int rows, cols;
You are a professional robber planning to rob houses along a street. Each house
has a certain amount of money stashed, the only constraint stopping you from
robbing each of them is that adjacent houses have security system connected and
it will automatically contact the police if two adjacent houses were broken into
on the same night.
Example 1:
Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5
(money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
Solution
Given a 2d grid map of '1's (land) and '0's (water), count the number of
islands. An island is surrounded by water and is formed by connecting adjacent
lands horizontally or vertically. You may assume all four edges of the grid are
all surrounded by water.
Example 1:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
Solution
05/08/2020 (Union-Find):
vector<int> sz;
public:
UnionFind(int n) {
id.resize(n);
iota(id.begin(), id.end(), 0);
sz.resize(n, 1);
}
int find(int x) {
if (x == id[x]) return x;
return id[x] = find(id[x]);
}
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if (grid.empty() || grid[0].empty()) return 0;
int m = grid.size(), n = grid[0].size();
UnionFind uf(m * n);
int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '0') continue;
for (int d = 0; d < 4; ++d) {
int ni = i + dir[d][0], nj = j + dir[d][1];
if (ni >= 0 && ni < m && nj >= 0 && nj < n && grid[ni][nj] == '1') {
uf.merge(i * n + j, ni * n + nj);
}
}
}
}
unordered_set<int> components;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int m = grid.size();
if (m == 0) return 0;
int n = grid[0].size(), ret = 0;
stack<pair<int, int>> st;
int d[4][2] = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} };
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
++ret;
st.emplace(i, j);
while (!st.empty()) {
pair<int, int> cur = st.top(); st.pop();
grid[cur.first][cur.second] = '0';
for (int k = 0; k < 4; ++k) {
int ni = cur.first + d[k][0], nj = cur.second + d[k][1];
if (ni > -1 && ni < m && nj > -1 && nj < n && grid[ni][nj] == '1')
st.emplace(ni, nj);
}
}
}
}
}
return ret;
}
};
Iterative DFS:
m =
Recursive DFS:
m =
Example 1:
Solution
05/20/2020:
207. Course Schedule
Description
There are a total of numCourses courses you have to take, labeled from 0 to
numCourses-1.
Some courses may have prerequisites, for example to take course 0 you have to
first take course 1, which is expressed as a pair: [0,1]
Example 1:
Constraints:
Solution
05/29/2020:
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> adj(numCourses);
for (auto& p : prerequisites) adj[p[1]].push_back(p[0]);
vector<bool> path(numCourses);
for (int i = 0; i < numCourses; ++i)
if (isCyclic(i, adj, path))
return false;
return true;
}
05/13/2020:
class Trie {
public:
/** Initialize your data structure here. */
Trie() {
root = new Node();
}
/** Returns if there is any word in the trie that starts with the given
prefix. */
bool startsWith(string prefix) {
Node* cur = find(prefix);
return cur != nullptr;
}
private:
struct Node {
bool isWord;
unordered_map<char, Node*> mp;
Node() : isWord(false) {}
};
Node* root;
word
word
word
/** Inserts a word into the trie. */
void insert(string word) {
int n = word.size();
Node* cur = root;
for (int i = 0; i < n; ++i) {
bool foundCharacter = false;
for (auto& c : cur->children) {
if (word[i] == c->character) {
cur = c;
foundCharacter = true;
break;
}
}
if (!foundCharacter) {
Node* c = new Node(word[i], false);
cur->children.push_back(c);
cur = c;
}
}
cur->isWord = true;
}
/** Returns if there is any word in the trie that starts with the given
prefix. */
bool startsWith(string prefix) {
Node* cur = root;
int n = prefix.size();
for (int i = 0; i < n; ++i) {
bool foundCharacter = false;
for (auto& c : cur->children) {
219. Contains Duplicate II
Description
Example 1:
Solution
05/20/2020:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> mp;
int n = nums.size();
for (int i = 0; i < n; ++i) {
if (mp.count(nums[i]) > 0) {
if (i - mp[nums[i]] <= k) {
return true;
}
}
mp[nums[i]] = i;
}
return false;
}
};
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_set<int> visited;
for (int i = 0; i < (int)nums.size(); ++i) {
if (visited.count(nums[i]) > 0) return true;
visited.insert(nums[i]);
if (i - k >= 0) visited.erase(nums[i - k]);
}
return false;
}
};
Output: 4
Solution
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m = matrix.size();
if (m == 0) return 0;
int n = matrix[0].size();
int ret = 0;
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
dp[i + 1][j + 1] = matrix[i][j] - '0';
if (dp[i + 1][j + 1] > 0) {
dp[i + 1][j + 1] = min(dp[i][j], min(dp[i][j + 1], dp[i + 1][j])) + 1;
ret = max(ret, dp[i + 1][j + 1]);
}
}
}
return ret * ret;
}
};
== 0 ==
m = n =
222. Count Complete Tree Nodes
Description
Note:
Example:
Input:
1
/ \
2 3
/ \ /
4 5 6
Output: 6
Solution
06/23/2020:
226. Invert Binary Tree
Description
3 6 9
7 2
Solution
06/01/2020:
3 6
2 4
Solution
05/20/2020:
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> st;
st.push(root);
unordered_set<TreeNode*> visited;
while (!st.empty()) {
TreeNode* cur = st.top(); st.pop();
if (cur->left && visited.count(cur->left) == 0) {
if (cur->right) st.push(cur->right);
st.push(cur);
st.push(cur->left);
} else {
if (!cur->left && cur->right) st.push(cur->right);
visited.insert(cur);
if (--k == 0) return cur->val;
}
}
return 0;
}
};
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
vector<int> list = BST2vector(root);
return list[k - 1];
}
=
Solution
06/07/2020:
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && builtin_popcount(n) == 1;
}
};
05/10/2020:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
if (node == nullptr) return;
if (node->next == nullptr) {
node = node->next;
} else {
node->val = node->next->val;
node->next = node->next->next;
node = node->next;
}
}
};
06/02/2020:
class Solution {
public:
void deleteNode(ListNode* node) {
*node = *(node->next);
}
};
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
242. Valid Anagram
Description
Example 1:
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution
to such case?
Solution
05/17/2020:
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> cntS(26, 0), cntT(26, 0);
for (int i = 0; i < (int)s.size(); ++i) ++cntS[s[i] - 'a'];
for (int i = 0; i < (int)t.size(); ++i) ++cntT[t[i] - 'a'];
for (int i = 0; i < 26; ++i) {
if (cntS[i] != cntT[i]) {
return false;
}
}
return true;
}
};
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
return s == t;
}
};
A strobogrammatic number is a number that looks the same when rotated 180
degrees (looked at upside down).
Example 1:
Input: "69"
Output: true
Example 2:
Input: "88"
Output: true
Example 3:
Input: "962"
Output: false
Solution
05/18/2020:
class Solution {
public:
bool isStrobogrammatic(string num) {
unordered_map<char, char> mp{ {'0', '0'}, {'1', '1'}, {'6', '9'}, {'8',
'8'}, {'9', '6'} };
string stro = "";
for (auto& n : num) {
if (mp.count(n) == 0) return false;
stro = mp[n] + stro;
}
return stro == num;
}
};