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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10499508.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

You're now a baseball game point recorder.

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

  1. Integer (one round's score): Directly represents the number of points you get in this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two validround's points.
  3. "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.
  4. "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: 30
Explanation: 
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","C","D","9","+","+"]
Output: 27
Explanation: 
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","2","C","D","+"]
输出: 30
解释: 
第1轮:你可以得到5分。总和是:5。
第2轮:你可以得到2分。总和是:7。
操作1:第2轮的数据无效。总和是:5。
第3轮:你可以得到10分(第2轮的数据已被删除)。总数是:15。
第4轮:你可以得到5 + 10 = 15分。总数是:30。

示例 2:

输入: ["5","-2","4","C","D","9","+","+"]
输出: 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             else
19             {
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 - temp
16                 
17             }else if ops[i] == "D" {
18                 points = points + poStack.last! * 2
19                 let po = poStack.last! * 2
20                 poStack.append(po)
21                 
22             }else{
23                 points = points + Int(ops[i])!
24                 poStack.append(Int(ops[i])!)
25             }
26         }
27         
28         return points
29     }
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 -= x
10             case "D":
11                 if let x = stack.last {
12                     stack.append(x * 2)
13                     sum += x * 2
14                 }
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 += x
26                 }
27             }
28         }
29         return sum
30     }
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 = 0
18         for item in stack {
19             ans += Int(item)!
20         }
21         return ans
22     }
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.count
11   }
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.last
23   }
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 = 0
35         for element in ops {
36             if let number = Int(element) { 
37                 // handle integers
38                 dataStack.push(number)
39                 totalSum += number
40             }
41             else {
42                 if element == "C" {
43                     // cancel case operation
44                     let val = dataStack.pop() ?? 0
45                     totalSum -= val
46                 } else if element == "D" {
47                     // double round
48                     var val = dataStack.peek() ?? 0
49                     val *= 2
50                     dataStack.push(val)
51                     totalSum += val
52                 } else {
53                     var val1 = dataStack.peek() ?? 0
54                     let val2 = dataStack.peekLastButOne() ?? 0
55                     val1 += val2
56                     dataStack.push(val1)
57                     totalSum += val1
58                     // sum of last 2 rounds results
59                 }
60             }
61         }
62         return totalSum
63     }
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 result
30         }
31         func isPurnInt(string: String) -> Bool {
32             let scan: Scanner = Scanner(string: string)
33             var val:Int = 0
34             return scan.scanInt(&val) && scan.isAtEnd
35             
36         }
37 }

 

转载于:https://www.cnblogs.com/strengthen/p/10499508.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/365427.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java调试器–权威的工具列表

Java调试是一个复杂的空间。 调试器的类型很多&#xff0c;并且有很多工具可供选择。 在此页面中&#xff0c;我们将介绍7种类型的调试器之间的区别&#xff0c;并查看每个类别中的主要工具&#xff0c;以帮助您为正确的工作选择正确的工具。 以下是我们涵盖的调试器类型&…

java项目中多个定时器_在java项目中如何使用Timer定时器

在java项目中如何使用Timer定时器发布时间&#xff1a;2020-11-16 16:36:16来源&#xff1a;亿速云阅读&#xff1a;97作者&#xff1a;Leah在java项目中如何使用Timer定时器&#xff1f;很多新手对此不是很清楚&#xff0c;为了帮助大家解决这个难题&#xff0c;下面小编将为大…

慎使用sql的enum字段类型

在sql的优化中&#xff0c;会有同学提到一点&#xff1a;使用enum字段类型&#xff0c;代替其他tinyint等类型。以前这也是不少人喜欢优化的&#xff0c;但是现在细想&#xff0c;是非常不合理的。 优点&#xff1a; 1.可以设置区间范围&#xff0c;比如设置性别&#xff1a;1男…

js对HTML字符转义与反转义

注意&#xff1a; 在编写html时&#xff0c;经常需要转义&#xff0c;才能正常显示在页面上。 并且&#xff0c;还可以防止xss。 解决方案&#xff1a; 一&#xff0c; 使用正则&#xff1a; 使用正则转码&#xff1a; var value document.getElementById(input).value.t…

python三维数据图_matplotlib中三维数据的热图

我想用我的三维数据生成一张热图。在我已经能够用这些数据绘制出trisurf。在有人能帮我制作热图吗&#xff1f;我看到了在线教程&#xff0c;但是它们对3D来说都很复杂&#xff0c;我在这个网站上找到了一个在matplotlib中生成带有散点的热图&#xff0c;但是它只有2D数据。在我…

区分基于Ant目标的Gradle任务

在我的博客文章《 从Ant Build演变Gradle构建&#xff1a;导入Ant构建文件》中 &#xff0c;我演示了如何使用Gradle内置的基于AntBuilder的Ant支持在Gradle构建中导入Ant目标。 然后&#xff0c;可以将这些Ant目标作为Gradle任务进行访问&#xff0c;并与Gradle构建直接引入的…

java显示长度和第一个字符_从Java字符串中以长度1的字符串返回的第一个字母的最佳方法是什么?...

假设以下内容&#xff1a;String example "something";String firstLetter "";以下分配方式firstLetter可能会影响性能&#xff0c;请注意是否存在差异&#xff1b; 哪个最好&#xff0c;为什么&#xff1f;firstLetter String.valueOf(example.charAt(…

sequelize difference between hasone and hasmany

Query is equal, hasone return the first instance from the collection,hasmany return the whole collection. 转载于:https://www.cnblogs.com/ybleeho/p/9772902.html

localStorage封装借口store.js的使用

localstorage是 HTML5 提供的在客户端存储数据的新方法&#xff0c;主要作用是将数据保存在客户端中&#xff0c;并且数据是永久保存的&#xff0c;除非人为干预删除。localstorage 的局限 1、只有版本较高的浏览器中才支持 localstorage2、localStorage的值的类型限定为strin…

AmazonSQS和Spring用于消息传递队列

下一篇文章将演示如何将Spring JMS模板和DLMC与AmazonSQS API一起使用&#xff0c;以放置消息队列。 我为什么要使用Amazon SQS&#xff1f; 易于配置 跨平台支持 从您的自我冗余&#xff0c;连带和扩展方面的烦恼中赚钱。 为什么我不使用Amazon SQS&#xff1f; 如果延迟…

3.3-3.9 周记

3.3-3.10 1. NIM游戏 百度链接&#xff1a;https://baike.baidu.com/item/Nim%E6%B8%B8%E6%88%8F/6737105?fraladdin 定义&#xff1a; P局面&#xff1a;先手必败N局面&#xff1a;先手必胜P局面的所有子局面都是N局面。N局面的子局面中必有一个是P局面 性质&#xff1a;\(a_…

关于类的使用的几个关键

类的定义和声明必须放在main函数前 如果类中只有申明类而没有定义&#xff0c;则只能定义指针&#xff1a;Test *test&#xff1b;如果不定义类而仅仅声明类的话&#xff0c;当使用Test test时&#xff0c;编译器只知道Test是个class&#xff0c;但留多大空间&#xff1f;怎么初…

java判断对象已死_Java的JVM判断对象已死的基本算法分析

jvm中有各种的垃圾收集器&#xff0c;每个收集器都有各自的算法。但是一切的根本都需要找到找到应该被消除的对象&#xff0c;理解如何找到死亡对象才是理解垃圾收集器的基础。01两个基本算法a、引用记数法&#xff1a;对象中加一个引用计数器&#xff0c;每次被引用计数器加一…

Java开发中更多常见的危险信号

在《 Java开发中的常见危险信号》一文中&#xff0c;我研究了一些不一定本身就是错误或不正确的做法&#xff0c;但它们可能表明存在更大的问题。 这些“红色标记”类似于“代码气味”的概念&#xff0c;我在这篇文章中引用的某些特定“红色标记”被称为“代码气味”。 正如我在…

python平均分由高到低顺序排列输出选手编号和最后得分_python 字典的使用案例二:求平均分,并按平均分由高到低输出选手编号和最后得分...

校园好声音大赛&#xff0c;三位选手得分&#xff0c;由8位评委给出。请根据评分表&#xff0c;将每们选手的得分去掉一个最高分和一个最低分后求平均分&#xff0c;并按照平均分由高到低输出选手编号和最后得分。dicScores {012: [90, 94, 97, 86, 85, 89, 88, 85], 005: [91…

java开发 职业技能_java编程开发程序员需要具备哪些职业技能

随着互联网的不断发展&#xff0c;java编程开发可以说是目前学习人数和应用范围非常多的一种编程语言了&#xff0c;而今天我们就一起来了解一下&#xff0c;java编程开发程序员需要具备哪些职业技能。1、数据结构和算法分析数据结构和算法分析&#xff0c;对于一名程序员来说&…

Django model转字典的几种方法

平常的开发过程中不免遇到需要把model转成字典的需求&#xff0c;尤其是现在流行前后端分离架构&#xff0c;Json格式几乎成了前后端之间数据交换的标准&#xff0c;这种model转dict的需求就更多了&#xff0c;本文介绍几种日常使用的方法以供参考&#xff0c;所有例子均基于Dj…

微信小程序日历课表

最近项目中使用到了日历&#xff0c;在网上找了一些参考&#xff0c;自己改改,先看效果图 wxml <view class"date"><image class"direction" src"/images/icon/left.png" bindtapminusMouth/><label>{{year}}年{{mouth}}月<…

Java:将条件移至消息文件

Java类ResourceBundle和MessageFormat提供了一个很好的工具集&#xff0c;用于解决Java应用程序内部的本地化消息。 这篇文章提供了一个小示例&#xff0c;说明如何使用ChoiceFormat将与消息相关的简单条件从Java代码移动到消息文件中。 如果您已经知道ChoiceFormat我认为您不会…

【LuoguP3241】[HNOI2015] 开店

题目链接 题意 给出一棵边带权的树&#xff0c;多次在线询问一个点到一个区间内的点的距离和。 Sol 分块过不了的 一个 trick &#xff0c;都知道要算两点之间距离可以拆成到根的距离和他们的 LCA 到根的距离 &#xff0c;其实要算多个点到一个点距离也可以使用一个类似的 tric…