LeetCode_Study_Plan/Programming Skills

1822. Sign of the Product of an Array java

개발하는루루 2022. 9. 19. 22:10

https://leetcode.com/problems/sign-of-the-product-of-an-array/?envType=study-plan&id=programming-skills-i 

 

Sign of the Product of an Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
    public int arraySign(int[] nums) {
        int cnt = 0;
        for (int i = 0 ; i < nums.length ; i++) {
          if (nums[i] == 0){
            return 0;
          }
          if (nums[i] < 0) {
            cnt +=1;
          }
        }

        if (cnt % 2 == 0) {
          return 1;
        } else {
          return -1;
        }
    }
}