My LeetCode
  • About this Gitbook
  • Cheat Sheet
    • Java
  • Arrays
    • 0001 Two Sum
    • 0004 Median of Two Sorted Arrays
    • 0012 Integer to Roman
    • 0033 Search in Rotated Sorted Array
    • 0034 Find First and Last Position of Element in Sorted Array
    • 0036 Valid Sudoku
    • 0038 Count and Say
    • 0048 Rotate Image
    • 0056 Merge Intervals
    • 0059 Spiral Matrix II
    • 0074 Search a 2D Matrix
    • 0075 Sort Colors
    • 0128 Longest Consecutive Sequence
    • 0136 Single Number
    • 0153 Find Minimum in Rotated Sorted Array
    • 0162 Find Peak Element
    • 0169 Majority Element
    • 0217 Contains Duplicate
    • 0240 Search a 2D Matrix II
    • 0242 Valid Anagram
    • 0334 Increasing Triplet Subsequence
    • 0347 Top K Frequent Elements
    • 0384 Shuffle an Array
    • 0435 Non-overlapping Intervals
    • 0560 Subarray Sum Equals K
    • 0706 Design HashMap
    • 1662 Check If Two String Arrays are Equivalent
    • 1832 Check if the Sentence Is Pangram
    • 2012 Sum of Beauty in the Array
    • 2131 Longest Palindrome by Concatenating Two Letter Words
  • String
    • 0043 Multiply Strings
    • 0049 Group Anagrams
    • 0187 Repeated DNA Sequences
    • 0290 Word Pattern
    • 0409 Longest Palindrome
    • 0763 Partition Labels
    • 0415 Add Strings
  • Tree
    • 0094 Binary Tree Inorder Traversal
    • 0098 Validate Binary Search Tree
    • 0100 Same Tree
    • 0103 Binary Tree Zigzag Level Order Traversal
    • 0104 Maximum Depth of Binary Tree
    • 0105 Construct Binary Tree from Preorder and Inorder Traversal
    • 0108 Convert Sorted Array to Binary Search Tree
    • 0110 Balanced Binary Tree
    • 0113 Path Sum II
    • 0117 Populating Next Right Pointers in Each Node II
    • 0144 Binary Tree Preorder Traversal
    • 0145 Binary Tree Postorder Traversal
    • 0173 Binary Search Tree Iterator
    • 0199 Binary Tree Right Side View
    • 0226 Invert Binary Tree
    • 0230 Kth Smallest Element in a BST
    • 0235 Lowest Common Ancestor of a Binary Search Tree
    • 0236 Lowest Common Ancestor of a Binary Tree
    • 0297 Serialize and Deserialize Binary Tree
    • 0429 N-ary Tree Level Order Traversal
    • 0450 Delete Node in a BST
    • 0543 Diameter of Binary Tree
    • 0572 Subtree of Another Tree
    • 0589 N-ary Tree Preorder Traversal
    • 0590 N-ary Tree Postorder Traversal
    • 1448 Count Good Nodes in Binary Tree
    • 2415 Reverse Odd Levels of Binary Tree
  • List
    • 0002 Add Two Numbers
    • 0019 Remove Nth Node From End of List
    • 0021 Merge Two Sorted Lists
    • 0023. Merge k Sorted Lists
    • 0024 Swap Nodes in Pairs
    • 0025 Reverse Nodes in k-Group
    • 0078 Subsets
    • 0082 Remove Duplicates from Sorted List II
    • 0083 Remove Duplicates from Sorted List
    • 0090 Subsets II
    • 0119 Pascal's Triangle II
    • 0138 Copy List with Random Pointer
    • 0141 Linked List Cycle
    • 0142 Linked List Cycle II
    • 0143 Reorder List
    • 0146 LRU Cache
    • 0160 Intersection of Two Linked Lists
    • 0206 Reverse Linked List
    • 0237 Delete Node in a Linked List
    • 0287 Find the Duplicate Number
    • 0430 Flatten a Multilevel Doubly Linked List
    • 0707 Design Linked List
    • 2095 Delete the Middle Node of a Linked List
  • Map
    • 0013 Roman to Integer
  • Dynamic Programming
    • 0005 Longest Palindromic Substring
    • 0045 Jump Game II
    • 0055 Jump Game
    • 0062 Unique Paths
    • 0070 Climbing Stairs
    • 0072 Edit Distance
    • 0091 Decode Ways
    • 0139 Word Break
    • 0152 Maximum Product Subarray
    • 0198 House Robber
    • 0213 House Robber II
    • 0300 Longest Increasing Subsequence
    • 0322 Coin Change
    • 0343 Integer Break
    • 0413 Arithmetic Slices
    • 0416 Partition Equal Subset Sum
    • 0583 Delete Operation for Two Strings
    • 0647 Palindromic Substrings
    • 0673 Number of Longest Increasing Subsequence
    • 0746 Min Cost Climbing Stairs
    • 1143 Longest Common Subsequence
    • 1335 Minimum Difficulty of a Job Schedule
    • 1567 Maximum Length of Subarray With Positive Product
    • 1531 String Compression II
  • Graph
    • 0127 Word Ladder
    • 0130 Surrounded Regions
    • 0200 Number of Islands
    • 0365 Water and Jug Problem
    • 0417 Pacific Atlantic Water Flow
    • 0433 Minimum Genetic Mutation
    • 0542 01 Matrix
    • 0547 Number of Provinces
    • 0695 Max Area of Island
    • 0733 Flood Fill
    • 0752 Open the Lock
    • 0785 Is Graph Bipartite?
    • 0797 All Paths From Source to Target
    • 0802 Find Eventual Safe States
    • 0841 Keys and Rooms
    • 0847 Shortest Path Visiting All Nodes
    • 0886 Possible Bipartition
    • 0934 Shortest Bridge
    • 0997 Find the Town Judge
    • 1020 Number of Enclaves
    • 1091 Shortest Path in Binary Matrix
    • 1129 Shortest Path with Alternating Colors
    • 1162 As Far from Land as Possible
    • 1254 Number of Closed Islands
    • 1306 Jump Game III
    • 1319 Number of Operations to Make Network Connected
    • 1376 Time Needed to Inform All Employees
    • 1466 Reorder Routes to Make All Paths Lead to the City Zero
    • 1557 Minimum Number of Vertices to Reach All Nodes
    • 1615 Maximal Network Rank
    • 1654 Minimum Jumps to Reach Home
    • 1905 Count Sub Islands
    • 1926 Nearest Exit from Entrance in Maze
  • Two Pointers
    • 0011 Container With Most Water
    • 0015 3Sum
    • 0042 Trapping Rain Water
    • 0121 Best Time to Buy and Sell Stock
    • 0125 Valid Palindrome
    • 0167 Two Sum II - Input Array Is Sorted
    • 0345 Reverse Vowels of a String
    • 0844 Backspace String Compare
    • 0986 Interval List Intersections
  • Heap/PriorityQueue
    • 0215 Kth Largest Element in an Array
    • 0347 Top K Frequent Elements
    • 0451 Sort Characters By Frequency
    • 0692 Top K Frequent Words
    • 0703 Kth Largest Element in a Stream
    • 0973 K Closest Points to Origin
    • 1046 Last Stone Weight
    • 2284 Sender With Largest Word Count
  • Stack
    • 0020 Valid Parentheses
    • 0022 Generate Parentheses
    • 0071 Simplify Path
    • 0084 Largest Rectangle in Histogram
    • 0150 Evaluate Reverse Polish Notation
    • 0155 Min Stack
    • 0739 Daily Temperatures
    • 0853 Car Fleet
    • 1249 Minimum Remove to Make Valid Parentheses
    • 1472 Design Browser History
  • Sliding Window
    • 0003 Longest Substring Without Repeating Characters
    • 0076 Minimum Window Substring
    • 0209 Minimum Size Subarray Sum
    • 0239 Sliding Window Maximum
    • 0424 Longest Repeating Character Replacement
    • 0438 Find All Anagrams in a String
    • 0567 Permutation in String
    • 0713 Subarray Product Less Than K
    • 1838 Frequency of the Most Frequent Element
  • Recursion & Backtracking
    • 0017 Letter Combinations of a Phone Number
    • 0022 Generate Parentheses
    • 0039 Combination Sum
    • 0040 Combination Sum II
    • 0047 Permutations II
    • 0079 Word Search
  • Bit Manipulation
    • 0201 Bitwise AND of Numbers Range
  • BackTracking
    • 0017 Letter Combinations of a Phone Number
    • 0051 N-Queens
    • 0079 Word Search
    • 0131 Palindrome Partitioning
  • Others
    • 0149 Max Points on a Line
    • 0202 Happy Number
    • 1823 Find the Winner of the Circular Game
Powered by GitBook
On this page
Edit on GitHub
  1. Graph

0200 Number of Islands

Previous0130 Surrounded RegionsNext0365 Water and Jug Problem

Last updated 2 years ago

My Solution

class Solution {
    private int n, m;
    public int numIslands(char[][] grid) {
        n = grid.length;
        m = grid[0].length;
        
        int count = 0;
        
        if(n == 0){
            return 0;
        }
        
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(grid[i][j] == '1'){
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        
        return count;
    }
    
    
    private void dfs(char[][] grid, int i, int j){
        if(i < 0 
          || j < 0 
          || i >= n
          || j >= m
          || grid[i][j] != '1'
          ){
            return;
        }
        
        
        grid[i][j] = '0';
        
        dfs(grid, i + 1, j);
        dfs(grid, i - 1, j);
        dfs(grid, i, j + 1);
        dfs(grid, i, j - 1);
    } 
}
Question