problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::zigzag_conversion::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static string convert (string s, int numRows)
 

详细描述

在文件 leetcode.h1425 行定义.

成员函数说明

◆ convert()

string leetcode::zigzag_conversion::Solution::convert ( string  s,
int  numRows 
)
static

在文件 leetcode.cpp3707 行定义.

3707 {
3708 if(numRows == 1) {
3709 return s;
3710 }
3711 auto oss = ostringstream();
3712 auto *m = new char *[numRows];
3713 for(int i = 0; i < numRows; i++) {
3714 m[i] = new char[s.length()];
3715 memset(m[i], ' ', s.length() * sizeof(char));
3716 }
3717 int current_x = 0;
3718 int current_y = 0;
3719 bool dir = true;// true = 下, false = 斜
3720 for(const char ch: s) {
3721 m[current_x][current_y] = ch;
3722 if(dir && current_x == numRows - 1 || !dir && current_x == 0) {
3723 dir = !dir;
3724 }
3725 if(dir) {
3726 current_x++;
3727 } else {
3728 current_x--;
3729 current_y++;
3730 }
3731 }
3732 for(int i = 0; i < numRows; i++) {
3733 for(int j = 0; j <= min(current_y, static_cast<int>(s.length() - 1)); j++) {
3734 if(m[i][j] != ' ') {
3735 oss << m[i][j];
3736 }
3737 }
3738 }
3739 for(int i = 0; i < numRows; i++) {
3740 delete[] m[i];
3741 }
3742 delete[] m;
3743 return oss.str();
3744 }

被这些函数引用 leetcode::zigzag_conversion::TEST().


该类的文档由以下文件生成: