{"id":3191,"date":"2018-07-16T07:51:27","date_gmt":"2018-07-16T14:51:27","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3191"},"modified":"2018-07-16T08:03:11","modified_gmt":"2018-07-16T15:03:11","slug":"leetcode-238-product-of-array-except-self","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-238-product-of-array-except-self\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 238. Product of Array Except Self"},"content":{"rendered":"<h1><strong>Problem:<\/strong><\/h1>\n<p>Given an array\u00a0<code>nums<\/code>\u00a0of\u00a0<em>n<\/em>\u00a0integers where\u00a0<em>n<\/em>\u00a0&gt; 1, \u00a0return an array\u00a0<code>output<\/code>\u00a0such that\u00a0<code>output[i]<\/code>\u00a0is equal to the product of all the elements of\u00a0<code>nums<\/code>\u00a0except\u00a0<code>nums[i]<\/code>.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>  <code>[1,2,3,4]<\/code> <b>Output:<\/b> <code>[24,12,8,6]<\/code><\/pre>\n<p><strong>Note:\u00a0<\/strong>Please solve it\u00a0<strong>without division<\/strong>\u00a0and in O(<em>n<\/em>).<\/p>\n<p><strong>Follow up:<\/strong><br \/>\nCould you solve it with constant space complexity? (The output array\u00a0<strong>does not<\/strong>\u00a0count as extra space for the purpose of space complexity analysis.)<\/p>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 36 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; productExceptSelf(vector&lt;int&gt;&amp; nums) {\r\n    const int n = nums.size();\r\n    vector&lt;int&gt; l(n, 1); \/\/ l[i] = prod(nums[0] ~ nums[i - 1])\r\n    vector&lt;int&gt; r(n, 1); \/\/ l[i] = prod(nums[i + 1] ~ nums[n - 1])\r\n    vector&lt;int&gt; ans(n);    \r\n    \r\n    for (int i = 1; i &lt; n; ++i)\r\n      l[i] = l[i - 1] * nums[i - 1];\r\n    \r\n    for (int i = n - 2; i &gt;= 0; --i)\r\n      r[i] = r[i + 1] * nums[i + 1];\r\n        \r\n    for (int i = 0; i &lt; n; ++i)\r\n      ans[i] = l[i] * r[i];\r\n    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 32 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; productExceptSelf(vector&lt;int&gt;&amp; nums) {\r\n    const int n = nums.size();\r\n    int l = 1; \/\/ l = prod(nums[0] ~ nums[i - 1])\r\n    int r = 1; \/\/ r = prod(nums[i + 1] ~ nums[n - 1])\r\n    vector&lt;int&gt; ans(n, 1);\r\n        \r\n    for (int i = 0; i &lt; n; ++i) {\r\n      ans[i] *= l;\r\n      ans[n - i - 1] *= r;\r\n      l *= nums[i];\r\n      r *= nums[n - i - 1];\r\n    }\r\n    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given an array\u00a0nums\u00a0of\u00a0n\u00a0integers where\u00a0n\u00a0&gt; 1, \u00a0return an array\u00a0output\u00a0such that\u00a0output[i]\u00a0is equal to the product of all the elements of\u00a0nums\u00a0except\u00a0nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Note:\u00a0Please&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,177,338],"class_list":["post-3191","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-product","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3191","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=3191"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3191\/revisions"}],"predecessor-version":[{"id":3195,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3191\/revisions\/3195"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}