Skip to content

Commit 0388479

Browse files
committed
add more problems solutions
1 parent c30938d commit 0388479

6 files changed

+873
-1
lines changed

Diff for: DRAFT.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ LeetCode
386386
| 037|[Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)| [C++](algorithms/cpp/sudokuSolver/sudokuSolver.cpp)|Hard|
387387
| 036|[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)| [C++](algorithms/036-valid-sudoku.ipynb)|Easy|
388388
| 035|[Search Insert Position](https://leetcode.com/problems/search-insert-position/)| [Python](algorithms/035-search-insert-position.py)|Medium|
389-
| 034|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)| [C++](algorithms/cpp/searchForRange/searchForRange.cpp), [Java](algorithms/java/src/searchForRange/searchForRange.java)|Medium|
389+
| 034|[Search for a Range](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [Python](algorithms/034-find-first-and-last-position-of-element-in-sorted-array.ipynb))|Medium|
390390
| 033|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)| [Python](algorithms/033-search-in-rotated-sorted-array.py)|Hard|
391391
| 032|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [Python](algorithms/032-longest-valid-parentheses.py)|Hard|
392392
| 031|[Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](algorithms/031-next-permutation.py)|Medium|

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LeetCode
66
| # | Title | Solution | Difficulty |
77
|---| ----- | -------- | ---------- |
88
|1143|[Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Python](algorithms/1143-longest-common-subsequence.ipynb)|Medium|
9+
|1004|[Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/) | [Python](algorithms/1004-max-consecutive-ones-iii.ipynb)|Medium|
10+
| 995|[Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [Python](algorithms/995-minimum-number-of-k-consecutive-bit-flips.ipynb)|Hard|
911
| 867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Python](algorithms/867-transpose-matrix.ipynb)|Easy|
1012
| 718|[Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/) | [Python](algorithms/718-maximum-length-of-repeated-subarray.ipynb), [Go](algorithms/718-maximum-length-of-repeated-subarray.go)|Medium|
1113
| 692|[Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](algorithms/692-top-k-frequent-words.ipynb)|Medium|
@@ -152,6 +154,7 @@ LeetCode
152154
| 038|[Count and Say](https://leetcode.com/problems/count-and-say/)| [Python](algorithms/038-count-and-say.ipynb)|Easy|
153155
| 036|[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)| [C++](algorithms/036-valid-sudoku.ipynb)|Easy|
154156
| 035|[Search Insert Position](https://leetcode.com/problems/search-insert-position/)| [Python](algorithms/035-search-insert-position.py)|Medium|
157+
| 034|[Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [Python](algorithms/034-find-first-and-last-position-of-element-in-sorted-array.ipynb))|Medium|
155158
| 033|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)| [Python](algorithms/033-search-in-rotated-sorted-array.py)|Hard|
156159
| 032|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [Python](algorithms/032-longest-valid-parentheses.py)|Hard|
157160
| 031|[Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](algorithms/031-next-permutation.py)|Medium|
@@ -160,6 +163,7 @@ LeetCode
160163
| 028|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)| [Python](algorithms/028-implement-strstr.py)|Easy|
161164
| 027|[Remove Element](https://leetcode.com/problems/remove-element/)| [Python](algorithms/027-remove-element.py)|Easy|
162165
| 026|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [Python](algorithms/026-remove-duplicates-from-sorted-array.py)|Easy|
166+
| 025|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)| [Python](algorithms/025-reverse-nodes-in-k-group.ipynb)|Hard|
163167
| 024|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| [Python](algorithms/024-swap-nodes-in-pairs.ipynb)|Medium|
164168
| 023|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| [Python](algorithms/023-merge-k-sorted-lists.py)|Hard|
165169
| 022|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) ♥| [Python](algorithms/022-generate-parentheses.ipynb)|Medium|

Diff for: algorithms/025-reverse-nodes-in-k-group.ipynb

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "cognitive-recovery",
6+
"metadata": {},
7+
"source": [
8+
"### [K 个一组翻转链表](https://leetcode-cn.com/problems/reverse-nodes-in-k-group/)\n",
9+
"\n",
10+
"给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。\n",
11+
"\n",
12+
"k 是一个正整数,它的值小于或等于链表的长度。\n",
13+
"\n",
14+
"如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。\n",
15+
"\n",
16+
"\n",
17+
"#### 示例:\n",
18+
"```\n",
19+
"给你这个链表:1->2->3->4->5\n",
20+
"\n",
21+
"当 k = 2 时,应当返回: 2->1->4->3->5\n",
22+
"\n",
23+
"当 k = 3 时,应当返回: 3->2->1->4->5\n",
24+
"```\n",
25+
"\n",
26+
"#### 说明:\n",
27+
"- 你的算法只能使用常数的额外空间。\n",
28+
"- 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 81,
34+
"id": "logical-breeding",
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"from typing import List\n",
39+
"\n",
40+
"class ListNode:\n",
41+
" def __init__(self, val=0, next=None):\n",
42+
" self.val = val\n",
43+
" self.next = next\n",
44+
" \n",
45+
" @staticmethod\n",
46+
" def fromList(nums: List[int]):\n",
47+
" n, head = len(nums), ListNode(nums[0])\n",
48+
" node = head\n",
49+
" for i in range(1, n):\n",
50+
" node.next = ListNode(nums[i])\n",
51+
" node = node.next\n",
52+
" return head\n",
53+
" \n",
54+
" def __str__(self):\n",
55+
" s = str(self.val)\n",
56+
" node, i = self.next, 0\n",
57+
" while node:\n",
58+
" s += \" -> %d\" % node.val\n",
59+
" node = node.next\n",
60+
" if i > 20:\n",
61+
" s += ' -> ...'\n",
62+
" break\n",
63+
" i += 1\n",
64+
" return s\n",
65+
" \n",
66+
"class Solution:\n",
67+
" def reverseKGroup(self, head: ListNode, k: int) -> ListNode:\n",
68+
" groupStart, groupEnd = head, head\n",
69+
" newhead, tail = None, None\n",
70+
" while groupEnd:\n",
71+
" i = 0\n",
72+
" while i < k:\n",
73+
" groupEnd = groupEnd.next\n",
74+
" i += 1\n",
75+
" if not groupEnd:\n",
76+
" break\n",
77+
" \n",
78+
" # [groupStart, groupEnd)\n",
79+
" if groupEnd or i == k:\n",
80+
" groupPrev, groupCurr = None, groupStart\n",
81+
" while groupCurr != groupEnd:\n",
82+
" tmp = groupCurr.next\n",
83+
" groupCurr.next = groupPrev\n",
84+
" groupPrev = groupCurr\n",
85+
" groupCurr = tmp\n",
86+
"\n",
87+
" if not newhead:\n",
88+
" newhead = groupPrev\n",
89+
" \n",
90+
" if tail:\n",
91+
" tail.next = groupPrev\n",
92+
" \n",
93+
" tail = groupStart\n",
94+
" groupStart = groupEnd\n",
95+
"\n",
96+
" tail.next = groupStart\n",
97+
" return newhead"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 82,
103+
"id": "subsequent-samba",
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"S = Solution()"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 83,
113+
"id": "attractive-livestock",
114+
"metadata": {},
115+
"outputs": [
116+
{
117+
"name": "stdout",
118+
"output_type": "stream",
119+
"text": [
120+
"3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7 -> 8\n"
121+
]
122+
}
123+
],
124+
"source": [
125+
"head = ListNode.fromList([1, 2, 3, 4, 5, 6, 7, 8])\n",
126+
"print(S.reverseKGroup(head, 3))"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 84,
132+
"id": "hidden-healthcare",
133+
"metadata": {},
134+
"outputs": [
135+
{
136+
"name": "stdout",
137+
"output_type": "stream",
138+
"text": [
139+
"2 -> 1 -> 4 -> 3 -> 5\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"print(S.reverseKGroup(ListNode.fromList([1, 2, 3, 4, 5]), 2))"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 85,
150+
"id": "certain-trauma",
151+
"metadata": {},
152+
"outputs": [
153+
{
154+
"name": "stdout",
155+
"output_type": "stream",
156+
"text": [
157+
"3 -> 2 -> 1 -> 4 -> 5\n"
158+
]
159+
}
160+
],
161+
"source": [
162+
"print(S.reverseKGroup(ListNode.fromList([1, 2, 3, 4, 5]), 3))"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 86,
168+
"id": "suffering-soccer",
169+
"metadata": {},
170+
"outputs": [
171+
{
172+
"name": "stdout",
173+
"output_type": "stream",
174+
"text": [
175+
"3 -> 2 -> 1 -> 6 -> 5 -> 4\n"
176+
]
177+
}
178+
],
179+
"source": [
180+
"print(S.reverseKGroup(ListNode.fromList([1, 2, 3, 4, 5, 6]), 3))"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 87,
186+
"id": "nutritional-dealer",
187+
"metadata": {},
188+
"outputs": [
189+
{
190+
"name": "stdout",
191+
"output_type": "stream",
192+
"text": [
193+
"1\n"
194+
]
195+
}
196+
],
197+
"source": [
198+
"print(S.reverseKGroup(ListNode.fromList([1]), 1))"
199+
]
200+
}
201+
],
202+
"metadata": {
203+
"kernelspec": {
204+
"display_name": "Python 3",
205+
"language": "python",
206+
"name": "python3"
207+
},
208+
"language_info": {
209+
"codemirror_mode": {
210+
"name": "ipython",
211+
"version": 3
212+
},
213+
"file_extension": ".py",
214+
"mimetype": "text/x-python",
215+
"name": "python",
216+
"nbconvert_exporter": "python",
217+
"pygments_lexer": "ipython3",
218+
"version": "3.8.6"
219+
}
220+
},
221+
"nbformat": 4,
222+
"nbformat_minor": 5
223+
}

0 commit comments

Comments
 (0)