云服务器网站解析,凡科小程序建站官网,建设糖果网站的好处有哪些,有人有片资源网文章目录1. 题目2. 解题1. 题目
已知函数 signFunc(x) 将会根据 x 的正负返回特定值#xff1a;
如果 x 是正数#xff0c;返回 1 。如果 x 是负数#xff0c;返回 -1 。如果 x 是等于 0 #xff0c;返回 0 。
给你一个整数数组 nums 。 令 product 为数组 nums 中所有元…
文章目录1. 题目2. 解题1. 题目
已知函数 signFunc(x) 将会根据 x 的正负返回特定值
如果 x 是正数返回 1 。如果 x 是负数返回 -1 。如果 x 是等于 0 返回 0 。
给你一个整数数组 nums 。 令 product 为数组 nums 中所有元素值的乘积。
返回 signFunc(product) 。
示例 1
输入nums [-1,-2,-3,-4,3,2,1]
输出1
解释数组中所有值的乘积是 144 且 signFunc(144) 1示例 2
输入nums [1,5,0,2,-3]
输出0
解释数组中所有值的乘积是 0 且 signFunc(0) 0示例 3
输入nums [-1,1,-1,1,-1]
输出-1
解释数组中所有值的乘积是 -1 且 signFunc(-1) -1提示
1 nums.length 1000
-100 nums[i] 100来源力扣LeetCode 链接https://leetcode-cn.com/problems/sign-of-the-product-of-an-array 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
记录负数的个数
class Solution {
public:int arraySign(vectorint nums) {int negative 0;for(auto n : nums){if(n 0)return 0;if(n 0)negative;}if(negative1) return -1;return 1;}
};4 ms 9.9 MB C 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步