双指针部分算法题(代码随想录)

双指针

1.移除元素

def removeElement(self, nums: List[int], val: int) -> int:
    n = len(nums)
    slow,fast = 0,0
    for fast in range(n):
        if nums[fast] != val:
            nums[slow] = nums[fast]
            slow += 1
        fast += 1
    return slow

2.反转字符串

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

3.替换空格

def replaceSpace(self, s: str) -> str:
    cnt = s.count(" ")
    res = list(s)
    res.extend([" "]*cnt*2)
    l,r = len(s) - 1,len(res) - 1
    while l >= 0:
        if s[l] == " ":
            res[r-2:r+1] = "%20"
            r -= 3
        else:
            res[r] = res[l]
            r -= 1
        l -= 1
    return "".join(res)

4.反转链表

def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
    pre = None
    cur = head
    while cur is not None:
        temp = cur.next
        cur.next = pre
        pre = cur
        cur = temp
    return pre

5.删除链表的倒数第 N 个结点(2)

def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
    hv = ListNode(next=head)
    slow,fast = hv,hv
    while n != 0:
        fast = fast.next
        n -= 1
    while fast.next is not None:
        fast = fast.next
        slow = slow.next
    slow.next = slow.next.next
    return hv.next

6.链表相交

def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
    lenA,lenB = 0,0
    curA,curB = headA,headB
    # 计算长度
    while curA is not None:
        lenA += 1
        curA = curA.next
    while curB is not None:
        lenB += 1
        curB = curB.next

    # 长的比短的多走|lenA - lenB|步
    curA, curB = headA, headB
    if lenA > lenB:
        cnt = lenA - lenB
        while cnt != 0:
            cnt -= 1
            curA = curA.next
    else:
        cnt = lenB - lenA
        while cnt != 0:
            cnt -= 1
            curB = curB.next
    
    while curA is not None:
        if curA == curB:
            return curA
        curA = curA.next
        curB = curB.next
    return None

7.环形链表 II(2)

详细解析见链表部分

def detectCycle(self, head: ListNode) -> ListNode:
    slow,fast = head,head
    while fast is not None:
        if fast.next is not None:
            fast = fast.next.next
        else:
            return None
        slow = slow.next
        if fast == slow:
            cur = head
            while cur is not None:
                if cur == slow:
                    return cur
                cur = cur.next
                slow = slow.next
    return None

8.三数之和(2)

def threeSum(self, nums: List[int]) -> List[List[int]]:
    r = []
    nums.sort()
    n = len(nums)
    for i in range(n - 2):
        if nums[i] > 0:
            return r
        if i >= 1 and nums[i] == nums[i - 1]:
            continue
        j,k = i + 1,n - 1
        while j < k:
            temp = nums[i] + nums[j] + nums[k]
            if temp > 0:
                k -= 1
            elif temp < 0:
                j += 1
            else:
                r.append([nums[i],nums[j],nums[k]])
                # 去重
                while j < k and nums[j] == nums[j + 1]:
                    j += 1
                while j < k and nums[k] == nums[k - 1]:
                    k -= 1
                # 本来不去重也要更新j,k
                j += 1
                k -= 1
    return r

9.四数之和(2)

def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
    n = len(nums)
    if nums is None or n < 4:
        return []
    r = []
    nums.sort()
    for i in range(n - 3):
        if i >= 1 and nums[i] == nums[i - 1]:
            continue
        for j in range(i + 1,n - 2):
            if j > i + 1 and nums[j] == nums[j - 1]:
                continue
            left,right = j + 1,n - 1
            while left < right:
                temp = nums[i] + nums[j] + nums[left] + nums[right]
                if temp > target:
                    right -= 1
                elif temp < target:
                    left += 1
                else:
                    r.append([nums[i],nums[j],nums[left],nums[right]])
                    while left < right and nums[left] == nums[left + 1]:
                        left += 1
                    while left < right and nums[right] == nums[right - 1]:
                        right -= 1
                    left += 1
                    right -= 1
    return r
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值