Skip to content

Commit a0ff92c

Browse files
committed
python-practice
1 parent 5af6d27 commit a0ff92c

2 files changed

Lines changed: 270 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"你现在是棒球比赛记录员。\n",
9+
"给定一个字符串列表,每个字符串可以是以下四种类型之一:\n",
10+
"\n",
11+
"1. 整数(一轮的得分):直接表示您在本轮中获得的积分数。\n",
12+
"2. \"+\"(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。\n",
13+
"3. \"D\"(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。\n",
14+
"4. \"C\"(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除。\n",
15+
"\n",
16+
"每一轮的操作都是永久性的,可能会对前一轮和后一轮产生影响。\n",
17+
"你需要返回你在所有回合中得分的总和。\n",
18+
"\n",
19+
"---\n",
20+
"示例 1:\n",
21+
"\n",
22+
"输入: [\"5\",\"2\",\"C\",\"D\",\"+\"]\n",
23+
"\n",
24+
"输出: 30\n",
25+
"\n",
26+
"解释: \n",
27+
"\n",
28+
"第1轮:你可以得到5分。总和是:5。\n",
29+
"\n",
30+
"第2轮:你可以得到2分。总和是:7。\n",
31+
"\n",
32+
"操作1:第2轮的数据无效。总和是:5。\n",
33+
"\n",
34+
"第3轮:你可以得到10分(第2轮的数据已被删除)。总数是:15。\n",
35+
"\n",
36+
"第4轮:你可以得到5 + 10 = 15分。总数是:30。\n",
37+
"\n",
38+
"#### 思路:\n",
39+
"使用栈的思想:\n",
40+
"- 如果是整数,压栈\n",
41+
"- 如果是操作符,按对应的运算处理"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 2,
47+
"metadata": {
48+
"ExecuteTime": {
49+
"end_time": "2019-07-01T01:48:00.107253Z",
50+
"start_time": "2019-07-01T01:48:00.090827Z"
51+
}
52+
},
53+
"outputs": [
54+
{
55+
"data": {
56+
"text/plain": [
57+
"30"
58+
]
59+
},
60+
"execution_count": 2,
61+
"metadata": {},
62+
"output_type": "execute_result"
63+
}
64+
],
65+
"source": [
66+
"class Solution(object):\n",
67+
" def calPoints(self, ops):\n",
68+
" \"\"\"\n",
69+
" :type ops: List[str]\n",
70+
" :rtype: int\n",
71+
" \"\"\"\n",
72+
" stack = []\n",
73+
" for op in ops:\n",
74+
" if op in \"CD+\":\n",
75+
" self.cal(stack, op)\n",
76+
" else:\n",
77+
" stack.append(int(op))\n",
78+
" return sum(stack)\n",
79+
" \n",
80+
" def cal(self, stack, op):\n",
81+
" if op == \"C\":\n",
82+
" if stack :stack.pop()\n",
83+
" if op == \"D\":\n",
84+
" stack.append(stack[-1]*2)\n",
85+
" if op == \"+\":\n",
86+
" stack.append(stack[-1]+stack[-2])\n",
87+
" \n",
88+
"ops = [\"5\",\"2\",\"C\",\"D\",\"+\"]\n",
89+
"solution = Solution()\n",
90+
"solution.calPoints(ops)"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": []
99+
}
100+
],
101+
"metadata": {
102+
"kernelspec": {
103+
"display_name": "python pratice",
104+
"language": "python",
105+
"name": "env_name"
106+
},
107+
"language_info": {
108+
"codemirror_mode": {
109+
"name": "ipython",
110+
"version": 3
111+
},
112+
"file_extension": ".py",
113+
"mimetype": "text/x-python",
114+
"name": "python",
115+
"nbconvert_exporter": "python",
116+
"pygments_lexer": "ipython3",
117+
"version": "3.7.1"
118+
},
119+
"toc": {
120+
"base_numbering": 1,
121+
"nav_menu": {},
122+
"number_sections": true,
123+
"sideBar": true,
124+
"skip_h1_title": false,
125+
"title_cell": "Table of Contents",
126+
"title_sidebar": "Contents",
127+
"toc_cell": false,
128+
"toc_position": {},
129+
"toc_section_display": true,
130+
"toc_window_display": false
131+
}
132+
},
133+
"nbformat": 4,
134+
"nbformat_minor": 2
135+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"你现在是棒球比赛记录员。\n",
9+
"给定一个字符串列表,每个字符串可以是以下四种类型之一:\n",
10+
"\n",
11+
"1. 整数(一轮的得分):直接表示您在本轮中获得的积分数。\n",
12+
"2. \"+\"(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。\n",
13+
"3. \"D\"(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。\n",
14+
"4. \"C\"(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除。\n",
15+
"\n",
16+
"每一轮的操作都是永久性的,可能会对前一轮和后一轮产生影响。\n",
17+
"你需要返回你在所有回合中得分的总和。\n",
18+
"\n",
19+
"---\n",
20+
"示例 1:\n",
21+
"\n",
22+
"输入: [\"5\",\"2\",\"C\",\"D\",\"+\"]\n",
23+
"\n",
24+
"输出: 30\n",
25+
"\n",
26+
"解释: \n",
27+
"\n",
28+
"第1轮:你可以得到5分。总和是:5。\n",
29+
"\n",
30+
"第2轮:你可以得到2分。总和是:7。\n",
31+
"\n",
32+
"操作1:第2轮的数据无效。总和是:5。\n",
33+
"\n",
34+
"第3轮:你可以得到10分(第2轮的数据已被删除)。总数是:15。\n",
35+
"\n",
36+
"第4轮:你可以得到5 + 10 = 15分。总数是:30。\n",
37+
"\n",
38+
"#### 思路:\n",
39+
"使用栈的思想:\n",
40+
"- 如果是整数,压栈\n",
41+
"- 如果是操作符,按对应的运算处理"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 2,
47+
"metadata": {
48+
"ExecuteTime": {
49+
"end_time": "2019-07-01T01:48:00.107253Z",
50+
"start_time": "2019-07-01T01:48:00.090827Z"
51+
}
52+
},
53+
"outputs": [
54+
{
55+
"data": {
56+
"text/plain": [
57+
"30"
58+
]
59+
},
60+
"execution_count": 2,
61+
"metadata": {},
62+
"output_type": "execute_result"
63+
}
64+
],
65+
"source": [
66+
"class Solution(object):\n",
67+
" def calPoints(self, ops):\n",
68+
" \"\"\"\n",
69+
" :type ops: List[str]\n",
70+
" :rtype: int\n",
71+
" \"\"\"\n",
72+
" stack = []\n",
73+
" for op in ops:\n",
74+
" if op in \"CD+\":\n",
75+
" self.cal(stack, op)\n",
76+
" else:\n",
77+
" stack.append(int(op))\n",
78+
" return sum(stack)\n",
79+
" \n",
80+
" def cal(self, stack, op):\n",
81+
" if op == \"C\":\n",
82+
" if stack :stack.pop()\n",
83+
" if op == \"D\":\n",
84+
" stack.append(stack[-1]*2)\n",
85+
" if op == \"+\":\n",
86+
" stack.append(stack[-1]+stack[-2])\n",
87+
" \n",
88+
"ops = [\"5\",\"2\",\"C\",\"D\",\"+\"]\n",
89+
"solution = Solution()\n",
90+
"solution.calPoints(ops)"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": []
99+
}
100+
],
101+
"metadata": {
102+
"kernelspec": {
103+
"display_name": "python pratice",
104+
"language": "python",
105+
"name": "env_name"
106+
},
107+
"language_info": {
108+
"codemirror_mode": {
109+
"name": "ipython",
110+
"version": 3
111+
},
112+
"file_extension": ".py",
113+
"mimetype": "text/x-python",
114+
"name": "python",
115+
"nbconvert_exporter": "python",
116+
"pygments_lexer": "ipython3",
117+
"version": "3.7.1"
118+
},
119+
"toc": {
120+
"base_numbering": 1,
121+
"nav_menu": {},
122+
"number_sections": true,
123+
"sideBar": true,
124+
"skip_h1_title": false,
125+
"title_cell": "Table of Contents",
126+
"title_sidebar": "Contents",
127+
"toc_cell": false,
128+
"toc_position": {},
129+
"toc_section_display": true,
130+
"toc_window_display": false
131+
}
132+
},
133+
"nbformat": 4,
134+
"nbformat_minor": 2
135+
}

0 commit comments

Comments
 (0)