2021-5-14二叉树之字型遍历/寻找和为0的三个元素/ifconfig命令/tcpdump

主题:

日常学习记录,包括:

  • 算法题:二叉树之字形遍历
  • 算法题:寻找和为0的三个元素
  • 命令:ifconfig
  • 命令:tcpdump

内容:

1 二叉树之字形遍历
问题描述:
给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)
例如:
给定的二叉树是{3,9,20,#,#,15,7},
该二叉树之字形层序遍历的结果是
[
[3],
[20,9],
[15,7]
]
思路:
仍然是之前的层序遍历的思路,只是分当前为单数层还是双数层,当为双数层的时候,需要
从右到左,当为单数层的时候,需要从左到右。实现从右到左,最简单的思路就是从左到右入栈,
然后出栈,就变成了从右到左。这样就可以完全按照正常的从左到右的总体流程来进行。当为偶数的
时候,用栈来反转一下就可以了。
代码:

vector<vector<int> > zigzagLevelOrder(TreeNode* root) {
        // write code here
        deque<TreeNode*> point;
        point.push_back(root);
        vector<vector<int>> res;
        if(root==NULL)
            return res;
        int flag = 1;
        while(!point.empty())
        {
            vector<int> layer;
            stack<int> s;
            int size =  point.size();
            if(flag%2==1)
            {
                while(size--)  //从左到右
                {
                    TreeNode* p = point.front();
                    layer.push_back(p->val);
                    point.pop_front();
                    if(p->left)  point.push_back(p->left);
                    if(p->right) point.push_back(p->right);
                }
            }
            if(flag%2==0)
            {
                while(size--)  //从右到左
                {
                    TreeNode* p = point.front();
                    s.push(p->val);
                    point.pop_front();
                    if(p->left)  point.push_back(p->left);
                    if(p->right) point.push_back(p->right);
                }
                while(!s.empty())
                {
                    layer.push_back(s.top());
                    s.pop();
                }
            }
            flag++;
            res.push_back(layer);
        }
        return res;
    }

2 寻找和为0的三个元素
问题描述:
给出一个有n个元素的数组S,S中是否有元素a,b,c满足a+b+c=0?找出数组S中所有满足条件的三元组。
注意:三元组(a、b、c)中的元素必须按非降序排列。(即a≤b≤c)解集中不能包含重复的三元组。
例如,给定的数组 S = {-10 0 10 20 -10 -40},解集为(-10, 0, 10) (-10, -10, 20)
思路:采用先排序(通过abc要求有序可以看出来),然后先确定第一个数,采用双指针来凑数的方式
也就是一前一后的方式,来逐渐逼近target,当两个指针对应的数字的和<target时,就右移
left指针,当>target时,就左移right指针,来调控两个数字相加的大小。
注意:要防止重复,防止i,left和right的元素重复。要跳过这些重复的元素。
可以在找到一组left,right值之后进行防止重复,在一轮i循环之后进行防止i的重复
代码:

vector<vector<int> > threeSum(vector<int> &num) {
        vector<vector<int>> res;
        if(num.size()<3)
            return res;   //特殊情况的处理
        sort(num.begin(), num.end()); //排序
        for(int i=0;i<num.size()-2;i++)
        {
            int left=i+1;  //
            int right = num.size()-1; //右指针
            int target = -num[i];
            while(left<right)  //开始进行查找
            {
                if(num[left]+num[right]>target) --right; //必须要家else,否则会执行错误
                else 
                    if(num[left]+num[right]<target) ++left;
                else
                    if(num[left]+num[right]==target)
                    {
                        vector<int> one = {num[i], num[left], num[right]};
                        res.push_back(one);
                        while(num[left+1]==num[left] && left+1<right) //防止重复,跳过重复元素
                        {
                            left++;
                        }
                        while(num[right-1] == num[right] && right-1>left) //停在最后一个重复元素上或者是靠近另外一个指针处
                        {
                            right--;
                        }
                        ++left;
                        --right;
                    }
            }
            while(i+1<num.size()-2 && num[i+1]==num[i]) ++i;
        }
        return res;
    }

3 ifconfig命令
ifconfig [-v] [-a] [-s] [interface] (查看接口配置命令,标志是没有option或者address)
ifconfig [-v] interface [aftype] options | address … (进行配置或者使用的命令)

Ifconfig is used to configure the kernel-resident network interfaces. It is
used at boot time to set up interfaces as necessary. After that, it is usually
only needed when debugging or when system tuning is needed.

If no arguments are given, ifconfig displays the “status of the currently active
interfaces”.此时会有两个项,一个是回环的,另外一个是当前活动的网卡
无参数

If a single interface argument is given, it displays the status of
the given interface only;
if a single -a argument is given, it displays the status of all interfaces, even those that are down.
Otherwise, it configures an interface.

对于接口的配置是符合第二个的语法规则的。需要有interface,有option,或者地址。
可以完成的设置包括,interface的打开和关闭(up和down),是否在interface上开启
arp协议(地址转换协议),开启promic模式,allmlti模式,可以设置ipv4 address(直接写地址)
设置子网掩码(netmask addr),增加ipv6 地址(add addr),删除ipv6 地址(del addr)
建立点对点通信(用pointopoint来设置对方的地址),设置设备类型(media type),设置
广播地址(broadcast)

4 tcpdump
tcpdump是一个抓包软件,并且可以设置过滤条件。过滤条件是有一定的规则的
类型关键字,使用广泛,用来修饰后面的内容是一个主机的ip,还是网络号,还是一个端口(host,net,port)
而src,dst,dst or src, dst and src,这些表示传输的方向。
第三种是协议的关键字,包扩ip,arp,tcp,udp等。
可以灵活运用and,or和括号,括号需要用\来转义。

当想要抓取特定的协议的信息的时候,一般在最前面,也就是tcpdump后面写上协议名,还可以配合
端口号,然后写host,否则,也可以在接在host后面加上and来连接。

-------------------------------end---------------------------
如果有帮助的话,请点一个赞吧!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值