File tree 4 files changed +113
-4
lines changed
solution/0600-0699/0682.Baseball Game
4 files changed +113
-4
lines changed Original file line number Diff line number Diff line change 53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
+ 栈实现。
57
+
56
58
<!-- tabs:start -->
57
59
58
60
### ** Python3**
59
61
60
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
63
62
64
``` python
63
-
65
+ class Solution :
66
+ def calPoints (self , ops : List[str ]) -> int :
67
+ stack = []
68
+ for op in ops:
69
+ if op == ' C' :
70
+ stack.pop()
71
+ elif op == ' D' :
72
+ stack.append(stack[- 1 ] << 1 )
73
+ elif op == ' +' :
74
+ stack.append(stack[- 1 ] + stack[- 2 ])
75
+ else :
76
+ stack.append(int (op))
77
+ return sum (stack)
64
78
```
65
79
66
80
### ** Java**
67
81
68
82
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
83
70
84
``` java
71
-
85
+ class Solution {
86
+ public int calPoints (String [] ops ) {
87
+ Deque<Integer > stack = new ArrayDeque<> ();
88
+ for (String op : ops) {
89
+ if (" C" . equals(op)) {
90
+ stack. pop();
91
+ } else if (" D" . equals(op)) {
92
+ stack. push(stack. peek() << 1 );
93
+ } else if (" +" . equals(op)) {
94
+ Integer a = stack. pop();
95
+ Integer b = stack. peek();
96
+ stack. push(a);
97
+ stack. push(a + b);
98
+ } else {
99
+ stack. push(Integer . valueOf(op));
100
+ }
101
+ }
102
+ int res = 0 ;
103
+ for (Integer score : stack) {
104
+ res += score;
105
+ }
106
+ return res;
107
+ }
108
+ }
72
109
```
73
110
74
111
### ** ...**
Original file line number Diff line number Diff line change @@ -109,13 +109,48 @@ Round 7: You could get 9 + 5 = 14 points. The sum is 27.
109
109
### ** Python3**
110
110
111
111
``` python
112
-
112
+ class Solution :
113
+ def calPoints (self , ops : List[str ]) -> int :
114
+ stack = []
115
+ for op in ops:
116
+ if op == ' C' :
117
+ stack.pop()
118
+ elif op == ' D' :
119
+ stack.append(stack[- 1 ] << 1 )
120
+ elif op == ' +' :
121
+ stack.append(stack[- 1 ] + stack[- 2 ])
122
+ else :
123
+ stack.append(int (op))
124
+ return sum (stack)
113
125
```
114
126
115
127
### ** Java**
116
128
117
129
``` java
118
-
130
+ class Solution {
131
+ public int calPoints (String [] ops ) {
132
+ Deque<Integer > stack = new ArrayDeque<> ();
133
+ for (String op : ops) {
134
+ if (" C" . equals(op)) {
135
+ stack. pop();
136
+ } else if (" D" . equals(op)) {
137
+ stack. push(stack. peek() << 1 );
138
+ } else if (" +" . equals(op)) {
139
+ Integer a = stack. pop();
140
+ Integer b = stack. peek();
141
+ stack. push(a);
142
+ stack. push(a + b);
143
+ } else {
144
+ stack. push(Integer . valueOf(op));
145
+ }
146
+ }
147
+ int res = 0 ;
148
+ for (Integer score : stack) {
149
+ res += score;
150
+ }
151
+ return res;
152
+ }
153
+ }
119
154
```
120
155
121
156
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int calPoints (String [] ops ) {
3
+ Deque <Integer > stack = new ArrayDeque <>();
4
+ for (String op : ops ) {
5
+ if ("C" .equals (op )) {
6
+ stack .pop ();
7
+ } else if ("D" .equals (op )) {
8
+ stack .push (stack .peek () << 1 );
9
+ } else if ("+" .equals (op )) {
10
+ Integer a = stack .pop ();
11
+ Integer b = stack .peek ();
12
+ stack .push (a );
13
+ stack .push (a + b );
14
+ } else {
15
+ stack .push (Integer .valueOf (op ));
16
+ }
17
+ }
18
+ int res = 0 ;
19
+ for (Integer score : stack ) {
20
+ res += score ;
21
+ }
22
+ return res ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def calPoints (self , ops : List [str ]) -> int :
3
+ stack = []
4
+ for op in ops :
5
+ if op == 'C' :
6
+ stack .pop ()
7
+ elif op == 'D' :
8
+ stack .append (stack [- 1 ] << 1 )
9
+ elif op == '+' :
10
+ stack .append (stack [- 1 ] + stack [- 2 ])
11
+ else :
12
+ stack .append (int (op ))
13
+ return sum (stack )
You can’t perform that action at this time.
0 commit comments