C# Is there a cleaner way to write this?

  • 0

    public class Solution {
    public int CalPoints(string[] ops) {

        var list = new List<string>();
        int sum=0;
        foreach(string s in ops){
            if(s == "C") list.RemoveAt(list.Count-1);
            else if(s == "D") list.Add(Convert.ToString(Int32.Parse(list[list.Count-1])*2));
            else if(s == "+") list.Add(Convert.ToString((Int32.Parse(list[list.Count-1]) + Int32.Parse(list[list.Count-2]))));
            else list.Add(s);
        foreach(string s in list)
            sum+= int.Parse(s);
        return sum;


Log in to reply

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