# 2022.11.27

## 1、二分查找(leetcode[704,278,35])

class Solution:
def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)-1
mid = left+(right-left)//2
while(left<=right):
if nums[mid]==target:
return mid
elif nums[mid]>target:
right = mid-1
mid = left+(right-left)//2
else:
left = mid+1
mid = left+(right-left)//2
return -1

class Solution:
def search(self, nums: List[int], target: int) -> int:
if len(nums)==0:
return -1
left = 0
right = len(nums)-1
mid = left+（right-left)//2
while(left<=right):
if nums[mid]==target:
right = mid-1  # 向左收缩
elif nums[mid]>target:
right = mid-1
else:
left = mid+1
mid = left+（right-left)//2
if left>=len(nums) or nums[left]!=target:
return -1
return left

搜索右边界

class Solution:
def search(self, nums: List[int], target: int) -> int:
if len(nums)==0:
return -1
left = 0
right = len(nums)-1
mid = left+（right-left)//2
while(left<=right):
if nums[mid]==target:
left = mid+1  # 向右收缩
elif nums[mid]>target:
right = mid-1
else:
left = mid+1
mid = left+（right-left)//2
if right<0 or nums[right]!=target:
return -1
return right

## 2、双指针(leetcode[977,283,167,344,876,19])

leetcode977--有序数组的平方

class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
n = len(nums)
result = [-1]*n
i,j,pos = 0,n-1,n-1
while(i<=j):
if nums[i]*nums[i]>nums[j]*nums[j]:
result[pos] = nums[i]*nums[i]
i += 1
else:
result[pos] = nums[j]*nums[j]
j -= 1
pos -= 1
return result

[1,2,3,4,5]----最大值平方25（右端点）

[-5,-4,-3,-2,-1]----最大值平方25（左端点）

[-5,-4,0,3,2]---最大值平方25（左端点）

leetcode977---移动零

class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
left = right =0
while(right<len(nums)):
if nums[right]!=0:
nums[left],nums[right] = nums[right],nums[left]
left += 1
right += 1
return nums

left指向的是已经处理好的序列的最右端，right指向的是未处理序列的最左端。右指针不断向右移动，每次右指针指向非零数，则将左右指针对应的数交换【也可以直接覆盖，最后用for循环对left及以后的赋值为0】，同时左指针右移。

leetcode167---两数之和 II - 输入有序数组

class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
i = 0
j = len(numbers)-1
while (i<j):
if numbers[i]+numbers[j]==target:
return [i+1,j+1]
elif numbers[i]+numbers[j]<target:
i=i+1
else:
j=j-1

leetcode344--反转字符串

class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left = 0
right = len(s)-1
while(left<right):
s[left],s[right] = s[right],s[left]
left += 1
right -= 1
return s


leetcode876--链表的中间结点

class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
while(fast and fast.next):
slow = slow.next
fast = fast.next.next
return slow

经典链表快慢指针，慢指针走一步，快指针走两步。快指针走到最后时，慢指针走到一半。

leetcode19--

class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
slow = dummy
for i in range(n):
fast = fast.next
while(fast):
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy.next

整体思路是让前面的指针先移动n步，之后前后指针共同移动直到前面的指针到尾部为止。这里注意链表常用技巧，设置哑节点dummy【不用特意考虑头节点】

## 3、其他题

class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(k):
nums.insert(0, nums.pop())
return nums

557. 反转字符串中的单词 III

class Solution:
def reverseWords(self, s: str) -> str:
words = s.split(" ")
result = ""
for i in words:
result += i[::-1]  # 从前到后，步长为-1----从后到前
result += " "
result = result[:-1]
return result

使用split函数先将每个单词分开然后对每个单独单词进行操作。最后一步result = result[:-1]是因为前面循环中多加了一个空格

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 2
评论
04-17
12-14 3222
11-15 4181
11-30 6375
07-28 4904
12-06 1832
11-08 15万+
03-08 2万+
03-30 6万+
09-08 1436
09-07 1209
09-11 354
09-12 886

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。