当前位置:网站首页>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
边栏推荐
- What's wrong with SD card data damage? How to recover SD card data damage
- Leetcode simple problem delete an element to strictly increment the array
- 2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
- [Thesis Writing] how to write the overall design of JSP tourism network
- vulnhub HA: Natraj
- FFMpeg filter
- Contents of welder (primary) examination and welder (primary) examination in 2022
- Joint set search: merge intervals and ask whether two numbers are in the same set
- 2.14 summary
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
猜你喜欢
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
[Thesis Writing] how to write the overall design of JSP tourism network
How to choose cross-border e-commerce multi merchant system
Golang -- realize file transfer
2022 P cylinder filling test content and P cylinder filling simulation test questions
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
UiPath实战(08) - 选取器(Selector)
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
Preliminary cognition of C language pointer
会员积分商城系统的功能介绍
随机推荐
Design and implementation of JSP logistics center storage information management system
How to retrieve the password for opening word files
JVM原理简介
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
Factor stock selection scoring model
Employee attendance management system based on SSM
怎么用Kotlin去提高生产力:Kotlin Tips
2022-02-14 (394. String decoding)
逆袭大学生的职业规划
Contents of welder (primary) examination and welder (primary) examination in 2022
一名外包仔的2022年中总结
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
金仓数据库KingbaseES 插件kdb_exists_expand
A outsourcing boy's mid-2022 summary
2022-02-13 (347. Top k high frequency elements)
Reptile exercise 03
[dynamic programming] subsequence problem
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Priv-app permission异常
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)