首页 > 编程语言 >算法--2023.1.16

算法--2023.1.16

时间:2023-01-16 12:33:07浏览次数:40  
标签:head temp 16 -- int 2023.1 new public

1.力扣200--岛屿数量

class Solution {
    //深度优先遍历
    public int cnt;
    public char[][] nums;
    public int[] dx,dy;
    public int m,n;
    public int numIslands(char[][] grid) {
        cnt = 0;
        dx = new int[]{1,0,-1,0};
        dy = new int[]{0,-1,0,1};
        m = grid.length;n = grid[0].length;
        nums = new char[m][n];
        //遍历每个节点,如果是1就进行深度优先遍历,将其周围的所有所有的1变成0
        //然后cnt+1
        for(int i = 0;i<m ;i++){
            for(int j = 0;j<n;j++){
                nums[i][j] = grid[i][j];
            }
        }
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(nums[i][j] == '1'){
                    nums[i][j] = 0;
                    dfs(i,j);
                    cnt++;
                    //System.out.println(cnt);
                }
            }
        }
        return cnt;
    }
    //深度优先遍历算法
    public void dfs(int i,int j){
        for(int p = 0;p<4;p++){
            int x = i + dx[p], y = j + dy[p];
            if(x>=0&&x<m&&y>=0&&y<n&&nums[x][y] == '1'){
                nums[x][y] = 0;
                dfs(x,y);
            }
        }
    }
}

2.力扣206--反转链表

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode tail = null;
        while(head!=null){
            ListNode temp = head.next;
            head.next = tail;
            tail = head;
            head = temp;
        }
        return tail;

    }
}

3.力扣207--课程表

class Solution {
    //拓扑排序
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        //用链表来记录图中的所有边
        List<List<Integer>> contain = new LinkedList<>();
        int[] nums = new int[numCourses];
        //遍历数组,将所有边添加进链表中
        for(int i = 0;i<numCourses;i++){
            contain.add(new LinkedList<>());
        }
        for(int[] temp : prerequisites){
            //b指向a有一条边
            int a = temp[0], b = temp[1];
            contain.get(b).add(a);
            //a的入度加一
            nums[a]++;
        }
        int res = 0;
        //队列用来做广度优先遍历,保存暂时入度为0的点
        Deque<Integer> queue = new LinkedList<>();
        for(int i = 0;i<numCourses;i++){
            if(nums[i] == 0){
                queue.offer(i);
            }
        }
        //遍历队列里面的元素
        while(!queue.isEmpty()){
            int tt = queue.poll();
            res++;
            //出队后值加一
            for(int t1 : contain.get(tt)){
                nums[t1]--;
                //然后遍历出队节点指向的所有子节点的度,所有子节点的度-1
                if(nums[t1] == 0){
                    //如果子节点的度为0,则将该节点放入队列中,以便下一次用来遍历该节点1的子节点
                    queue.offer(t1);
                }
            }
        }
        //System.out.println(res);
        //最后比较出队的节点与总结点的个数
        return res == numCourses;

    }
}

  

  

标签:head,temp,16,--,int,2023.1,new,public
From: https://www.cnblogs.com/lyjps/p/17055139.html

相关文章

  • 如何快速调试RTC?
    1、RTC介绍RTC是RealTimeClock的简称,它在硬件电路上单独供电,当系统关机时,CPU和其他外部硬件设备全部掉电,但是RTC仍然继续工作。这样就可以继续给设备提供精准的时钟,并提......
  • Curve 文件存储在 Elasticsearch 冷热数据存储中的应用实践
    Elasticsearch在生产环境中有广泛的应用,本文介绍一种方法,基于网易数帆开源的Curve文件存储,实现Elasticsearch存储成本、性能、容量和运维方面的显著提升。ES使用CurveF......
  • 电动自行车出欧盟需要做CE认证,EN15194标准测试
    电动滑板是以传统人力滑板为基础,加上电力套件的交通工具,目前的电动滑板一般分为双轮驱动或单轮驱动。电动滑板出口欧盟是需要办理CE认证的,电动滑板CE认证标准为EN15194:201......
  • 最详细破解nessus(windows)教程 插件更新至20220613
    Nessus是全世界最多人使用的系统漏洞扫描与分析软件。总共有超过75,000个机构使用Nessus作为扫描该机构电脑系统的软件。企业安全的好伙伴一、安装1、nessus官网下载......
  • day60-watch与计算属性的对比与监视属性的简写
    监视属性与计算属性对比监视属性简写 watch:{//正常写法/*isHot:{handler(newvalue,oldvalue){//当isHot改变的时候调用......
  • 正则表达式
    什么是正则表达式?正则是一个对象,语法规则:定义规则: const变量名=/表达式/查找: 变量名.test(被检测的字符串)返回true/false 变量名.exec(被检测......
  • YOLO家族系列模型的演变:从v1到v8(下)
    昨天的文章中,我们回顾了YOLO家族的前9个架构。本文中将继续总结最后3个框架,还有本月最新发布的YOLOV8.Backbone最初由一个分支(GoogLeNet、VGG、Darknet)组成,然后过......
  • B/S 以上超大文件上传和断点续传服务器的实现
    ​ 第一点:Java代码实现文件上传FormFilefile=manform.getFile();StringnewfileName= null;Stringnewpathname= null;StringfileAddre= "/numUp";try{......
  • 记一种有趣的团队管理和激励方式
    昨晚MMOSLG项目开了年前的一次总结会,主角是制作人,我刚加入项目不久,第一次见到这种管理和激励团队的方法,感觉挺有意思的,记录一下。先浅谈下团队管理,一个稍微大点的公司或者......
  • RK3568工业级核心板高温运行测试
    Rockchip RK3568 是一款通用型MPU,产品集成GPU、NPU,支持4K、HDMI、LVDS、MIPI、PCIe3.0、USB3.0、千兆以太网、CAN-BUS、UART等丰富外设接口。 RK3568的高温工作情况如何......