当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- AUTOSAR learning record (1) – ECUM_ Init
- Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
- 【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想
- [microservice openfeign] feign's log record
- [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
- Download xshell and xftp
- 力扣——求一组字符中的第一个回文字符
- ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
- Mysql与Redis一致性解决方案
猜你喜欢

ctfshow-web352,353(SSRF)
![[untitled]](/img/c2/63286ba00321c9cdef43ff40635a67.png)
[untitled]

2022 electrician (intermediate) recurrent training question bank and answers

电脑有网络,但所有浏览器网页都打不开,是怎么回事?

Software testing methods and techniques - overview of basic knowledge

【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想

Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)

Huawei modelarts training alexnet model

软件测试方法和技术 - 基础知识概括

2022 test question bank and simulation test of tea master (primary) operation certificate
随机推荐
Long way to go with technology
ctfshow-web351(SSRF)
kubernetes资源对象介绍及常用命令(二)
[MySQL learning notes 26] view
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
Eigen matrix operation Library
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
The triode is a great invention
PWN攻防世界int_overflow
Eigen矩阵运算库快速上手
关系数据库如何工作
组件的自定义事件②
【mysql学习笔记26】视图
Illusory and simple screen raindrop post-processing effect
【深圳IO】精确食品称(汇编语言的一些理解)
[skill] create Bat quick open web page
Detailed explanation of weback5 basic configuration
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
atguigu----脚手架--02-使用脚手架(2)
base64