LeetCode "Is Subsequence"
There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) complexity (TLE), DivideConquer can only be worse. Greedy can be O(n)! And its code is amazingly brief:
class Solution {
public:
bool isSubsequence(string s, string t) {
size_t lens = s.length();
size_t lent = t.length();
int is = , it = ;
while(is < lens && it < lent)
{
char cs = s[is], ct = t[it];
if(cs != ct) it ++;
else is++, it++;
}
return is == lens;
}
};
LeetCode "Is Subsequence"的更多相关文章
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- LeetCode "Wiggle Subsequence" !
Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, ...
- [LeetCode] Is Subsequence 题解
前言 这道题的实现方法有很多,包括dp,贪心算法,二分搜索,普通实现等等. 题目 Given a string s and a string t, check if s is subsequence ...
- LeetCode——Is Subsequence
Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...
- leetcode 376Wiggle Subsequence
用dp解 1)up定义为nums[i-1] < nums[i] down nums[i-1] > nums[i] 两个dp数组, up[i],记录包含nums[i]且nums[i-1] & ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- Sql Server 之 for xml (path,raw,auto,root)
1.for xml path('str') select ID,CreateTime from dbo.ArticleInfo for xml Path('mytitle') 结果:(注意:如果是s ...
- ajax登陆提示
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...
- iOS 源代码管理工具之SVN
源代码管理工具之SVN 源代码管理工具SVN是一款非常强大的源代码管理工具,现在国内70%-90%的公司都在使用SVN来管理源代码,下面就让小编给大家着重介绍一下SVN的使用,SVN的使用主要分为下面 ...
- a标签不跳转
<a href="javascript://">父级菜单</a> 结果是这种写法在一些浏览器下不能到达预期效果(无跳转),我没有花时间把这种写法在主流浏览器 ...
- PHP构造方法
构造方法 1.(特殊)对象创建完成后第一个自动调用的方法: 2.(特殊)方法名比较特殊,方法名可以与类相同: 3.给对象中的成员赋初始值: class boyfriend{ public $name; ...
- eclipse创建maven web项目
eclipse创建maven web项目: 1.安装eclipse maven插件 2.新建maven project选择webapp模板. 3.改造为maven文档结构. 4.添加项目的JAVAEE ...
- laypage
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> < ...
- find和grep的区别
find命令:在系统当中搜索符合条件的文件名,如果需要匹配,使用通配符匹配,通配符是完全匹配. grep命令:在文件当中搜索符合条件的字符串,如果需要匹配,使用正侧表达式匹配 正侧表达式是包含匹配.
- web服务器软件集成包问题
今天下了wampserver,运行错误缺少dll,然后下载apprev也是,最后下了个wampserver 32位了终于没问题了.
- 《C与指针》第十三章练习
本章例程 13.1类型无关的链表查找 #include <stdio.h> #include "node.h" Node *search_list(Node *node ...