Java实现-岛屿的个数

Java实现-岛屿的个数_第1张图片Java实现-岛屿的个数_第2张图片

public class Solution {
    /**
     * @param grid a boolean 2D matrix
     * @return an integer
     */
    public int numIslands(boolean[][] grids) {
        // Write your code here
        if(grids.length==0){
			return 0;
		}
		int [][]grid=new int[grids.length][grids[0].length];
		for(int i=0;i list=new ArrayList();
		int count=0;
        for(int i=0;i0){
        					Point temp=list.get(list.size()-grid[0].length);
        					if(temp.where>0){
        						p.where=temp.where;
        					}
        				}
        				if(j>0){
        					Point temp=list.get(list.size()-1);
        					if(temp.where>0){
        						if(p.where>0){
        							if(p.where>temp.where){
        								for(int k=0;k0){
        					list.add(p);
//        					System.out.println("***************"+i+" "+j+"   "+p.where);
        				}else{
        					p.where=++count;
//        					System.out.println("----------"+i+" "+j+"   "+p.where);
        					list.add(p);
        				}
        			}
        		}else{
        			list.add(p);
        		}
        	}
        }
		return count;

    }
}
class Point{
	int value;//值
	int where;//属于哪一个类0
	int visited;//是否访问过
	public Point(int value,int where,int visited){
		this.value=value;
		this.where=where;
		this.visited=visited;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public int getWhere() {
		return where;
	}
	public void setWhere(int where) {
		this.where = where;
	}
	public int getVisited() {
		return visited;
	}
	public void setVisited(int visited) {
		this.visited = visited;
	}
	
}



你可能感兴趣的:(斩杀LintCode,All,in,One,LintCode)