1.Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:
A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3
if(headA==NULL||headB==NULL)
return NULL;
ListNode* midA=headA;
ListNode* midB=headB;
int count1=0,count2=0;
while(headA->next!=NULL)
{
headA=headA->next;
count1++;
}
while(headB->next!=NULL)
{
headB=headB->next;
count2++;
}
if(headA!=headB)
return NULL;
else
{
if(count1>count2)
{
int num=count1-count2;
while(num>0)
{
midA=midA->next;
num--;
}
}
else
{
int num=count2-count1;
while(num>0)
{
midB=midB->next;
num--;
}
}
while(midA!=midB)
{
midA=midA->next;
midB=midB->next;
}
return midA;
}
}
2.Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
class MinStack {
public:
MinStack()
{
min=INT_MAX;
}
void push(int x) {
if(x<min) min=x;
mystack.push_back(x);
}
void pop() {
if(mystack.back()==min)
{
mystack.pop_back();
min=INT_MAX; int n=mystack.size();
for(int i=0;i<n;i++)
{
if(mystack[i]<min)
{
min=mystack[i];
}
}
}
else
{
mystack.pop_back();
}
}
int top() {
return mystack.back();
}
int getMin() {
return min;
}
private:
vector<int> mystack;
int min;
};
3.Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
int len=s.length();
if(len<=1)
return true;
int i=0;
while(i<len)
{
while ((s[i]<'0' || (s[i]>'9'&&s[i]<'A') || (s[i]>'Z'&&s[i]<'a')|| s[i]>'z')&&i<len)
{
i++;
}
while ((s[len-1]<'0' || (s[len-1]>'9'&&s[len-1]<'A') || (s[len-1]>'Z'&&s[len-1]<'a')|| s[len-1]>'z')&&i<len)
{
len--;
}
if (i>=len)
{
return true;
}
if(s[i]!=s[len-1]&&abs(s[i]-s[len-1])!=32)
{
return false;
}
i++;
len--;
}
return true;
}
4.Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]vector<vector<int> > generate(int numRows) {
vector<vector<int> > res;
if (numRows < 1) return res;
vector<int> row1(1, 1);
res.push_back(row1);
if (numRows== 1)
{
return res;
}
for (int i = 1; i <numRows; i++)
{
vector<int> row;
row.push_back(1);
for (int j = 1; j < i; j++)
{
row.push_back(res.at(i - 1).at(j - 1) + res.at(i - 1).at(j));
}
row.push_back(1);
res.push_back(row);
}
return res;
}
5.Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
vector<int> result;
result.push_back(1);
for(int i=1;i<=rowIndex;i++)
{
int temp=1;
for(int j=1;j<i;j++)
{
int last=result[j];
result[j]=temp+result[j];
temp=last;
}
result.push_back(1);
}
return result;
}
6.Path Sum
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.
该题实际就是一个深度优先遍历问题。
bool depth(TreeNode *root, int sum,int res)
{
if(root==NULL)
return false;
if(root->left==NULL&&root->right==NULL)
return sum==res+root->val;
return depth(root->left,sum,res+root->val)||depth(root->right,sum,res+root->val);
}
bool hasPathSum(TreeNode *root, int sum) {
depth(root,sum,0);
}
7.Minimum Depth of Binary Tree
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.
采用层次遍历的方法
int minDepth(TreeNode *root) {
if(root==NULL) return 0;
TreeNode *p=root;
int cur=0;
queue<pair<TreeNode*, int> > res;
res.push(make_pair(p,1));
while(!res.empty())
{
pair<TreeNode*, int> cur=res.front();
res.pop();
if(cur.first->left==NULL&&cur.first->right==NULL) return cur.second;
if(cur.first->left)
{
res.push(make_pair(cur.first->left,cur.second+1));
}
if(cur.first->right)
{
res.push(make_pair(cur.first->right,cur.second+1));
}
}
}
8.Maximum Depth of Binary Tree
int maxDepth(TreeNode *root) {
if(root==NULL) return 0;
TreeNode *p=root;
int result=0;
queue<pair<TreeNode*, int> > res;
res.push(make_pair(p,1));
while(!res.empty())
{
pair<TreeNode*, int> cur=res.front();
res.pop();
if(cur.first->left)
{
res.push(make_pair(cur.first->left,cur.second+1));
}
if(cur.first->right)
{
res.push(make_pair(cur.first->right,cur.second+1));
}
result=cur.second;
}
return result;
}
9.Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
int getDepth(TreeNode* tree, int currentDepth){if(tree == NULL){
return currentDepth;
}
return max(getDepth(tree->left, currentDepth+1),
getDepth(tree->right, currentDepth+1));
}
bool isBalanced(TreeNode *root) {
if(root==NULL) return true;
int ldepth = getDepth(root->left, 1);
int rdepth = getDepth(root->right, 1);
if(abs(ldepth-rdepth) > 1){
return false;
}else{
return isBalanced(root->left) && isBalanced(root->right);
}
}
10.Binary Tree Level Order Traversal
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,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > ret;
if(root==NULL) return ret;
vector<int> a;
TreeNode *p=root;
int curlevel=-1;
queue<pair<TreeNode*, int> > res;
res.push(make_pair(p,0));
while(!res.empty())
{
pair<TreeNode*, int> cur=res.front();
res.pop();
if(cur.first->left)
{
res.push(make_pair(cur.first->left,cur.second+1));
}
if(cur.first->right)
{
res.push(make_pair(cur.first->right,cur.second+1));
}
if(curlevel!=cur.second)
{
if(curlevel!=-1)
ret.push_back(a);
a.clear();
curlevel=cur.second;
a.push_back(cur.first->val);
}
else
a.push_back(cur.first->val);
}
ret.push_back(a);
return ret;
}