行933:字符34:运行时错误:引用绑定到类型为'struct value_type'(stl_vector.h)的空指针-Leet Code Spiral

时间:2019-01-13 14:51:59

标签: c++ spiral

我试图解决Leetcode问题54-螺旋 并卡在空向量输入中。

问题在于螺旋清单。输入为2d向量,输出应为螺旋方向写入的向量列表。

public class ConnectionClass {
    String ip = "public_address"; //works with local 192.168.100.4
    String classs = "net.sourceforge.jtds.jdbc.Driver";
    String db = "ProiectBD";  //baza de date
    String un = "proiectbd1";  //username
    String password = "123"; //password

@SuppressLint("NewApi")
public Connection CONN() {
    StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL = null;
    try {

        Class.forName(classs);
        ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                + "databaseName=" + db + ";user=" + un + ";password="
                + password + ";";
        conn = DriverManager.getConnection(ConnURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return conn;
}}

问题是输入为空列表时。

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]

它会产生运行时错误。

通过了另一个测试用例,除了 [] 之类的空输入。

在我的Mac OSX终端中进行测试时似乎没有运行时错误, 但是Leetcode说

    '行933:字符34:运行时错误:引用绑定到类型为'struct value_type'(stl_vector.h)的空指针     '

这里是链接 https://leetcode.com/problems/spiral-matrix/

我还在下面附加代码...

Input: [] 

2 个答案:

答案 0 :(得分:1)

感谢您的评论,我想出了解决自己的方法。 我改变了

if(matrix[0].size()<1

if(matrix.empty()) return {};

成功了。 我也发现我的算法是错误的,并对其进行了修复。

    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; 
    }
};

答案 1 :(得分:0)

@package

意味着您只能检查向量是否为空并立即返回。您不能做其他事情,例如声明变量。如果在检查空向量之前确实进行了其他操作,则会收到上述警告。

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)