20210601学习记录zip/unzip命令-二叉树右视图

主题:

日常学习记录,内容包括

  • zip/gzip/unzip–打包压缩相关命令
  • 算法题:输出二叉树的右视图

内容:

1 zip/gzip/unzip–打包压缩相关命令
zip的basic command format为
zip options archive inpath inpath …
archive可以是已经存在的,可以是不存在的。
已经存在的一般是进行archive的更新。更新可以使用的模式包括:替换或者添加,只替换,只添加,满足条件进行替换或添加
-r 可以完成将inpath的内容或者替换,或者添加到一个 已经存在的archive当中
对于已经存在的archive,有多种mode,大致分为两种,internal和external
Command modes. zip now supports two distinct types of command modes, external
and internal. The external modes (add, update, and freshen) read files from the
file system (as well as from an existing archive) while the internal modes
(delete and copy) operate exclusively on entries in an existing archive.

   add
          Update existing entries and add new files.  If the archive does not exist
          create it.  This is the default mode.

   update (-u)
          Update existing entries if newer on the file system and  add  new  files.
          If the archive does not exist issue warning then create a new archive.

   freshen (-f)
          Update  existing entries of an archive if newer on the file system.  Does
          not add new files to the archive.

   delete (-d)
          Select entries in an existing archive and delete them.

   copy (-U)
          Select entries in an existing archive and copy them  to  a  new  archive.
          This  new  mode is similar to update but command line patterns select en‐
          tries in the existing archive rather than files from the file system  and
          it  uses  the  --out  option to write the resulting archive to a new file
          rather than update the existing archive, leaving the original archive un‐
          changed.

archive如果是未存在的,一般是进行建立一个archive。这时,可以使用多个inpath,表示多个待压缩文件。archive
由于位置比较固定,一般可以省略zip后缀。
对于inpath,也可以通过使用option -@来接受stdin的文件名列表。可以配合管道进行使用。
zip可以把压缩的结果输出到stdout,一般配合管道进行使用。这种情况,使用-来作为archive
zip可以从stdin来读取要压缩的文件内容。也会配合管道进行使用。使用-来作为inpath

unzip 主要用来进行解压,也可以查看archive内容列表-l,可以测试时候没有损坏(zip内部有CRC校验,使用t进行测试
即可,使用tq来简单输出结果)。
解压有两种基本的方式:1 保存目录结构的解压 为默认 2 不保存目录结构,直接全部解压到目录中-j(junk path)
两种方式可以都可以使用-d选项,来选择解压到哪一个文件夹。
在一个archieve之后,可以加上archieve member list 表示只是解压这些成员 同archieve名一样,也可以使用
wildcard。通配符。
解压的时候,也可以使用-x来将某些zip成员排除掉。

2 输出二叉树的右视图

描述
请根据二叉树的前序遍历,中序遍历恢复二叉树,并打印出二叉树的右视图
输入:
[1,2,4,5,3],[4,2,5,1,3]
返回值:
[1,3,5]
思路:
1 根据前序遍历和中序遍历来构建出这个树。2 然后进行层序遍历。进行记录,每一层的最后一个元素
在构建树的时候,可以采用递归的方式来进行构建。
在层序遍历的时候,利用一个count计数器来记录上一层的元素个数。然后才可以将上一层的元素全部取出来。

代码:

vector<int> solve(vector<int>& xianxu, vector<int>& zhongxu) {
        // write code here
        vector<int> res;
        if(xianxu.empty())
            return res;
        TreeNode* root;
        root = rebuild(xianxu, 0, xianxu.size()-1, zhongxu, 0, zhongxu.size()-1);
        
        /*
        //进行层序遍历
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty())
        {
            vector<int> layer;
            TreeNode* temp;
            //采用queue进行逐层遍历,效率太低了,占用空间比较大,此处使用维护一个计数器的方法
            queue<TreeNode*> tempque;
            //进行一轮更新
            while(!q.empty())
            {
                temp = q.front();
                q.pop();
                tempque.push(temp);
            }
            
            while(!tempque.empty())
            {
                temp = tempque.front();
                tempque.pop();
                layer.push_back(temp->val);
                if(temp->left) q.push(temp->left);
                if(temp->right) q.push(temp->right);
            }
            res.push_back(layer.back());
        }*/
       /* 
        queue<TreeNode*> q;
        q.push(root);
        int count = 1;
        while(!q.empty())
        {
            for(int i=0;i<count;i++)
            {
                TreeNode* temp = q.front();
                q.pop();
                if(temp->left) q.push(temp->left);
                if(temp->right) q.push(temp->right);
                if(i == count-1) res.push_back(temp->val);
            }
            count = q.size();
        }
        return res;
    }
    
    TreeNode* rebuild(vector<int>& p, int p_l, int p_r, vector<int>& v, int v_l, int v_r)
    {
        //不需要考虑给叶子节点的left和right赋值为null的问题,因为默认就是null
        TreeNode* res = new TreeNode(p[p_l]);
        //if(p_l == p_r)
        //{
         //   return res;
        //}
        int index = v_l;
        while(v[index]!=p[p_l])
        {
            index++;
        }
        if(index>v_l) res->left = rebuild(p, p_l+1, p_l+index-v_l, v, v_l, index-1);
        if(index<v_r) res->right = rebuild(p, p_l+index-v_l+1, p_r, v, index+1, v_r);
        return res;
    }

如果觉得有用的话,不妨点个赞哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值