问题
I tried to solve Leetcode Problem 54 - spiral and got stuck in empty vector input.
the question is about spiral list. input is 2d vector and output should be vector list which written by spiral direction.
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
problem is when input is empty list.
Input: []
it produces runtime error.
another testcases passed except empty input like [] .
It seems no runtime error during testing in my mac OSX terminal, but Leetcode says
'Line 933: Char 34: runtime error: reference binding to null pointer of type 'struct value_type' (stl_vector.h) '
Here is link https://leetcode.com/problems/spiral-matrix/
Also I attach codes below ...
class Solution {
public:
vector<int> answer;
int left = 0, right = 0;
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix[0].size()<1) return {};
vector<vector<int>> flag(matrix.size(),vector<int>(matrix[0].size(),0));
while(1){
flag[left][right] =1;
answer.push_back(matrix[left][right]);
if(right+1<matrix[0].size() && flag[left][right+1] == 0){
++right;
continue;
}
else if(left+1<matrix.size() && flag[left+1][right] == 0 ){
++left;
continue;
}
else if(right-1>=0 && flag[left][right-1]==0){
--right;
continue;
}
else if(left-1>=0 && flag[left-1][right]==0){
--left;
continue;
}
else break;
}
return answer;
}
};
回答1:
Thanks for comments, I figure out how to solve this one myself. I changed
if(matrix[0].size()<1
to
if(matrix.empty()) return {};
and it worked. Also I found my algorithm was wrong and fixed it.
using namespace std;
enum class Direction{
RIGHT,
DOWN,
LEFT,
UP
};
class Solution {
public:
Direction direc;
vector<int> answer;
int left = 0, right = 0;
vector<int> spiralOrder(vector<vector<int>>& matrix) {
Direction direc = Direction::RIGHT;
if(matrix.empty()) return {};
vector<vector<int>> flag(matrix.size(),vector<int>(matrix[0].size(),0));
while(1){
flag[left][right] =1;
answer.push_back(matrix[left][right]);
switch(direc){
case Direction::RIGHT:
if(right+1<matrix[0].size() && flag[left][right+1] == 0){
++right;
continue;
}
else if(left+1<matrix.size() && flag[left+1][right] == 0 ){
++left;
direc = Direction::DOWN;
continue;
}
else break;
case Direction::DOWN:
if(left+1<matrix.size() && flag[left+1][right] == 0 ){
++left;
continue;
}
else if(right-1>=0 && flag[left][right-1]==0){
--right;
direc = Direction::LEFT;
continue;
}
else break;
case Direction::LEFT:
if(right-1>=0 && flag[left][right-1]==0){
--right;
continue;
}
else if(left-1>=0 && flag[left-1][right]==0){
--left;
direc = Direction::UP;
continue;
}
else break;
case Direction::UP:
if(left-1>=0 && flag[left-1][right]==0){
--left;
continue;
}
else if(right+1<matrix[0].size() && flag[left][right+1] == 0){
++right;
direc = Direction::RIGHT;
continue;
}
else break;
}
break;
} // switch-case
return answer;
}
};
回答2:
Line 923: Char 9: runtime error: reference binding to null pointer of type 'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >' (stl_vector.h)
Means you can only examine whether the vector is empty and return immediately. You can't do other things such as declaring variables. If you do have some other operations before the examinations of empty vector, you will get the above warning.
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size() == 0)
return "";
int len = strs.size();
int lens = strs[0].size();
string result = "";
for(int i = 0;i < lens;i++)
{
for(int j = 1;j < len;j++)
{
if(strs[0][i]!=strs[j][i])
{
return result;
break;
}
}
result+=strs[0][i];
}
return result;
}
来源:https://stackoverflow.com/questions/54170028/line-933-char-34-runtime-error-reference-binding-to-null-pointer-of-type-str