当前位置:网站首页>力扣每日一题-第32天-1822.数组元素积的符号
力扣每日一题-第32天-1822.数组元素积的符号
2022-07-01 07:46:00 【重邮研究森】
2022.6.30今天你刷题了吗?
题目:
已知函数 signFunc(x) 将会根据 x 的正负返回特定值:
如果 x 是正数,返回 1 。
如果 x 是负数,返回 -1 。
如果 x 是等于 0 ,返回 0 。
给你一个整数数组 nums 。令 product 为数组 nums 中所有元素值的乘积。
返回 signFunc(product) 。
分析:
给你一个数组,把数组所以元素乘起来得到ans,然后判断ans的正负关系输出相对应的值。
思路:这里不能直接把结果乘出来(因为题目要求),所以我们换个思路,只要里面判断到元素=0,那么直接返回0,如果元素中有负数,则记录一次,最后只需要判断负数的次数关系来进行结果输出。
解析:
1.暴力求解
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;
}
};
边栏推荐
- Browser local storage
- Discussion on several research hotspots of cvpr2022
- 如何让两融交易更极速
- Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
- Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
- The database is locked. Is there a solution
- Thesis learning -- Analysis and Research on similarity query of hydrological time series
- Huawei modelarts training alexnet model
- atguigu----脚手架--02-使用脚手架(2)
- redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
猜你喜欢
随机推荐
Custom events of components ①
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
2022 mobile crane driver test exercises and online simulation test
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
Which securities company is better or safer for mobile phone account opening
Eigen matrix operation Library
2022危险化学品经营单位主要负责人试题及模拟考试
[kv260] generate chip temperature curve with xadc
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
Oracle create auto increment ID
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
她就是那个「别人家的HR」|ONES 人物
继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
Kdtree notes
How relational databases work
Basic knowledge of MATLAB
熱烈祝賀五行和合酒成功掛牌
ctfshow-web352,353(SSRF)
[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