I got Wrong Answer for this problem but I don't know why


  • 0
    D

    Input: [[1,5]], [0,0]
    Output: [[1,5],[0,0]]
    Expected: [[0,0],[1,5]]

    My code is as follows:

    class Solution {
    public:
        vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
            int left=newInterval.start;
            int right=newInterval.end;
            vector<Interval> result;
            for(int i=0;i<intervals.size();i++)
            {
                if(right<intervals[i].start||left>intervals[i].end)
                        result.push_back(intervals[i]);
                else
                {
                    if(intervals[i].start<left)
                        left=intervals[i].start;
                    if(intervals[i].end>right)
                        right=intervals[i].end;
                }
            }
            result.push_back(Interval(left,right));
            return result;
        }
    };

  • 0
    Y

    the result must be sorted by the first element of intervals[i]


Log in to reply
 

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