微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift]LeetCode739. 每日温度 | Daily Temperatures

Given a list of daily temperatures T,return a list such that,for each day in the input,tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible,put 0instead.

For example,given the list of temperatures T = [73,74,75,71,69,72,76,73],your output should be [1,1,4,2,0].

Note: The length of temperatures will be in the range [1,30000]. Each temperature will be an integer in the range [30,100].

根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高的天数。如果之后都不会升高,请输入 0来代替。

例如,给定一个列表 temperatures = [73,73],你的输出应该是 [1,0]

提示气温 列表长度的范围是 [1,30000]。每个气温的值的都是 [30,100] 范围内的整数。

484ms

 1 class Solution {
 2     func dailyTemperatures(_ T: [Int]) -> [Int] {
 3         let n = T.count
 4         var res = Array(repeating: 0,count: n)
 5         for i in (0..<n).reversed() {
 6             var j = i + 1
 7             while j < n,T[j] <= T[i]  {
 8                 if res[j] > 0 {
 9                     j  += res[j]
10                 } else {
11                     j = n
12                 }
13             }
14             if j < n {
15                 res[i] = j - i
16             }
17         }
18         return res
19     }
20 }

488ms

 1 class Solution {
 2     func dailyTemperatures(_ T: [Int]) -> [Int] {
 3         var result = [Int](repeating: 0,count: T.count)
 4         var stack = [Int]()
 5         stack.append(0)
 6         for i in 1..<T.count {
 7             while let last = stack.last,T[last] < T[i] {
 8                 stack.removeLast()
 9                 result[last] = i - last
10             }
11             stack.append(i)
12         }
13         return result
14     }
15 }

556ms

 1 class Solution {
 2     func dailyTemperatures(_ T: [Int]) -> [Int] {
 3   var result = Array(repeating: 0,count: T.count)
 4   var tempStorage = Array(repeating: Int.max,count: 101)
 5 
 6   for i in 0..<T.count {
 7         tempStorage[T[i]] = i
 8   }
 9         
10   var minGreaterThanI = Int.max
11   
12   for currentIndex in (0..<T.count).reversed() {
13     let t = T[currentIndex]
14       
15     if currentIndex != tempStorage[t] {
16         tempStorage[t] = currentIndex
17     }
18       
19     for temp in t+1..<101 {
20       let indexFromList = tempStorage[temp]
21       if indexFromList > currentIndex && indexFromList < minGreaterThanI {
22           minGreaterThanI = indexFromList
23       }
24     }
25     
26       if minGreaterThanI != Int.max {
27           result[currentIndex] = minGreaterThanI - currentIndex
28           minGreaterThanI = Int.max
29       }
30   }
31   
32   return result
33   }
34 }

796ms

 1 class Solution {
 2     func dailyTemperatures(_ temperatures: [Int]) -> [Int] {
 3         guard temperatures.count > 1 else { return [0] }
 4 
 5         var result = [Int](repeating: 0,count: temperatures.count)
 6 
 7         var stack = [(index: Int,temperature: Int)]()
 8 
 9         for (index,curTemperature) in temperatures.enumerated() {
10 
11             while stack.count > 0 {
12                 var last = stack.last!
13                 if curTemperature > last.temperature {
14                     last = stack.removeLast()
15                     result[last.index] = index - last.index
16                 } else {
17                     break
18                 }
19             }
20 
21             stack.append((index,curTemperature))
22         }
23 
24         return result
25     }
26 }

832ms

 1 class Solution {
 2     
 3     struct Temperature {
 4         var temperature = 0
 5         var index = 0
 6     }
 7     
 8     struct Stack<Temperature> {
 9         private var temperatures = [Temperature]()
10         mutating func push(_ item:Temperature){
11             self.temperatures.append(item)
12         }
13         
14         mutating func pop()->Temperature?{
15             return self.temperatures.popLast()
16         }
17         
18         func isEmpty() -> Bool {
19             return self.temperatures.isEmpty
20         }
21         
22         func peek() -> Temperature? {
23            return self.temperatures.last
24         }
25     }
26     
27     func dailyTemperatures(_ T: [Int]) -> [Int] {
28         var resc:[Int] = Array.init(repeating: 0,count: T.count)
29         var stack:Stack = Stack<Temperature>()
30         
31         for (index,temperature) in T.enumerated() {
32             let tempr = Temperature.init(temperature: temperature,index: index)
33            
34             while let peek = stack.peek(),peek.temperature < temperature {
35                 resc[peek.index] = index - peek.index
36                 stack.pop()
37             }
38             stack.push(tempr)
39         }
40         return resc
41     }
42 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐