帅气咕杂货间

leetcode-6 题解

Word count: 246 / Reading time: 1 min
2018/08/12 Share

题目

将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:

1
2
3
P   A   H   N
A P L S I I G
Y I R

之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"

实现一个将字符串进行指定行数变换的函数:

1
string convert(string s, int numRows);

示例 1:

1
2
输入: s = "PAYPALISHIRING", numRows = 3
输出: "PAHNAPLSIIGYIR"

示例 2:

1
2
3
4
5
6
7
8
输入: s = "PAYPALISHIRING", numRows = 4
输出: "PINALSIGYAHRPI"
解释:

P I N
A L S I G
Y A H R
P I

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1){
return s;
}
int step[2];
string result;
for(int i = 0;i<numRows;i++){
step[result.length()%2] = (numRows-1-i)*2==0?2*i:(numRows-1-i)*2;
step[!(result.length()%2)] = 2*i==0?(numRows-1-i)*2:2*i;
for(int col = i;col<s.length();col+= step[!(result.length()%2)]){
result.push_back(s[col]);
}
}
return result;
}
};

为了省去step的选择标记,使用result字符串长度作为标记。

CATALOG
  1. 1. 题目
  2. 2. 直接上代码