-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-ZConvert.h
More file actions
61 lines (46 loc) · 820 Bytes
/
06-ZConvert.h
File metadata and controls
61 lines (46 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
void Test06();
string convert(string s, int numRows);
protected:
private:
};
void Solution::Test06()
{
string str = "LEETCODEISHIRING";
string strOut = convert(str, 3);
}
/*
°´ÐмÆËã
*/
std::string Solution::convert(string s, int numRows)
{
int nLen = s.length();
if (nLen <= 1 || numRows <= 1)
return s;
vector<string> Array(numRows,"");
int nSort = 0;
int nFlag = 1;
for (int iLoop = 0; iLoop < nLen; ++iLoop)
{
char stmp = s.at(iLoop);
Array[nSort]+=stmp;
if (nSort == numRows - 1)
nFlag = -1;
else if (nSort == 0)
nFlag = 1;
nSort += nFlag;
}
string strOut;
for (int iLoop = 0;iLoop < numRows;++iLoop)
{
strOut += Array[iLoop];
}
return strOut;
}