日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/1 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode682. 棒球比赛 | Baseball Game 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:?https://www.cnblogs.com/strengthen/p/10499508.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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: 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.

    你現(xiàn)在是棒球比賽記錄員。
    給定一個(gè)字符串列表,每個(gè)字符串可以是以下四種類型之一:
    1.整數(shù)(一輪的得分):直接表示您在本輪中獲得的積分?jǐn)?shù)。
    2.?"+"(一輪的得分):表示本輪獲得的得分是前兩輪有效?回合得分的總和。
    3.?"D"(一輪的得分):表示本輪獲得的得分是前一輪有效?回合得分的兩倍。
    4.?"C"(一個(gè)操作,這不是一個(gè)回合的分?jǐn)?shù)):表示您獲得的最后一個(gè)有效?回合的分?jǐn)?shù)是無效的,應(yīng)該被移除。

    每一輪的操作都是永久性的,可能會(huì)對(duì)前一輪和后一輪產(chǎn)生影響。
    你需要返回你在所有回合中得分的總和。

    示例 1:

    輸入: ["5","2","C","D","+"] 輸出: 30 解釋: 第1輪:你可以得到5分。總和是:5。 第2輪:你可以得到2分。總和是:7。 操作1:第2輪的數(shù)據(jù)無效。總和是:5。 第3輪:你可以得到10分(第2輪的數(shù)據(jù)已被刪除)。總數(shù)是:15。 第4輪:你可以得到5 + 10 = 15分。總數(shù)是:30。

    示例 2:

    輸入: ["5","-2","4","C","D","9","+","+"] 輸出: 27 解釋: 第1輪:你可以得到5分。總和是:5。 第2輪:你可以得到-2分。總數(shù)是:3。 第3輪:你可以得到4分。總和是:7。 操作1:第3輪的數(shù)據(jù)無效。總數(shù)是:3。 第4輪:你可以得到-4分(第三輪的數(shù)據(jù)已被刪除)。總和是:-1。 第5輪:你可以得到9分。總數(shù)是:8。 第6輪:你可以得到-4 + 9 = 5分。總數(shù)是13。 第7輪:你可以得到9 + 5 = 14分。總數(shù)是27。

    注意:

    • 輸入列表的大小將介于1和1000之間。
    • 列表中的每個(gè)整數(shù)都將介于-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 }

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/10499508.html

    總結(jié)

    以上是生活随笔為你收集整理的[Swift]LeetCode682. 棒球比赛 | Baseball Game的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 午夜精品久久久久久久久久久 | 国产精品久久久久久亚洲调教 | 91免费大片| 漂亮人妻被中出中文字幕 | 免费看黄色的视频 | 97人人爽| 亚洲少妇第一页 | 91久久婷婷| 中文字幕一区二区三区电影 | www日本高清 | 色永久 | 99久久精品一区二区成人 | 蜜臀视频在线观看 | 豆国产97在线 | 亚洲 | 精品一区二区亚洲 | 97视频国产 | 美女羞羞动态图 | 国产嘿咻视频 | 免费久久久久久 | 国产欧美综合一区 | 毛片毛片毛片毛片毛片毛片毛片毛片毛片 | 成人在线免费视频观看 | 久久久久久毛片 | 久久黄色网| 操她视频网站 | 亚洲区小说| 96精品国产 | 国产稀缺精品盗摄盗拍 | 大胸美女无遮挡 | 极品少妇xxxx精品少妇偷拍 | 波多野结av衣东京热无码专区 | 97高清国语自产拍 | 性高潮视频在线观看 | 97成人精品视频在线观看 | 小柔好湿好紧太爽了国产网址 | 操碰在线视频 | 免费a v网站 | 久久国产精品免费 | 欲色网站 | 日本在线加勒比 | 欧美调教视频 | 色亚洲色图 | 有码在线播放 | 国产成人亚洲精品无码h在线 | 天天干b| 久久精品无码专区免费 | 在线免费观看一级片 | 久久综合欧美 | 在线资源站 | 久久精品人人爽 | 天天操夜夜操夜夜操 | 88av在线播放| 亚洲免费视频一区二区 | 亚洲黑丝在线 | 欧美人妖69xxxxxhd3d | 懂色av一区二区三区四区五区 | 99热91| 天天躁日日躁狠狠躁av麻豆 | 在线播放少妇奶水过盛 | 欧美三级免费观看 | 少妇做爰k8经典 | 激情av综合 | chinese麻豆gay勾外卖 | 污到下面流水的视频 | 国产一区二区视频在线 | 波多野结衣视频在线看 | 国产一区二区三区在线观看免费 | 国产色悠悠 | 久久精品国产亚洲av久 | 老熟妇高潮一区二区三区 | 欧美高清不卡 | 免费淫片| 中文字幕在线导航 | 国产91色在线 | 国产91色| 欧美日本一区二区三区 | 亚洲熟妇无码乱子av电影 | 91黄址| 日韩欧美中文字幕一区二区三区 | 国产第八页 | 青青在线免费观看 | 欧美肥老妇视频九色 | 国产精品久久久久久久久免费相片 | 老妇高潮潮喷到猛进猛出 | 成人在线观 | 色哟哟av | 久久久香蕉 | 日韩夜夜高潮夜夜爽无码 | 国产做受高潮动漫 | 欧美怡红院一区二区三区 | 四房婷婷 | 成人毛片视频在线观看 | 久久合合 | 让男按摩师摸好爽 | 男人的天堂久久久 | 日本三级在线 | www.啪啪 | 欧美三级韩国三级日本三斤 | www.av免费 |