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

边栏推荐
- Why should programmers learn microservice architecture if they want to enter a large factory?
- Kubernetes source code analysis (I)
- Truncated sentences of leetcode simple questions
- arthas watch 抓取入参的某个字段/属性
- A outsourcing boy's mid-2022 summary
- What functions need to be set after the mall system is built
- 4 years of experience to interview test development, 10 minutes to end, ask too
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- 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
- Writing skills of multi plate rotation strategy -- strategy writing learning materials
猜你喜欢

2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical

Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended

Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended

How to retrieve the password for opening word files

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭

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

Jincang KFS data bidirectional synchronization scenario deployment

2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination

C language series - Section 3 - functions

FISCO bcos zero knowledge proof Fiat Shamir instance source code
随机推荐
消息队列(MQ)介绍
General undergraduate college life pit avoidance Guide
Truncated sentences of leetcode simple questions
Hj35 serpentine matrix
因子选股-打分模型
C Primer Plus Chapter 10, question 14 3 × 5 array
GFS分布式文件系统(光是遇见已经很美好了)
Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
MongoDB 慢查询语句优化分析策略
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
Dive Into Deep Learning——2.1数据操作&&练习
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
有道云笔记
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
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
2022-02-14 (394. String decoding)
逆袭大学生的职业规划
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Php+mysql registration landing page development complete code
7. Integrated learning