力扣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
相关推荐
wadesir3 分钟前
C语言模块化设计入门指南(从零开始构建清晰可维护的C程序)
c语言·开发语言·算法
t198751287 分钟前
MATLAB水声信道仿真程序
开发语言·算法·matlab
white-persist24 分钟前
VSCode 快捷键大全:从设计理念到场景化高效运用(详细解析)(文章末尾有vim快捷键大全)
linux·ide·vscode·python·编辑器·系统安全·vim
liliangcsdn28 分钟前
python threading异步并发调用示例
python
计算机毕设匠心工作室30 分钟前
【python大数据毕设实战】全面皮肤病症状数据可视化分析系统、Hadoop、计算机毕业设计、包括数据爬取、数据分析、数据可视化、机器学习、实战教学
后端·python·mysql
码界奇点1 小时前
基于Python与Pygame的多功能游戏系统设计与实现
python·游戏·毕业设计·pygame·源代码管理
万邦科技Lafite1 小时前
一键获取淘宝店铺所有商品信息,实时监控商品数据
开发语言·数据库·python·api·开放api·电商开放平台·淘宝开放平台
小糖学代码1 小时前
LLM系列:1.python入门:2.数值型对象
人工智能·python·ai
CoderYanger2 小时前
动态规划算法-简单多状态dp问题:15.买卖股票的最佳时机含冷冻期
开发语言·算法·leetcode·动态规划·1024程序员节
Xの哲學2 小时前
Linux RTC深度剖析:从硬件原理到驱动实践
linux·服务器·算法·架构·边缘计算