Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- python priority queue
- 릿코드
- leetcode풀이
- 파이썬 알고리즘 풀기
- 릿코드 풀기
- 알고리즘풀기
- 파이썬릿코드
- leetcode풀기
- binary search
- python 릿코드
- 파이썬 릿코드
- 알고리즘풀이
- 잇츠디모
- 파이썬 프로그래머스
- 파이썬알고리즘풀기
- 코틀린기초
- 릿코드풀이
- 파이썬릿코드풀기
- leetcode 풀기
- python Leetcode
- 파이썬 알고리즘
- LeetCode
- 릿코드 파이썬
- python 알고리즘
- 릿코드풀기
- python xor
- 상가수익률계산기
- python zip_longest
- 파이썬알고리즘
- python sorted
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 200. Number of Islands 본문
반응형
200. Number of Islands
https://leetcode.com/problems/number-of-islands/
Number of Islands - 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
문제)
솔루션1) DFS
class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
m,n = len(grid), len(grid[0])
visited = [[0]*n for i in range(m)]
island_count = 0
def recur(row, col):
# boundary check
if not (0 <= row < m): return
if not (0 <= col < n): return
if grid[row][col] == '0' or visited[row][col] == 1:
return
visited[row][col] = 1
recur(row-1, col)
recur(row+1, col)
recur(row, col-1)
recur(row, col+1)
for row in range(m):
for col in range(n):
if grid[row][col] == '1' and visited[row][col] == 0:
island_count += 1
recur(row, col)
return island_count
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
LeetCode 풀기 - 121. Best Time to Buy and Sell Stock (0) | 2022.02.25 |
---|---|
LeetCode 풀기 - 215. Kth Largest Element in an Array (0) | 2022.02.24 |
LeetCode 풀기 - 692. Top K Frequent Words (0) | 2022.02.23 |
LeetCode 풀기 - 171. Excel Sheet Column Number (0) | 2022.02.22 |
LeetCode 풀기 - 169. Majority Element (0) | 2022.02.22 |
Comments