leetcode 392. Is Subsequence 子序列判断 深度优先遍历DFS + 一个很简单的循环
来源:互联网 发布:易建联cba数据 编辑:程序博客网 时间:2024/06/07 03:17
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ace” is a subsequence of “abcde” while “aec” is not).
Example 1:
s = “abc”, t = “ahbgdc”
Return true.
Example 2:
s = “axc”, t = “ahbgdc”
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, … , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
这道题是子序列的判定问题,最直接的想法就是DFS深度优先遍历来做,但是后来想一想有更加简单的方法,就是一个两层循环即可,递归做法会超时
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>using namespace std;class Solution {public: bool isSubsequence(string s, string t) { //return isSub(s, 0, t, 0); int i = 0,j = 0; while (i < t.length()) { if (j == s.length()) return true; else if (t[i] == s[j]) { i++; j++; } else i++; } return j == s.length(); } bool isSub(string s, int i, string t, int j) { if (i == s.length()) return true; else { if (j == t.length()) return false; else { if (s[i] == t[j]) return isSub(s, i + 1, t, j + 1); else return isSub(s, i, t, j + 1); } } }};
- leetcode 392. Is Subsequence 子序列判断 深度优先遍历DFS + 一个很简单的循环
- [LeetCode] Is Subsequence 判断一个字符串是否为另一个的子序列
- Leetcode 392. Is Subsequence 判断子序列 解题报告
- leetcode 508. Most Frequent Subtree Sum 一个简单的DFS深度优先遍历的做法
- leetcode 513. Find Bottom Left Tree Value 一个简单的DFS深度优先遍历
- 392. Is Subsequence (子序列判断)
- leetcode 417. Pacific Atlantic Water Flow 一个很经典的DFS深度优先遍历做法
- 392.leetcode Is Subsequence (medium)[判断一个字符串是否是另一个字符串的子串]
- leetcode 684. Redundant Connection 邻接表的环的判断 + 深度优先遍历DFS
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- leetcode 401. Binary Watch 一个十分经典的DFS深度优先遍历的做法
- leetcode 464. Can I Win 一个十分棒的深度优先遍历DFS的做法
- leetcode 473. Matchsticks to Square 一个经典的深度优先遍历DFS的应用
- leetcode 491. Increasing Subsequences 一个典型的深度优先遍历DFS的做法
- LeetCode 之 DFS 深度优先遍历
- DFS深度优先遍历算法简单分析
- leetcode 343. Integer Break DFS深度优先遍历 + 最大子元素乘积
- [LeetCode]392. Is Subsequence 判断字符串是否为另一字符串的子串
- mysqldump的基本使用
- lua语言-《lua程序设计》1~5章基础学习
- Spring Boot之前传
- shiro基础知识
- js数组对象去重
- leetcode 392. Is Subsequence 子序列判断 深度优先遍历DFS + 一个很简单的循环
- Java分布式跟踪系统Zipkin(三):Brave源码分析-Tracing
- 【Scikit-Learn 中文文档】预处理数据
- SAP中采购订单历史记录类型文本
- rocket cmd 启动时出现 进程一直死掉
- Windows HLK Getting Started
- Linux网站服务器数据恢复_服务器宕机数据恢复
- POJ 1580 String Matching G++
- SVN分支/合并原理及最佳实践