看到第16题的时候,百感交集,面试拼多多的时候因为这个题突然卡壳,浪费了一次非常棒的机会,这也是我为什么要重新刷一次的原因,希望自己加油能够亡羊补牢吧。
反转链表
题目描述:输入一个链表,反转链表后,输出新链表的表头。
题目分析:这道题我相信我会记一辈子,当你走到最后一轮面试的时候,却因为这道题卡住了,脑子傻了。结果前功尽弃,唉
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if (pHead == nullptr)return nullptr;
if (pHead->next == nullptr)return pHead;
ListNode* pre=new ListNode(0);
ListNode* cur = pHead;
ListNode* after = pHead->next;
while (cur!=nullptr) {
cur->next = pre->next;
pre->next = cur;
cur = after;
if(after!=nullptr)after = after->next;
}
return pre->next;
}
};
合并两个排序链表
题目描述:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
题目分析:这道题其实挺简单,但是我花费了较多的时间,这个题应该是把两个链表合并,不需要额外申请空间把值附加过去,所以直接指向原值即可。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if (pHead1 == nullptr && pHead2 == nullptr)return nullptr; //特殊情况1
if (pHead1 == nullptr && pHead2 != nullptr)return pHead2; //特殊情况2
if (pHead1 != nullptr && pHead2 == nullptr)return pHead1; //特殊情况3
ListNode* root = new ListNode(-1);
ListNode* p0 = root;
ListNode* p1 = pHead1;
ListNode* p2 = pHead2;
while (p1 != nullptr || p2 != nullptr) {
if (p1 != nullptr && p2 == nullptr) { //p2到头,p1还有值
p0->next = p1;
p1 = p1->next;
p0 = p0->next;
continue;
}
if (p1 == nullptr && p2 != nullptr) { //p1到头,p2还有值
p0->next = p2;
p2 = p2->next;
p0 = p0->next;
continue;
}
if (p1 != nullptr && p2 != nullptr) { //p1和p2都有值的情况
if (p1->val <= p2->val) {
p0->next = p1;
p1 = p1->next;
p0 = p0->next;
}
else {
p0->next = p2;
p2 = p2->next;
p0 = p0->next;
}
continue;
}
}
return root->next;
}
};
树的子结构
题目描述:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
题目分析:树的题目一般都需要递归分析判断,因此这个题很自然的选择递归分析即可,关键就是找到B的根节点。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
bool IsSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
if (pRoot2 == nullptr)return true;
if (pRoot1==nullptr)return false;
if (pRoot1->val == pRoot2->val) {
return IsSubtree(pRoot1->left, pRoot2->left)&& IsSubtree(pRoot1->right, pRoot2->right);
}
else{
return false;
}
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if (pRoot1 == nullptr)return false;
if (pRoot2 == nullptr)return false;
int count;
if (pRoot1->val == pRoot2->val) {
count= IsSubtree(pRoot1, pRoot2);
}
return count+HasSubtree(pRoot1->left, pRoot2) + HasSubtree(pRoot1->right, pRoot2);
}
};
二叉树的镜像
题目描述:操作给定的二叉树,将其变换为源二叉树的镜像。
题目分析:这道题的核心是交换左右子树。
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if (pRoot == nullptr)return;
if (pRoot->left != nullptr || pRoot->right != nullptr) {
TreeNode* temp= pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = temp;
}
Mirror(pRoot->left);
Mirror(pRoot->right);
}
};
顺时针打印矩阵
题目描述:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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) {
int xnumber = matrix.size(); //判断矩阵的行数
int ynumber = matrix[0].size(); //判断矩阵的列数
vector<int>output;
if (xnumber == 0)return output;
int xstart = 0; int ystart = 0; //寻找下界
int xend = xnumber - 1; //寻找的上界
int yend = ynumber - 1;
while (xstart<=xend && ystart<=yend) { //循环终止条件
for (int i = ystart; i <= yend; i++) { //right
output.push_back(matrix[xstart][i]);
}
for (int i = xstart + 1; i <= xend; i++) { //down
output.push_back(matrix[i][yend]);
}
for (int i = yend - 1; i >=ystart; i--) { //left
if (xend != xstart) { //此处要注意防止只有一行时,往返重复打印
output.push_back(matrix[xend][i]);
}
}
for (int i = xend - 1; i > xstart; i--) { //up
if (ystart != yend) { //此处要注意防止只有一列时,往返重复打印
output.push_back(matrix[i][ystart]);
}
}
xstart++;
ystart++;
xend--;
yend--;
}
return output;
}
};
包含min函数的栈
题目描述:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
题目分析:这个题的难点就是要在栈中增加一个min函数,这要求我们在新元素添加进去后要判断这个元素是不是当前最小值并添加进去,因此考虑设计一个最小值栈,用来存放当前数据的最小值。
class Solution {
public:
void push(int value) {
mainstack.push(value);
if (minstack.empty() || value < minstack.top()) {
minstack.push(value);
}
}
void pop() {
if (mainstack.top() == minstack.top()) {
minstack.pop();
}
mainstack.pop();
}
int top() {
return mainstack.top();
}
int min() {
return minstack.top();
}
private:
stack<int>mainstack;
stack<int>minstack;
};