[Swift]LeetCode457. 環形數組循環 | Circular Array Loop

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

You are given an array of positive and negative integers. If a number n at an index is positive, then move forward n steps. Conversely, if it's negative (-n), move backward n steps. Assume the first element of the array is forward next to the last element, and the last element is backward next to the first element. Determine if there is a loop in this array. A loop starts and ends at a particular index with more than 1 element along the loop. The loop must be "forward" or "backward'.git

Example 1: Given the array [2, -1, 1, 2, 2], there is a loop, from index 0 -> 2 -> 3 -> 0.github

Example 2: Given the array [-1, 2], there is no loop.算法

Note: The given array is guaranteed to contain no element "0".數組

Can you do it in O(n) time complexity and O(1) space complexity?微信


給定一組含有正整數和負整數的數組。若是某個索引中的 n 是正數的,則向前移動 n 個索引。相反,若是是負數(-n),則向後移動 n 個索引。oop

假設數組首尾相接。判斷數組中是否有環。環中至少包含 2 個元素。環中的元素一概「向前」或者一概「向後」。this

示例 1:給定數組 [2, -1, 1, 2, 2], 有一個循環,從索引 0 -> 2 -> 3 -> 0。spa

示例 2:給定數組[-1, 2], 沒有循環。code

注意:給定數組保證不包含元素"0"。

你能寫出時間複雜度爲 O(n) 且空間複雜度爲 O(1) 的算法嗎?


8ms

 1 class Solution {
 2     func circularArrayLoop(_ nums: [Int]) -> Bool {
 3         var m:[Int:Int] = [Int:Int]()
 4         var n:Int = nums.count
 5         var visited:[Bool] = [Bool](repeating:false,count:n)
 6         for i in 0..<n
 7         {
 8             if visited[i] {continue}
 9             var cur:Int = i
10             while(true)
11             {
12                 visited[cur] = true
13                 var next:Int = (cur + nums[cur]) % n
14                 if next < 0 {next += n}
15                 if next == cur || nums[next] * nums[cur] < 0
16                 {
17                     break
18                 }
19                 if m[next] != nil {return true}
20                 m[cur] = next
21                 cur = next
22             }
23         }
24         return false
25     }
26 }

8ms

 1 class Solution {
 2     func circularArrayLoop(_ nums: [Int]) -> Bool {
 3         
 4         var visitedIndexes: Set<Int> = []
 5         
 6         for i in 0..<nums.count {
 7             if visitedIndexes.contains(i) {
 8                 continue
 9             }
10             
11             var currentIndex = i
12             var isForwardLoop = nums[currentIndex] > 0
13             var lastIndex = -1
14             var currentIndexes: Set<Int> = []
15             
16             while visitedIndexes.count < nums.count {
17                 visitedIndexes.insert(currentIndex)
18                 currentIndexes.insert(currentIndex)
19                 
20                 let nextIndex = findNextIndex(nums: nums, index: currentIndex)
21                 
22                 if isForwardLoop && nums[nextIndex] < 0 {
23                     break // we are doing foward loop, and this is backward
24                 } else if !isForwardLoop && nums[nextIndex] > 0 {
25                     break // we are doing backward loop, and this is forward
26                 } else if nextIndex == lastIndex {
27                     // we are looping without 1 number in-between which is fine
28                     break
29                 } else if currentIndexes.contains(nextIndex) {
30                     return true // there is a loop
31                 } else {
32                     lastIndex = currentIndex
33                     currentIndex = nextIndex
34                 }
35             }
36         }
37         
38         return false
39     }
40     
41     func findNextIndex(nums: [Int], index: Int) -> Int {
42         var number = nums[index] % nums.count
43         
44         var nextIndex = number
45         
46         if number > 0 {
47             nextIndex = (index + number) % nums.count
48         } else if number < 0 {
49             nextIndex = (index + number)
50             if nextIndex < 0 {
51                 nextIndex += nums.count // wrap it around
52             }
53         }
54         
55         return nextIndex
56     }
57 }
相關文章
相關標籤/搜索