LeetCode--Baseball Game

思路:

    先将数组转化为list,然后按逻辑遍历

class Solution {
    public int calPoints(String[] ops) {
        List<String> list= Arrays.asList(ops);
        List<String> nums=new ArrayList<String>(list);
        int i=0;
        int sum=0;
        while(i<nums.size()){
            String num=nums.get(i);
            if(num.equals("C")){
                sum-=Integer.parseInt(nums.get(i-1));
                nums.remove(i);
                nums.remove(i-1);
                i--;
            }
            else if(num.equals("D")){
                nums.set(i,""+Integer.parseInt(nums.get(i-1))*2);
                sum+=Integer.parseInt(nums.get(i));
                i++;
            }
            else if(num.equals("+")){
                nums.set(i,""+(Integer.parseInt(nums.get(i-1))+Integer.parseInt(nums.get(i-2))));
                sum+=Integer.parseInt(nums.get(i));
                i++;
            }
            else{
                sum+=Integer.parseInt(nums.get(i));
                i++;
            }
        }

        return sum;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_21752135/article/details/80224153