682. Baseball Game

来源:互联网 发布:修改apache配置文件 编辑:程序博客网 时间:2024/04/30 04:53

原题链接

import java.util.LinkedList;/** * Created by Joe on 2017/12/12. * https://leetcode.com/problems/baseball-game/discuss/ */public class P682 {    public int calPoints(String[] ops) {        int sum = 0;        LinkedList<Integer> list = new LinkedList<>();        for (String op : ops) {            if (op.equals("C")) {                sum -= list.removeLast();            }            else if (op.equals("D")) {                list.add(list.peekLast() * 2);                sum += list.peekLast();            }            else if (op.equals("+")) {                list.add(list.peekLast() + list.get(list.size() - 2));                sum += list.peekLast();            }            else {                list.add(Integer.parseInt(op));                sum += list.peekLast();            }        }        return sum;    }}

注:
欠缺是对java现有集合类的api熟悉程度。
这里新学习的方法为peekLast()