본문 바로가기
알고리즘/leetcode

1822. Sign of the Product of an Array

by 1.5볼트 2023. 5. 2.
728x90

 

리스트의 모든 원소를 곱했을때 -1 0 1 중에 어떤게 나올까

0이 존재한다면 0 

음수가 홀수개 이면 -1 

아니면 1 리턴

class Solution:
    def arraySign(self, nums: List[int]) -> int:
        m=0
        for i in nums:
            if i==0:
                return 0
            elif i<0:
                m+=1
        if m%2==1:
            return -1
        return 1

'알고리즘 > leetcode' 카테고리의 다른 글

1572. Matrix Diagonal Sum - python  (0) 2023.05.08
2215. Find the Difference of Two Arrays  (0) 2023.05.03
183. Customers Who Never Order  (0) 2023.04.30
178. Rank Scores  (0) 2023.04.29
184. Department Highest Salary  (0) 2023.04.28

댓글