{"id":8338,"date":"2021-04-12T09:34:21","date_gmt":"2021-04-12T16:34:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8338"},"modified":"2021-04-12T09:34:52","modified_gmt":"2021-04-12T16:34:52","slug":"leetcode-1822-sign-of-the-product-of-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1822-sign-of-the-product-of-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1822. Sign of the Product of an Array"},"content":{"rendered":"\n<p>There is a function&nbsp;<code>signFunc(x)<\/code>&nbsp;that returns:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1<\/code>&nbsp;if&nbsp;<code>x<\/code>&nbsp;is positive.<\/li><li><code>-1<\/code>&nbsp;if&nbsp;<code>x<\/code>&nbsp;is negative.<\/li><li><code>0<\/code>&nbsp;if&nbsp;<code>x<\/code>&nbsp;is equal to&nbsp;<code>0<\/code>.<\/li><\/ul>\n\n\n\n<p>You are given an integer array&nbsp;<code>nums<\/code>. Let&nbsp;<code>product<\/code>&nbsp;be the product of all values in the array&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>signFunc(product)<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-1,-2,-3,-4,3,2,1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The product of all values in the array is 144, and signFunc(144) = 1\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,5,0,2,-3]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The product of all values in the array is 0, and signFunc(0) = 0\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-1,1,-1,1,-1]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> The product of all values in the array is -1, and signFunc(-1) = -1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>-100 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sign Only<\/strong><\/h2>\n\n\n\n<p>No need to compute the product, only track the sign changes. Flip the sign when encounter a negative number, return 0 if there is any 0 in the array.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int arraySign(vector<int>& nums) {\n    int sign = 1;\n    for (int x : nums) {\n      if (x == 0) return 0;\n      else if (x < 0) sign = -sign;\n    }\n    return sign;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a function&nbsp;signFunc(x)&nbsp;that returns: 1&nbsp;if&nbsp;x&nbsp;is positive. -1&nbsp;if&nbsp;x&nbsp;is negative. 0&nbsp;if&nbsp;x&nbsp;is equal to&nbsp;0. You are given an integer array&nbsp;nums. Let&nbsp;product&nbsp;be the product of all values in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,31,705],"class_list":["post-8338","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-math","tag-sign","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8338","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=8338"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8338\/revisions"}],"predecessor-version":[{"id":8340,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8338\/revisions\/8340"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}