LeetCode1822. Sign of the Product of an Array

文章目录

一、题目

There is a function signFunc(x) that returns:

1 if x is positive.
-1 if x is negative.
0 if x is equal to 0.
You are given an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

Example 1:

Input: nums = [-1,-2,-3,-4,3,2,1]
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1
Example 2:

Input: nums = [1,5,0,2,-3]
Output: 0
Explanation: The product of all values in the array is 0, and signFunc(0) = 0
Example 3:

Input: nums = [-1,1,-1,1,-1]
Output: -1
Explanation: The product of all values in the array is -1, and signFunc(-1) = -1

Constraints:

1 <= nums.length <= 1000
-100 <= nums[i] <= 100

二、题解

class Solution {
   
public:
    int arraySign(vector<int>& nums) {
   
        int flag = 1;
        for(auto x:nums){
   
            if(x == 0){
   
                flag = 0;
                break;
            }
            else if(x < 0) flag *= -1;
        }
        return flag;
    }
};

相关推荐

  1. leetcode题目122

    2023-12-30 02:42:01       8 阅读
  2. LeetCode1822. Sign of the Product of an Array

    2023-12-30 02:42:01       30 阅读
  3. Leetcode14-判断句子是否为全字母句(1832

    2023-12-30 02:42:01       46 阅读
  4. LeetCode每日一题】1122. 数组的相对排序

    2023-12-30 02:42:01       31 阅读
  5. leetcode 122双周赛 解题思路+代码

    2023-12-30 02:42:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 02:42:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 02:42:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 02:42:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 02:42:01       18 阅读

热门阅读

  1. P1308 [NOIP2011 普及组] 统计单词数----有意思

    2023-12-30 02:42:01       29 阅读
  2. YCSB 测试表预分区

    2023-12-30 02:42:01       36 阅读
  3. Netty学习

    2023-12-30 02:42:01       41 阅读
  4. 聊一聊Spring Bean 的生命周期

    2023-12-30 02:42:01       35 阅读
  5. 【PHP】API传参时,判断数字最多两位小数

    2023-12-30 02:42:01       36 阅读
  6. 服务简介及问题答疑

    2023-12-30 02:42:01       31 阅读