Skip to content

Commit e03aad9

Browse files
committed
2 parents 7723b25 + ddea22e commit e03aad9

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

python/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Leetcode challenge in python
2+
learn basic data structure and practice in python
3+
4+
## data structure
5+
* Queue [Link to python documentation](https://docs.python.org/3/library/queue.html)
6+
* Stack
7+
* List
8+
* Dict(Map)
9+
* Tree
10+
11+
## algrithm highlight
12+
### binary tree dfs
13+
```python
14+
def preorder(root):
15+
if root:
16+
return [root.val] + preorder(root.left) + preorder(root.right)
17+
else:
18+
return []
19+
```
20+
21+
## Environment setup
22+
### python installation
23+
```bash
24+
pipenv install
25+
pipenv shell
26+
```
27+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.ascending.training.algorithm;
2+
3+
import java.util.Stack;
4+
5+
public class BaseBallWarmUp {
6+
public int calPoints(String[] ops) {
7+
int sum=0;
8+
Stack<Integer> history = new Stack();
9+
for(String str: ops){
10+
Integer currentPoint;
11+
switch (str) {
12+
case "+":
13+
if(history.size()>1){
14+
Integer last = history.pop();
15+
Integer lastLast = history.peek();
16+
currentPoint = last+lastLast;
17+
sum+=currentPoint;
18+
history.push(last);
19+
history.push(currentPoint);
20+
}
21+
break;
22+
case "D":
23+
if(history.size()>0){
24+
Integer lastRound = history.peek();
25+
currentPoint = lastRound*2;
26+
sum+=currentPoint;
27+
history.push(currentPoint);
28+
}
29+
break;
30+
case "C":
31+
if(history.size()>0){
32+
currentPoint = history.pop();
33+
sum-=currentPoint;
34+
}
35+
break;
36+
default:
37+
currentPoint = Integer.valueOf(str);
38+
sum+=currentPoint;
39+
history.push(currentPoint);
40+
break;
41+
}
42+
// System.out.println("current round:"+sum);
43+
}
44+
return sum;
45+
}
46+
public static void main(String[] args){
47+
String[] vals = {"5","-2","4","C","D","9","+","+"};
48+
BaseBallWarmUp bbwp = new BaseBallWarmUp();
49+
System.out.println(bbwp.calPoints(vals));
50+
}
51+
}

0 commit comments

Comments
 (0)