zoukankan      html  css  js  c++  java
  • 30 Day Challenge Day 22 | Leetcode 57. Insert Interval

    题解

    Medium

    避免使用 erase ,复杂度较高。

    class Solution {
    public:
        vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
            vector<vector<int>> res;
            int i = 0;
            
            while(i < intervals.size()) {
                if(newInterval[1] < intervals[i][0]) {
                    break;
                } else if(newInterval[0] > intervals[i][1]) {
                    res.push_back(intervals[i]);
                    i++;
                } else {
                    newInterval[0] = min(intervals[i][0], newInterval[0]);
                    newInterval[1] = max(intervals[i][1], newInterval[1]);
                    i++;
                }
            }
            
            res.push_back(newInterval);
            
            while(i < intervals.size()) {
                res.push_back(intervals[i]);
                i++;
            }
            
            return res;  
        }
    };
    
  • 相关阅读:
    成立移动互联网公司???
    C++的子对象
    单链表 操作的18种算法
    再论虚函数
    多线程(三)
    多线程(二)
    多线程(一)
    存储过程的参数
    git
    多态(三)
  • 原文地址:https://www.cnblogs.com/casperwin/p/13791488.html
Copyright © 2011-2022 走看看