File: root - text - article - 2019 - 12 - find-the-number-of-islands.txt.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 451 Views, 19780 Search Bots | 125 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 451 Views, 19780 Search Bots | 125 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by LinkedIn:
Given a 2-dimensional grid consisting of 1's (land blocks) and 0's (water blocks), count the number of islands present in the grid. The definition of an island is as follows:
1.) Must be surrounded by water blocks.
2.) Consists of land blocks (1's) connected to adjacent land blocks (either vertically or horizontally).
Assume all edges outside of the grid are water.
Example:
Input:
Output: 3
Here's your starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 451 Views, 19780 Search Bots | 125 Words Given a 2-dimensional grid consisting of 1's (land blocks) and 0's (water blocks), count the number of islands present in the grid. The definition of an island is as follows:
1.) Must be surrounded by water blocks.
2.) Consists of land blocks (1's) connected to adjacent land blocks (either vertically or horizontally).
Assume all edges outside of the grid are water.
Example:
Input:
10001
11000
10110
00000
Output: 3
Here's your starting point:
class Solution(object):
def inRange(self, grid, r, c):
numRow, numCol = len(grid), len(grid[0])
if r < 0 or c < 0 or r >= numRow or c >= numCol:
return False
return True
def numIslands(self, grid):
# Fill this in.
grid = [[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[1, 0, 0, 1, 1],
[0, 0, 0, 0, 0]]
print(Solution().numIslands(grid))
# 3
Related Articles
- Daily Interview Problem: Maximum Profit From Stocks
- Print a tree level-by-level, with line-breaks
- A trick for getting good at coding interviews FASTER
- [Daily Problem] Course Prerequisites
- Daily Interview Problem: Distribute Bonuses
- Plus One
- Daily Interview Problem: Sort Colors
- Daily Interview Problem: Largest BST in a Binary Tree
- Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
- Daily Interview Problem: Count Number of Unival Subtrees
©2006~2024 SteakOverCooked - 0.00722 Seconds(s) - 7570.761 KB/s - 44 Online Memory: 516.59 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !