I am doing leetcode 376. Wiggle Subsequence. It went wrong when testing the first instance of input [1,7,4,9,2,5]. It showed that "Line 922: Char 34: runtime error: reference binding to null pointer of type 'value_type' (stl_vector.h)". Could anyone tell me what is going wrong? Thanks a lot!
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if(nums.size() < 2){
return nums.size();
}
std::priority_queue<int> big_heap;
vector<int> flag;
int result;
int length = nums.size();
for(int i = 0; i + 1 < length; i++){
if(nums[i+1] > nums[i]){
flag[i] = 1;
}
else if(nums[i+1] < nums[i]){
flag[i] = -1;
}
else{
flag[i] = 0;
}
}
int count = 1;
for(int i = 0; i + 2 < length;i++){
cout <<flag[i]<<endl;
if(flag[i] + flag[i+1] == 0){
count ++;
}
else{
big_heap.push(count);
count = 1;
}
}
big_heap.push(count);
result = big_heap.top() + 1;
return result;
}
};
vector<int> flagnot being initialized. Move the declaration behind the line containingint length = ...and callflag.resize(length);after having declared flag.