Skip to content

Commit cfbdf2d

Browse files
committed
add more easy problem solutions
1 parent 4e3436c commit cfbdf2d

6 files changed

+658
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "visible-publisher",
6+
"metadata": {},
7+
"source": [
8+
"### [猜数字大小](https://leetcode-cn.com/problems/guess-number-higher-or-lower/)\n",
9+
"\n",
10+
"猜数字游戏的规则如下:\n",
11+
"\n",
12+
"- 每轮游戏,我都会从 1 到 n 随机选择一个数字。 请你猜选出的是哪个数字。\n",
13+
"- 如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。\n",
14+
"\n",
15+
"你可以通过调用一个预先定义好的接口 int guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0):\n",
16+
"- -1:我选出的数字比你猜的数字小 pick < num\n",
17+
"- 1:我选出的数字比你猜的数字大 pick > num\n",
18+
"- 0:我选出的数字和你猜的数字一样。恭喜!你猜对了!pick == num\n",
19+
"\n",
20+
"返回我选出的数字。\n",
21+
"\n",
22+
"\n",
23+
"#### 示例 1:\n",
24+
"```\n",
25+
"输入:n = 10, pick = 6\n",
26+
"输出:6\n",
27+
"```\n",
28+
"\n",
29+
"#### 示例 2:\n",
30+
"```\n",
31+
"输入:n = 1, pick = 1\n",
32+
"输出:1\n",
33+
"```\n",
34+
"\n",
35+
"#### 示例 3:\n",
36+
"```\n",
37+
"输入:n = 2, pick = 1\n",
38+
"输出:1\n",
39+
"```\n",
40+
"\n",
41+
"#### 示例 4:\n",
42+
"```\n",
43+
"输入:n = 2, pick = 2\n",
44+
"输出:2\n",
45+
"```\n",
46+
"\n",
47+
"#### 提示:\n",
48+
"- $ 1 <= n <= 2^{31} - 1 $\n",
49+
"- $ 1 <= pick <= n $"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 1,
55+
"id": "biological-spider",
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"# The guess API is already defined for you.\n",
60+
"# @param num, your guess\n",
61+
"# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0\n",
62+
"# def guess(num: int) -> int:\n",
63+
"\n",
64+
"class Solution:\n",
65+
" def guessNumber(self, n: int) -> int:\n",
66+
" \n",
67+
" def binarySearch(l, r):\n",
68+
" if l > r:\n",
69+
" return -1\n",
70+
" \n",
71+
" mid = l + (r - l) // 2\n",
72+
" result = guess(mid)\n",
73+
" if result == 1:\n",
74+
" return binarySearch(mid + 1, r)\n",
75+
" elif result == -1:\n",
76+
" return binarySearch(l, mid - 1)\n",
77+
" else:\n",
78+
" return mid\n",
79+
" \n",
80+
" return binarySearch(1, n)"
81+
]
82+
}
83+
],
84+
"metadata": {
85+
"kernelspec": {
86+
"display_name": "Python 3",
87+
"language": "python",
88+
"name": "python3"
89+
},
90+
"language_info": {
91+
"codemirror_mode": {
92+
"name": "ipython",
93+
"version": 3
94+
},
95+
"file_extension": ".py",
96+
"mimetype": "text/x-python",
97+
"name": "python",
98+
"nbconvert_exporter": "python",
99+
"pygments_lexer": "ipython3",
100+
"version": "3.8.6"
101+
}
102+
},
103+
"nbformat": 4,
104+
"nbformat_minor": 5
105+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "photographic-mandate",
6+
"metadata": {},
7+
"source": [
8+
"### [字符串中的第一个唯一字符](https://leetcode-cn.com/problems/first-unique-character-in-a-string/)\n",
9+
"\n",
10+
"给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。\n",
11+
"\n",
12+
"#### 示例:\n",
13+
"```\n",
14+
"s = \"leetcode\"\n",
15+
"返回 0\n",
16+
"```\n",
17+
"```\n",
18+
"s = \"loveleetcode\"\n",
19+
"返回 2\n",
20+
"```\n",
21+
"\n",
22+
"提示:你可以假定该字符串只包含小写字母。"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"id": "experienced-teaching",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"from collections import Counter\n",
33+
"\n",
34+
"def firstUniqChar(s: str) -> int:\n",
35+
" counter = Counter(s)\n",
36+
" for i, c in enumerate(s):\n",
37+
" if counter[c] == 1:\n",
38+
" return i\n",
39+
" return -1"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 4,
45+
"id": "soviet-optimization",
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"data": {
50+
"text/plain": [
51+
"2"
52+
]
53+
},
54+
"execution_count": 4,
55+
"metadata": {},
56+
"output_type": "execute_result"
57+
}
58+
],
59+
"source": [
60+
"firstUniqChar(\"loveleetcode\")"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 5,
66+
"id": "dated-labor",
67+
"metadata": {},
68+
"outputs": [
69+
{
70+
"data": {
71+
"text/plain": [
72+
"0"
73+
]
74+
},
75+
"execution_count": 5,
76+
"metadata": {},
77+
"output_type": "execute_result"
78+
}
79+
],
80+
"source": [
81+
"firstUniqChar(\"leetcode\")"
82+
]
83+
}
84+
],
85+
"metadata": {
86+
"kernelspec": {
87+
"display_name": "Python 3",
88+
"language": "python",
89+
"name": "python3"
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.8.6"
102+
}
103+
},
104+
"nbformat": 4,
105+
"nbformat_minor": 5
106+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "median-plaza",
6+
"metadata": {},
7+
"source": [
8+
"### [找不同](https://leetcode-cn.com/problems/find-the-difference/)\n",
9+
"\n",
10+
"给定两个字符串 s 和 t,它们只包含小写字母。\n",
11+
"\n",
12+
"字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。\n",
13+
"\n",
14+
"请找出在 t 中被添加的字母。\n",
15+
"\n",
16+
"#### 示例 1:\n",
17+
"```\n",
18+
"输入:s = \"abcd\", t = \"abcde\"\n",
19+
"输出:\"e\"\n",
20+
"解释:'e' 是那个被添加的字母。\n",
21+
"```\n",
22+
"\n",
23+
"#### 示例 2:\n",
24+
"```\n",
25+
"输入:s = \"\", t = \"y\"\n",
26+
"输出:\"y\"\n",
27+
"```\n",
28+
"\n",
29+
"#### 示例 3:\n",
30+
"```\n",
31+
"输入:s = \"a\", t = \"aa\"\n",
32+
"输出:\"a\"\n",
33+
"```\n",
34+
"\n",
35+
"示例 4:\n",
36+
"```\n",
37+
"输入:s = \"ae\", t = \"aea\"\n",
38+
"输出:\"a\"\n",
39+
"```\n",
40+
"\n",
41+
"#### 提示:\n",
42+
"- 0 <= s.length <= 1000\n",
43+
"- t.length == s.length + 1\n",
44+
"- s 和 t 只包含小写字母"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 2,
50+
"id": "connected-hughes",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"from collections import Counter\n",
55+
"\n",
56+
"def findTheDifference(s: str, t: str) -> str:\n",
57+
" counter1, counter2 = Counter(s), Counter(t)\n",
58+
" for c, v1 in counter2.items():\n",
59+
" if c not in counter1:\n",
60+
" return c\n",
61+
" else:\n",
62+
" v2 = counter1[c]\n",
63+
" if v1 > v2:\n",
64+
" return c\n",
65+
" return None"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 3,
71+
"id": "paperback-people",
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"'a'"
78+
]
79+
},
80+
"execution_count": 3,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"findTheDifference(\"ae\", \"aea\")"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 4,
92+
"id": "coordinated-nurse",
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"data": {
97+
"text/plain": [
98+
"'a'"
99+
]
100+
},
101+
"execution_count": 4,
102+
"metadata": {},
103+
"output_type": "execute_result"
104+
}
105+
],
106+
"source": [
107+
"findTheDifference(\"a\", \"aa\")"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 5,
113+
"id": "excited-offering",
114+
"metadata": {},
115+
"outputs": [
116+
{
117+
"data": {
118+
"text/plain": [
119+
"'y'"
120+
]
121+
},
122+
"execution_count": 5,
123+
"metadata": {},
124+
"output_type": "execute_result"
125+
}
126+
],
127+
"source": [
128+
"findTheDifference(\"\", \"y\")"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": 6,
134+
"id": "caroline-collection",
135+
"metadata": {},
136+
"outputs": [
137+
{
138+
"data": {
139+
"text/plain": [
140+
"'e'"
141+
]
142+
},
143+
"execution_count": 6,
144+
"metadata": {},
145+
"output_type": "execute_result"
146+
}
147+
],
148+
"source": [
149+
"findTheDifference(\"abcd\", \"abcde\")"
150+
]
151+
}
152+
],
153+
"metadata": {
154+
"kernelspec": {
155+
"display_name": "Python 3",
156+
"language": "python",
157+
"name": "python3"
158+
},
159+
"language_info": {
160+
"codemirror_mode": {
161+
"name": "ipython",
162+
"version": 3
163+
},
164+
"file_extension": ".py",
165+
"mimetype": "text/x-python",
166+
"name": "python",
167+
"nbconvert_exporter": "python",
168+
"pygments_lexer": "ipython3",
169+
"version": "3.8.6"
170+
}
171+
},
172+
"nbformat": 4,
173+
"nbformat_minor": 5
174+
}

0 commit comments

Comments
 (0)