ARTS-week56

Algorithms

Next Greater Element I
思路: num1中的元素是num2的子集,遍历num2中的的数字,当栈不为空且栈顶元素小于当前数字时,说明当前数字是栈顶元素右边第一个较大的数字,利用哈希表建立映射后去除当前栈顶元素,再将当前数字压入栈中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int> st;
vector<int> res;
unordered_map<int, int> um;

for (int num : nums2) {
while (!st.empty() && st.top() < num) {
um[st.top()] = num;
st.pop();
}
st.push(num);
}

for (int num : nums1) {
if (um.count(num))
res.push_back(um[num]);
else
res.push_back(-1);
}
return res;
}
};

Construct the Rectangle
思路: 先看看是不是正方形。

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
vector<int> constructRectangle(int area) {
int r = sqrt(area);
while (area % r != 0) {
--r;
}
return {area / r, r};
}
};

Review

本周阅读英文文章:
1、Illicit Trade in the Dark Web

2、Why Is There an F in Front of This String?

3、How To Boost Your Productivity as a Developer

Technique

Python中的字符串格式化,旧式的”%s”和新式的”format()”经常被使用,那么来看看另外两种方式:

格式化字符串字面值

在Python3.6之后的新功能f-strings,写法更加的简洁和优雅。

1
2
3
>>> name = 'Top'
>>> f'Hello, {name}!'
'Hello, Top!'

也可以嵌入Python表达式:

1
2
3
4
5
>>> a = 5
>>> b = 10
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
'Five plus ten is 15 and not 30.'
>>>

用在函数中:

1
2
3
4
5
6
>>> def greet(name, question):
... return f"Hello, {name}! How's it {question}?"
...
>>>
>>> greet('Top', 'going')
"Hello, Top! How's it going?"

也支持format()方法所使用的字符串格式化语法:

1
2
3
4
>>> name = 'Top'
>>> errno = 50159747054
>>> f"Hey {name}, there's a {errno:#x} error!"
"Hey Top, there's a 0xbadc0ffee error!"

模板字符串

1
2
3
4
5
6
7
8
>>> name = 'Top'
>>> errno = 50159747054
>>>
>>> from string import Template
>>> t = Template('Hello, $name!')
>>> t.substitute(name=name)
'Hello, Top!'
>>>

但是模板字符串不能使用格式说明符,需要将errno转换为十六进制字符串:

1
2
3
4
>>> tmp = 'Hello $name, there is a $errno error!'
>>> Template(tmp).substitute(name=name, errno=hex(errno))
'Hello Top, there is a 0xbadc0ffee error!'
>>>

Share

可以看看天御攻防实验室关于ATT&CK™的文章:
实战化ATT&CK™:引言

实战化ATT&CK™:威胁情报

参考

《深入理解Python特性》

https://docs.python.org/zh-cn/3.7/tutorial/inputoutput.html#fancier-output-formatting

https://www.python.org/dev/peps/pep-0498/

https://docs.python.org/zh-cn/3/reference/lexical_analysis.html#f-strings

https://realpython.com/python-f-strings/

0%