熟悉一下牛客网的编译器,面试笔试肯定用的到~
不间断更新中。。
1、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.
class Solution {
public:
int run(TreeNode *root) {
if(!root)
return 0;
if(!root->left && !root->right)
return 1;
else if(!root->left || !root->right)
return 1 + max(run(root->left), run(root->right));
else return 1 + min(run(root->left), run(root->right));
}
};
注:本题需要注意找的是最小深度,只有左右节点全为空才是最小深度。
2、evaluate-reverse-polish-notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
class Solution {
public:
int evalRPN(vector<string> &tokens) {
if(tokens.size()==0)
return 0;
stack<string> s;
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]!="+" && tokens[i]!="-" && tokens[i]!="*" && tokens[i]!="/")
s.push(tokens[i]);
else calculate(s, tokens[i]);
}
return stoi(s.top());
}
void calculate(stack<string> &s, string sign)
{
int num2 = stoi(s.top());
s.pop();
int num1 = stoi(s.top());
s.pop();
int num;
if(sign == "+")
num = num1 + num2;
if(sign == "-")
num = num1 - num2;
if(sign == "*")
num = num1 * num2;
if(sign == "/")
num = num1 / num2;
s.push(to_string(num));
}
};
注:本题一点都没有考虑特殊情况,包括tokens里的逆波兰表达式非法,运算结果过大,除数为0等。
3、max-points-on-a-line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
1
注:本题需要注意找的是最小深度,只有左右节点全为空才是最小深度。
4、sort-list
Sort a linked list in O(n log n) time using constant space complexity.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
if(!head || !head->next)
return head;
ListNode *slow = head, *fast = head, *ptr;
while(fast && fast->next)
{
ptr = slow;
slow = slow->next;
fast = fast->next->next;
}
ptr->next = nullptr;
ListNode *first = sortList(head);
ListNode *second = sortList(slow);
ListNode *result = merge(first, second);
return result;
}
ListNode * merge(ListNode *first, ListNode *second)
{
ListNode *m = new ListNode(0);
ListNode * p = m;
while(first || second)
{
int a = INT_MAX, b = INT_MAX;
if(first)
a = first->val;
if(second)
b = second->val;
if(a>b)
{
ListNode *cache = new ListNode(b);
p->next = cache;
second = second->next;
}
else
{
ListNode *cache = new ListNode(a);
p->next = cache;
first = first->next;
}
p = p->next;
}
return m->next;
}
};
注:采用归并排序的方法。
5、max-points-on-a-line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
1
注:本题需要注意找的是最小深度,只有左右节点全为空才是最小深度。
6、binary-tree-postorder-traversal
Given a binary tree, return the postorder traversal of its nodes' values.
/**
* Definition for binary tree
* 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) {
if(!root)
return {};
vector<int> output;
stack<TreeNode*> s;
s.push(root);
while(!s.empty())
{
TreeNode *p = s.top();
output.push_back(p->val);
s.pop();
if(p->left)
s.push(p->left);
if(p->right)
s.push(p->right);
}
reverse(output.begin(), output.end());
return output;
}
};
注:无
7、binary-tree-preorder-traversal
Given a binary tree, return the preorder traversal of its nodes' values.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
if(!root)
return {};
vector<int> output;
stack<TreeNode*> s;
s.push(root);
while(!s.empty())
{
TreeNode *p = s.top();
output.push_back(p->val);
s.pop();
if(p->right)
s.push(p->right);
if(p->left)
s.push(p->left);
}
return output;
}
};
注:用栈即可。
8、reorder-list
Given a singly linked list L: L 0→L 1→…→L n-1→L n,
reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…
You must do this in-place without altering the nodes' values.
For example: Given{1,2,3,4}, reorder it to{1,4,2,3}.
用栈:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
stack<ListNode *> s;
ListNode *p = head;
while(p)
{
s.push(p);
p = p->next;
}
while(!s.empty())
{
if(s.top() == head)
{
head->next = nullptr;
return;
}
else if(s.top() == head->next)
{
head->next->next = nullptr;
return ;
}
else
{
ListNode *q = s.top();
ListNode *cache = head->next;
head->next = q;
q->next = cache;
s.pop();
head = head->next->next;
}
}
}
};
原地:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
if(!head)
return;
ListNode *slow = head, *fast = head, *ptr;
while(fast && fast->next)
{
ptr = slow;
slow = slow->next;
fast = fast->next->next;
}
ptr->next = nullptr;
ListNode *first = head;
ListNode *second = reverse(slow);
if(first == second)
return ;
while(first->next)
{
ListNode *cache1 = first->next;
ListNode *cache2 = second->next;
first->next = second;
second->next = cache1;
first = first->next->next;
second = cache2;
}
first->next = second;
}
ListNode *reverse(ListNode *head)
{
ListNode *p = nullptr;
ListNode *q = head;
while(q)
{
ListNode *r = q->next;
q->next = p;
p = q;
q = r;
}
return p;
}
};
注:原地修改需要找到中间结点然后断开,翻转后一段的链表,然后合并两个链表。
9、linked-list-cycle-ii
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
Can you solve it without using extra space?
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *m = hasCycle(head);
if(!m)
return m;
int num = 1;
ListNode *p = m;
while(p->next != m)
{
num++;
p = p->next;
}
ListNode *slow = head, *fast = head;
while(num)
{
fast = fast->next;
num--;
}
while(slow!=fast)
{
slow = slow->next;
fast = fast->next;
}
return slow;
}
ListNode *hasCycle(ListNode *head) {
if(!head)
return nullptr;
ListNode *slow = head, *fast = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return slow;
}
return nullptr;
}
};
注:计算出环的长度L,然后让快指针先走L步。
10、linked-list-cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head)
return false;
ListNode *slow = head, *fast = head;
while(fast && fast->next)