【发布时间】:2020-10-11 05:42:03
【问题描述】:
是string子类下的leet代码problem,中等问题。
查询:我的程序在运行时为所有测试用例返回正确的结果,但是当我提交时,相同的测试用例没有通过。
我还制作了一个视频,click here 观看。
我的代码是:
string convert(string s, int numRows) {
int loc_rows = numRows-2;
int i=0;
int a=0,b=0;
int arr[1000][1000];
while(i<s.length())
{
if(a<numRows)
{
arr[a][b] = s[i];
a++;
i++;
}
else if(a>=numRows)
{
if(loc_rows>=1)
{
b++;
arr[loc_rows][b]=s[i];
i++;
loc_rows--;
}
else{
loc_rows=numRows-2;
b++;
a=0;
}
}
}
string result="";
for(int d=0;d<numRows;d++)
{
for(int y=0;y<b+1;y++)
{
char temp = (char)arr[d][y];
if((temp>='a' and temp<='z') or (temp>='A' and temp<='Z') )
result+=temp;
}
}
return result;
}
【问题讨论】:
-
只是备注:您的
arr变量使用大约。堆栈上有 4 MB。可能太多了。 -
要么分配给
arr,要么分配给std::vector<std::string> -
请刷新the help pages,采取SO tour,阅读How to Ask,以及this question checklist。问题应该自包含,问题陈述和所有要求都应该在问题本身中,而不是作为链接。
-
@ChaudharySarimurrab 如果 code_fodder 发布的答案是正确的,那么如果您使用过
std::vector,您将永远不会遇到问题,因为向量会自动将元素设置为 0。 -
请记住,如果您使用
arr中的行作为字符串(c 字符串)进行输出,则除非您手动执行,否则它们不会nul-terminated。如果需要初始化数组,可以memset (arr, 0, 1000*1000*sizeof(int));