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 b751418

Browse files
committed
add 剑指OFFER 03
1 parent 7a79dc8 commit b751418

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed

‎python basic/in2post.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#==================================================
2+
#==> Title: 中缀 --> 后缀
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date: 2/20/2021
7+
#==================================================
8+
9+
# coding:utf-8
10+
def priority(z):
11+
if z in [×ばつ', '*', '/']:
12+
return 2
13+
elif z in ['+', '-']:
14+
return 1
15+
16+
def in2post(expr):
17+
""" :param expr: 中缀表达式
18+
:return: 后缀表达式
19+
20+
Example:
21+
"1+((2+3)×ばつ4)-5"
22+
"1 2 3 + 4 ×ばつ + 5 -"
23+
"""
24+
stack = [] # 存储栈
25+
post = [] # 后缀表达式存储
26+
for z in expr:
27+
if z not in [×ばつ', '*', '/', '+', '-', '(', ')']: # 数字直接输出
28+
post.append(z)
29+
else:
30+
if z != ')' and (not stack or z == '(' or stack[-1] == '('
31+
or priority(z) > priority(stack[-1])): # stack 不空;栈顶为(;优先级大于
32+
stack.append(z) # 运算符入栈
33+
elif z == ')': # 右括号出栈
34+
while True:
35+
x = stack.pop()
36+
if x != '(':
37+
post.append(x)
38+
else:
39+
break
40+
else: # 比较运算符优先级,看是否入栈出栈
41+
while True:
42+
if stack and stack[-1] != '(' and priority(z) <= priority(stack[-1]):
43+
post.append(stack.pop())
44+
else:
45+
stack.append(z)
46+
break
47+
while stack: # 还未出栈的运算符,需要加到表达式末尾
48+
post.append(stack.pop())
49+
return post
50+
51+
# if __name__ == '__main__':
52+
s = "1+((2+3)×ばつ4)-5"
53+
post = in2post(s)
54+
print('后缀表达式: ',post)

‎test.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#==================================================
2+
#==> Title:
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date:
7+
#==================================================
8+
9+
while True:
10+
try:
11+
from collections import Counter
12+
n = int(input())
13+
nums = list(map(int, input()))
14+
if n & 1:
15+
i, j = n//2 - 1, n//2 + 1
16+
else:
17+
i, j = n//2 - 1, n//2
18+
d = Counter(nums)
19+
dl = Counter(nums[:i+1])
20+
dr = Counter(nums[j:])
21+
res = dl[0] + dr[1] # 左 1 右 0
22+
res0 = d[1] # 全 0
23+
res1 = d[0] # 全 1
24+
print(min(res, res0, res1))
25+
except:
26+
break

‎剑指offer/README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 剑指 Offer 题解
2+
3+
> Python 版本
4+
5+
[LeetCode 网站版](https://leetcode-cn.com/problemset/lcof/)
6+
7+
<hr />
8+
9+
### 数组、字符串
10+
11+
<img src="https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327102030.png" title="数组、字符串"/>

‎剑指offer/offer03.ipynb‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
"找出数组中重复的数字。\n",
32+
"\n",
33+
"在一个长度为 $n$ 的数组 `nums` 里的所有数字都在 0ドル〜n-1$ 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。\n",
34+
"\n",
35+
"示例 1: \n",
36+
"输入:$[2, 3, 1, 0, 2, 5, 3]$ \n",
37+
"输出:2ドル$ 或 3ドル$ \n",
38+
" \n",
39+
"限制: \n",
40+
"2ドル \\leq n \\leq 100000$\n",
41+
"\n",
42+
"来源:力扣(LeetCode) \n",
43+
"链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof \n",
44+
"著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。"
45+
],
46+
"cell_type": "markdown",
47+
"metadata": {}
48+
},
49+
{
50+
"source": [
51+
"### 计数法\n",
52+
"\n",
53+
"借助 [Counter 模块](https://matnoble.me/tech/programming/python/collections/#counter) `计数`,返回`计数`最多的数"
54+
],
55+
"cell_type": "markdown",
56+
"metadata": {}
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 1,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"from collections import Counter \n",
65+
"class Solution:\n",
66+
" def findRepeatNumber(self, nums):\n",
67+
" # 计数法\n",
68+
" return Counter(nums).most_common(1)[0][0]"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 2,
74+
"metadata": {},
75+
"outputs": [
76+
{
77+
"output_type": "execute_result",
78+
"data": {
79+
"text/plain": [
80+
"2"
81+
]
82+
},
83+
"metadata": {},
84+
"execution_count": 2
85+
}
86+
],
87+
"source": [
88+
"# 测试\n",
89+
"nums = [2, 3, 1, 0, 2, 5, 3]\n",
90+
"mat = Solution()\n",
91+
"mat.findRepeatNumber(nums)"
92+
]
93+
},
94+
{
95+
"source": [
96+
"### 哈希表\n",
97+
"\n",
98+
"用`哈希表`查找元素,时间复杂度$O(1)$"
99+
],
100+
"cell_type": "markdown",
101+
"metadata": {}
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 3,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"class Solution:\n",
110+
" def findRepeatNumber(self, nums):\n",
111+
" # 哈希表\n",
112+
" d = set()\n",
113+
" for num in nums:\n",
114+
" if num in d: return num\n",
115+
" d.add(num)"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 4,
121+
"metadata": {},
122+
"outputs": [
123+
{
124+
"output_type": "execute_result",
125+
"data": {
126+
"text/plain": [
127+
"2"
128+
]
129+
},
130+
"metadata": {},
131+
"execution_count": 4
132+
}
133+
],
134+
"source": [
135+
"# 测试\n",
136+
"nums = [2, 3, 1, 0, 2, 5, 3]\n",
137+
"mat = Solution()\n",
138+
"mat.findRepeatNumber(nums)"
139+
]
140+
},
141+
{
142+
"source": [
143+
"### 原地置换 环\n",
144+
"\n",
145+
"<img src=\"https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327105657.png\" width=400/>"
146+
],
147+
"cell_type": "markdown",
148+
"metadata": {}
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 5,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"class Solution:\n",
157+
" def findRepeatNumber(self, nums):\n",
158+
" # 原地置换 环\n",
159+
" i = 0\n",
160+
" while i < len(nums):\n",
161+
" if nums[i] == i: \n",
162+
" i += 1\n",
163+
" continue\n",
164+
" if nums[nums[i]] == nums[i]: \n",
165+
" return nums[i] # 返回环入口\n",
166+
" # swap i <- -> j\n",
167+
" j = nums[i]\n",
168+
" nums[i], nums[j] = nums[j], nums[i]"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 6,
174+
"metadata": {},
175+
"outputs": [
176+
{
177+
"output_type": "execute_result",
178+
"data": {
179+
"text/plain": [
180+
"2"
181+
]
182+
},
183+
"metadata": {},
184+
"execution_count": 6
185+
}
186+
],
187+
"source": [
188+
"# 测试\n",
189+
"nums = [2, 3, 1, 0, 2, 5, 3]\n",
190+
"mat = Solution()\n",
191+
"mat.findRepeatNumber(nums)"
192+
]
193+
}
194+
]
195+
}

0 commit comments

Comments
(0)

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