[Swift]LeetCode565. 数组嵌套 | Array Nesting

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10420496.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to the rule below.

Suppose the first element in S starts with the selection of element A[i] of index = i, the next element in S should be A[A[i]], and then A[A[A[i]]]… By that analogy, we stop adding right before a duplicate element occurs in S. 

Example 1:

Input: A = [5,4,0,3,1,6,2]
Output: 4
Explanation: 
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.

One of the longest S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0} 

Note:

  1. N is an integer within the range [1, 20,000].
  2. The elements of A are all distinct.
  3. Each element of A is an integer within the range [0, N-1].

索引从0开始长度为N的数组A,包含0N - 1的所有整数。找到并返回最大的集合SS[i] = {A[i], A[A[i]], A[A[A[i]]], ... }且遵守以下的规则。

假设选择索引为i的元素A[i]S的第一个元素,S的下一个元素应该是A[A[i]],之后是A[A[A[i]]]... 以此类推,不断添加直到S出现重复的元素。

示例 1:

输入: A = [5,4,0,3,1,6,2]
输出: 4
解释: 
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.

其中一种最长的 S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}

注意:

  1. N[1, 20,000]之间的整数。
  2. A中不含有重复的元素。
  3. A中的元素大小在[0, N-1]之间。

Runtime: 124 ms
Memory Usage: 19.5 MB
 1 class Solution {
 2     func arrayNesting(_ nums: [Int]) -> Int {
 3         var nums = nums
 4         var n:Int = nums.count
 5         var res:Int = 0
 6         for i in 0..<n
 7         {
 8             var cnt:Int = 1
 9             while(nums[i] != i && nums[i] != nums[nums[i]])
10             {
11                 nums.swapAt(i,nums[i])
12                 cnt += 1
13             }
14             res = max(res,cnt)
15         }
16         return res        
17     }
18 }

140ms

 1 class Solution {
 2     func arrayNesting(_ nums: [Int]) -> Int {
 3         var visited: Set<Int> = []
 4         var maxLen = 0
 5         for i in 0 ..< nums.count {
 6             if !visited.contains(i) {
 7                 var next = nums[i]
 8                 var count = 0
 9                 while !visited.contains(next) {
10                     visited.insert(next)
11                     next = nums[next]
12                     count += 1
13                 }
14                 maxLen = max(maxLen, count)
15             }
16         }
17         
18         return maxLen
19     }
20 }

184ms

 1 class Solution {
 2     func arrayNesting(_ nums: [Int]) -> Int {
 3         var visited = [Bool](repeating: false, count: nums.count)
 4         var result = 0
 5         
 6         for i in 0..<nums.count {
 7             var j = i
 8             var count = 0
 9             while !visited[j] {
10                 visited[j] = true
11                 j = nums[j]
12                 count += 1
13             }
14             result = max(result, count)
15         }
16         
17         return result
18     }
19 }
原文地址:https://www.cnblogs.com/strengthen/p/10420496.html