leetcode算法题:省份数量

leetcode算法题547
链接:https://leetcode.cn/problems/number-of-provinces

题目

有 n 个城市,其中一些彼此相连,另一些没有相连。如果城市 a 与城市 b 直接相连,且城市 b 与城市 c 直接相连,那么城市 a 与城市 c 间接相连。

省份 是一组直接或间接相连的城市,组内不含其他没有相连的城市。

给你一个 n x n 的矩阵 isConnected ,其中 isConnected[i][j] = 1 表示第 i 个城市和第 j 个城市直接相连,而 isConnected[i][j] = 0 表示二者不直接相连。

返回矩阵中 省份 的数量。

示例 1:
在这里插入图片描述

输入:isConnected = [[1,1,0],[1,1,0],[0,0,1]]
输出:2
示例 2:
在这里插入图片描述

输入:isConnected = [[1,0,0],[0,1,0],[0,0,1]]
输出:3

解法

使用并查集

	public static int findCircleNum(int[][] M) {
   
		if (M == null || M.length == 0) {
   
			return 0;
		}
		int length = M.length;
		UnionFind unionFind = new UnionFind(length);
		for (int i = 0; i < length; i++) {
   
			for (int j = i + 1; j < length; j++) {
   
				if (M[i][j] == 1) {
   
					unionFind.union(i, j);
				}
			}
		}
		return unionFind.getSize();
	}

	public static class UnionFind {
   
		private int[] parents;
		private int[] childSizes;
		private int size;

		public UnionFind(int N) {
   
			parents = new int[N];
			childSizes = new int[N];
			size = N;
			for (int i = 0; i < N; i++) {
   
				// 表示第i个位置的父节点为自己
				parents[i] = i;
				// 表示第i位置的子节点数
				childSizes[i] = 1;
			}
		}

		private int findParent(int index) {
   
			Stack<Integer> stack = new Stack<Integer>();
			// 从节点开始一直找到最上的父节点
			while (index != parents[index]) {
   
				stack.push(index);
				index = parents[index];
			}

			// 自己不是父节点,需要设置父节点
			while (!stack.isEmpty()) {
   
				parents[stack.pop()] = index;
			}
			return index;
		}

		public void union(int x, int y) {
   
			int a = findParent(x);
			int b = findParent(y);
			// 父节点不一样,需要合并
			if (a != b) {
   
				// 哪个子节点多,哪个成为父节点
				if (childSizes[a] >= childSizes[b]) {
   
					parents[b] = a;
					childSizes[a] += childSizes[b];
				} else {
   
					parents[a] = b;
					childSizes[b] += childSizes[a];
				}
				size--;
			}
		}

		public int getSize() {
   
			return size;
		}
	}

相关推荐

  1. leetcode 547.省份数量

    2023-12-14 19:26:02       27 阅读
  2. leetcode算法:岛屿数量

    2023-12-14 19:26:02       54 阅读
  3. leetcode-简单-数组算法

    2023-12-14 19:26:02       40 阅读
  4. 547. 省份数量

    2023-12-14 19:26:02       24 阅读
  5. 【并查集】 547. 省份数量

    2023-12-14 19:26:02       41 阅读
  6. 数组交集算法(leetcode第349)

    2023-12-14 19:26:02       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-14 19:26:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-14 19:26:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-14 19:26:02       82 阅读
  4. Python语言-面向对象

    2023-12-14 19:26:02       91 阅读

热门阅读

  1. 敏捷开发项目管理流程及scrum工具

    2023-12-14 19:26:02       58 阅读
  2. K8S(七)—污点、容忍

    2023-12-14 19:26:02       57 阅读
  3. k8s-Pod

    k8s-Pod

    2023-12-14 19:26:02      53 阅读
  4. hive客户机执行sql脚本无法显示表头

    2023-12-14 19:26:02       58 阅读
  5. 客户端注册账号-服务器-存入数据库..

    2023-12-14 19:26:02       43 阅读
  6. 【算法】【动规】单词拆分

    2023-12-14 19:26:02       60 阅读
  7. RESTful API

    2023-12-14 19:26:02       58 阅读
  8. 线程上下文设计模式

    2023-12-14 19:26:02       54 阅读
  9. Shiro框架权限控制

    2023-12-14 19:26:02       56 阅读
  10. 在ubuntu上rmp打包:准备工作

    2023-12-14 19:26:02       63 阅读
  11. spring事务(3)基于XML的声明式事务

    2023-12-14 19:26:02       60 阅读
  12. 使用opencv将Mat图像resize成检测输入的letterbox类型

    2023-12-14 19:26:02       48 阅读
  13. 2312llvm,编译X86的clang与llvm

    2023-12-14 19:26:02       64 阅读