Skip to content

Commit 23f645d

Browse files
committed
python-practice
1 parent 7e0d872 commit 23f645d

2 files changed

Lines changed: 242 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"使用队列实现栈的下列操作:\n",
9+
"\n",
10+
"push(x) -- 元素 x 入栈\n",
11+
"\n",
12+
"pop() -- 移除栈顶元素\n",
13+
"\n",
14+
"top() -- 获取栈顶元素\n",
15+
"\n",
16+
"empty() -- 返回栈是否为空\n"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"metadata": {
23+
"ExecuteTime": {
24+
"end_time": "2019-06-30T16:05:54.767128Z",
25+
"start_time": "2019-06-30T16:05:54.760572Z"
26+
}
27+
},
28+
"outputs": [],
29+
"source": [
30+
"class MyStack(object):\n",
31+
"\n",
32+
" def __init__(self):\n",
33+
" \"\"\"\n",
34+
" Initialize your data structure here.\n",
35+
" \"\"\"\n",
36+
" self.stack = []\n",
37+
"\n",
38+
" def push(self, x):\n",
39+
" \"\"\"\n",
40+
" Push element x onto stack.\n",
41+
" :type x: int\n",
42+
" :rtype: None\n",
43+
" \"\"\"\n",
44+
" self.stack.append(x)\n",
45+
"\n",
46+
" def pop(self):\n",
47+
" \"\"\"\n",
48+
" Removes the element on top of the stack and returns that element.\n",
49+
" :rtype: int\n",
50+
" \"\"\"\n",
51+
" return self.stack.pop()\n",
52+
" \n",
53+
"\n",
54+
" def top(self):\n",
55+
" \"\"\"\n",
56+
" Get the top element.\n",
57+
" :rtype: int\n",
58+
" \"\"\"\n",
59+
" return self.stack[-1]\n",
60+
" \n",
61+
"\n",
62+
" def empty(self):\n",
63+
" \"\"\"\n",
64+
" Returns whether the stack is empty.\n",
65+
" :rtype: bool\n",
66+
" \"\"\"\n",
67+
" return False if self.stack else True\n",
68+
" \n",
69+
"\n",
70+
"\n",
71+
"# Your MyStack object will be instantiated and called as such:\n",
72+
"# obj = MyStack()\n",
73+
"# obj.push(x)\n",
74+
"# param_2 = obj.pop()\n",
75+
"# param_3 = obj.top()\n",
76+
"# param_4 = obj.empty()"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": []
85+
}
86+
],
87+
"metadata": {
88+
"kernelspec": {
89+
"display_name": "python pratice",
90+
"language": "python",
91+
"name": "env_name"
92+
},
93+
"language_info": {
94+
"codemirror_mode": {
95+
"name": "ipython",
96+
"version": 3
97+
},
98+
"file_extension": ".py",
99+
"mimetype": "text/x-python",
100+
"name": "python",
101+
"nbconvert_exporter": "python",
102+
"pygments_lexer": "ipython3",
103+
"version": "3.7.1"
104+
},
105+
"toc": {
106+
"base_numbering": 1,
107+
"nav_menu": {},
108+
"number_sections": true,
109+
"sideBar": true,
110+
"skip_h1_title": false,
111+
"title_cell": "Table of Contents",
112+
"title_sidebar": "Contents",
113+
"toc_cell": false,
114+
"toc_position": {},
115+
"toc_section_display": true,
116+
"toc_window_display": false
117+
}
118+
},
119+
"nbformat": 4,
120+
"nbformat_minor": 2
121+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"使用队列实现栈的下列操作:\n",
9+
"\n",
10+
"push(x) -- 元素 x 入栈\n",
11+
"\n",
12+
"pop() -- 移除栈顶元素\n",
13+
"\n",
14+
"top() -- 获取栈顶元素\n",
15+
"\n",
16+
"empty() -- 返回栈是否为空\n"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"metadata": {
23+
"ExecuteTime": {
24+
"end_time": "2019-06-30T16:05:54.767128Z",
25+
"start_time": "2019-06-30T16:05:54.760572Z"
26+
}
27+
},
28+
"outputs": [],
29+
"source": [
30+
"class MyStack(object):\n",
31+
"\n",
32+
" def __init__(self):\n",
33+
" \"\"\"\n",
34+
" Initialize your data structure here.\n",
35+
" \"\"\"\n",
36+
" self.stack = []\n",
37+
"\n",
38+
" def push(self, x):\n",
39+
" \"\"\"\n",
40+
" Push element x onto stack.\n",
41+
" :type x: int\n",
42+
" :rtype: None\n",
43+
" \"\"\"\n",
44+
" self.stack.append(x)\n",
45+
"\n",
46+
" def pop(self):\n",
47+
" \"\"\"\n",
48+
" Removes the element on top of the stack and returns that element.\n",
49+
" :rtype: int\n",
50+
" \"\"\"\n",
51+
" return self.stack.pop()\n",
52+
" \n",
53+
"\n",
54+
" def top(self):\n",
55+
" \"\"\"\n",
56+
" Get the top element.\n",
57+
" :rtype: int\n",
58+
" \"\"\"\n",
59+
" return self.stack[-1]\n",
60+
" \n",
61+
"\n",
62+
" def empty(self):\n",
63+
" \"\"\"\n",
64+
" Returns whether the stack is empty.\n",
65+
" :rtype: bool\n",
66+
" \"\"\"\n",
67+
" return False if self.stack else True\n",
68+
" \n",
69+
"\n",
70+
"\n",
71+
"# Your MyStack object will be instantiated and called as such:\n",
72+
"# obj = MyStack()\n",
73+
"# obj.push(x)\n",
74+
"# param_2 = obj.pop()\n",
75+
"# param_3 = obj.top()\n",
76+
"# param_4 = obj.empty()"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": []
85+
}
86+
],
87+
"metadata": {
88+
"kernelspec": {
89+
"display_name": "python pratice",
90+
"language": "python",
91+
"name": "env_name"
92+
},
93+
"language_info": {
94+
"codemirror_mode": {
95+
"name": "ipython",
96+
"version": 3
97+
},
98+
"file_extension": ".py",
99+
"mimetype": "text/x-python",
100+
"name": "python",
101+
"nbconvert_exporter": "python",
102+
"pygments_lexer": "ipython3",
103+
"version": "3.7.1"
104+
},
105+
"toc": {
106+
"base_numbering": 1,
107+
"nav_menu": {},
108+
"number_sections": true,
109+
"sideBar": true,
110+
"skip_h1_title": false,
111+
"title_cell": "Table of Contents",
112+
"title_sidebar": "Contents",
113+
"toc_cell": false,
114+
"toc_position": {},
115+
"toc_section_display": true,
116+
"toc_window_display": false
117+
}
118+
},
119+
"nbformat": 4,
120+
"nbformat_minor": 2
121+
}

0 commit comments

Comments
 (0)