mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1479 from janeyziqinglin/master
更新 problems/0704.二分查找python版本; 更新0027.移除元素python版本;添加0977.有序数组的平方 python版本;添加0209.长度最小的子数组python版本;优化 0054.螺旋矩阵 python版本;更新面试题02.07.链表相交 python版本;优化0234.回文链表 python版本;优化0925.长按键入python版本
This commit is contained in:
@ -183,28 +183,24 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
```python
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
"""双指针法
|
def removeElement(self, nums: List[int], val: int) -> int:
|
||||||
时间复杂度:O(n)
|
if nums is None or len(nums)==0:
|
||||||
空间复杂度:O(1)
|
return 0
|
||||||
"""
|
l=0
|
||||||
|
r=len(nums)-1
|
||||||
@classmethod
|
while l<r:
|
||||||
def removeElement(cls, nums: List[int], val: int) -> int:
|
while(l<r and nums[l]!=val):
|
||||||
fast = slow = 0
|
l+=1
|
||||||
|
while(l<r and nums[r]==val):
|
||||||
while fast < len(nums):
|
r-=1
|
||||||
|
nums[l], nums[r]=nums[r], nums[l]
|
||||||
if nums[fast] != val:
|
print(nums)
|
||||||
nums[slow] = nums[fast]
|
if nums[l]==val:
|
||||||
slow += 1
|
return l
|
||||||
|
else:
|
||||||
# 当 fast 指针遇到要删除的元素时停止赋值
|
return l+1
|
||||||
# slow 指针停止移动, fast 指针继续前进
|
|
||||||
fast += 1
|
|
||||||
|
|
||||||
return slow
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -685,7 +685,21 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python3:
|
Python3:
|
||||||
|
```python
|
||||||
|
//暴力解法:
|
||||||
|
class Solution(object):
|
||||||
|
def strStr(self, haystack, needle):
|
||||||
|
"""
|
||||||
|
:type haystack: str
|
||||||
|
:type needle: str
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
m,n=len(haystack),len(needle)
|
||||||
|
for i in range(m):
|
||||||
|
if haystack[i:i+n]==needle:
|
||||||
|
return i
|
||||||
|
return -1
|
||||||
|
```
|
||||||
```python
|
```python
|
||||||
// 方法一
|
// 方法一
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -171,6 +171,30 @@ class Solution:
|
|||||||
|
|
||||||
return res
|
return res
|
||||||
```
|
```
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
|
||||||
|
r=len(matrix)
|
||||||
|
if r == 0 or len(matrix[0])==0:
|
||||||
|
return []
|
||||||
|
c=len(matrix[0])
|
||||||
|
res=matrix[0]
|
||||||
|
|
||||||
|
if r>1:
|
||||||
|
for i in range (1,r):
|
||||||
|
res.append(matrix[i][c-1])
|
||||||
|
for j in range(c-2, -1, -1):
|
||||||
|
res.append(matrix[r-1][j])
|
||||||
|
if c>1:
|
||||||
|
for i in range(r-2, 0, -1):
|
||||||
|
res.append(matrix[i][0])
|
||||||
|
|
||||||
|
M=[]
|
||||||
|
for k in range(1, r-1):
|
||||||
|
e=matrix[k][1:-1]
|
||||||
|
M.append(e)
|
||||||
|
|
||||||
|
return res+self.spiralOrder(M)
|
||||||
|
```
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -179,8 +179,27 @@ class Solution:
|
|||||||
index += 1
|
index += 1
|
||||||
return 0 if res==float("inf") else res
|
return 0 if res==float("inf") else res
|
||||||
```
|
```
|
||||||
|
```python3
|
||||||
|
#滑动窗口
|
||||||
|
class Solution:
|
||||||
|
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
|
||||||
|
if nums is None or len(nums)==0:
|
||||||
|
return 0
|
||||||
|
lenf=len(nums)+1
|
||||||
|
total=0
|
||||||
|
i=j=0
|
||||||
|
while (j<len(nums)):
|
||||||
|
total=total+nums[j]
|
||||||
|
j+=1
|
||||||
|
while (total>=target):
|
||||||
|
lenf=min(lenf,j-i)
|
||||||
|
total=total-nums[i]
|
||||||
|
i+=1
|
||||||
|
if lenf==len(nums)+1:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return lenf
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
func minSubArrayLen(target int, nums []int) int {
|
func minSubArrayLen(target int, nums []int) int {
|
||||||
|
@ -218,59 +218,41 @@ class Solution {
|
|||||||
```python
|
```python
|
||||||
#数组模拟
|
#数组模拟
|
||||||
class Solution:
|
class Solution:
|
||||||
def isPalindrome(self, head: ListNode) -> bool:
|
def isPalindrome(self, head: Optional[ListNode]) -> bool:
|
||||||
length = 0
|
list=[]
|
||||||
tmp = head
|
while head:
|
||||||
while tmp: #求链表长度
|
list.append(head.val)
|
||||||
length += 1
|
head=head.next
|
||||||
tmp = tmp.next
|
l,r=0, len(list)-1
|
||||||
|
while l<=r:
|
||||||
result = [0] * length
|
if list[l]!=list[r]:
|
||||||
tmp = head
|
|
||||||
index = 0
|
|
||||||
while tmp: #链表元素加入数组
|
|
||||||
result[index] = tmp.val
|
|
||||||
index += 1
|
|
||||||
tmp = tmp.next
|
|
||||||
|
|
||||||
i, j = 0, length - 1
|
|
||||||
while i < j: # 判断回文
|
|
||||||
if result[i] != result[j]:
|
|
||||||
return False
|
return False
|
||||||
i += 1
|
l+=1
|
||||||
j -= 1
|
r-=1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
#反转后半部分链表
|
#反转后半部分链表
|
||||||
class Solution:
|
class Solution:
|
||||||
def isPalindrome(self, head: ListNode) -> bool:
|
def isPalindrome(self, head: Optional[ListNode]) -> bool:
|
||||||
if head == None or head.next == None:
|
fast = slow = head
|
||||||
return True
|
|
||||||
slow, fast = head, head
|
# find mid point which including (first) mid point into the first half linked list
|
||||||
while fast and fast.next:
|
while fast and fast.next:
|
||||||
pre = slow
|
|
||||||
slow = slow.next
|
|
||||||
fast = fast.next.next
|
fast = fast.next.next
|
||||||
|
slow = slow.next
|
||||||
|
node = None
|
||||||
|
|
||||||
pre.next = None # 分割链表
|
# reverse second half linked list
|
||||||
cur1 = head # 前半部分
|
while slow:
|
||||||
cur2 = self.reverseList(slow) # 反转后半部分,总链表长度如果是奇数,cur2比cur1多一个节点
|
slow.next, slow, node = node, slow.next, slow
|
||||||
while cur1:
|
|
||||||
if cur1.val != cur2.val:
|
# compare reversed and original half; must maintain reversed linked list is shorter than 1st half
|
||||||
|
while node:
|
||||||
|
if node.val != head.val:
|
||||||
return False
|
return False
|
||||||
cur1 = cur1.next
|
node = node.next
|
||||||
cur2 = cur2.next
|
head = head.next
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def reverseList(self, head: ListNode) -> ListNode:
|
|
||||||
cur = head
|
|
||||||
pre = None
|
|
||||||
while(cur!=None):
|
|
||||||
temp = cur.next # 保存一下cur的下一个节点
|
|
||||||
cur.next = pre # 反转
|
|
||||||
pre = cur
|
|
||||||
cur = temp
|
|
||||||
return pre
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
@ -220,18 +220,20 @@ class Solution:
|
|||||||
|
|
||||||
(版本二)左闭右开区间
|
(版本二)左闭右开区间
|
||||||
|
|
||||||
```python
|
```class Solution:
|
||||||
class Solution:
|
|
||||||
def search(self, nums: List[int], target: int) -> int:
|
def search(self, nums: List[int], target: int) -> int:
|
||||||
left,right =0, len(nums)
|
if nums is None or len(nums)==0:
|
||||||
while left < right:
|
return -1
|
||||||
mid = (left + right) // 2
|
l=0
|
||||||
if nums[mid] < target:
|
r=len(nums)-1
|
||||||
left = mid+1
|
while (l<=r):
|
||||||
elif nums[mid] > target:
|
m = round(l+(r-l)/2)
|
||||||
right = mid
|
if nums[m] == target:
|
||||||
|
return m
|
||||||
|
elif nums[m] > target:
|
||||||
|
r=m-1
|
||||||
else:
|
else:
|
||||||
return mid
|
l=m+1
|
||||||
return -1
|
return -1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -129,29 +129,21 @@ class Solution {
|
|||||||
```
|
```
|
||||||
### Python
|
### Python
|
||||||
```python
|
```python
|
||||||
class Solution:
|
i = j = 0
|
||||||
def isLongPressedName(self, name: str, typed: str) -> bool:
|
while(i<len(name) and j<len(typed)):
|
||||||
i, j = 0, 0
|
# If the current letter matches, move as far as possible
|
||||||
m, n = len(name) , len(typed)
|
if typed[j]==name[i]:
|
||||||
while i< m and j < n:
|
while j+1<len(typed) and typed[j]==typed[j+1]:
|
||||||
if name[i] == typed[j]: # 相同时向后匹配
|
j+=1
|
||||||
i += 1
|
# special case when there are consecutive repeating letters
|
||||||
j += 1
|
if i+1<len(name) and name[i]==name[i+1]:
|
||||||
else: # 不相同
|
i+=1
|
||||||
if j == 0: return False # 如果第一位不相同,直接返回false
|
else:
|
||||||
# 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"
|
j+=1
|
||||||
while j < n - 1 and typed[j] == typed[j-1]: j += 1
|
i+=1
|
||||||
if name[i] == typed[j]:
|
else:
|
||||||
i += 1
|
return False
|
||||||
j += 1
|
return i == len(name) and j==len(typed)
|
||||||
else: return False
|
|
||||||
# 说明name没有匹配完
|
|
||||||
if i < m: return False
|
|
||||||
# 说明type没有匹配完
|
|
||||||
while j < n:
|
|
||||||
if typed[j] == typed[j-1]: j += 1
|
|
||||||
else: return False
|
|
||||||
return True
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
@ -41,6 +41,15 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def sortedSquares(self, nums: List[int]) -> List[int]:
|
||||||
|
res=[]
|
||||||
|
for num in nums:
|
||||||
|
res.append(num**2)
|
||||||
|
return sorted(res)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。
|
这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。
|
||||||
|
|
||||||
|
@ -160,6 +160,8 @@ class Solution:
|
|||||||
那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个
|
那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个
|
||||||
位置相遇
|
位置相遇
|
||||||
"""
|
"""
|
||||||
|
if headA is None or headB is None:
|
||||||
|
return None
|
||||||
cur_a, cur_b = headA, headB # 用两个指针代替a和b
|
cur_a, cur_b = headA, headB # 用两个指针代替a和b
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user