[LeetCode] ZigZag Conversion

string convert(string s, int nRows) 
{
	if (nRows <= 1)
	{
		return s;
	}
	string* rowStr = new string[nRows];
	bool down = true;
	int i = 0, row = 0;
	while (i < s.length())
	{
		if (down)
		{
			rowStr[row] += s[i];
			i++;
			row++;
			if (row >= nRows)
			{
				row = nRows - 2;
				down = false;
			}
		} 
		else
		{
			rowStr[row] += s[i];
			i++;
			row--;
			if (row < 0)
			{
				row = 1;
				down = true;
			}
		}
	}
	string strConvert;
	for (int i = 0; i < nRows; i++)
	{
		strConvert += rowStr[i];
	}
	return strConvert;
}
注意要考虑nRows<=1的情况

你可能感兴趣的:(LeetCode)