目录

力扣76~80题

题76(困难):

分析:

这道题其实不难,但是是我做最久的了,我居然去用res去接所有可能得值,然后再求长度导致空间暴力,我还以为是我queue的问题。。。

最后用暴力求解解的,使用双指针,移动前后指针,后指针用来找齐所有的t值,前指针用来压缩为最短值

python代码:

复制代码
class Solution:
    def minWindow(self, s: str, t: str) -> str:
        #思路就是双指针,应该start一个end
        #加上一个map用于记录各个需要多少个,need_len用与判断还缺否
        if len(s)<len(t):
            return ''
        res=''
        need={}
        need_len=len(t)
        for c in t:
            need[c]=need.get(c,0)+1
        start,end=0,0
        flag=1#用于判断应该移动start还是end,1为移动end,0为移动start

        while end<len(s):
            while flag== 1 and end<len(s):
                #移动end
                if s[end] in need:
                    #如果需要这个
                    if need[s[end]]>=1:
                        need_len-=1
                    need[s[end]]-=1
                    if need_len==0:
                        flag=0#说明不需要了
                    end+=1
                else:
                    end+=1
                    continue
            while flag == 0 and start<end:
                if s[start] in need:
                    need[s[start]]+=1
                    if need[s[start]]>0:
                        if res=='':
                            res=s[start:end]
                        else:
                            res=s[start:end] if len(res)>len(s[start:end]) else res
                        need_len+=1
                        flag=1
                    start+=1
                else:
                    start+=1
                    continue
        return res

题77(中等):

python代码:

复制代码
class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        n_list = [i for i in range(1, n + 1)]
        res = []

        def call_back(nums, k_now, res_now):
            if k_now == 0:
                res.append(res_now)
                return

            for i in range(len(nums)):
                if i > 0 and nums[i] == nums[i - 1]:
                    continue
                nums_new = nums.copy()
                nums_new = nums_new[i + 1:]
                res_new = res_now.copy()
                res_new.append(nums[i])
                call_back(nums_new, k_now - 1, res_new)

        call_back(n_list, k, [])
        return res

题78(中等):

python代码:

复制代码
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        n_list=nums
        res = []

        def call_back(nums, k_now, res_now):
            if k_now == 0:
                res.append(res_now)
                return

            for i in range(len(nums)):
                if i > 0 and nums[i] == nums[i - 1]:
                    continue
                nums_new = nums.copy()
                nums_new = nums_new[i + 1:]
                res_new = res_now.copy()
                res_new.append(nums[i])
                call_back(nums_new, k_now - 1, res_new)
            
        for i in range(len(nums)+1):
            call_back(n_list, i, [])
        
        return res

题79(中等):

python代码:

复制代码
class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        k_len = len(word)
        b_row = len(board)
        b_col = len(board[0])

        notice = [[0] * b_col for i in range(b_row)]

        def call_back(notice, x, y, k):  # x为当前的横坐标,y为当前纵坐标,k为word的第几个
            if board[x][y] != word[k]:
                return False

            if k == k_len-1:
                return True
            notice[x][y] = 1
            if x - 1 >= 0 and notice[x - 1][y] == 0:
                if call_back(notice, x - 1, y, k + 1):
                    return True
            if x + 1 < b_row and notice[x + 1][y] == 0:
                if call_back(notice, x + 1, y, k + 1):
                    return True

            if y - 1 >= 0 and notice[x][y - 1] == 0:
                if call_back(notice, x, y - 1, k + 1):
                    return True

            if y + 1 < b_col and notice[x][y + 1] == 0:
                if call_back(notice, x, y + 1, k + 1):
                    return True
            notice[x][y]=0
            return False

        for i in range(b_row):
            for j in range(b_col):
                if call_back(notice, i, j, 0):
                    return True

        return False

题80(中等):

python代码:

复制代码
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        # 还记得当初有个3国旗问题吗,那个前两指针在开头的---->T75
        p0, p1 = 0, 0
        cout, num = 0, 0
        while p1 < len(nums):
            if nums[p1] == num:
                # 表示这个数要放后面
                if cout >= 2:
                    p1 = p1 + 1
                else:
                    # 表示这个数不放后面
                    nums[p0], nums[p1] = nums[p1], nums[p0]
                    p0 += 1
                    p1 += 1
                cout += 1

            else:
                # 表示这个数是新的,不放后面
                cout = 1
                num = nums[p1]
                nums[p0], nums[p1] = nums[p1], nums[p0]
                p0 += 1
                p1+=1

        return p0
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
夜松云2 分钟前
自求导实现线性回归与PyTorch张量详解
人工智能·pytorch·算法·机器学习·线性回归·梯度下降·自动求导
XU磊2604 分钟前
使用 PyTorch 构建 UNet 图像去噪模型:从数据加载到模型训练的完整流程
人工智能·pytorch·python
renne4 分钟前
Open WebUI+MCP搭建个人AI智能体
python·openai
JavaEdge在掘金6 分钟前
LangChain4j + MCP:让你的 AI 轻松调用外部工具(内附GitHub-MCP实战)
python
BothSavage6 分钟前
部署Kimi-VL-A3B-Instruct视频推理
算法
SsummerC7 分钟前
【leetcode100】一和零
开发语言·python·leetcode·动态规划
不会计算机的捞地16 分钟前
【数据结构入门训练DAY-18】信息学奥赛一本通T1331-后缀表达式的值
数据结构·算法
Kusunoki_D22 分钟前
在 Anaconda 上安装多版本 Python 解释器并在 PyCharm 中配置
ide·python·pycharm
CodeJourney.1 小时前
DeepSeek与ECharts融合助力复杂图表高效制作
数据库·人工智能·算法·excel
傻啦嘿哟1 小时前
Python与图像处理:从基础操作到智能应用的全面解析
开发语言·图像处理·python