力扣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
相关推荐
console.log('npc')1 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
honker_X1 小时前
Python字符串全攻略:从入门到精通
开发语言·python
贾公子1 小时前
异步检测的轮询与超时——别把「还没好」当成「失败了」
后端·python
吃我一窝jio1 小时前
Docker+Jenkins+K8s全流程部署
java·开发语言·笔记·python
橘子132 小时前
【高级数据库】关系代数基本概念
算法
欢乐马87912 小时前
优化|MPSolver 和 pywraplp 到底谁是谁?一次讲清 OR-Tools 的三层结构
python
mengge.cloud4 小时前
Python基础保姆级!从入门到入土
开发语言·python·云原生·pygame
qq_158895411118 小时前
Suno-API 是什么?官方 V6 模型与 Python / TypeScript 接入指南
python·ai音乐·suno api
电科_银尘13 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
郝学胜-神的一滴16 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl