Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运行时错误:引用绑定到类型为';int';(stl_vector.h):leetocode907_C++_Runtime Error - Fatal编程技术网

C++ 运行时错误:引用绑定到类型为';int';(stl_vector.h):leetocode907

C++ 运行时错误:引用绑定到类型为';int';(stl_vector.h):leetocode907,c++,runtime-error,C++,Runtime Error,我在试907。Leetcode上的子阵列最小值之和 我不断地发现这个错误: 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/

我在试907。Leetcode上的子阵列最小值之和

我不断地发现这个错误:

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
以下是我尝试过的(我理解这是非常幼稚的,很可能会失败,但我也想知道是什么错误导致它无法工作):-

类解决方案{
公众:
国际空间站(矢量和arr){
向量ans;
整数和=0;
int mn=0;
int m=100000007;
int n=阵列大小();

对于(int i=0;i在第一次迭代中

  • ans
    没有元素
  • i=0
  • j=i
    j=0
然后,不会将任何元素推送到
ans
,因为
i
为false


因此,
mn=ans[0];
是无效的越界访问,因为
ans
仍然没有任何元素。

当一个问题要求回答“模一些数”时,除非一直保持“模一些数”。(
(a+b)%m==(a%m)+(b%m))%m
是一个重要的等式。)
class Solution {
public:
    int sumSubarrayMins(vector<int>& arr) {
        vector<int> ans;
        int sum =0; 
        int mn=0;
        int m = 1000000007;
        int n = arr.size();
        for(int i = 0 ; i<n; i++){
            for(int j = i; j<n; j++){
                for(int k = i; k<j; k++){
                    ans.push_back(arr[k]);
                }
                sort(ans.begin(), ans.end());
                mn = ans[0];
                sum = sum + mn;
                ans.clear();
            }
        }
        return sum%m;
    }
};