# LeetCode 695 Max Area of Island

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

Note: The length of each dimension in the given grid does not exceed 50.

class Solution {            public int maxAreaOfIsland(int[][] grid) {        int m = grid.length;        int n = grid[0].length;           int maxArea = 0;        //HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();                ArrayList<Point> visitedPoint = new ArrayList<Point>();                for(int i=0; i<m; i++) {            for(int j=0; j<n; j++) {                int currentArea = calculateArea(grid, i, j, visitedPoint);                if(maxArea<currentArea) {                    maxArea = currentArea;                }            }        }                return maxArea;    }            //ArrayList<Point> visitedPoint = new ArrayList<Point>();        public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {           int m = grid.length;           int n = grid[0].length;                if( !((0<=i && i<m) && (0<= j && j<m)) ) {            return 0;        }                if(grid[i][j] == 0) {                        return 0;         }                int area = 0;        Point currentPoint = new Point(i,j);        if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {            area++;            visitedPoint.add(currentPoint);                        area += calculateArea( grid, i,  j+1, visitedPoint);            area += calculateArea( grid, i,  j-1, visitedPoint);            area += calculateArea( grid, i+1,  j, visitedPoint);            area += calculateArea( grid, i-1, j, visitedPoint);                        return area;        } else {            return area;        }                //return 0;    }}        class Point {        public  int x;                int y;                public Point(int x, int y) {            this.x = x;            this.y = y;        }    }

结果：

Your answer

at java.util.ArrayList.contains(ArrayList.java:257)
at Solution.calculateArea(Solution.java:44)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)
at Solution.calculateArea(Solution.java:48)
at Solution.calculateArea(Solution.java:49)

o.equals(e)

class Solution {

public int maxAreaOfIsland(int[][] grid) {
int m = grid.length;
int n = grid[0].length;

int maxArea = 0;
//HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();

ArrayList<Point> visitedPoint = new ArrayList<Point>();

for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
int currentArea = calculateArea(grid, i, j, visitedPoint);
if(maxArea<currentArea) {
maxArea = currentArea;
}
}
}

return maxArea;
}

//ArrayList<Point> visitedPoint = new ArrayList<Point>();

public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {
int m = grid.length;
int n = grid[0].length;

if( !((0<=i && i<m) && (0<= j && j<m)) ) {
return 0;
}

if(grid[i][j] == 0) {

return 0;
}

int area = 0;
Point currentPoint = new Point(i,j);
if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {
area++;

area += calculateArea( grid, i,  j+1, visitedPoint);
area += calculateArea( grid, i,  j-1, visitedPoint);
area += calculateArea( grid, i+1,  j, visitedPoint);
area += calculateArea( grid, i-1, j, visitedPoint);

return area;
} else {
return area;
}

//return 0;
}
}

class Point {
public  int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Point point) {
if(this.x == point.x && this.y == point.y) {
return true;
} else {
return false;
}
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
}

class Solution {

public int maxAreaOfIsland(int[][] grid) {
int m = grid.length;
int n = grid[0].length;

int maxArea = 0;
//HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();

ArrayList<Point> visitedPoint = new ArrayList<Point>();

for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
int currentArea = calculateArea(grid, i, j, visitedPoint);
if(maxArea<currentArea) {
maxArea = currentArea;
}
}
}

return maxArea;
}

//ArrayList<Point> visitedPoint = new ArrayList<Point>();

public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {
int m = grid.length;
int n = grid[0].length;

if( !((0<=i && i<m) && (0<= j && j<m)) ) {
return 0;
}

if(grid[i][j] == 0) {

return 0;
}

int area = 0;
Point currentPoint = new Point(i,j);
if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {
area++;

area += calculateArea( grid, i,  j+1, visitedPoint);
area += calculateArea( grid, i,  j-1, visitedPoint);
area += calculateArea( grid, i+1,  j, visitedPoint);
area += calculateArea( grid, i-1, j, visitedPoint);

return area;
} else {
return area;
}

//return 0;
}
}

class Point {
public  int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object object) {
if(object instanceof Point) {
Point point = (Point) object;
return (this.x == point.x && this.y == point.y);
} else {
return super.equals(object);
}
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
}

Submission Result: Runtime Error
Runtime Error Message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Solution.calculateArea(Solution.java:37)
at Solution.calculateArea(Solution.java:48)
at Solution.maxAreaOfIsland(Solution.java:16)
at __DriverSolution__.__helper__(__Driver__.java:8)
at __Driver__.main(__Driver__.java:52)
Last executed input: [[0],[1]]

class Solution {

public int maxAreaOfIsland(int[][] grid) {
int m = grid.length;
int n = grid[0].length;

int maxArea = 0;
//HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();

ArrayList<Point> visitedPoint = new ArrayList<Point>();

for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
int currentArea = calculateArea(grid, i, j, visitedPoint);
if(maxArea<currentArea) {
maxArea = currentArea;
}
}
}

return maxArea;
}

//ArrayList<Point> visitedPoint = new ArrayList<Point>();

public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {
int m = grid.length;
int n = grid[0].length;

if( !((0<=i && i<m) && (0<= j && j<n)) ) {
return 0;
}

if(grid[i][j] == 0) {

return 0;
}

int area = 0;
Point currentPoint = new Point(i,j);
if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {
area++;

area += calculateArea( grid, i,  j+1, visitedPoint);
area += calculateArea( grid, i,  j-1, visitedPoint);
area += calculateArea( grid, i+1,  j, visitedPoint);
area += calculateArea( grid, i-1, j, visitedPoint);

return area;
} else {
return area;
}

//return 0;
}
}

class Point {
public  int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object object) {
if(object instanceof Point) {
Point point = (Point) object;
return (this.x == point.x && this.y == point.y);
} else {
return super.equals(object);
}
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
}

class Solution {

public int maxAreaOfIsland(int[][] grid) {
int m = grid.length;
int n = grid[0].length;

int maxArea = 0;

int[][] seen = new int[m][n];

for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
int currentArea = calculateArea(grid, i, j, seen);
if(maxArea<currentArea) {
maxArea = currentArea;
}
}
}

return maxArea;
}

//ArrayList<Point> visitedPoint = new ArrayList<Point>();

public int calculateArea(int[][] grid, int i, int j, int[][] seen) {
int m = grid.length;
int n = grid[0].length;

if( !((0<=i && i<m) && (0<= j && j<n)) ) {
return 0;
}

if(grid[i][j] == 0) {
return 0;
}

int area = 0;
//Point currentPoint = new Point(i,j);
if(grid[i][j] == 1 && !(seen[i][j] == 1)) {
area++;
seen[i][j] = 1;

area += calculateArea( grid, i,  j+1, seen);
area += calculateArea( grid, i,  j-1, seen);
area += calculateArea( grid, i+1,  j, seen);
area += calculateArea( grid, i-1, j, seen);

return area;
} else {
return area;
}

//return 0;
}
}

class Solution {

public int maxAreaOfIsland(int[][] grid) {
int m = grid.length;
int n = grid[0].length;

int maxArea = 0;

int[][] seen = new int[m][n];

for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
int currentArea = calculateArea(grid, i, j, seen);
if(maxArea<currentArea) {
maxArea = currentArea;
}
}
}

return maxArea;
}

//ArrayList<Point> visitedPoint = new ArrayList<Point>();

public int calculateArea(int[][] grid, int i, int j, int[][] seen) {
int m = grid.length;
int n = grid[0].length;

if( !((0<=i && i<m) && (0<= j && j<n)) ) {
return 0;
}

if(grid[i][j] == 0) {
return 0;
}

int area = 0;
//Point currentPoint = new Point(i,j);
if(grid[i][j] == 1 && !(seen[i][j] == 1)) {
area++;
seen[i][j] = 1;

/*area += calculateArea( grid, i,  j+1, seen);
area += calculateArea( grid, i,  j-1, seen);
area += calculateArea( grid, i+1,  j, seen);
area += calculateArea( grid, i-1, j, seen);*/

return area + calculateArea( grid, i,  j+1, seen) + calculateArea( grid, i,  j-1, seen) +
calculateArea( grid, i+1,  j, seen) + calculateArea( grid, i-1, j, seen);

//return area;
} else {
return area;
}

//return 0;
}
}

posted @ 2018-04-17 20:11  Zhao_Gang  阅读(119)  评论(0编辑  收藏  举报