zoukankan      html  css  js  c++  java
  • LeetCode-682 Baseball Game Solution (with Java)

    1. Description:

     Notes:

    2. Examples:

    3.Solutions:

     1 /**
     2  * Created by sheepcore on 2018-12-24
     3  */
     4 class Solution {
     5     public int calPoints(String[] ops) {
     6         Stack<Integer> numStack = new Stack<>();
     7         String op;
     8         for(int i = 0; i < ops.length; i++){
     9             op = ops[i];
    10             if(op.matches("-?\d+"))
    11                 numStack.push(Integer.parseInt(op));
    12 
    13             //cut off the point in last round
    14             else if(op.equals("C") || op.equals("c"))
    15                 numStack.pop();
    16             //double the point in last round
    17             else if(op.equals("D") || op.equals("d")){
    18                 int top = numStack.peek();
    19                 numStack.push((top << 1));
    20             }
    21             //add the last two round numbers
    22             else if(op.equals("+")){
    23                 int top = numStack.pop();
    24                 int stop = numStack.peek();
    25                 numStack.push(top);
    26                 numStack.push(top + stop);
    27             }
    28             //go cycling if not reach the end
    29             else
    30                 continue;
    31         }
    32         int totalPoints = 0;
    33         Iterator<Integer> iterator = numStack.iterator();
    34         while(iterator.hasNext()){
    35             totalPoints += iterator.next();
    36         }
    37         return totalPoints;
    38     }
    39 }
  • 相关阅读:
    《TD项目开发小结》
    感悟
    毕业两年了
    ip+port无法访问nginx问题
    问题解决之道
    调休9天的那些日子
    关于类加载器(ClassLoader)
    ios核心蓝牙之心率监控(swift)
    git(git-flow)的高效管理使用
    WKWebview加载本地图片时出现路径问题
  • 原文地址:https://www.cnblogs.com/sheepcore/p/12396514.html
Copyright © 2011-2022 走看看