博客
关于我
LeetCode 392 判断子序列[贪心] HERODING的LeetCode之路
阅读量:386 次
发布时间:2019-03-05

本文共 729 字,大约阅读时间需要 2 分钟。

在这里插入图片描述解题思路:

同时遍历两个字符串,使用双指针,一个遍历t,一个遍历s是否能与t匹配,直到某一个遍历结束,观察s是否遍历结束,结束说明是子序列,否则不是,代码如下:

class Solution {   public:    bool isSubsequence(string s, string t) {           // 定义长度        int len1 = s.length(), len2 = t.length();        if(len1 > len2) {               return false;        }        // 定义索引        int index1 = 0, index2 = 0;        while(index1 < len1 && index2 < len2) {               // 如果当前位置相等,更新s的索引            if(s[index1] == t[index2]) {                   index1 ++;            }            index2 ++;        }        // 返回是否遍历完s        return index1 >= len1;    }};/*作者:heroding链接:https://leetcode-cn.com/problems/is-subsequence/solution/ctan-xin-by-heroding-gbcp/来源:力扣(LeetCode)著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/
你可能感兴趣的文章
node基础(二)_模块以及处理乱码问题
查看>>
node安装卸载linux,Linux运维知识之linux 卸载安装node npm
查看>>
node安装及配置之windows版
查看>>
Node实现小爬虫
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node提示:npm does not support Node.js v12.16.3
查看>>
Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
查看>>
Node服务在断开SSH后停止运行解决方案(创建守护进程)
查看>>
node模块化
查看>>
node模块的本质
查看>>
node环境下使用import引入外部文件出错
查看>>
node环境:Error listen EADDRINUSE :::3000
查看>>
Node的Web应用框架Express的简介与搭建HelloWorld
查看>>
Node第一天
查看>>
node编译程序内存溢出
查看>>
Node读取并输出txt文件内容
查看>>