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, ...
随机推荐
- Qt图形视图框架公开课资料
接受CSDN学院的邀请,讲一次公开课,主题是Qt图形视图框架,报名链接在这里:http://edu.csdn.net/huiyiCourse/detail/228. 内容有两部分:自定义Item和拖放 ...
- 从零开始学习Node.js例子九 设置HTTP头
server.js //basic server的配置文件 ; var server = require('./basicserver').createServer(); server.useFavI ...
- SELF, self in CORE DATA
Predicate SELF Represents the object being evaluated. CORE DATA Retrieving Specific Objects If your ...
- 最近开发的ECG项目
最近参与公司开发了一款读取ECG心电接口程序 原理就是通过心电设备读取患者的心电数据 并生成ECG文件 然后通过ECG客户端程序读取ECG文件 并解析转换成图片 上传至服务器 下边是客户端程序截图 客 ...
- android ImageView组件属性
adjustViewBounds :该属性为真时可以在调整ImageView边界时保持图片的纵横比例(需要与maxHeight 或maxWidth一起使用). scaleType: 属性用以表示显示图 ...
- JSON入门
一.简介 1.描述 1)JavaScript 对象表示法(JavaScript Object Notation) 2)存储和交换文本信息的语法.类似 XML 3)比 XML 更 ...
- LintCode Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- EF里一对一、一对多、多对多关系的配置
EF关系规则 参考文章:http://www.cnblogs.com/feigao/p/4617442.html Entity Framework 实体间的关系,一对一,一对多,多对多,根据方向性来说 ...
- 同时有background-size background-positon 两个属性的时候,如何在合并的background样式中展示
今日写css,遇到background很多属性,于是想合并写,w3c只是说了各个属性都可以合并,但是并没有给出background-size background-positon合并的具体例子 bac ...
- 浏览器 HTTP 协议缓存机制详解
最近在准备优化日志请求时遇到了一些令人疑惑的问题,比如为什么响应头里出现了两个 cache control.为什么明明设置了 no cache 却还是发请求,为什么多次访问时有时请求里带了 etag, ...