《剑指offer》刷题打卡第6天

面试题16:合并两个排序的链表
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
链表节点定义如下:

struct ListNode
{
int m_nValue;
ListNode *m_pNext;
};

合并两个排序链表的过程:
在这里插入图片描述
在这里插入图片描述
分析合并两个链表的过程,合并两个链表的头节点开始。
链表1的头节点的值小于链表2的头节点的值,因此链表1的头节点将是合并后链表的头节点。
继续合并两个链表中剩余的节点,在两个链表中剩余的节点依然是排序的,因此合并过程同上。
在这里插入图片描述
鲁棒性:
若输入的第一个链表是空链表,则合并后是第二个链表;
若输入的第二个链表是空链表,则合并后是第一个链表;
若输入的两个链表都是空链表,则合并后也是空链表;

代码实现:

/*
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)
            return pHead2;
        else if(pHead2 == nullptr)
            return pHead1;
        
        ListNode *pMergedHead = nullptr;
        
        if(pHead1->val < pHead2->val)//第一个指针指向的节点小于第二个指针指向的节点,把第一个节点当作头节点
        {
            pMergedHead = pHead1;
            pMergedHead->next = Merge(pHead1->next,pHead2);
        }
        else{
            pMergedHead = pHead2;
            pMergedHead->next = Merge(pHead1,pHead2->next);
        }
        return pMergedHead;
    }
};

在这里插入图片描述
面试题17:树的子结构
输入两棵二叉树A和B,判断B是不是A的子结构。
二叉树节点的定义如下:

struct BinaryTreeNode
{
double  m_dbValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};

在这里插入图片描述
解题思路:
第一步:在树A中找到和树B的根节点的值一样的节点R;
第二步:判断树A中以R为根节点的子树是不是包含和树B一样的结构;

第一步在树A中查找与根节点的值一样的节点,这实际上就是树的遍历,可以采用递归或者循环去遍历树。
在这里插入图片描述
注意边界条件的检查,即检查空指针,当树A或树B为空的时候,定义相应的输出。

上述代码中,我们递归调用HasSubtree遍历二叉树A。如果发现某一节点的值和树B的头节点的值相同,则调用DoesTree1HaveTree2,进行第二步判断。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码实现:

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        bool result = false;
        
        if(pRoot1 != nullptr && pRoot2 != nullptr)
        {
            if(Equal(pRoot1->val,pRoot2->val))
                result = DoesTree1HaveTree2(pRoot1,pRoot2);
            if(!result)
                result = HasSubtree(pRoot1->left,pRoot2);
            if(!result)
                result = HasSubtree(pRoot1->right,pRoot2);
        }
           return result;
    }
    bool DoesTree1HaveTree2(TreeNode *pRoot1,TreeNode *pRoot2)
    {
        if(pRoot2 == nullptr)
            return true;
        if(pRoot1 == nullptr)
            return false;
        if(!Equal(pRoot1->val,pRoot2->val))
            return false;
        return DoesTree1HaveTree2(pRoot1->left,pRoot2->left)&&
        DoesTree1HaveTree2(pRoot1->right,pRoot2->right);
    }
    bool Equal(double num1,double num2)
    {
        if(num1-num2>-0.00001&&num1-num2<0.00001)
            return true;
        else
        return false;
    }
};

面试题18:二叉树的镜像
题目:请完成一个函数,输入一棵二叉树,该函数输出它的镜像。二叉树定义的节点定义如下:

struct BinaryTreeNode
{
int  m_nValue;
BinaryTreeNode * m_pLeft;
BinaryTreeNode *m_pRight;
};

在这里插入图片描述
先前序遍历这棵树的每个节点,如果遍历到节点有子节点,就交换它的两个子节点。当交换完所有非叶节点的左右子节点之后,就得到了树的镜像。

代码实现:

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) {
     //排除根节点或左右子节点不存在的情况
        if(pRoot == nullptr)
         return;
        if(pRoot->left == nullptr && pRoot->right == nullptr)
            return;
        //交换两个子节点
       TreeNode *pTemp = pRoot->left;
        pRoot->left = pRoot->right;
        pRoot->right = pTemp;
        
        if(pRoot->left)
            Mirror(pRoot->left);
            
            if(pRoot->right)
            Mirror(pRoot->right);

    }
};

在这里插入图片描述
面试题19:顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。例如:如果输入以下矩阵:
在这里插入图片描述
补充知识:
标准库类型vector
表示对象的集合,其中所有对象的类型都相同。集合中的每个对象都有一个与之对应的索引,索引用于访问对象。因为vector容纳着其他对象,所以它也常被称作容器。
要想使用vector,必须包含适当的头文件。
#include
Using std::vector
C++语言有类模板,也有函数模板,其中vector是一个类模板
Vector 成员函数:push_back:负责把一个值当成vector对象的尾元素“压到(push)”vector对象的“尾端(back)”
在这里插入图片描述
在这里插入图片描述
求容器的行:容器名.size()
求容器的列:容器名[0].size()

实现代码:

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
      int row = matrix.size();
      int col = matrix[0].size();
        vector<int> result;
        
        //处理非法输入
        if(row == 0 || col == 0)
            return result;
        //定义四个关键变量,表示左上和右下的打印范围
        int left = 0,top = 0,right = col - 1,bottom = row - 1;
        
        while(left <= right && top <= bottom)
        {
            //从左到右
            for(int i = left;i <= right;i++)
            {
                result.push_back(matrix[top][i]);
            }
            
            //从上到下
            for(int i = top + 1;i <= bottom;i++)
            {
                result.push_back(matrix[i][right]);
            }
            
            //从右到左
            if(top != bottom)
                for(int i = right - 1;i >= left;i--)
                {
                    result.push_back(matrix[bottom][i]);
                }
            
            //从下到上
            if(left != right)
            for(int i = bottom - 1;i > top; i--)
            {
                result.push_back(matrix[i][left]);
            }
            
            left++,top++,right--,bottom--;
        }
        return result;
    }
};

面试题20:包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数。在该栈中,调用min、push及pop的时间复杂度都是o(1)。

实现代码:

class Solution {
public:
    void push(int value) {
        stackInt.push(value);
            if(stackMin.empty())
                stackMin.push(value);
            else if(stackMin.top() < value)
                stackMin.push(stackMin.top());
            else
                stackMin.push(value);
    }
    
    void pop() {
        if(!stackInt.empty())
        {
            stackInt.pop();
            stackMin.pop();
        }
    }
    int top() {
        return stackInt.top();
    }
    
    int min() {
        return stackMin.top();
    }
    private:
    stack<int> stackInt;
    stack<int> stackMin;
};

猜你喜欢

转载自blog.csdn.net/tpf930726/article/details/88976876