Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a6808d

Browse files
✨feat: Add 1791
1 parent ed514cd commit 2a6808d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
| [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 |
106106
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |
107107
| [1763. 最长的美好子字符串](https://leetcode-cn.com/problems/longest-nice-substring/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-nice-substring/solution/cong-shu-ju-fan-wei-xuan-ze-he-gua-suan-n3y2a/) | 简单 | 🤩🤩🤩 |
108+
| [1791. 找出星型图的中心节点](https://leetcode-cn.com/problems/find-center-of-star-graph/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-center-of-star-graph/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-qoix/) | 简单 | 🤩🤩🤩 |
108109
| [1816. 截断句子](https://leetcode-cn.com/problems/truncate-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/truncate-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-l7gu/) | 简单 | 🤩🤩🤩🤩 |
109110
| [1834. 单线程 CPU](https://leetcode-cn.com/problems/single-threaded-cpu/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-threaded-cpu/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-1qk0/) | 中等 | 🤩🤩🤩🤩 |
110111
| [1893. 检查是否区域内所有整数都被覆盖](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-j83x/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1791. 找出星型图的中心节点](https://leetcode-cn.com/problems/find-center-of-star-graph/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-qoix/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
有一个无向的 星型 图,由 $n$ 个编号从 1ドル$ 到 $n$ 的节点组成。星型图有一个 中心 节点,并且恰有 $n - 1$ 条边将中心节点与其他每个节点连接起来。
10+
11+
给你一个二维整数数组 `edges` ,其中 $edges[i] = [u_i, v_i]$ 表示在节点 $u_i$ 和 $v_i$ 之间存在一条边。请你找出并返回 `edges` 所表示星型图的中心节点。
12+
13+
示例 1:
14+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/03/14/star_graph.png)
15+
```
16+
输入:edges = [[1,2],[2,3],[4,2]]
17+
18+
输出:2
19+
20+
解释:如上图所示,节点 2 与其他每个节点都相连,所以节点 2 是中心节点。
21+
```
22+
示例 2:
23+
```
24+
输入:edges = [[1,2],[5,1],[1,3],[1,4]]
25+
26+
输出:1
27+
```
28+
29+
提示:
30+
* 3ドル <= n <= 10^5$
31+
* $edges.length == n - 1$
32+
* $edges[i].length == 2$
33+
* 1ドル <= u_i, v_i <= n$
34+
* $u_i != v_i$
35+
* 题目数据给出的 `edges` 表示一个有效的星型图
36+
37+
---
38+
39+
### 模拟
40+
41+
根据题意,中心节点必然出现在所有的 $edges[i]$ 中,因此使用前两条边即可确定答案。
42+
43+
起始让 $edges[0][0]$ 和 $edges[0][1]$ 作为答案候选,然后在 $edges[1]$ 关系中检查哪个候选出现过。
44+
45+
代码:
46+
```Java
47+
class Solution {
48+
public int findCenter(int[][] edges) {
49+
int a = edges[0][0], b = edges[0][1];
50+
if (a == edges[1][0] || a == edges[1][1]) return a;
51+
else return b;
52+
}
53+
}
54+
```
55+
* 时间复杂度:$O(1)$
56+
* 空间复杂度:$O(1)$
57+
58+
---
59+
60+
### 进阶
61+
62+
**显然,如果将每个 $edges[i]$ 看做两点之间的「双向边」的话,那么星型图为「欧拉图」,所有点的出度均等于入度。**
63+
64+
容易将题拓展为求欧拉回路的问题:**给定星与星之间的距离,从某个星的位置出发,经过所有的边(可重复经过)并回到起点的最短距离,输出能够取得最短距离的路径(无解输出 $-1$)**
65+
66+
---
67+
68+
### 最后
69+
70+
这是我们「刷穿 LeetCode」系列文章的第 `No.1791` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
71+
72+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
73+
74+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
75+
76+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
77+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /