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

边栏推荐
- After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
- Dive Into Deep Learning——2.1数据操作&&练习
- [set theory] inclusion exclusion principle (including examples of exclusion principle)
- Ffmpeg tanscoding transcoding
- [nlp] - brief introduction to the latest work of spark neural network
- Kingbasees plug-in KDB of Jincang database_ database_ link
- AWS VPC
- [fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
- AWS VPC
- 怎么用Kotlin去提高生产力:Kotlin Tips
猜你喜欢

解决bp中文乱码

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

Number of 1 in binary (simple difficulty)

FuncS sh file not found when using the benchmarksql tool to test kingbases

Design and implementation of JSP logistics center storage information management system

Asp access teaching management system design finished product

vulnhub HA: Natraj

JVM原理简介

Leetcode simple question: check whether two string arrays are equal

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
随机推荐
Preliminary cognition of C language pointer
FuncS sh file not found when using the benchmarksql tool to test kingbases
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
Crazy scientist
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
Mount NFS in kubesphere
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
Dive Into Deep Learning——2.1数据操作&&练习
Internationalization and localization, dark mode and dark mode in compose
Leetcode simple question: the key with the longest key duration
General undergraduate college life pit avoidance Guide
Two points -leetcode-540 A single element in an ordered array
Kubernetes source code analysis (I)
Youdao cloud notes
Why should programmers learn microservice architecture if they want to enter a large factory?
Matplotlib -- save graph
[software testing-6] & Test Management
[dynamic programming] subsequence problem
Kingbasees plug-in KDB of Jincang database_ exists_ expand