# My 16ms c++ solution

• ``````class Solution {
``````

public:
string convert(string s, int numRows) {
if(numRows==1 ||numRows>=s.length())
return s;
string ret;
ret.resize(s.length());
int pos = 0;
for(int i = 1;i<=numRows;i++){
int curr = i;
int count = 0;
while(curr<=s.length()){
ret[pos++] = s[curr-1];
if(count%2==0){
curr += 2*(numRows-i);
if(i == numRows)
curr += 2*(i-1);
}
else{
curr += 2*(i-1);
if(i==1)
curr += 2*(numRows-i);
}
count++;
}
}
return ret;
}

};

• #include<iostream>
#include<string.h>
using namespace std;

void input(char a[],int size)
{
cout<<"enter d string"<<endl;
for(int i=0;i<size;i++)
{
cin>>a[i];
}
}

void print(char a[],int size)
{

``````for(int i=0;i<size;i++)
{
cout<<a[i];
}
``````

}

void func(char a[],int size,int row)
{
int i=1,j=0,x=0;//x is the index of
char b[size+1]; //j is acting as a track of indexes of array visited
while(i<=row)
{
if(i==1 || i==row)
{
for(int z=j;z<=size;z=z+((row*2)-2))
{
b[x]=a[z];
x++;
}
j++;
}

``````    else
{
for(int z=j;z<size;z=z+(row-1))
{
b[x]=a[z];
x++;
}
j++;
}
i++;

}
cout<<endl<<"now printing the string after func() called :"<<endl;
print(b,size);
``````

}

int main()
{
int size,row;
char a[50];
cout<<"enter the size of string"<<endl;
cin>>size;
input(a,size);
cout<<"string entered before func() called is :"<<endl;
print(a,size);
cout<<endl<<"enter the no of rows"<<endl;
cin>>row;
func(a,size,row);
return 0;
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.