第16题:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
//递归
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1 == nullptr)
return pHead2;
if(pHead2 == nullptr)
return pHead1;
if(pHead1->val <= pHead2->val)
{
pHead1->next = Merge(pHead1->next, pHead2);
return pHead1;
}
else
{
pHead2->next = Merge(pHead1, pHead2->next);
return pHead2;
}
}
};
//循环
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1 == nullptr)
{
return pHead2;
}
if(pHead2 == nullptr)
{
return pHead1;
}
ListNode* MergeNode;
ListNode* pHead;
if(pHead1->val <= pHead2->val)
{
MergeNode = pHead1;
pHead1 = pHead1->next;
}
else
{
MergeNode = pHead2;
pHead2 = pHead2->next;
}
pHead = MergeNode;
while(pHead1 != nullptr && pHead2 != nullptr)
{
if(pHead1->val <= pHead2->val)
{
pHead->next = pHead1;
pHead1 = pHead1->next;
pHead = pHead->next;
}
else
{
pHead->next = pHead2;
pHead2 = pHead2->next;
pHead = pHead->next;
}
}
//必须判断,出了两次问题了
if(pHead1 == nullptr)
pHead->next = pHead2;
if(pHead2 == nullptr)
pHead->next = pHead1;
return MergeNode;
}
};
第17题:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
//树的子结构
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if(pRoot2 == nullptr || pRoot1 == nullptr)
return false;
return (pRoot1HaspRoot2(pRoot1, pRoot2) || HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2));
}
bool pRoot1HaspRoot2(TreeNode* node1, TreeNode* node2)
{
if(node2 == nullptr)
return true;
if(node1 == nullptr)
return false;
return (node1->val == node2->val && pRoot1HaspRoot2(node1->left, node2->left) && pRoot1HaspRoot2(node1->right, node2->right));
}
};
第18题:操作给定的二叉树,将其变换为源二叉树的镜像。
//二叉树的镜像
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot == nullptr || (pRoot->left == nullptr && pRoot->right == nullptr))
return;
TreeNode *tmp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = tmp;
if(pRoot->left)
Mirror(pRoot->left);
if(pRoot->right)
Mirror(pRoot->right);
}
};
//栈实现
//操作二叉树的方法,先创建一个空的根节点,不能用pRoot直接操作
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot == nullptr)
return;
stack<TreeNode*> s;
TreeNode* p = nullptr;
s.push(pRoot);
while(s.size())
{
p = s.top();
s.pop();
swap(p->left, p->right);
if(p->left)
s.push(p->left);
if(p->right)
s.push(p->right);
}
}
};
//队列实现
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot == nullptr)
return;
queue<TreeNode*> q;
TreeNode* p = nullptr;
q.push(pRoot);
while(q.size())
{
p = q.front();
q.pop();
swap(p->left, p->right);
if(p->left)
q.push(p->left);
if(p->right)
q.push(p->right);
}
}
};
第19题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
//顺时针打印二维数组
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> res;
res.clear();
int row = matrix.size();
int col = matrix[0].size();
int left = 0, right = col-1, top = 0,bottom = row-1;
while(left <= right && top <= bottom)
{
for(int i = left; i <= right; ++i)
{
res.push_back(matrix[top][i]);
}
for(int i = top+1; i <= bottom; ++i)
{
res.push_back(matrix[i][right]);
}
if(top != bottom)
{
for(int i = right-1; i >= left; --i)
{
res.push_back(matrix[bottom][i]);
}
}
if(right != left)
{
for(int i = bottom-1; i > top;--i)
{
res.push_back(matrix[i][left]);
}
}
left++,right--,top++,bottom--;
}
return res;
}
};
第20题:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))
//包含min函数的栈
class Solution {
public:
stack<int> s1, s2;
void push(int value) {
s1.push(value);
if(s2.empty())
{
s2.push(value);
}
else{
if(value <= s2.top())
{
s2.push(value);
}
}
}
void pop() {
//如果弹出最小元素,下次还需要弹出次小元素,所以必须判断
if(s1.top() == s2.top())
s2.pop();
s1.pop();
}
int top() {
return s1.top();
}
int min() {
return s2.top();
}
};