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

边栏推荐
- C primre plus Chapter 10 question 6 inverted array
- GFS distributed file system (it's nice to meet it alone)
- [pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
- Kubernetes source code analysis (I)
- Redis persistence principle
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- Introduction of pointer variables in function parameters
- Design and implementation of JSP logistics center storage information management system
- 2022-02-14 (394. String decoding)
- 逆袭大学生的职业规划
猜你喜欢
![[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN

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

540. Single element in ordered array

FFMpeg filter

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

Introduction of pointer variables in function parameters

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

Human resource management system based on JSP

SSM based campus part-time platform for College Students

UiPath实战(08) - 选取器(Selector)
随机推荐
7. Integrated learning
一名外包仔的2022年中总结
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
金仓数据库KingbaseES 插件kdb_date_function
普通本科大学生活避坑指南
[BMZCTF-pwn] 18-RCTF-2017-Recho
4 years of experience to interview test development, 10 minutes to end, ask too
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
Hj35 serpentine matrix
Kingbasees plug-in KDB of Jincang database_ date_ function
Internationalization and localization, dark mode and dark mode in compose
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
P35-P41 fourth_ context
What's wrong with SD card data damage? How to recover SD card data damage
Youdao cloud notes
PostgreSQL database high availability Patroni source code learning - etcd class
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
RSRS index timing and large and small disc rotation