Skip to content

Commit f3ec7a5

Browse files
Merge pull request youngyangyang04#1479 from janeyziqinglin/master
更新 problems/0704.二分查找python版本; 更新0027.移除元素python版本;添加0977.有序数组的平方 python版本;添加0209.长度最小的子数组python版本;优化 0054.螺旋矩阵 python版本;更新面试题02.07.链表相交 python版本;优化0234.回文链表 python版本;优化0925.长按键入python版本
2 parents ee9ee49 + e427568 commit f3ec7a5

9 files changed

+145
-105
lines changed

problems/0027.移除元素.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,24 @@ class Solution {
183183

184184
Python:
185185

186-
```python
186+
```python3
187187
class Solution:
188-
"""双指针法
189-
时间复杂度:O(n)
190-
空间复杂度:O(1)
191-
"""
192-
193-
@classmethod
194-
def removeElement(cls, nums: List[int], val: int) -> int:
195-
fast = slow = 0
196-
197-
while fast < len(nums):
198-
199-
if nums[fast] != val:
200-
nums[slow] = nums[fast]
201-
slow += 1
202-
203-
# 当 fast 指针遇到要删除的元素时停止赋值
204-
# slow 指针停止移动, fast 指针继续前进
205-
fast += 1
206-
207-
return slow
188+
def removeElement(self, nums: List[int], val: int) -> int:
189+
if nums is None or len(nums)==0:
190+
return 0
191+
l=0
192+
r=len(nums)-1
193+
while l<r:
194+
while(l<r and nums[l]!=val):
195+
l+=1
196+
while(l<r and nums[r]==val):
197+
r-=1
198+
nums[l], nums[r]=nums[r], nums[l]
199+
print(nums)
200+
if nums[l]==val:
201+
return l
202+
else:
203+
return l+1
208204
```
209205

210206

problems/0028.实现strStr.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,21 @@ class Solution {
685685
```
686686

687687
Python3:
688-
688+
```python
689+
//暴力解法:
690+
class Solution(object):
691+
def strStr(self, haystack, needle):
692+
"""
693+
:type haystack: str
694+
:type needle: str
695+
:rtype: int
696+
"""
697+
m,n=len(haystack),len(needle)
698+
for i in range(m):
699+
if haystack[i:i+n]==needle:
700+
return i
701+
return -1
702+
```
689703
```python
690704
// 方法一
691705
class Solution:

problems/0054.螺旋矩阵.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ class Solution:
171171

172172
return res
173173
```
174-
174+
```python3
175+
class Solution:
176+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
177+
r=len(matrix)
178+
if r == 0 or len(matrix[0])==0:
179+
return []
180+
c=len(matrix[0])
181+
res=matrix[0]
182+
183+
if r>1:
184+
for i in range (1,r):
185+
res.append(matrix[i][c-1])
186+
for j in range(c-2, -1, -1):
187+
res.append(matrix[r-1][j])
188+
if c>1:
189+
for i in range(r-2, 0, -1):
190+
res.append(matrix[i][0])
191+
192+
M=[]
193+
for k in range(1, r-1):
194+
e=matrix[k][1:-1]
195+
M.append(e)
196+
197+
return res+self.spiralOrder(M)
198+
```
175199
-----------------------
176200
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0209.长度最小的子数组.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,27 @@ class Solution:
179179
index += 1
180180
return 0 if res==float("inf") else res
181181
```
182-
183-
182+
```python3
183+
#滑动窗口
184+
class Solution:
185+
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
186+
if nums is None or len(nums)==0:
187+
return 0
188+
lenf=len(nums)+1
189+
total=0
190+
i=j=0
191+
while (j<len(nums)):
192+
total=total+nums[j]
193+
j+=1
194+
while (total>=target):
195+
lenf=min(lenf,j-i)
196+
total=total-nums[i]
197+
i+=1
198+
if lenf==len(nums)+1:
199+
return 0
200+
else:
201+
return lenf
202+
```
184203
Go:
185204
```go
186205
func minSubArrayLen(target int, nums []int) int {

problems/0234.回文链表.md

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -218,59 +218,41 @@ class Solution {
218218
```python
219219
#数组模拟
220220
class Solution:
221-
def isPalindrome(self, head: ListNode) -> bool:
222-
length = 0
223-
tmp = head
224-
while tmp: #求链表长度
225-
length += 1
226-
tmp = tmp.next
227-
228-
result = [0] * length
229-
tmp = head
230-
index = 0
231-
while tmp: #链表元素加入数组
232-
result[index] = tmp.val
233-
index += 1
234-
tmp = tmp.next
235-
236-
i, j = 0, length - 1
237-
while i < j: # 判断回文
238-
if result[i] != result[j]:
221+
def isPalindrome(self, head: Optional[ListNode]) -> bool:
222+
list=[]
223+
while head:
224+
list.append(head.val)
225+
head=head.next
226+
l,r=0, len(list)-1
227+
while l<=r:
228+
if list[l]!=list[r]:
239229
return False
240-
i += 1
241-
j -= 1
242-
return True
243-
230+
l+=1
231+
r-=1
232+
return True
233+
244234
#反转后半部分链表
245235
class Solution:
246-
def isPalindrome(self, head: ListNode) -> bool:
247-
if head == None or head.next == None:
248-
return True
249-
slow, fast = head, head
236+
def isPalindrome(self, head: Optional[ListNode]) -> bool:
237+
fast = slow = head
238+
239+
# find mid point which including (first) mid point into the first half linked list
250240
while fast and fast.next:
251-
pre = slow
252-
slow = slow.next
253241
fast = fast.next.next
254-
255-
pre.next = None # 分割链表
256-
cur1 = head # 前半部分
257-
cur2 = self.reverseList(slow) # 反转后半部分,总链表长度如果是奇数,cur2比cur1多一个节点
258-
while cur1:
259-
if cur1.val != cur2.val:
242+
slow = slow.next
243+
node = None
244+
245+
# reverse second half linked list
246+
while slow:
247+
slow.next, slow, node = node, slow.next, slow
248+
249+
# compare reversed and original half; must maintain reversed linked list is shorter than 1st half
250+
while node:
251+
if node.val != head.val:
260252
return False
261-
cur1 = cur1.next
262-
cur2 = cur2.next
253+
node = node.next
254+
head = head.next
263255
return True
264-
265-
def reverseList(self, head: ListNode) -> ListNode:
266-
cur = head
267-
pre = None
268-
while(cur!=None):
269-
temp = cur.next # 保存一下cur的下一个节点
270-
cur.next = pre # 反转
271-
pre = cur
272-
cur = temp
273-
return pre
274256
```
275257

276258
### Go

problems/0704.二分查找.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,21 @@ class Solution:
220220

221221
(版本二)左闭右开区间
222222

223-
```python
224-
class Solution:
223+
```class Solution:
225224
def search(self, nums: List[int], target: int) -> int:
226-
left,right =0, len(nums)
227-
while left < right:
228-
mid = (left + right) // 2
229-
if nums[mid] < target:
230-
left = mid+1
231-
elif nums[mid] > target:
232-
right = mid
225+
if nums is None or len(nums)==0:
226+
return -1
227+
l=0
228+
r=len(nums)-1
229+
while (l<=r):
230+
m = round(l+(r-l)/2)
231+
if nums[m] == target:
232+
return m
233+
elif nums[m] > target:
234+
r=m-1
233235
else:
234-
return mid
235-
return -1
236+
l=m+1
237+
return -1
236238
```
237239

238240
**Go:**

problems/0925.长按键入.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,21 @@ class Solution {
129129
```
130130
### Python
131131
```python
132-
class Solution:
133-
def isLongPressedName(self, name: str, typed: str) -> bool:
134-
i, j = 0, 0
135-
m, n = len(name) , len(typed)
136-
while i< m and j < n:
137-
if name[i] == typed[j]: # 相同时向后匹配
138-
i += 1
139-
j += 1
140-
else: # 不相同
141-
if j == 0: return False # 如果第一位不相同,直接返回false
142-
# 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"
143-
while j < n - 1 and typed[j] == typed[j-1]: j += 1
144-
if name[i] == typed[j]:
145-
i += 1
146-
j += 1
147-
else: return False
148-
# 说明name没有匹配完
149-
if i < m: return False
150-
# 说明type没有匹配完
151-
while j < n:
152-
if typed[j] == typed[j-1]: j += 1
153-
else: return False
154-
return True
132+
i = j = 0
133+
while(i<len(name) and j<len(typed)):
134+
# If the current letter matches, move as far as possible
135+
if typed[j]==name[i]:
136+
while j+1<len(typed) and typed[j]==typed[j+1]:
137+
j+=1
138+
# special case when there are consecutive repeating letters
139+
if i+1<len(name) and name[i]==name[i+1]:
140+
i+=1
141+
else:
142+
j+=1
143+
i+=1
144+
else:
145+
return False
146+
return i == len(name) and j==len(typed)
155147
```
156148

157149
### Go

problems/0977.有序数组的平方.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public:
4141
}
4242
};
4343
```
44+
```python3
45+
class Solution:
46+
def sortedSquares(self, nums: List[int]) -> List[int]:
47+
res=[]
48+
for num in nums:
49+
res.append(num**2)
50+
return sorted(res)
51+
```
52+
4453

4554
这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。
4655

problems/面试题02.07.链表相交.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ class Solution:
160160
那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个
161161
位置相遇
162162
"""
163+
if headA is None or headB is None:
164+
return None
163165
cur_a, cur_b = headA, headB # 用两个指针代替a和b
164166

165167

0 commit comments

Comments
 (0)