力扣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
相关推荐
Greedy Alg6 小时前
LeetCode 142. 环形链表 II
算法
睡不醒的kun6 小时前
leetcode算法刷题的第三十二天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
独行soc7 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
先做个垃圾出来………7 小时前
残差连接的概念与作用
人工智能·算法·机器学习·语言模型·自然语言处理
SuperCandyXu9 小时前
P3205 [HNOI2010] 合唱队-普及+/提高
c++·算法·洛谷
_OP_CHEN9 小时前
数据结构(C语言篇):(十二)实现顺序结构二叉树——堆
c语言·数据结构·算法·二叉树·学习笔记··顺序结构二叉树
Y学院9 小时前
Python 数据分析:从新手到高手的“摸鱼”指南
python·数据分析
Yingjun Mo9 小时前
1. 统计推断-基于神经网络与Langevin扩散的自适应潜变量建模与优化
人工智能·神经网络·算法·机器学习·概率论
深耕AI9 小时前
【PyTorch训练】准确率计算(代码片段拆解)
人工智能·pytorch·python