【发布时间】:2021-08-11 00:16:04
【问题描述】:
我在解决这个 leetcode 问题时遇到以下错误:
https://leetcode.com/problems/maximal-rectangle/
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9
这是我的代码:
int area(vector<int> heights) {
stack<int> s;
int n = heights.size();
int res = 0;
for(int i = 0; i<n; i++) {
while(!s.empty() && heights[s.top()] >= heights[i]) {
int temp = s.top();
s.pop();
int a = heights[temp]*((s.empty()) ? (i) : (i-s.top()-1));
res = max(res, a);
}
s.push(heights[i]);
}
while(!s.empty()) {
int temp = s.top();
s.pop();
int a = heights[temp]*((s.empty()) ? (n) : (n-s.top()-1));
if(a > res) res = a;
}
return res;
}
int maximalRectangle(vector<vector<char>>& matrix) {
vector<int> heights;
for(int i = 0; i<matrix[0].size(); i++) {
heights[i] = (matrix[0][i] - '0');
}
int res = area(heights);
for(int i = 1; i<matrix.size(); i++) {
for(int j = 0; j<matrix[i].size(); j++) {
if(matrix[i][j] == '0') heights[j] = 0;
else heights[j] += 1;
}
res = max(res, area(heights));
}
return res;
}
请帮我找出我的错误。
【问题讨论】: