[LeetCode]#1822. Sign of the Product of an Array

  • 1 if x is positive.
  • -1 if x is negative.
  • 0 if x is equal to 0.
Input: nums = [-1,-2,-3,-4,3,2,1]
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1
  1. init ans=1
  2. ans=ans x nums[i] by using loop
  3. if ans >0, return 1
  4. if ans <0, return -1
  5. if ans==0, return 0
class Solution:
def arraySign(self, nums):
ans=1
for i in range(len(nums)):
ans=ans*nums[i]
if ans >0:
return 1
if ans <0:
return -1
if ans ==0:
return 0

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Oracle Analytics Desktop Doesn’t Connect To Snowflake?!?!

Arduino,Mengakses sensor dht11,suhu dan kelembaban

Programming best-practices for beginners

How to Deploy a Fedora Linux VM Instance in GCP using Terraform

How to Make Money From Web Scraping Without Selling Data

Tree-Set data structure in C++

Recreating “Azure SignalR” Scale-Out in DotNetify

Feren OS 2022.03 — the newest delayed release

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2068. Check Whether Two Strings are Almost Equivalent

Merge two sorted linked lists

Cracking the Coding interview Book Problem Series: Problem 10

The Tortoise, the Hare, and the Cyclical Linked List