Created
January 18, 2019 20:22
-
-
Save joxer/3fe29b9317c4655a463bfd5440d94692 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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