力扣第六题:N字形变换
为什么我的输出和预期结果是一样的,但还是错了。
这是我写的内容:
class Solution {
public:
string convert(string s, int numRows)
{
int len=s.size();
//cout<<"len="<<len<<endl;
if(len<=numRows || numRows==1)
{
return s;
}
int t=2*numRows-2;
int num=len/t;
int res=len%t;
//cout<<"num="<<num<<endl;
//cout<<"t="<<t<<endl;
string result;
if(len<=t)
{
for(int i=0;i<numRows;i++)
{
result+=s[i];
//cout<<"i: "<<i<<endl;
if(i != 0 && i != (numRows-1))
{
result+=s[t-i];
//cout<<"result: "<<result<<endl;
}
}
}
else
{
for(int i=0;i<numRows;i++)
{
for(int j=0;j<num;j++)
{
result+=s[j*t+i];
//cout<<"i "<<i<<endl;
if(i != 0 && i != (numRows-1))
{
result+=s[(j+1)*t-i];
//cout<<j<<endl;
}
}
if(i<res)
{
result+=s[(num)*t+i];
}
}
}
return result;
}
};
题目是什么
为什么不贴题目呢?