剑指offer 第二版(41——60)

 剑指 Offer II 041. 滑动窗口的平均值

 /** Initialize your data structure here. */
    queue<int> q;
    int sz=0;
    double sum=0;
    MovingAverage(int size) {
        sz=size;
    }
    
    double next(int val) {
        if(q.size()==sz){
            sum-=q.front();
            q.pop();
            sum+=val;
            q.push(val);
            return sum/q.size();
        }else {
            q.push(val);
            sum+=val;
            return sum/q.size();
        }
    }

 剑指 Offer II 042. 最近请求次数

vector<int> vec;
    RecentCounter() {

    }
    
    int ping(int t) {
        vec.push_back(t);
        int res=0;
        for(int i=vec.size()-1;i>=0;i--)
         if(vec[i]>=t-3000) res++;
         else break;
        
        return res;
    }

 剑指 Offer II 043. 往完全二叉树添加节点

TreeNode* tree;
    vector<TreeNode*>v;
    CBTInserter(TreeNode* root) { 
        tree=root;
        queue<TreeNode*> q;
        if(root!=NULL){
            q.push(tree);
            while(q.size()){
                auto cc=q.front();
                v.push_back(cc);
                q.pop();
                if(cc->left) q.push(cc->left);
                if(cc->right) q.push(cc->right);
            }
        }
       // for(int i=0;i<v.size();i++) cout<<v[i]->val<<" ";
       // cout<<endl;
    }
    
    int insert(int x) {
      
        TreeNode* cc= new TreeNode();
        cc->val=x;
        if(v.size()%2==0){
            TreeNode* fa=v[v.size()/2-1];
            fa->right=cc;
            v.push_back(cc);
            return fa->val; 
        } else{
            TreeNode* fa=v[v.size()/2];
            fa->left=cc;
            v.push_back(cc);
            return fa->val;
        } 
    }
    
    TreeNode* get_root() {
        return tree;
    }

 剑指 Offer II 044. 二叉树每层的最大值

vector<int> largestValues(TreeNode* root) {
        vector<int> res;
        queue<TreeNode*>q;
        if(root==NULL) return res;
        else q.push(root);
        while(q.size()){
            int xx=INT_MIN;
            int len=q.size();
            for(int i=0;i<len;i++){
                auto cc=q.front();
                q.pop();
                if(cc->left) q.push(cc->left);
                if(cc->right) q.push(cc->right);
                xx=max(xx,cc->val);
            }
            res.push_back(xx);
        }
        return res;
    }

 剑指 Offer II 045. 二叉树最底层最左边的值

int res=0,h=0;
    int findBottomLeftValue(TreeNode* root) {
        dfs(root,1);
        return res;
    }
    void  dfs(TreeNode* root,int height){
        if(root==NULL) return;
        dfs(root->left,height+1);
        dfs(root->right,height+1);
       // cout<<root->val<<" "<<height<<endl;
        if(height>h)  res=root->val,h=height;
    }

 剑指 Offer II 046. 二叉树的右侧视图

vector<int> res;
    vector<int> rightSideView(TreeNode* root) {
        queue<TreeNode*> q;
        if(root==NULL) return res;
        else q.push(root);
        while(q.size()){
            int len=q.size();
            for(int i=0;i<len;i++){
                auto cc=q.front();
                q.pop();
                if(cc->left) q.push(cc->left);
                if(cc->right) q.push(cc->right);
                if(i==len-1) res.push_back(cc->val);
            }
        }
        return res;
    }

剑指 Offer II 047. 二叉树剪枝

TreeNode* pruneTree(TreeNode* root) {
        xx(root);
        return root;
    }
    void xx(TreeNode* root){
        if(root==NULL) return ;
        xx(root->left);
        xx(root->right);
        if(root->val==0&&!root->left&&!root->right)   root=NULL;
    }

剑指 Offer II 048. 序列化与反序列化二叉树

// Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string res;
        dfs_s(root,res);
       // cout<<res<<endl;
        return res;
    }
    void dfs_s(TreeNode* root,string& res){
        if(root==NULL) {
            res+="#,";
           return ;
        }
        res+=to_string(root->val)+',';
        dfs_s(root->left,res);
        dfs_s(root->right,res);
    }
    
    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
       int idx=0;
       return  dfs_d(idx,data);
    }

    TreeNode* dfs_d(int& idx,string res){
        if(res[idx]=='#'){
            idx+=2;
            return NULL;
        }
        auto cc=new TreeNode();
        int oriIdx=idx;
        while(res[idx]!=',') idx++;
        cc->val=atoi(res.substr(oriIdx,idx-oriIdx).c_str());
        idx++;
        cc->left=dfs_d(idx,res);
        cc->right=dfs_d(idx,res);
        return cc;
    }

剑指 Offer II 049. 从根节点到叶节点的路径数字之和

int sum=0;
    int sumNumbers(TreeNode* root) {
            string str;
            dfs(root,str);
            return sum;
    }
    void dfs(TreeNode* root,string str){
        if(root&&!root->left&&!root->right) {
            str+=to_string(root->val);
            sum+=atoi(str.c_str());
            return ;
        }
        if(root==NULL){
            return ;
        }
        str+=to_string(root->val);
        dfs(root->left,str);
        dfs(root->right,str);
    }

剑指 Offer II 050. 向下的路径节点之和

 int res=0;
    int pathSum(TreeNode* root, int targetSum) {
        dfs(root,targetSum);
        return res;
    }
    void dfs(TreeNode* root,int targetSum){
        if(root==NULL) return ;
        dfs_s(root,targetSum,0);
        dfs(root->left,targetSum);
        dfs(root->right,targetSum);
    }
    void dfs_s(TreeNode* root,int targetSum,long s){
        if(root==NULL) return ;
        s+=root->val;
        if(s==targetSum) res++;
        dfs_s(root->left,targetSum,s);
        dfs_s(root->right,targetSum,s);
    }

 剑指 Offer II 051. 节点之和最大的路径

 vector<int> vec;
    TreeNode* increasingBST(TreeNode* root) {
        dfs(root);
        return xx(0);
    }
    void dfs(TreeNode* tree){
        if(!tree)  return;
        dfs(tree->left);
        vec.push_back(tree->val);
        dfs(tree->right);
        
    }
    TreeNode* xx(int idx){
        if(idx>=vec.size()) return NULL;
        auto cc=new TreeNode(vec[idx]);
        cc->left=NULL;
        cc->right=xx(idx+1);
        return cc;
    }

剑指 Offer II 052. 展平二叉搜索树

 int xx=0;
    TreeNode* res;
    TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
        xx=p->val;
        int st=0;
        dfs(root,xx,st);
        return res;
    }
    void dfs(TreeNode* root,int xx,int& st){
        if(st==2) return ;
        if(root==NULL) return ;
        dfs(root->left,xx,st);
        if(st==1){
            res=new TreeNode(root->val);
            st=2;
        }
        if(root->val==xx) st=1;
       
        dfs(root->right,xx,st);
    }

 剑指 Offer II 053. 二叉搜索树中的中序后继

int res=INT_MIN;
    int maxPathSum(TreeNode* root) {
        dfs(root);
        return res;
    }
    int dfs(TreeNode* root){
        if(root&&!root->left&&!root->right){
            res=max(res,root->val);
            return root->val;
        } 
        if(root==NULL) return 0;
        int le=dfs(root->left);
        int re=dfs(root->right);
        int sum=le+re+root->val;
       // cout<<le<<"  "<<re<<"  "<<sum<<endl;
      
        if(res<le&&le==0&&!root->left){
            int xx=max(max(sum,root->val),max(re,re+root->val));
            res=max(res,xx);
        } 
        else  if(res<re&&re==0&&!root->right){
            int xx=max(max(sum,root->val),max(le,le+root->val));
            res=max(res,xx);
        } 
        else{
            int xx=max(res, max(max( max(le,re),max(sum,root->val)),max(le,re)+root->val ));
            res=max(res,xx);
        }   
        return max(max(le,re)+root->val,root->val);
    }

 剑指 Offer II 054. 所有大于等于节点的值之和

int sum=0;
    TreeNode* convertBST(TreeNode* root) {
        dfs(root);
        return root;
    }
    void dfs(TreeNode* root){
        if(root==NULL) return ;
        dfs(root->right) ;
        int ori=root->val;
        root->val+=sum;
        sum+=ori;
        dfs(root->left);
    }

 剑指 Offer II 055. 二叉搜索树迭代器

 vector<int> res;
    int idx=0;
    BSTIterator(TreeNode* root) {
        res.push_back(-1);
        dfs(root);
    }

    void dfs(TreeNode* root){
        if(!root) return ;
        dfs(root->left);
        res.push_back(root->val);
        dfs(root->right);
    }

    
    int next() {
        return res[++idx];
    }
    
    bool hasNext() {
        if(idx<res.size()-1)return true;
        else return false;
    }

 剑指 Offer II 056. 二叉搜索树中两个节点之和

 vector<int> res;
    bool findTarget(TreeNode* root, int k) {
        dfs(root);
      
        for(int i=0,j=res.size()-1;i<j;i++){
                while(i<j&&res[i]+res[j]>k) j--;
                if(i<j&&res[i]+res[j]==k){
                    return true;
                } 
        }

        return false;
    }

    void dfs(TreeNode* tree){
        if(!tree) return ;
        dfs(tree->left);
        res.push_back(tree->val);
        dfs(tree->right);
    }

 剑指 Offer II 057. 值和下标之差都在给定的范围内

bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        typedef long long LL;
        multiset<LL> S;
        S.insert(1e18);
        S.insert(-1e18);
        for(int i=0,j=0;i<nums.size();i++){
            while(i-j>k) S.erase(S.find(nums[j++]));
            auto it= S.lower_bound(nums[i]);
            if(*it-nums[i]<=t) return true;
            it--;
            if(nums[i]-*it<=t) return true;
            S.insert(nums[i]);
        }
        return false;
    }

 剑指 Offer II 058. 日程表

vector<vector<int>> res; 
    MyCalendar() {
    }
    
    bool book(int start, int end) {
        for(int i=0;i<res.size();i++){
            if(start<=res[i][0]&&end>res[i][0]) return false;
            if(start<res[i][1]&&end>=res[i][1]) return false;
            if(start>=res[i][0]&&end<=res[i][1]) return false;
        }
        vector<int> xx;
        xx.push_back(start),xx.push_back(end);
        res.push_back(xx);
        return true;
    }

 剑指 Offer II 059. 数据流的第 K 大数值

int kk=0;
    multiset<int> nums;
    KthLargest(int k, vector<int>& xx) {
        kk=k;
        for(int i=0;i<xx.size();i++) nums.insert(xx[i]);
    }  
    int add(int val) {
        nums.insert(val);
        int xx=nums.size()-kk;
        auto it=nums.begin();
        while(xx--) it++;
        return *it;

    }

 剑指 Offer II 060. 出现频率最高的 k 个数字

static bool cmp(const pair<int,int>& a, const pair<int,int >& b ){
            return a.second>b.second;
    }
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> map;
        for(int i=0;i<nums.size();i++) map[nums[i]]++;

        vector<pair<int,int>> xx(map.begin(),map.end());
        vector<int> res;
        sort(xx.begin(),xx.end(),cmp);
        int cnt=0;
        for(auto cc:xx){
            res.push_back(cc.first);
            cnt++;
            if(cnt==k) return res;
        }
        return res;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值