{"id":2104,"date":"2016-08-19T21:02:43","date_gmt":"2016-08-19T13:02:43","guid":{"rendered":"http:\/\/boweihe.me\/?p=2104"},"modified":"2016-08-19T21:02:43","modified_gmt":"2016-08-19T13:02:43","slug":"leetcode-385-mini-parser","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2104","title":{"rendered":"LeetCode 385. Mini Parser"},"content":{"rendered":"<h3>\u601d\u8def<\/h3>\n<p>\u4e00\u5f00\u59cb\u8fd8\u4ee5\u4e3a\u53c8\u56de\u5230\u4e86\u7f16\u8bd1\u539f\u7406\uff0c\u60f3\u5230\u4e86\u72b6\u6001\u8f6c\u6362\u56fe\u554a\u4e4b\u7c7b\u7684\u3002\u540e\u6765\u53d1\u73b0\u8fd9\u4e2a\u9898\u76ee\u6ca1\u6709\u8fd9\u4e48\u53d8\u6001\uff0c\u8981\u8003\u8651\u7684\u662f\u4e2a\u6808\u7684\u95ee\u9898\u3002<br \/>\n\u4e3a\u4ec0\u4e48\u4f1a\u60f3\u5230\u6808\u5462\uff1f\u56e0\u4e3a\u770b\u5230\u4e86\u62ec\u53f7\uff0c\u56e0\u4e3a\u62ec\u53f7\u7ed3\u675f\u7684\u65f6\u5019\u6211\u4eec\u8981\u628a\u67d0\u4e2a\u90e8\u5206\u7684\u5185\u5bb9\u6dfb\u5230\u66f4\u5927\u7684\u62ec\u53f7\u91cc\u5934\u53bb\u3002<br \/>\n\u6211\u7684\u5904\u7406\u65b9\u5f0f\u662f\uff1a<\/p>\n<ul>\n<li>\u6784\u9020\u4e00\u4e2a\u5de5\u4f5c\u6808\uff0c\u4e00\u5f00\u59cb\u662f\u7a7a\u7684<\/li>\n<li>\u00a0\u78b0\u5230\u6570\u5b57\n<ul>\n<li>\u751f\u6210\u4e00\u4e2a\u53ea\u5305\u542b\u6570\u5b57\u7684NestedInteger\n<ul>\n<li>\u5982\u679c\u5de5\u4f5c\u6808\u7a7a\uff0c\u90a3\u4e48\u5b57\u7b26\u4e32\u5c31\u662f\u4e2a\u7eaf\u6570\u5b57\uff0c\u76f4\u63a5\u8fd4\u56de<\/li>\n<li>\u5982\u679c\u5de5\u4f5c\u6808\u975e\u7a7a\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5c31\u8981\u52a0\u5230\u5f53\u524d\u5de5\u4f5c\u6808\u9876\u7aef\u7684\u5143\u7d20\u540e\u9762\uff08\u8003\u8651\u60c5\u51b5[35,345]\u8fd9\u79cd\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u78b0\u5230'[&#8216;\n<ul>\n<li>\u65b0\u5efa\u4e00\u4e2a\u7a7a\u7684NestedInteger\uff08\u81f3\u4e8e\u91cc\u9762\u586b\u4ec0\u4e48\uff0c\u4e0d\u662f\u8fd9\u91cc\u8981\u8003\u8651\u7684\u4e8b\u513f\uff09<\/li>\n<li>\u628a\u5b83\u538b\u5165\u6808<\/li>\n<\/ul>\n<\/li>\n<li>\u78b0\u5230&#8217;]&#8217;\n<ul>\n<li>\u53d6\u51fa\u5f53\u524d\u6808\u9876\u5143\u7d20formerTop<\/li>\n<li>\u5224\u65ad\u6808\u662f\u5426\u7a7a\u4e86\n<ul>\n<li>\u975e\u7a7a\uff0c\u5219formerTop\u8981\u52a0\u5230\u5f53\u524d\u6808\u9876\u7684\u540e\u9762<\/li>\n<li>\u7a7a\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e86<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u78b0\u5230&#8217;,&#8217;\n<ul>\n<li>\u5176\u5b9e\u6ca1\u4ec0\u4e48\u7528\uff0c\u5f80\u524d\u632a\u52a8\u5427<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u6b65\u9aa4\u8fd8\u80fd\u518d\u7cbe\u7b80\u4e00\u4e0b\u7684\uff0c\u5224\u65ad\u6808\u662f\u5426\u7a7a\uff0c\u662f\u5426\u8fd4\u56de\u7684\u90e8\u5206\u662f\u91cd\u590d\u4e86<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true\">\/**\n * \/\/ This is the interface that allows for creating nested lists.\n * \/\/ You should not implement it, or speculate about its implementation\n * class NestedInteger {\n *   public:\n *     \/\/ Constructor initializes an empty nested list.\n *     NestedInteger();\n *\n *     \/\/ Constructor initializes a single integer.\n *     NestedInteger(int value);\n *\n *     \/\/ Return true if this NestedInteger holds a single integer, rather than a nested list.\n *     bool isInteger() const;\n *\n *     \/\/ Return the single integer that this NestedInteger holds, if it holds a single integer\n *     \/\/ The result is undefined if this NestedInteger holds a nested list\n *     int getInteger() const;\n *\n *     \/\/ Set this NestedInteger to hold a single integer.\n *     void setInteger(int value);\n *\n *     \/\/ Set this NestedInteger to hold a nested list and adds a nested integer to it.\n *     void add(const NestedInteger &amp;ni);\n *\n *     \/\/ Return the nested list that this NestedInteger holds, if it holds a nested list\n *     \/\/ The result is undefined if this NestedInteger holds a single integer\n *     const vector&lt;NestedInteger&gt; &amp;getList() const;\n * };\n *\/\nclass Solution {\nprivate:\n    stack&lt;NestedInteger*&gt; workingStack;\npublic:\n    int readIntVal(const string&amp; s, int&amp; pos)\n    {\n        \/\/ pos will be set to the end index of current integer\n        int value = 0;\n        bool neg = false;\n        if(s[pos] == '-'){\n            neg = true;\n            pos++;\n        }\n        while(pos &lt; s.size() &amp;&amp;\n                (s[pos] &gt;= '0' &amp;&amp; s[pos] &lt;= '9'))\n        {\n            value *= 10;\n            value += (s[pos] - '0');\n            pos++;\n        }\n        pos--;\n        return neg? -value : value;\n    }\n    NestedInteger deserialize(string s) {\n        int pos = 0;\n        while(pos &lt; s.size())\n        {\n            char c = s[pos];\n            if(c == '-' || (c &gt;= '0' &amp;&amp; c &lt;= '9'))\n            {\n                NestedInteger* digitNI = new NestedInteger(readIntVal(s, pos)); \/\/Integer containing NestedInteger\n                if(workingStack.empty())\n                {\n                    \/\/Should return\n                    return *digitNI;\n                }\n                else\n                {\n                    \/\/Append to existing NI\n                    NestedInteger* currNI = workingStack.top();\n                    currNI-&gt;add(*digitNI);\n                    pos++;\n                }\n            }\n            else if(c == ',')\n            {\n                pos++;\n            }\n            else if(c == '[')\n            {\n                \/\/Create an NI and push to working stack\n                NestedInteger* ni = new NestedInteger();\n                workingStack.push(ni);\n                pos++;\n            }\n            else if(c == ']')\n            {\n                \/\/pop workingStack and append former-top item to current top\n                NestedInteger* formerTop = workingStack.top();\n                workingStack.pop();\n                if(workingStack.empty())\n                    return *formerTop;\n                else\n                {\n                    \/\/Append\n                    NestedInteger* currTop = workingStack.top();\n                    currTop-&gt;add(*formerTop);\n                    pos++;\n                }\n            }\n        }\n        \/\/Shouldn't be here!\n        return NestedInteger();\n    }\n};<\/pre>\n<h3><\/h3>\n<h3>\u9898\u76ee<\/h3>\n<p>Given a nested list of integers represented as a string, implement a parser to deserialize it.<br \/>\nEach element is either an integer, or a list &#8212; whose elements may also be integers or other lists.<br \/>\n<b>Note:<\/b> You may assume that the string is well-formed:<\/p>\n<ul>\n<li>String is non-empty.<\/li>\n<li>String does not contain white spaces.<\/li>\n<li>String contains only digits <code>0-9<\/code>, <code>[<\/code>, <code>-<\/code> <code>,<\/code>, <code>]<\/code>.<\/li>\n<\/ul>\n<p><b>Example 1:<\/b><\/p>\n<pre>Given s = \"324\",\nYou should return a NestedInteger object which contains a single integer 324.\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre>Given s = \"[123,[456,[789]]]\",\nReturn a NestedInteger object containing a nested list with 2 elements:\n1. An integer containing value 123.\n2. A nested list containing two elements:\n    i.  An integer containing value 456.\n    ii. A nested list with one element:\n         a. An integer containing value 789.\n<\/pre>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def \u4e00\u5f00\u59cb\u8fd8\u4ee5\u4e3a\u53c8\u56de\u5230\u4e86\u7f16\u8bd1\u539f\u7406\uff0c\u60f3\u5230\u4e86\u72b6\u6001\u8f6c\u6362\u56fe\u554a\u4e4b\u7c7b\u7684\u3002\u540e\u6765\u53d1\u73b0\u8fd9\u4e2a\u9898\u76ee\u6ca1\u6709\u8fd9\u4e48\u53d8\u6001\uff0c\u8981\u8003\u8651\u7684\u662f\u4e2a\u6808\u7684\u95ee\u9898\u3002 \u4e3a\u4ec0\u4e48\u4f1a\u60f3\u5230\u6808\u5462\uff1f\u56e0\u4e3a\u770b\u5230\u4e86\u62ec\u53f7\uff0c\u56e0\u4e3a\u62ec\u53f7\u7ed3\u675f\u7684\u65f6\u5019\u6211\u4eec\u8981\u628a\u67d0\u4e2a\u90e8\u5206\u7684\u5185\u5bb9\u6dfb\u5230\u66f4\u5927\u7684\u62ec\u53f7\u91cc\u5934\u53bb\u3002 \u6211\u7684\u5904\u7406\u65b9\u5f0f\u662f\uff1a \u6784\u9020\u4e00\u4e2a\u5de5\u4f5c\u6808\uff0c\u4e00\u5f00\u59cb\u662f\u7a7a\u7684 \u00a0\u78b0\u5230\u6570\u5b57 \u751f\u6210\u4e00\u4e2a\u53ea\u5305\u542b\u6570\u5b57\u7684NestedInteger \u5982\u679c\u5de5\u4f5c\u6808\u7a7a\uff0c\u90a3\u4e48\u5b57\u7b26\u4e32\u5c31\u662f\u4e2a\u7eaf\u6570\u5b57\uff0c\u76f4\u63a5\u8fd4\u56de \u5982\u679c\u5de5\u4f5c\u6808\u975e\u7a7a\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5c31\u8981\u52a0\u5230\u5f53\u524d\u5de5\u4f5c\u6808\u9876\u7aef\u7684\u5143\u7d20\u540e\u9762\uff08\u8003\u8651\u60c5\u51b5[35,345]\u8fd9\u79cd\uff09 \u78b0\u5230'[&#8216; \u65b0\u5efa\u4e00\u4e2a\u7a7a\u7684NestedInteger\uff08\u81f3\u4e8e\u91cc\u9762\u586b\u4ec0\u4e48\uff0c\u4e0d\u662f\u8fd9\u91cc\u8981\u8003\u8651\u7684\u4e8b\u513f\uff09 \u628a\u5b83\u538b\u5165\u6808 \u78b0\u5230&#8217;]&#8217; \u53d6\u51fa\u5f53\u524d\u6808\u9876\u5143\u7d20formerTop \u5224\u65ad\u6808\u662f\u5426\u7a7a\u4e86 \u975e\u7a7a\uff0c\u5219formerTop\u8981\u52a0\u5230\u5f53\u524d\u6808\u9876\u7684\u540e\u9762 \u7a7a\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e86 \u78b0\u5230&#8217;,&#8217; \u5176\u5b9e\u6ca1\u4ec0\u4e48\u7528\uff0c\u5f80\u524d\u632a\u52a8\u5427 \u8fd9\u4e2a\u6b65\u9aa4\u8fd8\u80fd\u518d\u7cbe\u7b80\u4e00\u4e0b\u7684\uff0c\u5224\u65ad\u6808\u662f\u5426\u7a7a\uff0c\u662f\u5426\u8fd4\u56de\u7684\u90e8\u5206\u662f\u91cd\u590d\u4e86 \u4ee3\u7801 \/** * \/\/ This is the interface that allows for creating nested lists. * \/\/ You should not implement it, or speculate about its implementation * class NestedInteger { * public: * \/\/ Constructor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[66,151],"class_list":["post-2104","post","type-post","status-publish","format-standard","hentry","category-study","category-technical","tag-leetcode-oj","tag-151"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2104"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2104\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}