{"id":226635,"date":"2020-11-30T18:11:25","date_gmt":"2020-11-30T10:11:25","guid":{"rendered":"http:\/\/4563.org\/?p=226635"},"modified":"2020-11-30T18:11:25","modified_gmt":"2020-11-30T10:11:25","slug":"leetcode-%e5%88%b7%e9%a2%98%e7%96%91%e6%83%91%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88%e4%b8%8d%e4%bd%bf%e7%94%a8-final-%e4%bf%ae%e9%a5%b0%e4%bc%9a%e6%8f%90%e5%8d%87%e6%95%88%e7%8e%87%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=226635","title":{"rendered":"LeetCode \u5237\u9898\u7591\u60d1\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 final \u4fee\u9970\u4f1a\u63d0\u5347\u6548\u7387\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  LeetCode \u5237\u9898\u7591\u60d1\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 final \u4fee\u9970\u4f1a\u63d0\u5347\u6548\u7387\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : persona5 <\/span>  <span><i><\/i> 7<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u9898\u76ee\u672c\u8eab\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301 peek \u64cd\u4f5c\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<p>Peeking Iterator<\/p>\n<p>Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation &#8212; it essentially peek() at the element that will be returned by the next call to next().<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code>Assume that the iterator is initialized to the beginning of the list: [1,2,3].  Call next() gets you 1, the first element in the list. Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.  You call next() the final time and it returns 3, the last element.  Calling hasNext() after that should return false. <\/code><\/pre>\n<p>\u6211\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre><code>class PeekingIterator implements Iterator&lt;Integer&gt; {     private final Iterator&lt;Integer&gt; iterator;     private Integer value;      public PeekingIterator(Iterator&lt;Integer&gt; iterator) {         this.iterator = iterator;         this.value = null;     }      public Integer peek() {         if (value != null) return value;         value = iterator.next();         return value;     }      @Override     public boolean hasNext() {         return value != null || iterator.hasNext();     }      @Override     public Integer next() {         Integer temp;         if (value != null) {             temp = value;             value = null;         } else {             temp = iterator.next();         }         return temp;     } } <\/code><\/pre>\n<p>Runtime \u662f 93.72%<\/p>\n<p>iterator \u53bb\u6389 final \u4fee\u9970\uff0cvalue \u4e0d\u5728 Constructor \u4e2d\u8d4b\u503c null\uff0cRuntime \u5c31\u53d8\u4e3a 100.00% \u4e86\u3002<\/p>\n<pre><code>public class PeekingIterator implements Iterator&lt;Integer&gt; {      private Iterator&lt;Integer&gt; iterator;     private Integer value = null;      public PeekingIterator(Iterator&lt;Integer&gt; iterator) {         this.iterator = iterator;     }      ...... } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4594103\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chendy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u7528\u6539\uff0c\u591a\u63d0\u4ea4\u51e0\u6b21\uff0c\u65f6\u95f4\u548c\u5185\u5b58\u6d88\u8017\u90fd\u4e0d\u4e00\u6837\u7684<br \/>\u6bd5\u7adf\u662f java\uff0c\u4e0d\u786e\u5b9a\u7684\u4e1c\u897f\u6bd4\u8f83\u591a\uff0c\u9664\u975e\u5148\u9884\u70ed\u4e2a\u51e0\u4e07\u6b21\uff0c\u5426\u5219\u8fd0\u884c\u65f6\u95f4\u4e5f\u5c31\u56fe\u4e00\u4e50                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594104\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shuax <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540c\u6837\u7684\u4ee3\u7801\uff0c\u6bcf\u6b21\u8fd0\u884c\u65f6\u95f4\u90fd\u4e0d\u4e00\u6837\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594105\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : andrewpsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a\u8bf4\u7684\u5bf9\u3002<\/p>\n<p>\u8bb0\u5f97\u6709\u4e00\u9898\u8d2a\u5fc3\u6574\u7684\u8bdd\u65f6\u95f4\u590d\u6742\u5ea6\u662f m+n \u4f46\u662f\u7528\u4e8c\u5206\u6cd5\u662f mlog(n)\uff0c\u7136\u540e\u6211\u4e24\u4e2a\u89e3\u6cd5\u90fd\u5199\u4e86\u5728\u7a0b\u5e8f\u5165\u53e3\u7528 m \u548c n \u8fd0\u7b97\u4e00\u4e0b\u6765\u51b3\u5b9a\u662f\u7528\u8d2a\u5fc3\u8fd8\u662f\u4e8c\u5206\u3002\u6211\u63d0\u4ea4\u4e86\u53ef\u80fd\u6709\u4e8c\u4e09\u5341\u6b21\uff0c\u5b8c\u5168\u770b\u4e0d\u51fa\u4f18\u52bf\uff0c\u8fde\u89c4\u5f8b\u90fd\u6478\u4e0d\u6e05                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594106\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : pkupyx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5927\u6982\u7f16\u8bd1\u540e\u8fd0\u884c\u65f6\u7684\u778e\u68c0\u67e5\u7684\u4ee3\u7801\u91cf\u4f1a\u53d8\u591a\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594107\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Philippa <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b97\u6cd5\u7684\u6838\u5fc3\u662f\u590d\u6742\u5ea6\uff0c\u800c\u4e0d\u662f\u673a\u5668\u6027\u80fd\u7684\u6ce2\u52a8\u548c\u8bed\u8a00\u672c\u8eab\u7684\u5947\u6280\u6deb\u5de7\u3002\u6709\u4e9b\u4eba\u5728 leetcode \u4e0a\u4f7f\u7528\u4e00\u4e9b\u5947\u602a\u7684\u6beb\u65e0\u9053\u7406\u7684\u6280\u5de7\u6765\u83b7\u5f97\u66f4\u9ad8\u7684\u6392\u540d\u5176\u5b9e\u6beb\u65e0\u610f\u4e49\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594108\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : raaaaaar <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @andrewpsy #3 \u6211\u4eec\u4e00\u822c\u8bf4\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u6700\u5dee\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8981\u6570\u636e\u91cf\u8db3\u591f\u5927\u65f6\u624d\u80fd\u660e\u663e\u4f53\u73b0\u51fa\u6765\uff0c\u5982\u679c\u6570\u636e\u91cf\u4e0d\u5927\uff0c\u90a3\u4e0d\u9075\u5b88\u90a3\u4e2a\u6bd4\u8f83\u89c4\u5f8b\u5f88\u6b63\u5e38                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>LeetCode \u5237\u9898\u7591\u60d1\uff0c\u4e3a\u4ec0\u4e48&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\/226635"}],"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=226635"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/226635\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=226635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=226635"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=226635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}