{"id":162976,"date":"2020-10-02T17:11:19","date_gmt":"2020-10-02T09:11:19","guid":{"rendered":"http:\/\/4563.org\/?p=162976"},"modified":"2020-10-02T17:11:19","modified_gmt":"2020-10-02T09:11:19","slug":"google-%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%90%88%e5%b9%b6%e5%8c%ba%e9%97%b4","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=162976","title":{"rendered":"Google \u9762\u8bd5\u9898\uff1a\u5408\u5e76\u533a\u95f4"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Google \u9762\u8bd5\u9898\uff1a\u5408\u5e76\u533a\u95f4               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7ed9\u51fa\u82e5\u5e72\u95ed\u5408\u533a\u95f4\uff0c\u5408\u5e76\u6240\u6709\u91cd\u53e0\u7684\u90e8\u5206\u3002<\/p>\n<p>\u70b9\u6b64\u5728\u7ebf\u505a\u9898<\/p>\n<h2>\u6837\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165: [(1,3)] \u8f93\u51fa: [(1,3)] <\/code><\/pre>\n<h2>\u6837\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165:  [(1,3),(2,6),(8,10),(15,18)] \u8f93\u51fa: [(1,6),(8,10),(15,18)] <\/code><\/pre>\n<h2>\u89e3\u9898\u601d\u8def<\/h2>\n<p>\u5bf9\u5e94\u4e24\u4e2a\u533a\u95f4 a, b\uff0c\u5982\u4f55\u5224\u65ad\u5b83\u4eec\u76f8\u4ea4\uff1f \u5f53 a, b \u6ee1\u8db3 max(a.start,b.start)&lt;=min(a.end,b.end)max(a.start,b.start)&lt;=min(a.end,b.end)\u65f6\uff0c\u4e24\u8005\u76f8\u4ea4\u3002 \u6211\u4eec\u5e94\u8003\u8651\u67d0\u79cd\u6392\u5e8f\u65b9\u5f0f\uff0c\u4f7f\u5f97\u6240\u6709\u76f8\u4ea4\u7684\u533a\u95f4\u5bf9\u5e94\u4e00\u6bb5\u8fde\u7eed\u7684\u6570\u7ec4\u4e0b\u6807\uff0c\u8fd9\u6837\u65b9\u4fbf\u6211\u4eec\u8fdb\u884c\u4e4b\u540e\u7684\u5408\u5e76\u64cd\u4f5c\u3002 \u8fd9\u79cd\u6392\u5e8f\u65b9\u5f0f\u5e94\u8be5\u662f\u5bf9\u533a\u95f4\u7684\u5de6\u7aef\u70b9\u6309\u4ece\u5c0f\u5230\u5927\u7684\u65b9\u5f0f\u8fdb\u884c\u6392\u5e8f\uff0c\u5047\u8bbe\u6211\u4eec\u5b58\u5728\u4e00\u4e2a\u5df2\u7ecf\u6309\u7167\u4e0a\u9762\u65b9\u5f0f\u6392\u5e8f\u7684\u6570\u7ec4\uff1a[1\uff0c2][1\uff0c3][2\uff0c4][5\uff0c7][6\uff0c10][6\uff0c11]\u3002 \u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u6211\u904d\u5386\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u6bcf\u6b21\u8bbf\u95ee\u4e00\u4e2a\u533a\u95f4 ai\uff0c\u90fd\u80fd\u4fdd\u8bc1\uff1a\u5982\u679c ai \u548c\u5b83\u524d\u9762\u7684\u533a\u95f4\u4e0d\u76f8\u4ea4\uff0c\u90a3\u4e48 ai \u540e\u9762\u7684\u4efb\u610f\u533a\u95f4\u90fd\u4e0d\u80fd\u548c ai \u524d\u9762\u7684\u4efb\u610f\u533a\u95f4\u76f8\u4ea4\u3002\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u7ea7\u522b\uff0c\u5373\u4e0d\u4f1a\u51fa\u73b0\u5176\u4ed6\u7684\u904d\u5386\u3002 \u6bd4\u5982\u533a\u95f4[5\uff0c7]\uff0c\u4ed6\u548c\u524d\u9762\u7684\u533a\u95f4\u90fd\u6ca1\u6709\u76f8\u4ea4\uff0c\u4ed6\u540e\u9762\u7684\u6240\u6709\u533a\u95f4\u548c\u5b83\u4e00\u6837\uff0c\u6ca1\u6709\u548c[5\uff0c7]\u524d\u9762\u7684\u533a\u95f4\u6709\u4ea4\u96c6\u3002<\/p>\n<h2>\u4ee3\u7801\u601d\u8def<\/h2>\n<ol>\n<li>\u5bf9\u533a\u95f4\u6570\u7ec4\u6309\u533a\u95f4\u7684\u5de6\u7aef\u70b9 start \u6392\u5e8f\u3002<\/li>\n<li>\u5c06\u6700\u540e\u7684\u533a\u95f4\u8d4b\u503c lastinterval \u4e3a intervals[0]\u3002<\/li>\n<li>\u904d\u5386\u8f93\u5165\uff0c\u5982\u679c lastinterval \u548c\u5f53\u524d\u533a\u95f4\u76f8\u4ea4\uff0c\u5408\u5e76\u4e24\u4e2a\u533a\u95f4\u3002<\/li>\n<li>\u5982\u679c\u4e0d\u76f8\u4ea4\uff0c\u5c06 lastinterval \u52a0\u5165\u7ed3\u679c\uff0c\u5e76\u5c06 lastinterval \u8d4b\u503c\u4e3a\u5f53\u524d\u7684\u533a\u95f4\u3002<\/li>\n<\/ol>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<p>\u8bbe\u533a\u95f4\u7684\u4e2a\u6570\u4e3a N \u3002 \u65f6\u95f4\u590d\u6742\u5ea6<\/p>\n<ul>\n<li>\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(NlogN)\u3002<\/li>\n<li>\u904d\u5386\u4e00\u904d\u6570\u7ec4\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N)\u3002<\/li>\n<li>\u603b\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(NlogN)\u3002 \u7a7a\u95f4\u590d\u6742\u5ea6<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u53ef\u80fd\u5b58\u5728\u6bcf\u4e00\u4e2a\u533a\u95f4\u90fd\u4e0d\u4e0e\u4efb\u4f55\u4e00\u6bb5\u533a\u95f4\u76f8\u4ea4\uff0c\u8fd4\u56de\u7684\u7b54\u6848\u548c\u4f20\u5165\u7684\u53c2\u6570\u957f\u5ea6\u76f8\u7b49\u3002<\/li>\n<\/ul>\n<pre><code>public class Solution {     \/**      * @param intervals: interval list.      * @return: A new interval list.      *\/     public List&lt;Interval&gt; merge(List&lt;Interval&gt; intervals) {         if (intervals.size() == 0) {             return intervals;         }                  \/\/ \u6839\u636e\u533a\u95f4\u7684 start \u503c\u6392\u5e8f         intervals.sort(Comparator.comparing(i -&gt; i.start));                  List&lt;Interval&gt; result = new ArrayList&lt;Interval&gt;();         Interval lastInterval = intervals.get(0);                  \/\/ \u5982\u679c\u4e24\u6bb5\u533a\u95f4\u6709\u4ea4\u96c6\u7684\u8bdd\uff0c\u5408\u5e76\u4e24\u6bb5\u533a\u95f4         \/\/ \u6ca1\u6709\u7684\u8bdd\uff0c\u5c06\u6700\u540e\u7684\u533a\u95f4\u52a0\u5165\u7b54\u6848\uff0c\u5e76\u4ee4\u65b0\u7684\u533a\u95f4\u4f5c\u4e3a\u6700\u540e\u7684\u533a\u95f4         for (Interval interval: intervals) {             if (haveIntercation(lastInterval, interval)) {                 lastInterval = mergeTwoIntervals(lastInterval, interval);             }             else {                 result.add(lastInterval);                 lastInterval = interval;             }         }         result.add(lastInterval);                  return result;     }     \/\/ \u5408\u5e76\u4e24\u6bb5\u533a\u95f4     private Interval mergeTwoIntervals(Interval a, Interval b) {         return new Interval(Math.min(a.start, b.start), Math.max(a.end, b.end));     }     \/\/ \u5224\u65ad\u533a\u95f4\u662f\u5426\u6709\u4ea4\u96c6\uff0c\u8981\u6ee1\u8db3\u8f83\u5927\u7684 start \u5c0f\u4e8e\u7b49\u4e8e\u8f83\u5c0f\u7684 end     private boolean haveIntercation(Interval a, Interval b) {         return Math.max(a.start, b.start) &lt;= Math.min(a.end, b.end);     } }  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Google \u9762\u8bd5\u9898\uff1a\u5408\u5e76\u533a\u95f4 \u8cc7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/162976"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=162976"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/162976\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=162976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=162976"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=162976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}