第二周,
Alogrithm
题目—Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
1 | /** |
68ms,还是慢,需要想想怎么优化。
在网上找到的duolabaobao的答案
1 | /** |
简洁了特别多,主要在于如下几点:
- 不用傻乎乎写
if (l1 != nullptr)
而直接判断if (l1)
就好 - 赋值判断可以直接通过
<condition>?<true>:<false>
实现
传上去发现也得64ms
1 | /** |
前面的问题在于while的判断次数比较多,需要把carray单独拆出来不用那么复杂的判断再跑一遍。同时不能有太多判断什么的操作放在里面,初始化什么的尽量在外面做好。
题目 - Longest Substring Without Repeating Characters
查找没有重复字母的最大子串。
最Brute Force的方法略去,想到可以做一个string
记录目前检验的子串,用一个int
存储当前最大长度的子串长度。用一个循环遍历每个字母,如果这个字母在buf子串里出现过,就更新buf子串去掉前面的部分并对比目前最大子串长度值看是否需要更新。
1 | int Solution::lengthOfLongestSubstring(std::string s) { |
由于std::string::find
的复杂度为O(N * M)
,在本例中M为1,所以最差时间复杂度为O(N^2)
,好处是后面一个N不会大于128(ASCII),所以其实可以说是O(128N)
,但还可以再优化。
优化的方向是放弃用一个字符串来存储找到的字段,直接用HashMap
存储字母-位置
的对应关系,用一个int
记录目前不重复字符串的头位置。如果遇到重复只用更新头位置和对应关系里的位置,并更新最大长度就好。
1 | int Solution::lengthOfLongestSubstring(std::string s) { |
Review
本周文章是一个2016年的slide : gRPC Design and Implementation :
Review - gRPC Design and Implementation
Tip
安装过程用有很多时候需要知道电脑或服务器的架构,比如说到底是:
- x86
- x86-64/amd64
- MIPS
- ARM
其中的那种。
这个问题可以通过一行命令:
1 | uname -p |
解决。或者可以使用:
1 | lscpu |
了解
Share
本周推荐Youtube上一个节目频道:
Vox Borders
讲各种有趣的边境故事的。