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

边栏推荐
- 一名外包仔的2022年中总结
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
- Writing skills of multi plate rotation strategy -- strategy writing learning materials
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- 金仓数据库KingbaseES 插件kdb_exists_expand
- Smart contract security audit company selection analysis and audit report resources download - domestic article
- Asp access teaching management system design finished product
- C language series - Section 3 - functions
- Web security - CSRF (token)
- 带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
猜你喜欢

2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

2022 P cylinder filling test content and P cylinder filling simulation test questions

2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank

Contents of welder (primary) examination and welder (primary) examination in 2022

GFS distributed file system (it's nice to meet it alone)

跨境电商多商户系统怎么选

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

2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units

Redis persistence principle
随机推荐
Preliminary cognition of C language pointer
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
2022-02-14 (394. String decoding)
Crazy scientist
Learning practice: comprehensive application of cycle and branch structure (I)
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
怎么用Kotlin去提高生产力:Kotlin Tips
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Library management system based on SSM
Employee attendance management system based on SSM
Web - Information Collection
Two drawing interfaces - 1 Matlab style interface
BMZCTF simple_ pop
Function introduction of member points mall system
X-ray normal based contour rendering
金仓KFS数据双向同步场景部署
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
SSM based campus part-time platform for College Students
Joint set search: merge intervals and ask whether two numbers are in the same set
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)