Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 59d59fb

Browse files
committed
update offer
1 parent 1d961bd commit 59d59fb

File tree

2 files changed

+215
-1
lines changed

2 files changed

+215
-1
lines changed

‎剑指offer/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<img src="https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327102030.png" title="数组、字符串" width=600/>
1212

1313
- [剑指 Offer 03. 数组中重复的数字](./offer03.ipynb) 🌟
14-
- [剑指 Offer 04. 二维数组中的查找](./offer04.ipynb) 🌟🌟
14+
- [剑指 Offer 04. 二维数组中的查找](./offer04.ipynb) 🌟🌟
15+
- [剑指 Offer 58 - II. 左旋转字符串](./offer58.ipynb) 🌟

‎剑指offer/offer58.ipynb‎

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.8.5-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "python3",
18+
"display_name": "Python 3.8.5 64-bit",
19+
"metadata": {
20+
"interpreter": {
21+
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
22+
}
23+
}
24+
}
25+
},
26+
"nbformat": 4,
27+
"nbformat_minor": 2,
28+
"cells": [
29+
{
30+
"source": [
31+
"**剑指 Offer 58 - II. 左旋转字符串**\n",
32+
"\n",
33+
"字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串 `\"abcdefg\"` 和数字 `2`,该函数将返回左旋转两位得到的结果 `\"cdefgab\"`\n",
34+
"\n",
35+
"**示例 1:** \n",
36+
"输入: `s = \"abcdefg\", k = 2` \n",
37+
"输出: `\"cdefgab\"`\n",
38+
"\n",
39+
"**示例 2:** \n",
40+
"输入: `s = \"lrloseumgh\", k = 6` \n",
41+
"输出: `\"umghlrlose\"`\n",
42+
"\n",
43+
"**限制:** \n",
44+
"1ドル \\leq k < s.length \\leq 10000$\n",
45+
"\n",
46+
"来源:力扣(LeetCode) \n",
47+
"链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof \n",
48+
"著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。"
49+
],
50+
"cell_type": "markdown",
51+
"metadata": {}
52+
},
53+
{
54+
"source": [
55+
"### Python 切片\n",
56+
"\n",
57+
"[Python 高级特性](https://github.com/MatNoble/leetcode/issues/1#issuecomment-755976025)"
58+
],
59+
"cell_type": "markdown",
60+
"metadata": {}
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 1,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"class Solution:\n",
69+
" def reverseLeftWords(self, s: str, k: int) -> str:\n",
70+
" # 切片\n",
71+
" return s[k:] + s[:k]"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 2,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"mat = Solution()"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 3,
86+
"metadata": {},
87+
"outputs": [
88+
{
89+
"output_type": "execute_result",
90+
"data": {
91+
"text/plain": [
92+
"'cdefgab'"
93+
]
94+
},
95+
"metadata": {},
96+
"execution_count": 3
97+
}
98+
],
99+
"source": [
100+
"# 测试\n",
101+
"s = \"abcdefg\"\n",
102+
"k = 2\n",
103+
"mat.reverseLeftWords(s, k)"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": 4,
109+
"metadata": {},
110+
"outputs": [
111+
{
112+
"output_type": "execute_result",
113+
"data": {
114+
"text/plain": [
115+
"'umghlrlose'"
116+
]
117+
},
118+
"metadata": {},
119+
"execution_count": 4
120+
}
121+
],
122+
"source": [
123+
"s = \"lrloseumgh\"\n",
124+
"k = 6\n",
125+
"mat.reverseLeftWords(s, k)"
126+
]
127+
},
128+
{
129+
"source": [
130+
"### 旋转字符串\n"
131+
],
132+
"cell_type": "markdown",
133+
"metadata": {}
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": 6,
138+
"metadata": {},
139+
"outputs": [],
140+
"source": [
141+
"class Solution:\n",
142+
" def reverseLeftWords(self, s: str, k: int) -> str:\n",
143+
" def reverse(s):\n",
144+
" \"\"\"旋转字符串\"\"\"\n",
145+
" s = list(s) # str --> list\n",
146+
" i, j = 0, len(s)-1\n",
147+
" while i < j: # 双指针交换\n",
148+
" s[i], s[j] = s[j], s[i]\n",
149+
" i += 1\n",
150+
" j -= 1\n",
151+
" return \"\".join(s)\n",
152+
" s = reverse(s)\n",
153+
" s1 = reverse(s[:len(s)-k])\n",
154+
" s2 = reverse(s[-k:])\n",
155+
" return s1+s2"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 7,
161+
"metadata": {},
162+
"outputs": [],
163+
"source": [
164+
"mat = Solution()"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 8,
170+
"metadata": {},
171+
"outputs": [
172+
{
173+
"output_type": "execute_result",
174+
"data": {
175+
"text/plain": [
176+
"'cdefgab'"
177+
]
178+
},
179+
"metadata": {},
180+
"execution_count": 8
181+
}
182+
],
183+
"source": [
184+
"# 测试\n",
185+
"s = \"abcdefg\"\n",
186+
"k = 2\n",
187+
"mat.reverseLeftWords(s, k)"
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": 9,
193+
"metadata": {},
194+
"outputs": [
195+
{
196+
"output_type": "execute_result",
197+
"data": {
198+
"text/plain": [
199+
"'umghlrlose'"
200+
]
201+
},
202+
"metadata": {},
203+
"execution_count": 9
204+
}
205+
],
206+
"source": [
207+
"s = \"lrloseumgh\"\n",
208+
"k = 6\n",
209+
"mat.reverseLeftWords(s, k)"
210+
]
211+
}
212+
]
213+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /