【LeetCode】1822. 数组元素积的符号

时间:2021-04-13 12:40:39   收藏:0   阅读:0

思路:遍历判断每个数的正负

class Solution {
public:
    int arraySign(vector<int>& nums) {
        int ans = 1;
        for(auto &n : nums){
            if(n == 0) return 0;
            ans *= (n > 0) ? 1 : -1;
        }
        return ans;
    }
};
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!