Created
May 25, 2015 16:19
-
-
Save ianpark/07275f140330528a36f7 to your computer and use it in GitHub Desktop.
LeetCode / ZigZag string conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string convert(string s, int numRows) { | |
if (numRows == 0) return ""; | |
if (numRows == 1) return s; | |
vector<string> rows(numRows); | |
int row = 0; | |
int dir = -1; | |
for (int i = 0; i < s.size(); i++) { | |
rows[row].push_back(s[i]); | |
if (row == numRows - 1 || row == 0) { | |
dir = -dir; | |
} | |
row += dir; | |
} | |
std::stringstream out; | |
for (int i = 0; i < rows.size(); ++i) { | |
out << rows[i]; | |
} | |
return out.str(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment