当前位置:网站首页>Li Kou daily question - Day 32 -1822 Symbol of array element product
Li Kou daily question - Day 32 -1822 Symbol of array element product
2022-07-01 07:48:00 【Chongyou research Sen】
2022.6.30 Did you brush the questions today ?
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) .
analysis :
Give you an array , Multiply all the elements of the array to get ans, And then determine ans The corresponding value is output in the positive and negative relationship of .
Ideas : You can't multiply the result directly here ( Because the title requires ), So let's change our mind , As long as the element is judged inside =0, Then go straight back 0, If there are negative numbers in the element , Record once , Finally, we only need to judge the number of negative numbers to output the results .
analysis :
1. Violent solution
class Solution {
public:
int arraySign(vector<int>& nums) {
bool sum = true;
for (auto num : nums)
{
if (num == 0)
{
return 0;
}
if (num < 0)
{
sum = !sum;
}
}
return sum ? 1 : -1;
}
};
边栏推荐
- The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
- LeetCode+ 71 - 75
- [target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)
- 【R语言】两个/N个数据合并merge函数
- Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
- [recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
- Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
- [MySQL learning notes 26] view
- Subclasses call methods and properties of the parent class with the same name
- The triode is a great invention
猜你喜欢
I bet on performance and won the CTO of the company. I want to build Devops platform!
[kv260] generate chip temperature curve with xadc
The database is locked. Is there a solution
【mysql学习笔记25】sql语句优化
IMDB practice of emotion classification (simplernn, LSTM, Gru)
2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
She is the "HR of others" | ones character
Redisson uses the full solution - redisson official document + comments (Part 2)
MySQL and redis consistency solution
[MySQL learning notes 25] SQL statement optimization
随机推荐
Oracle create auto increment ID
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
[programming training] delete public characters (hash mapping) + team competition (greedy)
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
[MySQL learning notes 28] storage function
Redisson uses the full solution - redisson official documents + comments (Part 2)
ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
Apple账号密码自动填充
[Shenzhen IO] precise Food Scale (some understanding of assembly language)
base64
Gru of RNN
PWN攻防世界int_overflow
【R语言】两个/N个数据合并merge函数
Huawei modelarts training alexnet model
redisson使用全解——redisson官方文档+注释(上篇)
Caesar
【mysql学习笔记27】存储过程
浏览器本地存储
[MySQL learning notes 26] view