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

[Swift]LeetCode739. 每日温度 | Daily Temperatures,第1张

概述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

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 = n12                 }13             }14             if j < n {15                 res[i] = j - i16             }17         }18         return res19     }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 - last10             }11             stack.append(i)12         }13         return result14     }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.max11   12   for currentIndex in (0..<T.count).reversed() {13     let t = T[currentIndex]14       15     if currentIndex != tempStorage[t] {16         tempStorage[t] = currentIndex17     }18       19     for temp in t+1..<101 {20       let indexFromList = tempStorage[temp]21       if indexFromList > currentIndex && indexFromList < minGreaterThanI {22           minGreaterThanI = indexFromList23       }24     }25     26       if minGreaterThanI != Int.max {27           result[currentIndex] = minGreaterThanI - currentIndex28           minGreaterThanI = Int.max29       }30   }31   32   return result33   }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.index16                 } else {17                     break18                 }19             }20 21             stack.append((index,curTemperature))22         }23 24         return result25     }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.isEmpty20         }21         22         func peek() -> Temperature? {23            return self.temperatures.last24         }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.index36                 stack.pop()37             }38             stack.push(tempr)39         }40         return resc41     }42 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode739. 每日温度 | Daily Temperatures全部内容,希望文章能够帮你解决[Swift]LeetCode739. 每日温度 | Daily Temperatures所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1018769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存