当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
猜你喜欢

Eigen matrix operation Library

ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?

Discussion on several research hotspots of cvpr2022

Software testing methods and techniques - overview of basic knowledge

组件的自定义事件①

How relational databases work

MATLAB之基础知识

1286_ Implementation analysis of task priority setting in FreeRTOS

关系数据库如何工作

浅谈CVPR2022的几个研究热点
随机推荐
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
长路漫漫、技术作伴
Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
C # read and write customized config file
The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
The computer has a network, but all browser pages can't be opened. What's the matter?
Software testing methods and techniques - overview of basic knowledge
【编程强训2】排序子序列+倒置字符串
熱烈祝賀五行和合酒成功掛牌
三极管是一项伟大的发明
Reply and explanation on issues related to "online training of network security education in 2022"
2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
Eigen矩阵运算库快速上手
Huawei modelarts training alexnet model
热烈祝贺五行和合酒成功挂牌
Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
[MySQL learning notes 26] view
C# Newtonsoft. Use of job in JSON
2022 mobile crane driver test exercises and online simulation test
软件测试方法和技术 - 基础知识概括