[Swift]LeetCode565. 數組嵌套 | Array Nesting

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bevxfbee-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git

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. github

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]]], ... }且遵照如下的規則。spa

假設選擇索引爲i的元素A[i]S的第一個元素,S的下一個元素應該是A[A[i]],以後是A[A[A[i]]]... 以此類推,不斷添加直到S出現重複的元素。code

示例 1:htm

輸入: 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}

注意:blog

  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 }
相關文章
相關標籤/搜索