# LeetCode: 682. 棒球比赛¶

## 1、题目描述¶

输入: ["5","2","C","D","+"]



输入: ["5","-2","4","C","D","9","+","+"]



• 输入列表的大小将介于1和1000之间。
• 列表中的每个整数都将介于-30000和30000之间。

## 2、解题思路¶

int sToNum(char *s) {

int result = 0;
int left = 0;
int sign = 1;
if (s[0] == '-') {
sign = -1;
left = 1;
}

for (int i = left; i < strlen(s); i++) {
result = result * 10 + s[i] - '0';
}

return result * sign;

}

int calPoints(char** ops, int opsSize) {
// 标识无效数据
const int MIN = -1000000;
int *result = (int *) malloc(sizeof(int) * opsSize);

// 初始化
for (int i = 0; i < opsSize; i++) {
result[i] = MIN;
}

int first = MIN;
int second = MIN;

int j;
for (int i = 0; i < opsSize; i++) {

switch (ops[i][0]) {
case 'C':
j = i;
while (result[j] == MIN) {
j--;
}
if (j >= 0) {
result[j] = MIN;
}

break;
case 'D':
j = i;
while (result[j] == MIN) {
j--;
}
if (j >= 0) {
result[i] = 2 * result[j];
}
break;
case '+':
first = MIN;
second = MIN;

j = i;
while (result[j] == MIN) {
j--;
}
first = result[j];
j--;
while (result[j] == MIN) {
j--;
}
second = result[j];
result[i] = first + second;

break;
default:
result[i] = sToNum(ops[i]);
break;
}

}

int ans = 0;

for (int i = 0; i < opsSize; i++) {
if (result[i] != MIN) {
ans += result[i];
}
printf("%d\n",result[i]);
}
return ans;
}