当前位置:网站首页>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

边栏推荐
- [PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
- When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
- 消息队列(MQ)介绍
- [Thesis Writing] how to write the overall design of JSP tourism network
- The simple problem of leetcode: dismantling bombs
- Reptile exercise 02
- vulnhub HA: Natraj
- Arthas watch grabs a field / attribute of the input parameter
- Mount NFS in kubesphere
- JS multidimensional array to one-dimensional array
猜你喜欢
![[Thesis Writing] how to write the overall design of JSP tourism network](/img/02/841e8870c2ef871c182b9bb8252a83.jpg)
[Thesis Writing] how to write the overall design of JSP tourism network

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test

Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found

Redis persistence principle

7. Integrated learning
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

stm32逆向入门

I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional

Php+mysql registration landing page development complete code
随机推荐
GFS distributed file system (it's nice to meet it alone)
[dynamic programming] subsequence problem
Leetcode simple problem delete an element to strictly increment the array
Function introduction of member points mall system
2022-02-12 (338. Bit count)
Leetcode simple question: check whether the string is an array prefix
因子选股-打分模型
Library management system based on SSM
UiPath实战(08) - 选取器(Selector)
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
Truncated sentences of leetcode simple questions
220214c language learning diary
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
Php+mysql registration landing page development complete code
Hj35 serpentine matrix
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Human resource management system based on JSP
data2vec! New milestone of unified mode