Here’s a collection of common yet essential LeetCode problems that we’ve solved with clean logic and explanations. These cover strings, arrays, trees, maps, and custom data structures — perfect for building problem-solving muscle.
Problem: Check if string s is a subsequence of t.
Use two pointers: advance both if characters match; otherwise, move pointer of t.
bool isSubsequence(string s, string t) {
int j = 0;
for (int i = 0; i < t.size(); i++)
if (j < s.size() && s[j] == t[i]) j++;
return j == s.size();
}
Problem: Find two lines that form the container with maximum water.
Two-pointer approach from both ends, always move the smaller height.
int maxArea(vector<int>& height) {
int i = 0, j = height.size() - 1, area = 0;
while (i < j) {
area = max(area, min(height[i], height[j]) * (j - i));
(height[i] < height[j]) ? i++ : j--;
}
return area;
}
Problem: Return the nth row of Pascal's Triangle.
Build the triangle row-by-row.
vector<int> getRow(int rowIndex) {
vector<int> row = {1};
for (int i = 1; i <= rowIndex; i++) {
vector<int> temp = {1};
for (int j = 0; j < row.size() - 1; j++)
temp.push_back(row[j] + row[j + 1]);
temp.push_back(1);
row = temp;
}
return row;
}
Problem: Reverse bits of a 32-bit unsigned integer.
Shift and add bits one by one.
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; i++) {
res <<= 1;
res |= (n & 1);
n >>= 1;
}
return res;
}
Problem: Determine if a number eventually becomes 1 by repeatedly summing the square of its digits.
Use set to detect loops.
bool isHappy(int n) {
unordered_set<int> seen;
while (n != 1 && !seen.count(n)) {
seen.insert(n);
int sum = 0;
while (n) {
int d = n % 10;
sum += d * d;
n /= 10;
}
n = sum;
}
return n == 1;
}
Problem: Remove all nodes from the list that have a given value.
Handle edge cases (head node removal), then loop.
ListNode* removeElements(ListNode* head, int val) {
while (head && head->val == val) head = head->next;
ListNode* curr = head;
while (curr && curr->next) {
if (curr->next->val == val)
curr->next = curr->next->next;
else
curr = curr->next;
}
return head;
}
Problem: Determine if two strings can be mapped character-by-character bijectively.
Map s[i] → t[i] and ensure mapping is consistent and one-to-one.
bool isIsomorphic(string s, string t) {
unordered_map<char, char> m1, m2;
for (int i = 0; i < s.size(); i++) {
if (m1.count(s[i]) && m1[s[i]] != t[i]) return false;
if (m2.count(t[i]) && m2[t[i]] != s[i]) return false;
m1[s[i]] = t[i];
m2[t[i]] = s[i];
}
return true;
}
Problem: Mirror a binary tree.
Swap left and right at every node recursively.
TreeNode* invertTree(TreeNode* root) {
if (!root) return nullptr;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
Problem: Return all root-to-leaf paths in a binary tree.
DFS traversal and backtracking.
void dfs(TreeNode* root, vector<string>& res, string path) {
if (!root) return;
path += "->" + to_string(root->val);
if (!root->left && !root->right)
res.push_back(path.substr(2));
dfs(root->left, res, path);
dfs(root->right, res, path);
}
Problem: Implement a queue using two stacks.
Reverse stack every time in push() to keep front on top.
class MyQueue {
stack<int> st1, st2;
public:
void push(int x) {
while (!st1.empty()) {
st2.push(st1.top());
st1.pop();
}
st1.push(x);
while (!st2.empty()) {
st1.push(st2.top());
st2.pop();
}
}
int pop() { int x = st1.top(); st1.pop(); return x; }
int peek() { return st1.top(); }
bool empty() { return st1.empty(); }
};
This collection covers a variety of interview-style problems using clean and optimal C++ solutions, with a strong focus on clarity, edge-case handling, and real-world patterns.
0
2
0