4.7力扣刷题记录

1.旋转字符串–KMP

在这里插入图片描述
题目地址
KMP字符串匹配算法,找出 p 0 . . . p j − 1 p_0...p_{j-1} p0...pj1中前缀子串和后缀子串相同的最大值。
KMP算法,包括改进后的next数组
在这里插入图片描述
代码如下

class Solution {
public:
    bool rotateString(string s, string goal) {
        if(s.size()!=goal.size())
            return false;
        vector<int> next(goal.size());
        getnext(next,goal);
        string str=s+s;
        int pos=0;
        for (int i = 0; i < str.size(); ) {//匹配
            while(i<str.size()&&pos<goal.size()&&str[i]==goal[pos]){
                pos++;
                i++;
            }
            if(pos==0)
                i++;
            else if(pos==goal.size())
                return true;
            else
                pos=next[pos];
        }
        return false;
    }
    void getnext(vector<int> next,string s){//next数组
        next[0]=-1;
        int k=-1;
        for(int i=1;i<s.size();){
            if(k==-1||s[i]==s[k]){
                k++;
                next[i]=k;
                i++;
            }
            else{
                k=next[k];
            }
        }

    }
};

2.二叉树前中后遍历的迭代写法

前序遍历

//前序遍历
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        if(root==NULL)
            return ans;
        stack<TreeNode* > s;
        s.push(root);
        while(!s.empty()){
            auto temp=s.top();
            s.pop();
            ans.push_back(temp->val);
            if(temp->right!=NULL)
                s.push(temp->right);
            if(temp->left!=NULL)
                s.push(temp->left);
        }

        return ans;
    }
};

中序遍历

//中序遍历
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if(root==nullptr)
            return {};
        vector<int> ans;
        stack<TreeNode* > s;
        while(!s.empty()||root!=nullptr){
            while(root!=nullptr){
                s.push(root);
                root=root->left;
            }
            root = s.top();
            s.pop();
            ans.push_back(root->val);
            root=root->right;
        }

        return ans;
    }
};

后序遍历

//后序遍历
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        if(root==nullptr)
            return {};
        vector<int> ans;
        stack<TreeNode* > s;
        TreeNode* cur = nullptr;
        while(!s.empty()||root!=nullptr){
            while(root!=nullptr){
                s.push(root);
                root=root->left;
            }
            root=s.top();
            if(root->right==nullptr||root->right==cur){
                cur=root;
                ans.push_back(root->val);
                root=nullptr;
                s.pop();
            }
            else{
                root=root->right;
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值