力扣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
相关推荐
yonuyeung1 小时前
代码随想录算法【Day57】
数据结构·算法
居然有人6541 小时前
27.贪心算法5
算法·贪心算法
mosquito_lover12 小时前
Python的PyTorch+CNN深度学习技术在人脸识别项目中的应用
pytorch·python·深度学习·cnn
idealmu2 小时前
PyTorch Lightning 的简单使用示例
人工智能·pytorch·python
夏末秋也凉2 小时前
力扣-动态规划-62 不同路径
算法·leetcode·动态规划
工大一只猿2 小时前
贪心算法-455分发饼干
算法·贪心算法
pljnb3 小时前
【LeetCode 热题100】 240. 搜索二维矩阵 II的算法思路及python代码
算法·leetcode·矩阵
过期动态3 小时前
【动手学深度学习】基于Python动手实现线性神经网络
人工智能·python·深度学习·神经网络
互联网杂货铺3 小时前
Jmeter基础知识总结
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·测试用例
码界筑梦坊3 小时前
基于大数据的去哪儿网景区可视化及协同过滤推荐系统
大数据·python·信息可视化·flask·毕业设计