LC 986. Interval List Intersections

Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.

Return the intersection of these two interval lists.

(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.  The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval.  For example, the intersection of [1, 3] and [2, 4] is [2, 3].)

class Solution {
public:
  vector<Interval> intervalIntersection(vector<Interval>& A, vector<Interval>& B) {
    int ai = 0, bi = 0;
    vector<Interval> ret;
    while(ai < A.size() && bi < B.size()) {
      if(A[ai].end < B[bi].start) {
        ai++;
        continue;
      } else if(B[bi].end < A[ai].start) {
        bi++;
        continue;
      }
      ret.push_back(Interval(max(A[ai].start, B[bi].start), min(A[ai].end, B[bi].end)));
      if(A[ai].end <= B[bi].end) {
        ai++;
      }else{
        bi++;
      }
    }
    return ret;
  }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10350421.html

时间: 2024-11-02 19:46:23

LC 986. Interval List Intersections的相关文章

【leetcode】986. Interval List Intersections

题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order. Return the intersection of these two interval lists. (Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with 

LeetCode 986. Interval List Intersections

原题链接在这里:https://leetcode.com/problems/interval-list-intersections/ 题目: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order. Return the intersection of these two interval lists. (Formally, a closed inte

[Swift Weekly Contest 122]LeetCode986. 区间列表的交集 | Interval List Intersections

Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order. Return the intersection of these two interval lists. (Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <=

【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日补充的报告.当时不想写.rank:1093/3924,AC:2/4.还是太慢了. [984]String Without AAA or BBB(第一题 4分)(Greedy, M) 给了两个数字,A 代表 A 个 'A', B 代表 B 个'B' 在字符串里面.返回一个可行的字符串,字符串中包含 A

1272. Remove Interval

Given a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the set of real numbers x such that a <= x < b. We remove the intersections between any interval in intervals and the interval toBeRemoved. Return a sorted lis

JsSIP.UA.JsSIP 总是返回错误:422 Session Interval Too Small

在JsSIP 中 JsSIP.UA.call 总是 返回错误:422 Session Interval Too Small 关于错详情在这篇文章中解释的比较详尽:http://www.cnblogs.com/yoyotl/p/4980817.html 但是没有JsSIP的解决方法 具体的解决方法如下: JsSIP.js中的搜索 SESSION_EXPIRES: 把这个参数改大,  大于120就行了, (要是还不行,就继续加大吧^.^) SESSION_EXPIRES: 90 加大: SESSIO

LeetCode Insert Interval

原题链接在这里:https://leetcode.com/problems/insert-interval/ AC Java: 1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8

AngularJS中$timeout和$interval的用法详解

1. 先将$interval,$timeout,作为参数注入到controller中,例如rds.controller('controllerCtrl', ['app', '$scope','$http','$routeParams','$filter','$location','$interval','$timeout', function (app, $scope,$http,$routeParams,$filter,$location,$interval,$timeout) {2.在需要用

Lc.exe已退出 代码为-1

今天使用vs2010开发,有人在vss项目中增加了一个第三方组件,后来删除了,我的计算机上没有这个第三方组件,结果导致了LC.exe错误:"Lc.exe已退出 代码为-1 " 解决方法: 1.把项目文件夹下Properties文件夹下的licenses.licx文件删除,重新编译即可: 2.文本方式打开*.csproj文件,在文件中查找licenses.licx字样,删除对应节点. 注意:还有一种情况就是Properties文件夹下已经没有licenses.licx文件了,程序还是报这