-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path682.cpp
92 lines (91 loc) · 2.5 KB
/
682.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
int calPoints(vector<string>& ops) {
std::stack<int> opS;
int sum {0};
for (const auto& op : ops) {
if (op == "C") {
if (!opS.empty()) {
int top = opS.top();
sum -= top;
opS.pop();
}
} else if (op == "D") {
if (!opS.empty()) {
int top = opS.top();
opS.push(top*2);
sum += (opS.top());
}
} else if (op == "+") {
int top0 {0}, top0Flag {0};
int top1 {0}, top1Flag {0};
if (!opS.empty()) {
top0 = opS.top();
top0Flag = 1;
opS.pop();
}
if (!opS.empty()) {
top1 = opS.top();
top1Flag = 1;
opS.pop();
}
if (top1Flag == 1) {
opS.push(top1);
}
if (top0Flag == 1) {
opS.push(top0);
}
opS.push(top0 + top1);
sum += opS.top();
} else {
int opV = std::stoi(op);
sum += opV;
opS.push(opV);
}
}
return sum;
}
};
static int _ = [](){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
return 0;
}();
__________________________________________________________________________________________________
sample 9316 kb submission
class Solution {
public:
int calPoints(vector<string>& ops) {
int n=ops.size();
int arr[n];
int top=-1;
for(int i=0;i<n;i++)
{
if(ops[i]=="C")
top--;
else if(ops[i]=="D")
{
arr[top+1]=2*arr[top];
top++;
}
else if(ops[i]=="+")
{
arr[top+1]=arr[top]+arr[top-1];
top++;
}
else
arr[++top]=stoi(ops[i]);
}
int sum=0;
for(int i=0;i<=top;i++)
{
sum+=arr[i];
}
return sum;
}
};
__________________________________________________________________________________________________