当前位置:网站首页>Symbol of array element product of leetcode simple problem
Symbol of array element product of leetcode simple problem
2022-07-03 04:33:00 【·Starry Sea】
subject
Known functions signFunc(x) Will be based on x The positive and negative of returns a specific value :
If x Positive number , return 1 .
If x It's a negative number , return -1 .
If x Is equal to 0 , return 0 .
Give you an array of integers nums . Make product Is an array nums The product of the values of all elements in .
return signFunc(product) .
Example 1:
Input :nums = [-1,-2,-3,-4,3,2,1]
Output :1
explain : The product of all the values in the array is 144 , And signFunc(144) = 1
Example 2:
Input :nums = [1,5,0,2,-3]
Output :0
explain : The product of all the values in the array is 0 , And signFunc(0) = 0
Example 3:
Input :nums = [-1,1,-1,1,-1]
Output :-1
explain : The product of all the values in the array is -1 , And signFunc(-1) = -1
Tips :
1 <= nums.length <= 1000
-100 <= nums[i] <= 100
source : Power button (LeetCode)
Their thinking
You only need to count the number of negative numbers to traverse the array , If the number of negative numbers is odd and there is no 0, So that is -1, If the number of negative numbers is even and there is no 0, So that is 1, If... Exists in the array 0, So that is 0.
class Solution:
def arraySign(self, nums: List[int]) -> int:
count=0
for i in nums:
if i<0:
count+=1
elif i==0:
return 0
return -1 if count%2 else 1
边栏推荐
- [fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
- Number of uniform strings of leetcode simple problem
- Leetcode simple question: check whether the array is sorted and rotated
- Contents of welder (primary) examination and welder (primary) examination in 2022
- AWS VPC
- data2vec! New milestone of unified mode
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
- How to choose cross-border e-commerce multi merchant system
- Web security - CSRF (token)
- What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
猜你喜欢
UiPath实战(08) - 选取器(Selector)
MC Layer Target
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Triangular rasterization
Joint set search: merge intervals and ask whether two numbers are in the same set
Basic use of continuous integration server Jenkins
[fxcg] market analysis today
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
Library management system based on SSM
随机推荐
[dynamic programming] subsequence problem
RSRS index timing and large and small disc rotation
[nlp] - brief introduction to the latest work of spark neural network
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
Joint set search: merge intervals and ask whether two numbers are in the same set
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
Writing skills of multi plate rotation strategy -- strategy writing learning materials
7. Integrated learning
Library management system based on SSM
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
2.14 summary
PostgreSQL database high availability Patroni source code learning - etcd class
[set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
[set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
Matplotlib -- save graph
Preliminary cognition of C language pointer
金仓数据库KingbaseES 插件kdb_exists_expand
Asp access teaching management system design finished product
关于开学的准备与专业认知