{"id":3304,"date":"2018-07-26T07:50:44","date_gmt":"2018-07-26T14:50:44","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3304"},"modified":"2018-07-26T07:51:36","modified_gmt":"2018-07-26T14:51:36","slug":"leetcode-22-generate-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-22-generate-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 22. Generate Parentheses"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given\u00a0<i>n<\/i>\u00a0pairs of parentheses, write a function to generate all combinations of well-formed parentheses.<\/p>\n<p>For example, given\u00a0<i>n<\/i>\u00a0= 3, a solution set is:<\/p>\n<pre class=\"crayon:false\">[\r\n  \"((()))\",\r\n  \"(()())\",\r\n  \"(())()\",\r\n  \"()(())\",\r\n  \"()()()\"\r\n]<\/pre>\n<h1><strong>Solution: DFS<\/strong><\/h1>\n<p>Time complexity: O(2^n)<\/p>\n<p>Space complexity:\u00a0O(k + n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true  \">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; generateParenthesis(int n) {\r\n    vector&lt;string&gt; ans;\r\n    string cur;\r\n    if (n &gt; 0) dfs(n, n, cur, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void dfs(int l, int r, string&amp; s, vector&lt;string&gt;&amp; ans) {\r\n    if (l + r == 0) {\r\n      ans.push_back(s);\r\n      return;\r\n    }\r\n    if (r &lt; l) return;\r\n    if (l &gt; 0) {\r\n      dfs(l - 1, r, s += \"(\", ans);\r\n      s.pop_back();\r\n    }\r\n    if (r &gt; 0) {\r\n      dfs(l, r - 1, s += \")\", ans);\r\n      s.pop_back();\r\n    }\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-301-remove-invalid-parentheses\/\">\u82b1\u82b1\u9171 LeetCode 301. Remove Invalid Parentheses<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-678-valid-parenthesis-string\/\">\u82b1\u82b1\u9171 LeetCode 678. Valid Parenthesis String<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given\u00a0n\u00a0pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given\u00a0n\u00a0= 3, a solution set is: [ &#8220;((()))&#8221;, &#8220;(()())&#8221;,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[122,33,177,42],"class_list":["post-3304","post","type-post","status-publish","format-standard","hentry","category-searching","tag-combination","tag-dfs","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3304"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3304\/revisions"}],"predecessor-version":[{"id":3306,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3304\/revisions\/3306"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}