python leetcode 21 - 30

# 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, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1:
            return l2
        if not l2:
            return l1
        if l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next , l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1 , l2.next)
            return l2
# 22 leetcode里第一道典型的dfs题 0 0
class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        res = []
        def dfs(tmp , left , right):
            if len(tmp) == 2 * n:
                res.append(tmp)
            if left:
                dfs(tmp + "(" , left - 1 , right)
            if left < right:
                dfs(tmp + ")" , left , right - 1)
        dfs('', n , n)
        return res
# 23 加强版的21
# 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):
        if not lists:
            return
        n=len(lists)
        return self.merge(lists,0,n-1)
    def merge(self,lists,left,right):
        if left==right:
            return lists[left]
        mid=left+(right-left)//2
        l1=self.merge(lists,left,mid)
        l2=self.merge(lists,mid+1,right)
        return self.Twolist(l1,l2)
    def Twolist(self,l1,l2):
        if not l1:
            return l2
        if not l2:
            return l1
        if l1.val<l2.val:
            l1.next=self.Twolist(l1.next,l2)
            return l1
        else:
            l2.next=self.Twolist(l1,l2.next)
            return l2
# 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: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        tmp=head.next
        head.next = self.swapPairs(head.next.next)
        tmp.next = head
        return tmp
# 25 谁知道我两个月前写的代码62MS 今天运行就3000MS了 o(╥﹏╥)o 测试集又变态了吗 有时间再改吧
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(self, head, k):
        dummy = ListNode(0)
        p = dummy
        while True:
            cnt = k
            tmp = head
            stack = []
            while cnt and tmp:
                stack.append(tmp)
                tmp = tmp.next
                cnt -= 1
            if cnt:
                p.next = head
                break
            while stack:
                p.next = stack.pop()
                p = p.next
            p.next = tmp
            head = tmp
            print(p)
        return dummy.next
# 26 我也不知道他们的双指针在干啥.....可能我太菜了?
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        tmp = 1
        for i in range(1 , len(nums)):
            if nums[i] != nums[i - 1]:
                nums[tmp] = nums[i]
                tmp += 1
        return tmp
# 27
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        for i in range(len(nums)-1,-1,-1):
            if nums[i] == val:
                nums.pop(i)
# 28
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        L, n = len(needle), len(haystack)
        for start in range(n - L + 1):
            if haystack[start: start + L] == needle:
                return start
        return -1
# 29
class Solution:
    def divide(self, dividend: int, divisor: int) -> int:
        MIN_INT, MAX_INT = -2147483648, 2147483647  
        flag = 1                                    
        if dividend < 0: flag, dividend = -flag, -dividend
        if divisor < 0: flag, divisor  = -flag, -divisor 
        res = 0
        while dividend >= divisor:                 
            cur = divisor
            multiple = 1
            while cur + cur < dividend:         
                cur += cur                     
                multiple += multiple
            dividend -= cur
            res += multiple
        res = res if flag > 0 else -res            
        if res < MIN_INT:                         
            return MIN_INT
        elif MIN_INT <= res <= MAX_INT:
            return res
        else:
            return MAX_INT

# 30
class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        if not words:
            return []
        w_len, s_len = len(words[0]), len(s)
        t_len = w_len * len(words) 

        word_dict = {}  # words的哈希表
        for word in words:
            word_dict[word] = word_dict.get(word, 0) + 1

        ans = []
        for offset in range(w_len):
            lo, lo_max = offset, s_len - t_len
            while lo <= lo_max:
                tmp_dict = word_dict.copy()
                match = True
                for hi in range(lo + t_len, lo, -w_len):    
                    word = s[hi - w_len: hi]
                    if word not in tmp_dict or tmp_dict.get(word, 0) == 0:
                        match = False
                        break  
                    tmp_dict[word] -= 1
                if match:
                    ans.append(lo)
                lo = hi     
        return ans

#咸鱼皓出版 转载请说明

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值