
题目描述
将字符串按锯齿状排列,然后按行输出。
题目分析
使用k个容器,让字符依次进入某个容器,然后将容器中的字符按容器顺序输出。
代码
#include <vector>
class Solution {
public:
std::string convert(std::string s, int numRows) {
std::vector<char>* p = new std::vector<char>[numRows];
int k = 1;
bool flag = false;
for (unsigned i = 0; i < s.length(); ++i) {
k = getNext(k, numRows, flag);
p[k].push_back(s[i]);
}
char buf[1010];
int q = 0;
for (int i = 0; i < numRows; ++i) {
for (unsigned j = 0; j < p[i].size(); ++j) {
buf[q++] = p[i][j];
}
}
buf[q] = '




近期评论