leetcode第六题"Z字变换"
这是我的
#include <iostream>
#include <string>
#include <math.h>
#include <string.h>
using namespace std;
class Solution
{
public:
string convert(string s, int numRows)
{
if (numRows == 1 || s == "")
return s;
int len = s.size();
char output[300][1000];
for (int i = 0; i < 300; i++)
{
memset(output[i], '\n', 1000 * sizeof(char));
}
int case1 = 1, count = 0;
int t = len / (2 * numRows - 2);
int cols = ((len - t * (2 * numRows - 2)) > numRows) ? 2 * t+2 : 2 * t+1;
for (int i = 0; i < cols; i++)
{
case1 = !case1;
if (!case1)
{
for (int j = 0; j < numRows&&count<len; j++)
{
output[i][j] = s[count++];
}
}
else if (case1)
for (int j = numRows - 2; j >= 1&&count<len; j--)
{
output[i][j] = s[count++];
}
}
string res;
count=0;
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < cols; j++)
{
if (output[j][i] != '\n')
{
//cout << output[j][i];
res.push_back(output[j][i]);
}
}
}
return res;
}
};
int main()
{
Solution solution;
string a = "PAYPALISHIRING";
cout << solution.convert(a, 3);
getchar();
return 0;
}
这是大佬的
#include <iostream>
#include <string>
#include <math.h>
#include <vector>
using namespace std;
class Solution
{
public:
string convert(string s, int numRows) {
if (numRows == 1) return s;
vector<string> rows(min(numRows, int(s.size())));
int curRow = 0;
bool goingDown = false;
for (char c : s) {
rows[curRow] += c;
if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
curRow += goingDown ? 1 : -1;
}
string ret;
for (string row : rows) ret += row;
return ret;
}
};
int main()
{
Solution solution;
string a="LEETCODEISHIRING";
cout<<solution.convert(a,3);
getchar();
return 0;
}
你看看,这就叫差距
看看,什么叫国际巨星啊,这就叫国际巨星