To the Top
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 | 137 Views, 15204 Search Bots | 125 Words

Subscribe to Feed Burner | 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:

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
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 137 Views, 15204 Search Bots | 125 Words Subscribe to Feed Burner

Related Articles

  1. Daily Interview Problem: Running Median
  2. Daily Interview Problem: Reverse Integer
  3. Daily Interview Problem: Count Number of Unival Subtrees
  4. Algorithm Interview: No Adjacent Repeating Characters
  5. Algorithm Interview: Subarray With Target Sum
  6. Two-Sum
  7. Daily Interview Question: Longest Sequence with Two Unique Numbers
  8. Daily Interview Problem: Largest Product of 3 Elements I
  9. [Daily Problem] Move Zeros
  10. Daily Interview Problem: Circle of Chained Words

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />