2020-11-03

岛屿数量

第一种方法,用队列来完成一个BFS
注意访问过的节点,就应该把它从1变成0,这样才不会重复

import java.util.*;
class Point{
    int x;
    int y;
    Point(int x,int y){
        this.x=x;
        this.y=y;
    }
}

public class Solution {
    /**
     * 判断岛屿数量
     * @param grid char字符型二维数组 
     * @return int整型
     */

    
    public int solve (char[][] grid) {
        // write code here
        if (grid==null ||grid.length==0) return 0;
        int row=grid.length;
        int col=grid[0].length;
        
        int count=0;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if (grid[i][j]=='1'){
                    Queue<Point> queue=new LinkedList();
                    queue.offer(new Point(i,j));
                    grid[i][j]='0';
                    bfs(queue,grid);
                    count++;
                }
            }
        }
        return count;
    }
     public void bfs(Queue<Point> queue,char[][] grid){
         while(!queue.isEmpty()){
             Point point=queue.poll();
             int x=point.x;
             int y=point.y;
             if(x-1>=0 && grid[x-1][y]=='1'){
                 queue.offer(new Point(x-1,y));
                 grid[x-1][y]='0';
             }
             if(y-1>=0 && grid[x][y-1]=='1'){
                 queue.offer(new Point(x,y-1));
                 grid[x][y-1]='0';
             }
             if(x+1<grid.length && grid[x+1][y]=='1'){
                 queue.offer(new Point(x+1,y));
                 grid[x+1][y]='0';
             }
             if(y+1<grid[0].length && grid[x][y+1]=='1'){
                 queue.offer(new Point(x,y+1));
                 grid[x][y+1]='0';
             }
         }
     }
}

第二种方法,使用并查集,记得背并查集的写法:
1.构造一个数组作为并查集
2.find方法找到老祖宗,注意需要一直往前找,直到找到最老的老祖宗,因此就是一个用fx==x来做条件的while循环
3.union方法,吧老祖宗一样的合并,老祖宗不同的不合并

基于并查集的想法,进行这些操作:
1.初始化并查集,用records[i*col+j]表示grid[i][j]节点
2.count计数矩阵中1的个数,表示图的总分支
3.遍历矩阵,当矩阵是1时,合并它右边和下边的1,合并成功分支减一,合并失败说明两个节点已经在同一个分支不再减一
4.count表示图的分支即是岛屿的数量

import java.util.*;
//写一个并查集
class UnionFind{
    private int[] records;
    //构造一个并查集
    public UnionFind(int n){
        records=new int[n];
        for(int i=0;i<n;i++){
            records[i]=i;
        }
    }
    
    public int find(int x){
        int fx=x;
        while(records[fx]!=fx){
            fx=records[fx];
        }
        while(x!=fx){
            int temp=records[x];
            records[x]=fx;
            x=temp;
        }
        return fx;
    }
    
    public boolean union(int x,int y){
        int fx=find(x);
        int fy=find(y);
        if(fx!=fy){
            records[fx]=fy;
            return true;//合并成功
        }
        return false;//合并失败
    }
}



public class Solution {
    /**
     * 判断岛屿数量
     * @param grid char字符型二维数组 
     * @return int整型
     */

    
    public int solve (char[][] grid) {
        // write code here
        int row=grid.length;
        int col=grid[0].length;
        UnionFind unionfind=new UnionFind(row*col);
        int count=0;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(grid[i][j]=='1'){
                    count++;
                    if(i+1<row && grid[i+1][j]=='1' && unionfind.union(i*col+j,(i+1)*col+j)){
                        count--;
                    }
                    if(j+1<col && grid[i][j+1]=='1' && unionfind.union(i*col+j,i*col+j+1)){
                        count--;
                    }
                    
                }
            }
        }
        return count;
}
}

链表中的节点每k个一组翻转

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode reverseKGroup (ListNode head, int k) {
        // write code here
        if (head==null || head.next==null || k<2) return head;
        ListNode dummy=new ListNode(0);
        dummy.next=head;
        ListNode pre=dummy,cur=head,temp;
        int len=0;
        while(head !=null){
            len++;
            head =head.next;
        }
        
        for(int i=0;i<len/k;i++){
            for(int j=1;j<k;j++){
                temp=cur.next;
                cur.next=temp.next;
                temp.next=pre.next;
                pre.next=temp;
            }
            pre=cur;
            cur=cur.next;
        }
        return dummy.next;
        
    }
}

==

三号了,34567又只有五天了…啊!!!有什么办法能让我不要浪费时间
1.买东西
2.做kinectv2的课题
3.五号之前把基础的题和教程给过了,沉下心来开始做项目了,leetcode也需要刷难一点的了
4.感觉得留一个月做项目包装
5.明天敲个时间去医院开药
6.看看要不要投pp
坚持,坚持

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值