首页 > 其他分享 >Study Plan For Algorithms - Part49

Study Plan For Algorithms - Part49

时间:2024-10-24 22:34:52浏览次数:1  
标签:node val s3 s2 Study Part49 Algorithms s1 dp

1. 交错字符串
给定三个字符串 s1、s2、s3,请验证 s3 是否是由 s1 和 s2 交错 组成的。
两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:
s = s1 + s2 + ... + sn
t = t1 + t2 + ... + tm
|n - m| <= 1
交错 是 s1 + t1 + s2 + t2 + s3 + t3 + ... 或者 t1 + s1 + t2 + s2 + t3 + s3 + ...
注意:a + b 意味着字符串 a 和 b 连接。

class Solution:
    def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
        m, n, l = len(s1), len(s2), len(s3)
        if m + n!= l:
            return False
        dp = [[False] * (n + 1) for _ in range(m + 1)]
        dp[0][0] = True
        for i in range(m + 1):
            for j in range(n + 1):
                if i > 0 and s1[i - 1] == s3[i + j - 1]:
                    dp[i][j] = dp[i][j] or dp[i - 1][j]
                if j > 0 and s2[j - 1] == s3[i + j - 1]:
                    dp[i][j] = dp[i][j] or dp[i][j - 1]
        return dp[m][n]

2. 验证二叉搜索树
给定一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:

        def helper(node, min_val, max_val):
            if not node:
                return True
            if not min_val < node.val < max_val:
                return False
            return helper(node.left, min_val, node.val) and helper(node.right, node.val, max_val)

        return helper(root, float('-inf'), float('inf'))

标签:node,val,s3,s2,Study,Part49,Algorithms,s1,dp
From: https://www.cnblogs.com/stephenxiong001/p/18501468

相关文章

  • StudyTonight-Java-中文教程-六-
    StudyTonightJava中文教程(六)原文:StudyTonight协议:CCBY-NC-SA4.0JavaCharacter.isLetter(char)方法原文:https://www.studytonight.com/java-wrapper-class/java-character-isletterchar-ch-methodJavaisLetter(charch)方法是Character类的一部分。此方法用于检查指......
  • StudyTonight-Java-中文教程-二-
    StudyTonightJava中文教程(二)原文:StudyTonight协议:CCBY-NC-SA4.0JavaFloat类原文:https://www.studytonight.com/java/float-class.phpFloat类将基元类型的浮点值包装在对象中。Float类型的对象包含一个类型为浮点的字段。此外,此类提供了几种将浮点转换为字符串和将......
  • StudyTonight-C-C---中文教程-一-
    StudyTonightC/C++中文教程(一)原文:StudyTonight协议:CCBY-NC-SA4.0C基础知识C语言概述原文:https://www.studytonight.com/c/overview-of-c.php欢迎来到C语言教程系列。这是网上最好的C语言教程集,会帮助你学习C语言。C语言是由丹尼斯·里奇于1972年在贝尔......
  • StudyTonight-Web-中文教程-一-
    StudyTonightWeb中文教程(一)原文:StudyTonight协议:CCBY-NC-SA4.0HTMLHTML标签AHTML<a>标签原文:https://www.studytonight.com/html5-references/html-a-tagHTML<a>标签是一个锚点,用来创建一个超链接。超链接用于将当前网页与其他网页或互联网上可用的任何其他网......
  • phpstudy php8.2.9 redis使用
    1、卸载8.22、安装8.23、先开启redis缓存4、设置缓存内存大小5、把所有扩展放在C:\php\ext(复制:D:\phpstudy_pro\Extensions\php\php8.2.9nts\ext到C:\php\ext)6、把redis.dll复制进C:\php\ext(下载路径:https://downloads.php.net/~windows/pecl/releases/redis/6.0.2/php_red......
  • vscode+phpstudy+xdebug无法断点(踩坑记)
    参考文档:https://zhuanlan.zhihu.com/p/113171737安装vscode、下载phpstudy最新版这2步都不说了,网上大把教程。本文主要把phpstudy的一个坑点记录一下配置网站配置伪静态location/{if(!-e$request_filename){rewrite^(.*)$/index.php?s=$1las......
  • DDR Study - LPDDR Initial
    参考来源:JESD209-4B在之前的DDRStudy-BasicUnderstanding中介绍了DDR的基础概念,从这篇文章开始,会基于LPDDR4依次按照如下顺序对LPDDR内容进行简单分析:LPDDRInitial→LPDDRWriteLevelingandDQTraining→LPDDRReadandTraining→LPDDRWriteandTraining......
  • phpstorm+phpstudy快速配置xdebug
    1.下载安装小皮面板2.下载安装phpstorm3.开启phpxdebug默认php环境是7.3.4,一般不需要洞这个,本文也是使用这个版本的php进行快速配置,如果PHP5.X版本的配置此方法可能不同4.配置php文件访问\phpstudy_pro\Extensions\php\php7.3.4nts\php.ini最下面会看到如下配置......
  • python系列&deep_study系列:【已解决】Failed to initialize NVML: Driver/library ver
    【已解决】FailedtoinitializeNVML:Driver/libraryversionmismatchNVMLlibraryversion:535.161【已解决】FailedtoinitializeNVML:Driver/libraryversionmismatchNVMLlibraryversion:535.161问题描述原因分析和解决当我把这个问题喂给`chatgpt`的时......
  • CRICOS Data Structures and AlgorithmsHash Tables
    DataStructuresandAlgorithmsHashTablesPage1of3CRICOSProvideCode:00301J Note:hashArraystoresthekey,valueandstate(used,free,orpreviously-used)ofeveryhashEntry.WemuststoreboththekeyandvaluesinceweneedtocheckhashArrayto......