我剛剛在寫Leet code的491. Non-decreasing Subsequences,因為是第一次寫,所以我先用linux的gcc編譯器來編譯,最後跑出了正確的結果才放到Leet code的編譯器跑,但是他卻出現了Runtime Error,如下圖:
我的在linux編譯的code如下:
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums)
{
vector<vector<int> > finalResult;
int vectSize = nums.size();
int interval = 1;
bool cont = true;
for(int i = 0 ; i < vectSize-1 ; i++)
{
cont = true;
interval = 1;
while(cont)
{
int curr = i+interval ;
vector<int> tmp;
tmp.emplace_back(nums[i]);
while(nums[curr] >= tmp[tmp.size()-1])
{
tmp.emplace_back(nums[curr]);
curr++;
if(finalResult.size() == 0)
{
finalResult.emplace_back(tmp);
}
else
{
for(int k = 0 ; k < finalResult.size() ; k++)
{
if(finalResult[k] == tmp)
{
break;
}
if(k==finalResult.size()-1)
{
finalResult.emplace_back(tmp);
}
}
}
}
if(interval < vectSize-i)
{
interval ++;
}
else
{
cont = false;
}
tmp.clear();
}
}
return finalResult;
}
vector<int> nums;
};
int main(int argc, char* argv[])
{
Solution solution;
vector<int> vect1{4,6,7,7};
vector<int> vect2{4,4,3,2,1};
if(strcmp(argv[1], "0") == 0)
{
solution.nums = vect1;
}
else if(strcmp(argv[1], "1") == 0)
{
solution.nums = vect2;
}
else
{
cout << "Please check for the valid input!" << endl;
}
vector<vector<int> > finalSol = solution.findSubsequences(solution.nums);
cout << "[" ;
for(int i = 0 ; i < finalSol.size() ; i++)
{
cout << "[" ;
for(int j = 0 ; j < finalSol[i].size() ; j++)
{
cout << finalSol[i][j];
if(j<finalSol[i].size()-1)
{
cout << " " ;
}
}
cout << "]" ;
if(i < finalSol.size()-1)
{
cout << ", " ;
}
}
cout << "]" << endl;
}
繳交進Leet code的部分就是class中的findSubsequences部份而已(也就是):
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums)
{
vector<vector<int> > finalResult;
int vectSize = nums.size();
int interval = 1;
bool cont = true;
for(int i = 0 ; i < vectSize-1 ; i++)
{
cont = true;
interval = 1;
while(cont)
{
int curr = i+interval ;
vector<int> tmp;
tmp.emplace_back(nums[i]);
while(nums[curr] >= tmp[tmp.size()-1])
{
tmp.emplace_back(nums[curr]);
curr++;
if(finalResult.size() == 0)
{
finalResult.emplace_back(tmp);
}
else
{
for(int k = 0 ; k < finalResult.size() ; k++)
{
if(finalResult[k] == tmp)
{
break;
}
if(k==finalResult.size()-1)
{
finalResult.emplace_back(tmp);
}
}
}
}
if(interval < vectSize-i)
{
interval ++;
}
else
{
cont = false;
}
tmp.clear();
}
}
return finalResult;
}
};
想請問一下這個到底是發生甚麼問題了呢?
起來錯誤訊息是說:
您的程式超過了記憶體的範圍。特別是,AddressSanitizer 報告了 heap-buffer-overflow 錯誤,指出您正在讀取超出分配的區域,這可能是因為數組下標超出界限,或者您没有分配足夠的記憶體,或者您的記憶體是有錯誤的,因此無法安全地訪問。
試試看加上一些防呆,確保 curr 不超出陣列範圍
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums)
{
vector<vector<int> > finalResult;
int vectSize = nums.size();
int interval = 1;
bool cont = true;
for(int i = 0 ; i < vectSize-1 ; i++)
{
cont = true;
interval = 1;
while(cont)
{
int curr = i+interval ;
// 如果curr已經超出了nums數組的範圍,則直接跳出while
if (curr >= vectSize)
{
break;
}
vector<int> tmp;
tmp.emplace_back(nums[i]);
// 加入 curr < vectSize 的限制
while(nums[curr] >= tmp[tmp.size()-1] && curr < vectSize)
{
tmp.emplace_back(nums[curr]);
curr++;
if(finalResult.size() == 0)
{
finalResult.emplace_back(tmp);
}
else
{
for(int k = 0 ; k < finalResult.size() ; k++)
{
if(finalResult[k] == tmp)
{
break;
}
if(k==finalResult.size()-1)
{
finalResult.emplace_back(tmp);
}
}
}
}
if(interval < vectSize-i)
{
interval ++;
}
else
{
cont = false;
}
tmp.clear();
}
}
return finalResult;
}
vector<int> nums;
};
int main(int argc, char* argv[])
{
Solution solution;
vector<int> vect1{4,6,7,7};
vector<int> vect2{4,4,3,2,1};
if(strcmp(argv[1], "0") == 0)
{
solution.nums = vect1;
}
else if(strcmp(argv[1], "1") == 0)
{
solution.nums = vect2;
}
else
{
cout << "Please check for the valid input!" << endl;
}
vector<vector<int> > finalSol = solution.findSubsequences(solution.nums);
cout << "[" ;
for(int i = 0 ; i < finalSol.size() ; i++)
{
cout << "[" ;
for(int j = 0 ; j < finalSol[i].size() ; j++)
{
cout << finalSol[i][j];
if(j<finalSol[i].size()-1)
{
cout << " " ;
}
}
cout << "]" ;
if(i < finalSol.size()-1)
{
cout << ", " ;
}
}
cout << "]" << endl;
}