Skip to content

Instantly share code, notes, and snippets.

@joxer
Created January 18, 2019 20:22
Show Gist options
  • Save joxer/3fe29b9317c4655a463bfd5440d94692 to your computer and use it in GitHub Desktop.
Save joxer/3fe29b9317c4655a463bfd5440d94692 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
class Solution {
static int getNumberOfIslands(int[][] binaryMatrix) {
int island = 0;
for(int i = 0; i < binaryMatrix.length; i++){
for(int j = 0; j < binaryMatrix[i].length; j++){
if(binaryMatrix[i][j] == 1){
island += 1;
recursiveCheck(binaryMatrix,i,j);
}
}
}
return island;
}
static void recursiveCheck(int[][] binaryMatrix, int i, int j){
binaryMatrix[i][j] = 2;
if(binaryMatrix.length > i+1 && binaryMatrix[i+1][j] == 1){
recursiveCheck(binaryMatrix, i+1, j);
}
if( i-1 > -1 && binaryMatrix[i-1][j] == 1){
recursiveCheck(binaryMatrix, i-1, j);
}
if(binaryMatrix[0].length > j+1 && binaryMatrix[i][j+1] == 1){
recursiveCheck(binaryMatrix, i, j+1);
}
if(j-1 > -1 && binaryMatrix[i][j-1] == 1){
recursiveCheck(binaryMatrix, i, j-1);
}
}
public static void main(String[] args) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment