Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created September 22, 2018 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/97504ae93d8b504334f1a29390ce67ed to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/97504ae93d8b504334f1a29390ce67ed to your computer and use it in GitHub Desktop.
class Solution {
public:
string convert(string s, int num) {
int len = s.size();
string finalStr;
if(num == 1||len<=1) return s;
int k = 2 * num - 2;
for(int i = 0; i< num; i++)
{
for(int j = 0; i + j<len; j = j + k)
{
finalStr.push_back(s[i+j]);
// get rid of the first and last line
if(i!=0 && (i!= num - 1) && (j + k - i < len);
finalStr.push_back(s[j+k - i]);
}
}
return finalStr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment