[LeetCode 200 Number of Islands] [python] 2. 파이썬 문법에 익숙해지고 있다.
Algorithm/LeetCode 문제 풀이 2019. 3. 30. 21:12https://leetcode.com/problems/number-of-islands/
733 Flood Fill 과 비슷한 재귀 함수 문제..
약간만 더 생각하면 되는 문제였다. ㅎㅎ
오랜만에 파이썬을 했더니 프린트 하는 문법이 좀 헷깔렸다.
디버깅을 하기 위해 파이썬 2.x에서 프린트를 하려면 아래와 같이 해주면 되었다
괄호도 필요없고, 출력할 값은 %뒤에 괄호로 묶어서 적어주면 되었다.
print "print "grid[%d][%d] = %s" % (i, j, grid[i][j])
내가 쓴 답은 허접하지만 공유해 보겠다. ㅎㅎ
class Solution(object):
def checkIslands(self, grid, r, c):
if self.g[r][c] == '0':
return 0
self.g[r][c] = '0'
if r > 0: self.checkIslands(grid, r-1, c)
if r < self.r_len - 1: self.checkIslands(grid, r+1, c)
if c > 0: self.checkIslands(grid, r, c - 1)
if c < self.c_len - 1: self.checkIslands(grid, r, c+1)
return 1
def numIslands(self, grid):
self.r_len = len(grid)
if(self.r_len > 0):
self.c_len = len(grid[0])
else:
return 0
cnt = 0
self.g = grid
for i in range(self.r_len):
for j in range(self.c_len):
if self.checkIslands(grid, i, j) == 1:
cnt+=1
return cnt
"""
:type grid: List[List[str]]
:rtype: int
"""
사내 역량평가 준비를 위해 기초 문법 먼저 공부하기로 했다.
1. 232 : implement Queue using Stacks
2. 283 : Move Zeroes
3. 290 : word pattern
4. 485 : Max Consecutive Ones
5. 492 : Construct the Rectangle
BST
6. 501. Find Mode in Binary Search Tree
Minimum Absolute Difference in BST
543. Diameter of Binary Tree
563. Binary Tree Tilt
linked list
203. Remove Linked List Elements
206. Reverse Linked List
conversion
504. Base 7
string
557. Reverse Words in a String III
189. Rotate Array
599. Minimum Index Sum of Two Lists
matrix
566. Reshape the Matrix
783. Minimum Distance Between BST Nodes
Hash
705. Design HashSet
그 다음에 graph로 넘어가기
'Algorithm > LeetCode 문제 풀이' 카테고리의 다른 글
[LeetCode 485] python - 예외 처리에 주의하기.. (0) | 2019.04.02 |
---|---|
[LeetCode 283] python 리스트(큐/스택) 이용하기 (0) | 2019.04.02 |
[LeetCode 232, 283, 290 - 완료, 485, 492] python (0) | 2019.03.31 |
[LeetCode 542. 01 Matrix] [python] 4. 재귀 함수 (0) | 2019.03.31 |
[LeetCode 232. Implement Queue using Stacks] [python] 3. 스택을 이용하여 큐를 구현하기 (0) | 2019.03.30 |