ARTS 第 2周
1.Algorithm:每周至少做一个 leetcode 的算法题
6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
解题思路:考虑建立一个numRows大小字符串数组rows,把输入字符串s,划分numRows个字符作为z字为一列,再划分numRows 2个字符作为z字中间部分;这样整体是2numRows - 2为一个划分单位 //前numRows个字符正序添加到数组rows中,后numRows - 2个字符逆序添加到rows中,最后拼接到一起
暴露出的问题:对c++ stl不够熟悉,常用方法不记得; 对于边界考虑不够细致,
//实现
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
if(numRows <= 1 || s.size() == 0)
return s;
vector <string> rows(numRows);
//1 取numRows 作为一列,再取numRows - 2作为z中间元素 == 共取 2numRows - 2个元素,不足依次排列
for(int i = 0, index = 0; i < s.size(); ++i, ++index){
//2 按照顺序依次放入vector中
if(index < numRows){
rows[index] = rows[index] + s[i];
} else {
rows[2 * numRows - index - 2] = rows[2 * numRows - index - 2] + s[i];
}
if((index + 1)% (2 * numRows - 2) == 0){
index = -1;
}
}
//3 打印输出
string ret;
for(int i = 0; i < numRows; ++i){
ret += rows[i];
}
return ret;
}
};
int main(){
Solution sol;
string s = "PAYPALISHIRING";
cout << sol.convert(s, 3) << endl;//PAHNAPLSIIGYIR
//s = "";
cout << sol.convert(s, 4) << endl;//PINALSIGYAHRPI
s = "ABCD";
cout << sol.convert(s, 2) << endl;//ACB
return 0;
}
2.Review:阅读并点评至少一篇英文技术文章
本周学习了RabbitMQ,这是一种消息队列,消息队列让解析来自不同host的消息更加简单;原来在项目中使用的是tcp,有诸多限制,比如需要自己编/解码消息、容易粘包、断连等各种问题,使用消息队列不妨是另一种选择
https://www.rabbitmq.com/getstarted.html
3.Tip:学习至少一个技术技巧
回顾了下vim快捷键,vim使用的好在linux编程很高效
4.Share:分享一篇有观点和思考的技术文章
最近看了python dict set list的实现,list底层是个数组,而dict set底层是散列表,所以在数据量大时插入删除操作上,set dict 比list效率高很多