在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a list of daily temperatures For example, given the list of temperatures Note: The length of 根据每日 例如,给定一个列表 提示: 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 }
|
请发表评论