Swift Solution


  • 0
    J
    class Solution {
        func calPoints(_ ops: [String]) -> Int {
    
            var round = 0
            var arr:[Int] = []
            while round < ops.count{
                switch ops[round] {
                case "+":
                    arr.append(arr[arr.count - 1] + arr[arr.count - 2])
                case "D":
                    arr.append(arr[arr.count - 1] * 2)
                case "C":
                    arr.removeLast()
                default:
                    arr.append(Int(ops[round])!)
                }
                round += 1
            }
            return arr.reduce(0, {
                $0 + $1
            })
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.