leetcode的run time error# JobHunting - 待字闺中
s*x
1 楼
大家好,最近做了两题leetcode, 但是都遇到了run time error. 但是我遇到run time
error 的例子在别的c++编译器上都顺利通过了,请大家帮我看一下是什么问题. 谢谢
大家!
问题一:next permutation:
class Solution {
public:
int findSecondMax(vector &num, int cur){
int max1=-1, max2=-2;
int index1=-1, index2=-2;
for(int i=cur; i if(max1 max2=max1;
index2=index1;
max1=num[i];
index1=i;
}
}
if(max2!=-2 || max2!=-1) return index2;
else return index1;
}
void nextPermutation(vector &num) {
if(num.empty()) return;
int cur=int(num.size())-1;
while(cur>0 && num.at(cur-1)>=num.at(cur)) cur--;
if(cur==0){
sort(num.begin(), num.end());
}
else{
int i=findSecondMax(num, cur);
int temp=num[i];
num[i]=num[cur-1];
num[cur-1]=temp;
sort(num.begin()+cur, num.end());
}
}
};
第一题得到run time error 的例子是vector num=[1, 1]
第二题:Spiral Matrix
class Solution {
public:
vector spiralOrder(vector > &matrix) {
vector v;
if(matrix.empty() || matrix[0].empty()) return v;
int row=matrix.size(), col=matrix[0].size();
int Rstart=0, Rend=row-1;
int Cstart=0, Cend=col-1;
int count=0;
while(true){
int flag1=0, flag2=0;
int curRow=count, curCol=count;
v.push_back(matrix[curRow][curCol++]);
if(curCol<=Cend){
while(curCol<=Cend) v.push_back(matrix[curRow][curCol++]);
--curCol, ++curRow, flag1=1;
}
if(curRow<=Rend){
while(curRow<=Rend) v.push_back(matrix[curRow++][curCol]);
--curRow; --curCol; flag2=1;
}
if(flag1==1 && flag2==1){
while(curCol>=0) v.push_back(matrix[curRow][curCol--]);
++curCol; --curRow;
while(curRow>0) v.push_back(matrix[curRow--][curCol]);
}
Rstart++;
Cstart++;
Rend--;
Cend--;
if(Rstart<=Rend && Cstart<=Cend) ++count;
else break;
}
return v;
}
};
第二题得到run time error的例子是matrix=[[1,2],[3,4]]
error 的例子在别的c++编译器上都顺利通过了,请大家帮我看一下是什么问题. 谢谢
大家!
问题一:next permutation:
class Solution {
public:
int findSecondMax(vector
int max1=-1, max2=-2;
int index1=-1, index2=-2;
for(int i=cur; i
index2=index1;
max1=num[i];
index1=i;
}
}
if(max2!=-2 || max2!=-1) return index2;
else return index1;
}
void nextPermutation(vector
if(num.empty()) return;
int cur=int(num.size())-1;
while(cur>0 && num.at(cur-1)>=num.at(cur)) cur--;
if(cur==0){
sort(num.begin(), num.end());
}
else{
int i=findSecondMax(num, cur);
int temp=num[i];
num[i]=num[cur-1];
num[cur-1]=temp;
sort(num.begin()+cur, num.end());
}
}
};
第一题得到run time error 的例子是vector
第二题:Spiral Matrix
class Solution {
public:
vector
vector
if(matrix.empty() || matrix[0].empty()) return v;
int row=matrix.size(), col=matrix[0].size();
int Rstart=0, Rend=row-1;
int Cstart=0, Cend=col-1;
int count=0;
while(true){
int flag1=0, flag2=0;
int curRow=count, curCol=count;
v.push_back(matrix[curRow][curCol++]);
if(curCol<=Cend){
while(curCol<=Cend) v.push_back(matrix[curRow][curCol++]);
--curCol, ++curRow, flag1=1;
}
if(curRow<=Rend){
while(curRow<=Rend) v.push_back(matrix[curRow++][curCol]);
--curRow; --curCol; flag2=1;
}
if(flag1==1 && flag2==1){
while(curCol>=0) v.push_back(matrix[curRow][curCol--]);
++curCol; --curRow;
while(curRow>0) v.push_back(matrix[curRow--][curCol]);
}
Rstart++;
Cstart++;
Rend--;
Cend--;
if(Rstart<=Rend && Cstart<=Cend) ++count;
else break;
}
return v;
}
};
第二题得到run time error的例子是matrix=[[1,2],[3,4]]