每日一练 — 2021.12.31


一,正则表达式匹配

1,程序简介

  • 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。

    (1)'.' 匹配任意单个字符
    (2)'*' 匹配零个或多个前面的那一个元素
    
  • 所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。

示例 1:
  • 输入:s = “aa” p = “a”
  • 输出:false
  • 解释:“a” 无法匹配 “aa” 整个字符串。
示例 2:
  • 输入:s = “aa” p = “a*”
  • 输出:true
  • 解释:因为 ‘*’ 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 ‘a’。因此,字符串 “aa” 可被视为 ‘a’ 重复了一次。
示例 3:
  • 输入:s = “ab” p = “.*”
  • 输出:true
  • 解释:"." 表示可匹配零个或多个(’’)任意字符(’.’)。
示例 4:
  • 输入:s = “aab” p = “cab”
  • 输出:true
  • 解释:因为 ‘*’ 表示零个或多个,这里 ‘c’ 为 0 个, ‘a’ 被重复一次。因此可以匹配字符串 “aab”。
示例 5:
  • 输入:s = “mississippi” p = “misisp*.”
  • 输出:false

提示:

0 <= s.length <= 20
0 <= p.length <= 30
s 可能为空,且只包含从 a-z 的小写字母。
p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *。
保证每次出现字符 * 时,前面都匹配到有效的字符

以下程序实现了这一功能,请你填补空白处内容:

class Solution:
	def isMatch(self, s: str, p: str) -> bool:
		if len(p) == 0:
			return len(s) == 0
		head_match = len(s) > 0 and (s[0] == p[0] or p[0] == '.')
		if len(p) > 1 and p[1] == '*':
			__________________________;
		else:
			if not head_match:
				return False
			return self.isMatch(s[1:], p[1:])
# %%
s = Solution()
print(s.isMatch(s = "aa" , p = "a"))

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 31 15:43:48 2021
Function: 正则表达式匹配
@author: 小梁aixj
"""
class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        if len(p) == 0:
            return len(s) == 0
        head_match = len(s) > 0 and (s[0] == p[0] or p[0] == '.')
        if len(p) > 1 and p[1] == '*':
            if head_match and self.isMatch(s[1:], p):
                return True
            return self.isMatch(s, p[2:])
        else:
            if not head_match:
                return False
            return self.isMatch(s[1:], p[1:])
#%%
s = Solution()
print(s.isMatch( s = 'aa', p = 'a'))#false
print(s.isMatch(s = 'aa', p = 'a*'))#true

3,运行结果

在这里插入图片描述

二,合并区间

1,程序简介

  • 以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。
示例 1:
  • 输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
  • 输出:[[1,6],[8,10],[15,18]]
  • 解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
  • 输入:intervals = [[1,4],[4,5]]
  • 输出:[[1,5]]
  • 解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。

提示:

  • 1 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 104

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 31 15:44:42 2021
Function: 合并区间
@author: 小梁aixj
"""
class Interval(object):
    def __init__(self, s=0, e=0):
        self.start = s
        self.end = e
class Solution(object):
    def list2interval(self, list_interval):
        ret = []
        for i in list_interval:
            interval = Interval(i[0], i[1])
            ret.append(interval)
        return ret
    def interval2list(self, interval):
        ret = []
        x = [0,0]
        for i in interval:
            x[0] = i.start
            x[1] = i.end
            ret.append(x)
            x = [0,0]
        return ret
    def merge(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: List[Interval]
        """
        if intervals is None:
            return
        ls = len(intervals)
        if ls <= 1:
            return intervals
        intervals = self.list2interval(intervals)        
        intervals.sort(key=lambda x: x.start)
        pos = 0
        while pos < len(intervals) - 1:
            if intervals[pos].end >= intervals[pos + 1].start:
                next = intervals.pop(pos + 1)
                if next.end > intervals[pos].end:
                    intervals[pos].end = next.end
            else:
                pos += 1
        intervals = self.interval2list(intervals)
        return intervals
if __name__ == '__main__':
    s = Solution()
    print (s.merge(intervals = [[1,4],[4,5]]))

3,运行结果

在这里插入图片描述

三,合并K个升序链表

1,程序简介

  • 给你一个链表数组,每个链表都已经按升序排列。
  • 请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 1:
  • 输入:lists = [[1,4,5],[1,3,4],[2,6]]
  • 输出:[1,1,2,3,4,4,5,6]
  • 解释:链表数组如下:[ 1->4->5, 1->3->4, 2->6]将它们合并到一个有序链表中得到。1->1->2->3->4->4->5->6
示例 2:
  • 输入:lists = []
  • 输出:[]
示例 3:
  • 输入:lists = [[]]
  • 输出:[]

提示:

k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] 按 升序 排列
lists[i].length 的总和不超过 10^4

以下程序实现了这一功能,请你填补空白处内容:

from typing import List


class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


class LinkList:
    def __init__(self):
        self.head = None

    def initList(self, data):
        self.head = ListNode(data[0])
        r = self.head
        p = self.head
        for i in data[1:]:
            node = ListNode(i)
            p.next = node
            p = p.next
        return r

    def convert_list(self, head):
        ret = []
        if head == None:
            return
        node = head
        while node != None:
            ret.append(node.val)
            node = node.next
        return ret


class Solution(object):
    def mergeKLists(self, lists):
        if lists is None:
            return None
        elif len(lists) == 0:
            return None
        return self.mergeK(lists, 0, len(lists) - 1)

    def mergeK(self, lists, low, high):
        if low == high:
            return lists[int(low)]
        elif low + 1 == high:
            return self.mergeTwolists(lists[int(low)], lists[int(high)])
        mid = (low + high) / 2
        return self.mergeTwolists(self.mergeK(lists, low, mid),
                                  self.mergeK(lists, mid + 1, high))

    def mergeTwolists(self, l1, l2):
        l = LinkList()
        if type(l1) == list:
            l1 = l.initList(l1)
        if type(l2) == list:
            l2 = l.initList(l2)
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        head = curr = ListNode(-1)
        while l1 is not None and l2 is not None:
			____________________;
            curr = curr.next
        if l1 is not None:
            curr.next = l1
        if l2 is not None:
            curr.next = l2
        return head.next


# %%
l = LinkList()
list1 = [[1, 4, 5], [1, 3, 4], [2, 6]]
s = Solution()
print(l.convert_list(s.mergeKLists(list1)))

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 31 15:44:53 2021
Function: 合并K个升序链表
@author: 小梁aixj
"""
from typing import List

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class LinkList:
    def __init__(self):
        self.head = None
        
    def initList(self, data):
        self.head = ListNode(data[0])
        r = self.head
        p = self.head
        for i in data[1:]:
            node = ListNode(i)
            p.next = node
            p = p.next
        return r   

    def convert_list(self, head):
        ret = []
        if head == None:
            return
        node = head
        while node != None:
            ret.append(node.val)
            node = node.next
        return ret        
        
class Solution(object):
    def mergeKLists(self, lists):
        if lists is None:
            return None
        elif len(lists) == 0:
            return None
        return self.mergeK(lists, 0, len(lists) - 1)

    def mergeK(self, lists, low, high):
        if low == high:
            return lists[int(low)]
        elif low + 1 == high:
            return self.mergeTwolists(lists[int(low)], lists[int(high)])
        mid = (low + high) / 2
        return self.mergeTwolists(self.mergeK(lists, low, mid),self.mergeK(lists, mid + 1, high))
    def mergeTwolists(self, l1, l2):
        l = LinkList()
        if type(l1) == list:
            l1 = l.initList(l1)
        if type(l2) == list:
            l2 = l.initList(l2)
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        head = curr = ListNode(-1)
        while l1 is not None and l2 is not None:
            
            
            if l1.val <= l2.val:
                curr.next = l1
                l1 = l1.next
            else:
                curr.next = l2
                l2 = l2.next
                
                
            curr = curr.next
        if l1 is not None:
            curr.next = l1
        if l2 is not None:
            curr.next = l2
        return head.next
# %%
l = LinkList()
list1 = [[1, 4, 5], [1, 3, 4], [2, 6]]
s = Solution()
print(l.convert_list(s.mergeKLists(list1)))#[1,1,2,3,4,4,5,6]

3,运行结果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁辰兴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值