资讯专栏INFORMATION COLUMN

leetcode-98- Interleaving String

jackwang / 481人阅读

"""
97. Interleaving String
Description
HintsSubmissionsDiscussSolution
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

"""


class Solution:
    # BFS
    def isInterleave(self, s1, s2, s3):
        lx, ly, lz = len(s1), len(s2), len(s3)
        if lx + ly != lz:
            return False
        haved = [(0, 0)]
        visitedx = set()
        visitedy = set()
        while haved:
            x, y = haved.pop(0)
            if x + y == lz:
                return True
            # print(x,y)
            # if  x           
               
                                           
                       
                 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44550.html

相关文章

  • leetcode97. Interleaving String

    摘要:题目要求输入数组和判断是不是由和中的元素交替组成的。思路一乍一看这题可以通过递归的方式来求解。而走到和对应的中的也是确定的。这里我们利用数组记录判断情况。所以我们可以将的二维数组简化为一维数组的数据结构。提高空间的利用率。 题目要求 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2....

    dmlllll 评论0 收藏0
  • 97 Interleaving String

    摘要:和一样给出和两种方法。使用可以避免初始化的和结果的混淆。 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = aabcc, s2 = dbbca, When s3 = aadbbcbcac, return true. When s...

    renweihub 评论0 收藏0
  • [LintCode] Interleaving Positive and Negative Numb

    摘要:注意,若正数多于负数,则序列以正数开始,正数结束。所以先统计正数个数,若超过序列长度的一半,则正指针从开始,反之则负指针从开始。注意交换函数的形式,必须是交换指针所指数字的值,而非坐标。 Problem Given an array with positive and negative integers. Re-range it to interleaving with positiv...

    calx 评论0 收藏0
  • Leetcode98. 验证二叉搜索树

    摘要:题目给定一个二叉树,判断其是否是一个有效的二叉搜索树。所有左子树和右子树自身必须也是二叉搜索树。题解这道题目主要是利用二叉搜索树的一个性质二叉搜索树的中序遍历结果是一个升序的序列。 题目 给定一个二叉树,判断其是否是一个有效的二叉搜索树。 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数。 节点的右子树只包含大于当前节点的数。 所有左子树和右子树自身必须也是二叉搜...

    nidaye 评论0 收藏0
  • leetcode98. Validate Binary Search Tree

    摘要:题目要求检验二叉查找树是否符合规则。二叉查找树是指当前节点左子树上的值均比其小,右子树上的值均比起大。因此在这里我们采用栈的方式实现中序遍历,通过研究中序遍历是否递增来判断二叉查找树是否符合规则。 题目要求 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is...

    codercao 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<