码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 695 Max Area of Island

时间:2018-04-17 20:53:52      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:array   run   note   AC   boolean   数组   sla   thread   except   

题目:

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.

 

分析:

 

分析每一个元素的特点:

如果为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<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

Exception in thread "main" java.lang.StackOverflowError
    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)

 

需要重写Point的equals 方法。或许还有hashCode方法?

 

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;
        }
        
        @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++;
            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;
        }
        
        @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++;
            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;
        }
        
        @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);  
        }  
    }

 

结果:

技术分享图片

这一结果显然时间复杂度很高。 猜测是引入了新类Point引起的。

 

总结:

这道题花了不少时间。 主要是一开始忘记了递归这一利器。

LeetCode 695 Max Area of Island

标签:array   run   note   AC   boolean   数组   sla   thread   except   

原文地址:https://www.cnblogs.com/hzg1981/p/8819735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!