当前位置:网站首页>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;
}
};边栏推荐
- Long way to go with technology
- Custom events of components ②
- 2022年流动式起重机司机考试练习题及在线模拟考试
- [R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
- Warm congratulations on the successful listing of five elements hehe liquor
- Eigen矩阵运算库快速上手
- Sorting out tcp/udp communication problems
- MATLAB之基础知识
- [programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
- 浏览器本地存储
猜你喜欢

Vhost kick & call principle

【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)

微软宣布开源 (GODEL) 语言模型聊天机器人

2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform

2022 test questions and mock examinations for main principals of hazardous chemicals business units

Software testing methods and techniques - overview of basic knowledge

Custom events of components ②

2022危险化学品经营单位主要负责人试题及模拟考试

Cyclic neural network

【无标题】
随机推荐
Vscode automatically formats code according to eslint specification
浏览器本地存储
下载Xshell和Xftp
Redisson uses the full solution - redisson official documents + comments (Part 2)
What information does the supplier need to know about Audi EDI project?
【mysql学习笔记28】存储函数
Vhost kick & call principle
ctfshow-web351(SSRF)
2022电工(中级)复训题库及答案
力扣——求一组字符中的第一个回文字符
How relational databases work
[MySQL learning notes27] stored procedure
如何让两融交易更极速
软件测试方法和技术 - 基础知识概括
AUTOSAR learning record (1) – ECUM_ Init
华为ModelArts训练Alexnet模型
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?