<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sign Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/sign/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/sign/</link>
	<description></description>
	<lastBuildDate>Mon, 12 Apr 2021 16:34:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>sign Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/sign/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1822. Sign of the Product of an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1822-sign-of-the-product-of-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1822-sign-of-the-product-of-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 12 Apr 2021 16:34:21 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[sign]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8338</guid>

					<description><![CDATA[<p>There is a function&#160;signFunc(x)&#160;that returns: 1&#160;if&#160;x&#160;is positive. -1&#160;if&#160;x&#160;is negative. 0&#160;if&#160;x&#160;is equal to&#160;0. You are given an integer array&#160;nums. Let&#160;product&#160;be the product of all values in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1822-sign-of-the-product-of-an-array/">花花酱 LeetCode 1822. Sign of the Product of an Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>There is a function&nbsp;<code>signFunc(x)</code>&nbsp;that returns:</p>



<ul><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>



<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>



<p>Return&nbsp;<code>signFunc(product)</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,-2,-3,-4,3,2,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The product of all values in the array is 144, and signFunc(144) = 1
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,5,0,2,-3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The product of all values in the array is 0, and signFunc(0) = 0
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,1,-1,1,-1]
<strong>Output:</strong> -1
<strong>Explanation:</strong> The product of all values in the array is -1, and signFunc(-1) = -1
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>-100 &lt;= nums[i] &lt;= 100</code></li></ul>



<h2><strong>Solution: Sign Only</strong></h2>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int arraySign(vector&lt;int&gt;&amp; nums) {
    int sign = 1;
    for (int x : nums) {
      if (x == 0) return 0;
      else if (x &lt; 0) sign = -sign;
    }
    return sign;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1822-sign-of-the-product-of-an-array/">花花酱 LeetCode 1822. Sign of the Product of an Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1822-sign-of-the-product-of-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
