Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string nLine(int LineIndex,string &s,int numRows){
- string line;
- int firstSkip,secondSkip;
- firstSkip=2*(numRows-LineIndex)-3;
- secondSkip=2*LineIndex-1;
- line+=s[LineIndex];
- for(int i=1,j=LineIndex;true;i++){
- j++;
- if(LineIndex==numRows-1)
- j+=secondSkip;
- else
- j+=firstSkip;
- if(j>=s.length())
- break;
- line+=s[j];
- j++;
- if(LineIndex==0)
- j+=firstSkip;
- else
- j+=secondSkip;
- if(j>=s.length())
- break;
- line+=s[j];
- }
- return line;
- }
- class Solution {
- public:
- string convert(string s, int numRows) {
- if(numRows>s.length())
- numRows=s.length();
- if(numRows==1)
- return s;
- string ZigZag;
- if(s.length()==0)
- return "";
- for(int i=0;i<numRows;i++){
- ZigZag=ZigZag+nLine(i,s,numRows);
- }
- return ZigZag;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement