ARTS 第 2周

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效率高很多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

love_0_love

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值