作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
它是字符串,中等问题的子类别下的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;
}
最佳答案
我相信问题可能出在您未初始化的数组/变量上。
尝试设置初始化数组:int arr[1000][1000] = {0};
[a-zA-Z]
)。这样您就可以使用最终循环(仅尝试打印字符)来重构输出。幸运的是,
arr
在您的值(或至少不是字母)之间的间隔中包含0,这是第一次工作。第二次是第一次出现的垃圾(真的-您不知道这是什么,但是实际上,它可能只是您上次留下的值)。因此,即使您每次都在
arr
中输入正确的值-您的最终循环都会在
non-alpha
ay中找到一些旧的
arr
值-因此您的程序不正确...
关于c++ - 我的代码正确,但Leetcode平台不接受。 (之字形转换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64300946/
我有一个问题。我尝试实现 Zig-Zag 算法 ( Rail Fence )。 我的代码如下所示: int n = 3; int j = 0; int charCounter
所以我有一个像这样的 4x4 矩阵 |0 1 2 3 -+------- 0|0 1 3 6 1|2 4 7 a 2|5 8 b d 3|9 c e f 并且我是按照其中的十六进制字符指定的顺序遍历
我是一名优秀的程序员,十分优秀!