You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Questions: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' to 1...
Idea: dfs + dp;
class Solution {
vector<int> dp;
int nDec(string &s, int i){
if(i==s.size()) return 1;
if(i==s.size()-1) return s[s.size()-1] !='0';
if(dp[i]>=0) return dp[i];
int x = int(s[i]-'0'), y = int(s[i+1]-'0');
return dp[i] = int(x!=0)*(nDec(s,i+1) + int(x*10+y<=26)*nDec(s,i+2));
}
public:
int numDecodings(string s) {
if(s.empty()) return 0;
dp = vector<int>(s.size(),-1);
return nDec(s,0);
}
};
92, Reverse Linked List II
Questions: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Idea: One pass doing the job:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(!head || !head->next || m==n) return head;
ListNode preHead(0);
preHead.next = head;
auto p = &preHead;
for(int i=1;i<m;++i) p = p->next;
auto p1 = p->next, pt = p->next;
auto p2 = p1->next;
for(int i=m;i<n && p2;i++){
auto tmp = p2->next;
p2->next = p1;
p1 = p2;
p2 = tmp;
}
pt->next = p2;
p->next = p1;
return preHead.next;
}
};
93, Restore IP Addresses
Questions: Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
Idea: dfs (Need to care about the special case when s[i]=='0')
typedef vector<int> vi;
class Solution {
void help(vector<vi> &res,vi cur, string &s, int i, int k){
if(k==4){
if(i==s.size()) res.push_back(cur);
return;
}
if(i==s.size()) return;
if(s[i] == '0'){
cur.push_back(0);
help(res,cur,s,i+1,k+1);
return;
}
for(int j=i+1;j<=s.size();j++){
int m = stoi(s.substr(i,j-i));
if(m>255) return;
cur.push_back(m);
help(res,cur,s,j,k+1);
cur.pop_back();
}
return;
}
public:
vector<string> restoreIpAddresses(string s) {
vector<vi> res;
help(res,vi(),s,0,0);
vector<string> ans;
for(auto vec:res){
s = to_string(vec[0]);
s += "." + to_string(vec[1]) + "." + to_string(vec[2]) + "." + to_string(vec[3]);
ans.push_back(s);
}
return ans;
}
};
94, Binary Tree Inorder Traversal
Questions: Given a binary tree, return the inorder traversal of its nodes' values. (left -> root -> right)
Questions: Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
Idea: using dfs //Note that, every time if we need to add new tree into the set, we need to create a new root instead of the original one.
Questions: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
Idea: Using dfs (number of unique BST is only depends on the number of node n) //Simple mathematical problem
Questions: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca" When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false.
Idea: Using dp //dp[i][j] denote whether s1.substr(0,i) and s2.substr(0,j) are forming s3.substr(0,i+j).
Questions: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
Questions: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
Questions: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Idea: Recursion?
/**
* 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:
int minDepth(TreeNode* root) {
if(!root) return 0;
if(!root->left) return 1 + minDepth(root->right);
if(!root->right) return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left),minDepth(root->right));
}
};
112, Path Sum
Questions: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Questions: Given a string S and a string T, count the number of distinct subsequences of T in S. Here is an example: S = "rabbbit", T = "rabbit", then Return 3.
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.
117, Populating Next Right Pointers in Each Node II
Questions: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work?