Skip to content

Commit a537cb8

Browse files
committed
python-practice
1 parent b596ac4 commit a537cb8

2 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。\n",
9+
"\n",
10+
"在 S 上反复执行重复项删除操作,直到无法继续删除。\n",
11+
"\n",
12+
"在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。\n",
13+
"\n",
14+
" \n",
15+
"\n",
16+
"示例:\n",
17+
"\n",
18+
"输入:\"abbaca\"\n",
19+
"\n",
20+
"输出:\"ca\"\n",
21+
"\n",
22+
"解释:\n",
23+
"\n",
24+
"例如,在 \"abbaca\" 中,我们可以删除 \"bb\" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 \"aaca\",其中又只有 \"aa\" 可以执行重复项删除操作,所以最后的字符串为 \"ca\"\n",
25+
"\n",
26+
"----\n",
27+
"#### 思路\n",
28+
"使用栈的思想\n",
29+
"- 遍历字符串\n",
30+
"- 字符串与栈顶元素进行比较,如果相等,就出栈\n",
31+
"- 遍历完,返回栈中元素"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 1,
37+
"metadata": {
38+
"ExecuteTime": {
39+
"end_time": "2019-07-01T02:21:29.071551Z",
40+
"start_time": "2019-07-01T02:21:29.054935Z"
41+
}
42+
},
43+
"outputs": [
44+
{
45+
"data": {
46+
"text/plain": [
47+
"'ca'"
48+
]
49+
},
50+
"execution_count": 1,
51+
"metadata": {},
52+
"output_type": "execute_result"
53+
}
54+
],
55+
"source": [
56+
"class Solution(object):\n",
57+
" def removeDuplicates(self, S):\n",
58+
" \"\"\"\n",
59+
" :type S: str\n",
60+
" :rtype: str\n",
61+
" \"\"\"\n",
62+
" # 初始化栈\n",
63+
" stack = []\n",
64+
" # 遍历栈元素\n",
65+
" for e in S:\n",
66+
" if stack and stack[-1] == e:\n",
67+
" stack.pop()\n",
68+
" else:\n",
69+
" stack.append(e)\n",
70+
" return \"\".join(stack)\n",
71+
" \n",
72+
" \n",
73+
"S = \"abbaca\"\n",
74+
"solution = Solution()\n",
75+
"solution.removeDuplicates(S)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "python pratice",
89+
"language": "python",
90+
"name": "env_name"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.7.1"
103+
},
104+
"toc": {
105+
"base_numbering": 1,
106+
"nav_menu": {},
107+
"number_sections": true,
108+
"sideBar": true,
109+
"skip_h1_title": false,
110+
"title_cell": "Table of Contents",
111+
"title_sidebar": "Contents",
112+
"toc_cell": false,
113+
"toc_position": {},
114+
"toc_section_display": true,
115+
"toc_window_display": false
116+
}
117+
},
118+
"nbformat": 4,
119+
"nbformat_minor": 2
120+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。\n",
9+
"\n",
10+
"在 S 上反复执行重复项删除操作,直到无法继续删除。\n",
11+
"\n",
12+
"在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。\n",
13+
"\n",
14+
" \n",
15+
"\n",
16+
"示例:\n",
17+
"\n",
18+
"输入:\"abbaca\"\n",
19+
"\n",
20+
"输出:\"ca\"\n",
21+
"\n",
22+
"解释:\n",
23+
"\n",
24+
"例如,在 \"abbaca\" 中,我们可以删除 \"bb\" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 \"aaca\",其中又只有 \"aa\" 可以执行重复项删除操作,所以最后的字符串为 \"ca\"\n",
25+
"\n",
26+
"----\n",
27+
"#### 思路\n",
28+
"使用栈的思想\n",
29+
"- 遍历字符串\n",
30+
"- 字符串与栈顶元素进行比较,如果相等,就出栈\n",
31+
"- 遍历完,返回栈中元素"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 1,
37+
"metadata": {
38+
"ExecuteTime": {
39+
"end_time": "2019-07-01T02:21:29.071551Z",
40+
"start_time": "2019-07-01T02:21:29.054935Z"
41+
}
42+
},
43+
"outputs": [
44+
{
45+
"data": {
46+
"text/plain": [
47+
"'ca'"
48+
]
49+
},
50+
"execution_count": 1,
51+
"metadata": {},
52+
"output_type": "execute_result"
53+
}
54+
],
55+
"source": [
56+
"class Solution(object):\n",
57+
" def removeDuplicates(self, S):\n",
58+
" \"\"\"\n",
59+
" :type S: str\n",
60+
" :rtype: str\n",
61+
" \"\"\"\n",
62+
" # 初始化栈\n",
63+
" stack = []\n",
64+
" # 遍历栈元素\n",
65+
" for e in S:\n",
66+
" if stack and stack[-1] == e:\n",
67+
" stack.pop()\n",
68+
" else:\n",
69+
" stack.append(e)\n",
70+
" return \"\".join(stack)\n",
71+
" \n",
72+
" \n",
73+
"S = \"abbaca\"\n",
74+
"solution = Solution()\n",
75+
"solution.removeDuplicates(S)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "python pratice",
89+
"language": "python",
90+
"name": "env_name"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.7.1"
103+
},
104+
"toc": {
105+
"base_numbering": 1,
106+
"nav_menu": {},
107+
"number_sections": true,
108+
"sideBar": true,
109+
"skip_h1_title": false,
110+
"title_cell": "Table of Contents",
111+
"title_sidebar": "Contents",
112+
"toc_cell": false,
113+
"toc_position": {},
114+
"toc_section_display": true,
115+
"toc_window_display": false
116+
}
117+
},
118+
"nbformat": 4,
119+
"nbformat_minor": 2
120+
}

0 commit comments

Comments
 (0)