leetcode 21-30(2024.08.16)

立个flag,1-100题每天分配10题,不会就先空着(5,9)。

1. 21:合并两个有序链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        dummy = res = ListNode()
        while list1 and list2:
            if list1.val < list2.val:
                res.next = list1
                list1 = list1.next
            else:
                res.next = list2
                list2 = list2.next
            res = res.next
        if list1:
            res.next = list1
        if list2:
            res.next = list2
        return dummy.next

2. 22:括号生成

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        def backtracking(left, right, n, path, res):
            if left > n or right > n or right > left:
                return
            if len(path) == 2 * n:
                valid = "".join(path.copy())
                res.append(valid)
            path.append('(')
            backtracking(left + 1, right, n, path, res)
            path.pop()
            path.append(')')
            backtracking(left, right + 1, n, path, res)
            path.pop()

        res = []
        path = []
        left = 0
        right = 0
        backtracking(left, right, n, path, res)
        return res

3. 23:合并K个升序链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        def mergeTwoLists(l1, l2):
            dummy = res = ListNode()
            while l1 and l2:
                if l1.val < l2.val:
                    res.next = l1
                    l1 = l1.next
                else:
                    res.next = l2
                    l2 = l2.next
                res = res.next
            if l1:
                res.next = l1
            if l2:
                res.next = l2
            return dummy.next

        res = None
        for i in lists:
            res = mergeTwoLists(res, i)
        return res

 4. 24:两两交换链表中的节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = res = ListNode()
        res.next = head
        while head and head.next:
            temp = head.next
            head.next = head.next.next
            temp.next = head
            res.next = temp

            head = head.next
            res = res.next.next
        return dummy.next

 5. 25:K个一组翻转链表 

 6. 26:删除有序数组中的重复项 

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        slow = 0
        fast = 0
        while fast < len(nums):
            if nums[slow] != nums[fast]:
                slow = slow + 1
                nums[slow] = nums[fast]
            fast = fast + 1
        return slow + 1

 7. 27:移除元素

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        slow = 0
        fast = 0
        while fast < len(nums):
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow = slow + 1
            fast = fast + 1
        return slow

 8. 28:找出字符串中第一个匹配项的下标

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        lis = []
        for i in range(len(haystack)):
            if haystack[i] == needle[0]:
                lis.append(i)
        res = -1
        for i in lis:
            if haystack[i : i + len(needle)] == needle:
                res = i
                break
        return res

 9. 29:两数相除 

 10. 30:串联所有单词的子串

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        def pair(st, haxi, leng):
            begin = 0
            while begin + leng <= len(st):
                word = st[begin : begin + leng]
                if word in haxi and haxi[word] > 0:
                    haxi[word] = haxi[word] - 1
                    begin = begin + leng
                else:
                    return False
            return True

        haxi = {}
        for word in words:
            if word in haxi:
                haxi[word] = haxi[word] + 1
            else:
                haxi[word] = 1

        res = []
        num = len(words)
        leng = len(words[0])
        length = num * leng
        left = 0
        right = left + length
        while right <= len(s):
            st = s[left:right]
            output = pair(st, haxi.copy(), leng)
            if output:
                res.append(left)
            left = left + 1
            right = left + length
        return res

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值