题目链接:
https://leetcode.cn/problems/sign-of-the-product-of-an-array/description/?envType=study-plan-v2&envId=programming-skills
思路:
设置一个符号位,初始值为1,遍历整个初始数组:
若碰到数组元素为0:直接返回0
数组元素小于0:符号位*-1;
数组元素大于0:符号位不做变化
代码:
class Solution {public int arraySign(int[] nums) {int flag = 1;for(int i = 0;i<nums.length;i++) {if(nums[i]==0) {return 0;}if(nums[i]<0)flag*=-1;}return flag;}
}