ARTS-week45

Algorithms

Sum of Left Leaves
思路: 二叉树遍历,求左子叶和的话,需要知道当前结点是否是左子节点,如果是左子节点,而且该左子节点不存在子节点,说明其是左子叶,就将其值加入结果sum中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (!root || (!root->left && !root->right)) {
return 0;
}
int sum = 0;
helper(root, sum);
return sum;
}

void helper(TreeNode* node, int& sum) {
if (node->left)
helper(node->left, sum);
if (node->left && !node->left->left && !node->left->right)
sum += node->left->val;
if (node->right)
helper(node->right, sum);
}
};

Convert a Number to Hexadecimal
思路: 先将十进制转换成二进制,再将二进制每4位转成一个十六进制字符。十进制转二进制时,只需要num & 15(0xf),再将num右移四位,要注意题目中有说The given number is guaranteed to fit within the range of a 32-bit signed integer.,所以要注意次数,否则会报出Memory Limit Exceeded

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
string toHex(int num) {
if(num == 0) {
return "0";
}

string result = "";
const string s = "0123456789abcdef";

while (num != 0 && result.length() < 8) {
result += s[num & 0xf];
num >>= 4;
}
reverse(result.begin(), result.end());
return result;
}
};

Review

本周阅读英文文章:
1、Flask — Host Your Python Machine Learning Model On Web

2、Beginner’s Recommendation Systems with Python

3、Good Database Design Starts Here

Technique

抱着省事的想法,使用6666端口时遇到ERR_UNSAFE_PORT的问题,原因和解决方式单独写在下面这篇文章里了:
Chrome错误代码:ERR_UNSAFE_PORT

Share

在B站看了Up主 老师好我叫何同学 的视频【何同学】有多快?5G在日常使用中的真实体验,视频中的思路、文案真的是超级棒,尤其是在思考5G有什么用时,搜索了2012-2013这个时间段人们对4G的看法,这个真的要疯狂点赞投币,人对未来的预测都跳不出当下技术和思维的限制这句话说也很好。

0%