코딩 테스트/leetcode

Count Negative Numbers in a Sorted Matrix

ornni 2024. 6. 20. 10:00
728x90
반응형

첫번째 코드

 

단순하게 하나하나 돌아가면서 음수인 경우의 개수를 센다.

이때 list안에 list가 있는 형태이므로 이에 주의하여 인덱싱한다.

 

class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:
        answer = 0
        for i in range(len(grid)):
            for j in grid[i]:
                if j < 0:
                    answer += 1
        return answer

 

통과!


링크

https://github.com/ornni/leetcode/tree/main/1351-count-negative-numbers-in-a-sorted-matrix

 

leetcode/1351-count-negative-numbers-in-a-sorted-matrix at main · ornni/leetcode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub v3](https://github.com/raphaelheinz/LeetHub-3.0) - ornni/leetcode

github.com

 

반응형