本文主要是介绍leetcode08-棒球比赛,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:
https://leetcode.cn/problems/baseball-game/description/?envType=study-plan-v2&envId=programming-skills
思路:
模拟题,思路见代码即可。
代码:
class Solution {public int calPoints(String[] operations) {int[] sum = new int[1010];int n = operations.length;int idx= 0,res = 0;//进行模拟for(int i = 0;i<n;i++,idx++) {if(operations[i].equals("+")) {sum[idx] = sum[idx-1]+sum[idx-2];}else if(operations[i].equals("D")) {sum[idx] = sum[idx-1]*2;}else if(operations[i].equals("C")) {idx-=2;}else {sum[idx] = Integer.parseInt(operations[i]);}}for(int i =0;i<idx;i++)res+=sum[i];return res;}
}
这篇关于leetcode08-棒球比赛的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!