[英]Runtime Error AddressSanitizer:DEADLYSIGNAL in leetcode
I am getting the following Runtime error:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==32==ERROR: AddressSanitizer: stack-overflow on address 0x7ffcdc3e2ff8 (pc 0x00000038409c bp 0x7ffcdc3e3020 sp 0x7ffcdc3e3000 T0)
==32==ABORTING
while solving https://leetcode.com/problems/unique-paths-iii/同时解决https://leetcode.com/problems/unique-paths-iii/
I am unable to figure out the root cause of it.我无法弄清楚它的根本原因。
My source code is as follows:我的源代码如下:
#define vvi vector<vector<int> >
#define vi vector<int>
class Solution {
public:
int helper(vvi &grid, vvi &dp, int m, int n, int i, int j) {
if(i < 0 || i >= n || j < 0 || j >= m || -1 == grid[i][j])
return 0;
if(0 == dp[i][j]) {
if(2 == grid[i][j])
dp[i][j] = 1;
else
dp[i][j] = helper(grid, dp, m, n, i - 1, j) +
helper(grid, dp, m, n, i, j + 1) +
helper(grid, dp, m, n, i + 1, j) +
helper(grid, dp, m, n, i, j - 1);
}
return dp[i][j];
}
int uniquePathsIII(vector<vector<int>>& grid) {
if(grid.size() == 0)
return 0;
int n = grid.size();
int m = grid[0].size();
vvi dp(n, vi(m, 0));
return helper(grid, dp, m, n, 0, 0);
}
};
Thanks for any help:)谢谢你的帮助:)
The issue has been resolved.问题已解决。 It was happening because the visited nodes were being pushed onto the stack again for computation and this was resulting in infinite recursion.发生这种情况是因为被访问的节点再次被推入堆栈进行计算,这导致了无限递归。
It was solved using a visited array that ensures that a specific block in the path is checked only once.它是使用访问数组来解决的,该数组确保路径中的特定块只检查一次。
If you are facing similar problems then check if your recursion will terminate or loop endlessly.如果您遇到类似的问题,请检查您的递归是否会终止或无限循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.