Java实现-岛屿的个数
来源:互联网 发布:报纸排版软件 adobe 编辑:程序博客网 时间:2024/04/30 07:19
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<grids.length;i++){for(int j=0;j<grids[0].length;j++){if(grids[i][j]){grid[i][j]=1;}else{grid[i][j]=0;}}}ArrayList<Point> list=new ArrayList<Point>();int count=0; for(int i=0;i<grid.length;i++){ for(int j=0;j<grid[0].length;j++){ Point p=new Point(grid[i][j], 0,1); if(grid[i][j]==1){ if(list.isEmpty()){ p.where=++count; list.add(p); }else{ if(i>0){ 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;k<list.size();k++){ if(list.get(k).where==p.where){ list.get(k).where=temp.where; } } count--; p.where=temp.where; }else if(p.where<temp.where){ for(int k=0;k<list.size();k++){ if(list.get(k).where==temp.where){ list.get(k).where=p.where; } } p.where=p.where; count--; }else{ p.where=temp.where; } }else{ p.where=temp.where; } } } if(p.where>0){ 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;//属于哪一个类0int 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;}}
阅读全文
0 0
- Java实现-岛屿的个数
- 题目:岛屿的个数
- LintCode 岛屿的个数
- LinkCode-岛屿的个数
- 岛屿的个数
- 岛屿的个数
- 岛屿的个数-lintcode
- 岛屿的个数
- 岛屿的个数
- LintCode:岛屿的个数
- 433-岛屿的个数
- 岛屿的个数
- 岛屿的个数-LintCode
- 岛屿的个数(LintCode)
- LintCode 433. 岛屿的个数
- 岛屿个数
- Lintcode解题笔记 - 岛屿的个数
- LintCode——岛屿的个数
- 最小费用最大流总结
- Gensim入门教程
- 基于装饰者模式的变种
- Codeforces 813 C The Tag Game
- %运算符总结
- Java实现-岛屿的个数
- Android相对布局
- 前端面试题-基础理论汇总
- 简单计算几何+卡精度 New Year Table CodeForces
- Android二布局
- Android TextView的一些特殊用法
- Gensim进阶教程
- Appium(三):python appium包中webdriver源码查看方法
- 跟我一起写 Makefile(一)