1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最长公共子路径 [longest-common-subpath].html
2022-03-29 12:43:11 +08:00

49 lines
2.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>一个国家由 <code>n</code> 个编号为 <code>0</code> 到 <code>n - 1</code> 的城市组成。在这个国家里,<strong>每两个</strong> 城市之间都有一条道路连接。</p>
<p>总共有 <code>m</code> 个编号为 <code>0</code> 到 <code>m - 1</code> 的朋友想在这个国家旅游。他们每一个人的路径都会包含一些城市。每条路径都由一个整数数组表示,每个整数数组表示一个朋友按顺序访问过的城市序列。同一个城市在一条路径中可能 <strong>重复</strong> 出现,但同一个城市在一条路径中不会连续出现。</p>
<p>给你一个整数 <code>n</code> 和二维数组 <code>paths</code> ,其中 <code>paths[i]</code> 是一个整数数组,表示第 <code>i</code> 个朋友走过的路径,请你返回 <strong>每一个</strong> 朋友都走过的 <strong>最长公共子路径</strong> 的长度,如果不存在公共子路径,请你返回 <code>0</code> 。</p>
<p>一个 <strong>子路径</strong> 指的是一条路径中连续的城市序列。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>n = 5, paths = [[0,1,<strong>2,3</strong>,4],
[<strong>2,3</strong>,4],
[4,0,1,<strong>2,3</strong>]]
<b>输出:</b>2
<b>解释:</b>最长公共子路径为 [2,3] 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>n = 3, paths = [[0],[1],[2]]
<b>输出:</b>0
<b>解释:</b>三条路径没有公共子路径。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>n = 5, paths = [[<strong>0</strong>,1,2,3,4],
[4,3,2,1,<strong>0</strong>]]
<b>输出:</b>1
<b>解释:</b>最长公共子路径为 [0][1][2][3] 和 [4] 。它们长度都为 1 。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>m == paths.length</code></li>
<li><code>2 <= m <= 10<sup>5</sup></code></li>
<li><code>sum(paths[i].length) <= 10<sup>5</sup></code></li>
<li><code>0 <= paths[i][j] < n</code></li>
<li><code>paths[i]</code> 中同一个城市不会连续重复出现。</li>
</ul>