ARTS-week11

Algorithms

本周算法题:
Length of Last Word

思路:只求最后一个单词的长度,可以直接从字符串末尾开始,先将末尾的空格都去掉,然后找非空格的字符的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int lengthOfLastWord(string s) {
int n = s.size();
if (n == 0) {
return 0;
}

int right = n - 1;
while (right >= 0 && s[right] == ' ') {
--right;
}
int res = 0;
while (right >= 0 && s[right] != ' ') {
--right;
++res;
}
return res;
}
};

Plus One

思路:最低位开始按位相加,temp保存进位信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int n = digits.size();
if (n == 0) {
return digits;
}

int temp = 1;
for (int i = n - 1; i >= 0; --i) {
if (temp == 0) {
return digits;
}
int sum = digits[i] + temp;
digits[i] = sum % 10;
temp = sum / 10;
}
if (temp == 1) {
digits.insert(digits.begin(), 1);
}
return digits;
}
};

Review

本周阅读英文文章 To Take Control of Your Time, Ditch the Scarcity Mentality

Scarcity leaves you liable to act impulsively and less mindful of your future interests.

Attend to when time pressure works, when it gets to be too much, and you might start enjoying it. If nothing else, build your tolerance for time pressure and it will be less stressful — making it harder for others to drive you into cognitive tunnels.

Technique

场景:需要从PCAP中单独截取一个指定编号的数据包,其他编号全部过滤掉。

解决方案:Wireshark中带有工具editcap可以做到上述要求。手册中描述:

An optional list of packet numbers can be specified on the command tail; individual packet numbers separated by whitespace and/or ranges of packet numbers can be specified as start-end, referring to all packets from start to end. By default the selected packets with those numbers will not be written to the capture file. If the -r flag is specified, the whole packet selection is reversed; in that case only the selected packets will be written to the capture file.

手册:
https://www.wireshark.org/docs/man-pages/editcap.html

同时也提供了命令行样例:

1
2
3
4
5
6
7
To exclude packets 1, 5, 10 to 20 and 30 to 40 from the new file use:

editcap capture.pcap exclude.pcap 1 5 10-20 30-40

To select just packets 1, 5, 10 to 20 and 30 to 40 for the new file use:

editcap -r capture.pcap select.pcap 1 5 10-20 30-40

Share

本周分享《Linux系统编程手册》拓展属性

文件的拓展属性(EA),即以名称-值对形式将任意元数据与文件i节点关联起来的技术。EA可用于实现访问列表和文件能力。还可以利用EA去记录文件的版本号、与文件的MIME类型/字符集有关的信息,或是指向图符的指针。

EA的命名格式为namespace.name。其中namespace用来把EA从功能上划分为截然不同的几大类,而name用来在既定命名空间内唯一标识某个EA。

可供namespace使用的值有4个:user、trusted、system、security。

user EA将在文件权限检查的制约下由非特权级进程操控。欲获取user EA值,需要有文件的读权限;欲改变user EA,则需要写权限。在ext2、ext3、ext4或者Reiserfs文件系统上,如欲将user EA与一文件关联,在装配底层文件系统时需要带有user_xattr选项。

trusted EA也可由用户驱使。要操作trusted EA,进程必须具有特权(CAP_SYS_ADMIN)。

system EA供内核使用,将系统对象与一文件关联。

security EA的作用:其一,用来存储服务于操作系统安全模块的文件安全标签;其二,将可执行文件与能力关联起来。

一个i节点可拥有多个相关EA,其所从属的命名空间可以相同,也可不同。

在shell中,通过setfattr(1)和getfattr(1)来设置和查看文件的EA。

1
2
3
4
5
6
7
8
9
10
11
$ touch tfile
$ setfattr -n user.x -v "The past is not dead." tfile
$ setfattr -n user.y -v "In fact, it's not even past." tfile
$ getfattr -n user.x tfile
# file: tfile
user.x="The past is not dead."

$ getfattr -d tfile
# file: tfile
user.x="The past is not dead."
user.y="In fact, it's not even past."
0%