当前位置:网站首页>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
- JVM原理简介
- Dive Into Deep Learning——2.1数据操作&&练习
- [set theory] binary relation (example of binary relation on a | binary relation on a)
- P35-P41 fourth_ context
- Youdao cloud notes
- [fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
- 2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
- MC Layer Target
- [set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
猜你喜欢

Introduction of pointer variables in function parameters

带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...

Leetcode simple question: check whether the array is sorted and rotated

Jincang KFS data bidirectional synchronization scenario deployment

Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected

Employee attendance management system based on SSM

540. Single element in ordered array

Design and implementation of JSP logistics center storage information management system

Pyqt control part (II)

MC Layer Target
随机推荐
关于开学的准备与专业认知
Square root of X
What's wrong with SD card data damage? How to recover SD card data damage
Php+mysql registration landing page development complete code
UiPath实战(08) - 选取器(Selector)
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Mount NFS in kubesphere
Ffmpeg mix
Triangular rasterization
4 years of experience to interview test development, 10 minutes to end, ask too
vulnhub HA: Natraj
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
Some information about the developer environment in Chengdu
Prefix and (continuously updated)
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Truncated sentences of leetcode simple questions
[free completion] development of course guidance platform (source code +lunwen)
How do you use lodash linking function- How do you chain functions using lodash?
Two drawing interfaces - 1 Matlab style interface