我的代码是正确的,但不被 Leetcode 平台接受。 (锯齿形转换)
My code is right but not accepted by Leetcode platoform. (ZigZag Conversion)
这是一个leet代码problem在string子类别下,中等问题
查询:我的程序在 运行 时为所有测试用例返回正确的结果,但是当我提交时,相同的测试用例没有通过。
我也做了一个视频,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;
}
我认为问题可能出在您的 un-initialised 数组/变量上。
尝试设置初始化数组:int arr[1000][1000] = {0};
您不能依赖这些数组中的数据,因此初始化值非常重要。
注意: 这是因为您依赖数组中的空值不是字母 ([a-zA-Z]
)。这样您就可以 re-construct 使用最终循环尝试仅打印字符的输出。这是第一次起作用,因为幸运的是 arr
在您的值(或至少不是字母)之间的间隙中包含 0。第二次它包含一些第一次的垃圾(真的 - 你不知道这会是什么,但实际上它可能只是你上次留下的值)。因此,即使您每次都将正确的值输入 arr
- 您的最终循环会在 arr
找到一些旧的 non-alpha
值 - 因此您的程序不正确...
或者,我们也可以使用 unsigned int 来提高效率:
// The following block might slightly improve the execution time;
// Can be removed;
static const auto __optimize__ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
// Most of headers are already included;
// Can be removed;
#include <cstdint>
#include <vector>
#include <string>
static const struct Solution {
using ValueType = std::uint_fast16_t;
static const std::string convert(
const std::string s,
const int num_rows
) {
if (num_rows == 1) {
return s;
}
std::vector<std::string> res(num_rows);
ValueType row = 0;
ValueType direction = -1;
for (ValueType index = 0; index < std::size(s); ++index) {
if (!(index % (num_rows - 1))) {
direction *= -1;
}
res[row].push_back(s[index]);
row += direction;
}
std::string converted;
for (const auto& str : res) {
converted += str;
}
return converted;
}
};
这是一个leet代码problem在string子类别下,中等问题
查询:我的程序在 运行 时为所有测试用例返回正确的结果,但是当我提交时,相同的测试用例没有通过。
我也做了一个视频,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;
}
我认为问题可能出在您的 un-initialised 数组/变量上。
尝试设置初始化数组:int arr[1000][1000] = {0};
您不能依赖这些数组中的数据,因此初始化值非常重要。
注意: 这是因为您依赖数组中的空值不是字母 ([a-zA-Z]
)。这样您就可以 re-construct 使用最终循环尝试仅打印字符的输出。这是第一次起作用,因为幸运的是 arr
在您的值(或至少不是字母)之间的间隙中包含 0。第二次它包含一些第一次的垃圾(真的 - 你不知道这会是什么,但实际上它可能只是你上次留下的值)。因此,即使您每次都将正确的值输入 arr
- 您的最终循环会在 arr
找到一些旧的 non-alpha
值 - 因此您的程序不正确...
或者,我们也可以使用 unsigned int 来提高效率:
// The following block might slightly improve the execution time;
// Can be removed;
static const auto __optimize__ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
// Most of headers are already included;
// Can be removed;
#include <cstdint>
#include <vector>
#include <string>
static const struct Solution {
using ValueType = std::uint_fast16_t;
static const std::string convert(
const std::string s,
const int num_rows
) {
if (num_rows == 1) {
return s;
}
std::vector<std::string> res(num_rows);
ValueType row = 0;
ValueType direction = -1;
for (ValueType index = 0; index < std::size(s); ++index) {
if (!(index % (num_rows - 1))) {
direction *= -1;
}
res[row].push_back(s[index]);
row += direction;
}
std::string converted;
for (const auto& str : res) {
converted += str;
}
return converted;
}
};