Skip to content

Commit 6a6069a

Browse files
committed
python-practice
1 parent c50a8cd commit 6a6069a

4 files changed

Lines changed: 488 additions & 0 deletions
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"\n",
9+
"设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。\n",
10+
"\n",
11+
"- insert(val):当元素 val 不存在时,向集合中插入该项。\n",
12+
"- remove(val):元素 val 存在时,从集合中移除该项。\n",
13+
"- getRandom:随机返回现有集合中的一项。每个元素应该有相同的概率被返回。"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 1,
19+
"metadata": {
20+
"ExecuteTime": {
21+
"end_time": "2019-07-03T12:19:39.083134Z",
22+
"start_time": "2019-07-03T12:19:39.074369Z"
23+
}
24+
},
25+
"outputs": [],
26+
"source": [
27+
"class RandomizedSet(object):\n",
28+
" import random\n",
29+
" def __init__(self):\n",
30+
" \"\"\"\n",
31+
" Initialize your data structure here.\n",
32+
" \"\"\"\n",
33+
" self.hashset = set()\n",
34+
" \n",
35+
"\n",
36+
" def insert(self, val):\n",
37+
" \"\"\"\n",
38+
" Inserts a value to the set. Returns true if the set did not already contain the specified element.\n",
39+
" :type val: int\n",
40+
" :rtype: bool\n",
41+
" \"\"\"\n",
42+
" if val not in self.hashset:\n",
43+
" self.hashset.add(val)\n",
44+
" return True\n",
45+
" else:\n",
46+
" return False\n",
47+
" \n",
48+
"\n",
49+
" def remove(self, val):\n",
50+
" \"\"\"\n",
51+
" Removes a value from the set. Returns true if the set contained the specified element.\n",
52+
" :type val: int\n",
53+
" :rtype: bool\n",
54+
" \"\"\"\n",
55+
" if val in self.hashset:\n",
56+
" self.hashset.remove(val)\n",
57+
" return True\n",
58+
" else:\n",
59+
" return False\n",
60+
"\n",
61+
" def getRandom(self):\n",
62+
" \"\"\"\n",
63+
" Get a random element from the set.\n",
64+
" :rtype: int\n",
65+
" \"\"\"\n",
66+
" return random.choice(list(self.hashset))\n",
67+
" \n",
68+
"\n",
69+
"\n",
70+
"# Your RandomizedSet object will be instantiated and called as such:\n",
71+
"# obj = RandomizedSet()\n",
72+
"# param_1 = obj.insert(val)\n",
73+
"# param_2 = obj.remove(val)\n",
74+
"# param_3 = obj.getRandom()"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": []
83+
}
84+
],
85+
"metadata": {
86+
"kernelspec": {
87+
"display_name": "python pratice",
88+
"language": "python",
89+
"name": "env_name"
90+
},
91+
"language_info": {
92+
"codemirror_mode": {
93+
"name": "ipython",
94+
"version": 3
95+
},
96+
"file_extension": ".py",
97+
"mimetype": "text/x-python",
98+
"name": "python",
99+
"nbconvert_exporter": "python",
100+
"pygments_lexer": "ipython3",
101+
"version": "3.7.1"
102+
},
103+
"toc": {
104+
"base_numbering": 1,
105+
"nav_menu": {},
106+
"number_sections": true,
107+
"sideBar": true,
108+
"skip_h1_title": false,
109+
"title_cell": "Table of Contents",
110+
"title_sidebar": "Contents",
111+
"toc_cell": false,
112+
"toc_position": {},
113+
"toc_section_display": true,
114+
"toc_window_display": false
115+
}
116+
},
117+
"nbformat": 4,
118+
"nbformat_minor": 2
119+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转。\n",
9+
"\n",
10+
" \n",
11+
"\n",
12+
"示例 1:\n",
13+
"\n",
14+
"- 输入:\"ab-cd\"\n",
15+
"- 输出:\"dc-ba\"\n",
16+
"\n",
17+
"示例 2:\n",
18+
"\n",
19+
"- 输入:\"a-bC-dEf-ghIj\"\n",
20+
"- 输出:\"j-Ih-gfE-dCba\"\n",
21+
"\n",
22+
"示例 3:\n",
23+
"\n",
24+
"- 输入:\"Test1ng-Leet=code-Q!\"\n",
25+
"- 输出:\"Qedo1ct-eeLg=ntse-T!\"\n",
26+
"\n",
27+
"#### 思路\n",
28+
"\n",
29+
"双指针:\n",
30+
"- 双指针,指向首部和尾部\n",
31+
"- 遇到非字母保持不变,指针向前或向后移动一位\n",
32+
"- 遇到字母,首部和尾部交换数据,指针向前或向后移动一位"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 6,
38+
"metadata": {
39+
"ExecuteTime": {
40+
"end_time": "2019-07-03T13:09:07.655759Z",
41+
"start_time": "2019-07-03T13:09:07.640340Z"
42+
}
43+
},
44+
"outputs": [
45+
{
46+
"data": {
47+
"text/plain": [
48+
"'Qedo1ct-eeLg=ntse-T!'"
49+
]
50+
},
51+
"execution_count": 6,
52+
"metadata": {},
53+
"output_type": "execute_result"
54+
}
55+
],
56+
"source": [
57+
"class Solution(object):\n",
58+
" def reverseOnlyLetters(self, S):\n",
59+
" \"\"\"\n",
60+
" :type S: str\n",
61+
" :rtype: str\n",
62+
" \"\"\"\n",
63+
" S = list(S)\n",
64+
" front, end = 0, len(S) - 1\n",
65+
" while front < end:\n",
66+
" if not S[front].isalpha():\n",
67+
" front += 1\n",
68+
" elif not S[end].isalpha():\n",
69+
" end -= 1\n",
70+
" else:\n",
71+
" S[front], S[end] = S[end], S[front]\n",
72+
" front += 1\n",
73+
" end -= 1\n",
74+
" return \"\".join(S)\n",
75+
" \n",
76+
" \n",
77+
"# S = \"a-bC-dEf-ghIj\"\n",
78+
"S = \"Test1ng-Leet=code-Q!\"\n",
79+
"solution = Solution()\n",
80+
"solution.reverseOnlyLetters(S)"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": []
89+
}
90+
],
91+
"metadata": {
92+
"kernelspec": {
93+
"display_name": "python pratice",
94+
"language": "python",
95+
"name": "env_name"
96+
},
97+
"language_info": {
98+
"codemirror_mode": {
99+
"name": "ipython",
100+
"version": 3
101+
},
102+
"file_extension": ".py",
103+
"mimetype": "text/x-python",
104+
"name": "python",
105+
"nbconvert_exporter": "python",
106+
"pygments_lexer": "ipython3",
107+
"version": "3.7.1"
108+
},
109+
"toc": {
110+
"base_numbering": 1,
111+
"nav_menu": {},
112+
"number_sections": true,
113+
"sideBar": true,
114+
"skip_h1_title": false,
115+
"title_cell": "Table of Contents",
116+
"title_sidebar": "Contents",
117+
"toc_cell": false,
118+
"toc_position": {},
119+
"toc_section_display": true,
120+
"toc_window_display": false
121+
}
122+
},
123+
"nbformat": 4,
124+
"nbformat_minor": 2
125+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"\n",
9+
"设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构。\n",
10+
"\n",
11+
"- insert(val):当元素 val 不存在时,向集合中插入该项。\n",
12+
"- remove(val):元素 val 存在时,从集合中移除该项。\n",
13+
"- getRandom:随机返回现有集合中的一项。每个元素应该有相同的概率被返回。"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 1,
19+
"metadata": {
20+
"ExecuteTime": {
21+
"end_time": "2019-07-03T12:19:39.083134Z",
22+
"start_time": "2019-07-03T12:19:39.074369Z"
23+
}
24+
},
25+
"outputs": [],
26+
"source": [
27+
"class RandomizedSet(object):\n",
28+
" import random\n",
29+
" def __init__(self):\n",
30+
" \"\"\"\n",
31+
" Initialize your data structure here.\n",
32+
" \"\"\"\n",
33+
" self.hashset = set()\n",
34+
" \n",
35+
"\n",
36+
" def insert(self, val):\n",
37+
" \"\"\"\n",
38+
" Inserts a value to the set. Returns true if the set did not already contain the specified element.\n",
39+
" :type val: int\n",
40+
" :rtype: bool\n",
41+
" \"\"\"\n",
42+
" if val not in self.hashset:\n",
43+
" self.hashset.add(val)\n",
44+
" return True\n",
45+
" else:\n",
46+
" return False\n",
47+
" \n",
48+
"\n",
49+
" def remove(self, val):\n",
50+
" \"\"\"\n",
51+
" Removes a value from the set. Returns true if the set contained the specified element.\n",
52+
" :type val: int\n",
53+
" :rtype: bool\n",
54+
" \"\"\"\n",
55+
" if val in self.hashset:\n",
56+
" self.hashset.remove(val)\n",
57+
" return True\n",
58+
" else:\n",
59+
" return False\n",
60+
"\n",
61+
" def getRandom(self):\n",
62+
" \"\"\"\n",
63+
" Get a random element from the set.\n",
64+
" :rtype: int\n",
65+
" \"\"\"\n",
66+
" return random.choice(list(self.hashset))\n",
67+
" \n",
68+
"\n",
69+
"\n",
70+
"# Your RandomizedSet object will be instantiated and called as such:\n",
71+
"# obj = RandomizedSet()\n",
72+
"# param_1 = obj.insert(val)\n",
73+
"# param_2 = obj.remove(val)\n",
74+
"# param_3 = obj.getRandom()"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": []
83+
}
84+
],
85+
"metadata": {
86+
"kernelspec": {
87+
"display_name": "python pratice",
88+
"language": "python",
89+
"name": "env_name"
90+
},
91+
"language_info": {
92+
"codemirror_mode": {
93+
"name": "ipython",
94+
"version": 3
95+
},
96+
"file_extension": ".py",
97+
"mimetype": "text/x-python",
98+
"name": "python",
99+
"nbconvert_exporter": "python",
100+
"pygments_lexer": "ipython3",
101+
"version": "3.7.1"
102+
},
103+
"toc": {
104+
"base_numbering": 1,
105+
"nav_menu": {},
106+
"number_sections": true,
107+
"sideBar": true,
108+
"skip_h1_title": false,
109+
"title_cell": "Table of Contents",
110+
"title_sidebar": "Contents",
111+
"toc_cell": false,
112+
"toc_position": {},
113+
"toc_section_display": true,
114+
"toc_window_display": false
115+
}
116+
},
117+
"nbformat": 4,
118+
"nbformat_minor": 2
119+
}

0 commit comments

Comments
 (0)