runtime error??


  • 0
    N

    Hi, below is my code. I did not check nums size is 0 case, because for condition has already handled it .
    However, I still got runtime error for empty input. Could anyone tell me why ?

    Runtime Error Message:
    reference binding to null pointer of type 'value_type'

    class Solution {
    public:
        void wiggleSort(vector<int>& nums) {
            for (int i = 0; i < (nums.size() - 1); i++) {
                if ((i % 2 == 0 && nums[i] > nums[i + 1]) ||
                    (i % 2 != 0 && nums[i] < nums[i + 1])) {
                    swap(nums[i], nums[i + 1]);
                }
    
            }
            
            return;
        }
    };
    
    

  • 1

    @nathan nums.size() is of type size_t which is often the same as unsigned long long. If nums.size() == 0, subtracting 1 from it would result in an underflow. Perhaps reconsider the expression i < (nums.size() - 1);.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.