[Swift]LeetCode682. 棒球比赛 | Baseball Game

[Swift]LeetCode682. 棒球比赛 | Baseball Game,第1张

概述You‘re now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types: Integer (one round‘s score): Directly represents the number of points you get in th

You‘re Now a baseball game point recorder.

Given a List of strings,each string can be one of the 4 following types:

Integer (one round‘s score): Directly represents the number of points you get in this round. "+" (one round‘s score): Represents that the points you get in this round are the sum of the last two valIDround‘s points. "D" (one round‘s score): Represents that the points you get in this round are the doubled data of the last valID round‘s points. "C" (an operation,which isn‘t a round‘s score): Represents the last valID round‘s points you get were invalID and should be removed. 

Each round‘s operation is permanent and Could have an impact on the round before and the round after.

You need to return the sum of the points you Could get in all the rounds.

Example 1:

input: ["5","2","C","D","+"]Output: 30Explanation: Round 1: You Could get 5 points. The sum is: 5.Round 2: You Could get 2 points. The sum is: 7.Operation 1: The round 2‘s data was invalID. The sum is: 5.  Round 3: You Could get 10 points (the round 2‘s data has been removed). The sum is: 15.Round 4: You Could get 5 + 10 = 15 points. The sum is: 30. 

Example 2:

input: ["5","-2","4","9","+","+"]Output: 27Explanation: Round 1: You Could get 5 points. The sum is: 5.Round 2: You Could get -2 points. The sum is: 3.Round 3: You Could get 4 points. The sum is: 7.Operation 1: The round 3‘s data is invalID. The sum is: 3.  Round 4: You Could get -4 points (the round 3‘s data has been removed). The sum is: -1.Round 5: You Could get 9 points. The sum is: 8.Round 6: You Could get -4 + 9 = 5 points. The sum is 13.Round 7: You Could get 9 + 5 = 14 points. The sum is 27. 

Note:

The size of the input List will be between 1 and 1000. Every integer represented in the List will be between -30000 and 30000.

你现在是棒球比赛记录员。
给定一个字符串列表,每个字符串可以是以下四种类型之一:
1.整数(一轮的得分):直接表示您在本轮中获得的积分数。
2. "+"(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。
3. "D"(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。
4. "C"(一个 *** 作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除。

每一轮的 *** 作都是永久性的,可能会对前一轮和后一轮产生影响。
你需要返回你在所有回合中得分的总和。

示例 1:

输入: ["5","+"]输出: 30解释: 第1轮:你可以得到5分。总和是:5。第2轮:你可以得到2分。总和是:7。 *** 作1:第2轮的数据无效。总和是:5。第3轮:你可以得到10分(第2轮的数据已被删除)。总数是:15。第4轮:你可以得到5 + 10 = 15分。总数是:30。

示例 2:

输入: ["5","+"]输出: 27解释: 第1轮:你可以得到5分。总和是:5。第2轮:你可以得到-2分。总数是:3。第3轮:你可以得到4分。总和是:7。 *** 作1:第3轮的数据无效。总数是:3。第4轮:你可以得到-4分(第三轮的数据已被删除)。总和是:-1。第5轮:你可以得到9分。总数是:8。第6轮:你可以得到-4 + 9 = 5分。总数是13。第7轮:你可以得到9 + 5 = 14分。总数是27。

注意:

输入列表的大小将介于1和1000之间。 列表中的每个整数都将介于-30000和30000之间。 Runtime: 28 ms Memory Usage: 20.1 MB
 1 class Solution { 2     func calPoints(_ ops: [String]) -> Int { 3         var v:[Int] = [Int]() 4         for op in ops 5         { 6             if op == "+" 7             { 8                 v.append(v.last! + v[v.count - 2]) 9             }10             else if op == "D"11             {12                 v.append(2 * v.last!)13             }14             else if op == "C"15             {16                 v.popLast()17             }18             else19             {20                 v.append(Int(op)!)                21             }           22         }23         return v.reduce(0,+)24     }25 }

28ms

 1 class Solution { 2     func calPoints(_ ops: [String]) -> Int { 3         var points:Int = 0 4         var poStack:[Int] = [] 5          6         for i in 0..<ops.count { 7             if ops[i] == "+" { 8                 let n:Int = poStack.count - 2 9                 points = points + poStack[n] + poStack.last!10                 poStack.append(poStack[n] + poStack.last!)11                 12             }else if ops[i] == "C"{13                 let temp:Int = poStack.last!14                 poStack.removeLast()15                 points = points - temp16                 17             }else if ops[i] == "D" {18                 points = points + poStack.last! * 219                 let po = poStack.last! * 220                 poStack.append(po)21                 22             }else{23                 points = points + Int(ops[i])!24                 poStack.append(Int(ops[i])!)25             }26         }27         28         return points29     }30 }

32ms

 1 class Solution { 2     func calPoints(_ ops: [String]) -> Int { 3         var stack = [Int]() 4         var sum = 0 5         for ch in ops { 6             switch ch { 7             case "C": 8                 let x = stack.removeLast() 9                 sum -= x10             case "D":11                 if let x = stack.last {12                     stack.append(x * 2)13                     sum += x * 214                 }15             case "+":16                 if stack.count >= 2 {17                     let x = stack[stack.count - 1]18                     let y = stack[stack.count - 2]19                     stack.append(x + y)20                     sum += (x + y)21                 }22             default:23                 if let x = Int(ch) {24                     stack.append(x)25                     sum += x26                 }27             }28         }29         return sum30     }31 }

36ms

 1 class Solution { 2     func calPoints(_ ops: [String]) -> Int { 3         var stack = [String]() 4         for op in ops { 5             if Int(op) != nil { 6                 stack.append(op) 7             } else if op == "C" && stack.count > 0 { 8                 stack.removeLast() 9             } else if op == "D" && stack.count > 0 {10                 stack.append(String(Int(stack.last!)! * 2))11             } else if stack.count >= 2 {12                 let sum = String(Int(stack.last!)! + Int(stack[stack.count - 2])!)13                 stack.append(sum)14             }15         }16         17         var ans = 018         for item in stack {19             ans += Int(item)!20         }21         return ans22     }23 }

40ms

 1 struct Stack<T: Equatable> { 2   private var List: [T] 3   init() { 4     List = [T]() 5   } 6   var isEmpty:Bool { 7     return List.count == 0 8   } 9   var count: Int {10     return List.count11   }12   mutating func push(_ value: T) {13     List.append(value)14   }15   16   mutating func pop() -> T? {17     guard !isEmpty else { return nil }18     return List.removeLast()19   }20   21   func peek() -> T? {22     return List.last23   }24   func peekLastButOne() -> T? {25     guard !isEmpty else { return nil }26     guard count > 1 else { return nil }27     return List[count-2]28   }29 }30 31 class Solution {32     func calPoints(_ ops: [String]) -> Int {33         var dataStack = Stack<Int>()34         var totalSum = 035         for element in ops {36             if let number = Int(element) { 37                 // handle integers38                 dataStack.push(number)39                 totalSum += number40             }41             else {42                 if element == "C" {43                     // cancel case operation44                     let val = dataStack.pop() ?? 045                     totalSum -= val46                 } else if element == "D" {47                     // double round48                     var val = dataStack.peek() ?? 049                     val *= 250                     dataStack.push(val)51                     totalSum += val52                 } else {53                     var val1 = dataStack.peek() ?? 054                     let val2 = dataStack.peekLastButOne() ?? 055                     val1 += val256                     dataStack.push(val1)57                     totalSum += val158                     // sum of last 2 rounds results59                 }60             }61         }62         return totalSum63     }64 }

60ms

 1 class Solution { 2     func calPoints(_ ops: [String]) -> Int { 3         var history = [Int]() 4         for op in ops { 5             if op == "+" { 6                 history.append(history[history.count-1] + history[history.count-2]) 7             } else if op == "D" { 8                 history.append(history[history.count-1] * 2) 9             } else if op == "C" {10                 history.removeLast()11             } else {12                 history.append(Int(op)!)13             }14         }15 16         return history.reduce(0) { $0 + $1 }17     }18 }

76ms

 1 class Solution { 2     func calPoints(_ ops: [String]) -> Int { 3             if ops.count < 1 { 4                 return 0 5             } 6             var result = 0 7             var statckArray : [Int] = [] 8             var temp = 0 9             for score in ops {10                 if score == "C" && statckArray.count > 0{11                     statckArray.removeLast()12                 }else if score == "D" && statckArray.count > 0 {13                     temp = statckArray.last!14                     statckArray.append(temP*2)15                 }else if score == "+" && statckArray.count > 1 {16                     temp = statckArray.last! + statckArray[statckArray.count-2]17                     statckArray.append(temp)18                 }else if score == "+" && statckArray.count == 1 {19                     temp = statckArray.last!20                     statckArray.append(temp)21                 }else if isPurnInt(string: score) {22                     statckArray.append(Int(score)!)23                 }24             }25             while statckArray.count > 0 {26                 result = result + statckArray.last!27                 statckArray.removeLast()28             }29             return result30         }31         func isPurnInt(string: String) -> Bool {32             let scan: Scanner = Scanner(string: string)33             var val:Int = 034             return scan.scanInt(&val) && scan.isAtEnd35             36         }37 }
@H_397_1419@ 总结

以上是内存溢出为你收集整理的[Swift]LeetCode682. 棒球比赛 | Baseball Game全部内容,希望文章能够帮你解决[Swift]LeetCode682. 棒球比赛 | Baseball Game所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1019517.html

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

发表评论

登录后才能评论

评论列表(0条)

保存