leetcode 392. Is Subsequence

实测|龙虾机器人(OpenClaw)Windows系统部署全攻略(含避坑指南) 先简单科普下:龙虾机器人本质是一款开源AI代理框架,核心优势是“能行动、可本地、高灵活”——它不内置大模型,需要对接第三方AI接口(如GPT、Claude、阿里云百炼等),但能将AI的指令转化为实际的系统操作,相当于给AI配了一个“能动手的身体”,这也是它和普通对话大模型的核心区别。今天就结合自己的实测经历,从环境准备、分步部署、初始化配置,到常见问题排查,写一篇保姆级攻略,不管是新手还是有一定技术基础的同学,都能跟着一步步完成部署,少走弯路~解决方案:检查聊天软件插件是否安装成功,权限是否配置完整; 阅读详情

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?

题目中问s是否是t的子序列,而且表示t会很长

思路:

  1. 简单粗暴的逐个字符比较,如果只有一个s一个t的比较还是可以的,对follow up的问题有很多s一个接一个地和t比较来说就比较慢了
    i指向s,j指向 t
    当结束时如果i指向s的末尾就表示s是t的子序列
//18ms
    public boolean isSubsequence(String s, String t) {
        if (s == null || t == null) {
            return false;
        }
        
        int i = 0;
        int j = 0;
        
        while (i < s.length() && j < t.length()) {
            if (s.charAt(i) == t.charAt(j)) {
                i ++;
                j ++;
            } else {
                j ++;
            }
        }
        
        if (i >= s.length()) {
            return true;
        }
        
        return false;
    }

代码精简后如下:

//13ms
    public boolean isSubsequence(String s, String t) {
        if (s == null || t == null) {
            return false;
        }
        
        int i = 0;
        int j = 0;
        
        while (i < s.length() && j < t.length()) {
            if (s.charAt(i) == t.charAt(j++)) {
                i ++;
            } 
        }
      
        return (i == s.length());
    }

2.对follow up中很多s一个接一个和t比较的问题,要在t上作文章,
参照https://www.cnblogs.com/grandyang/p/5842033.html的方法2

字母就只有26个,而t很长,对应的位置会有很多个,所以用hashMap来保存每个字母对应的位置
*说到hashMap,又习惯性地想起了它和hashTable的区别,这里不需要考虑线程安全,就用hashMap

直接引用 “然后遍历字符串s中的每个字符,对于每个遍历到的字符c,我们到 HashMap 中的对应的字符数组中去搜索,由于位置数组是有序的,我们使用二分搜索来加快搜索速度,这里需要注意的是,由于子序列是有顺序要求的,所以需要一个变量 pre 来记录当前匹配到t字符串中的位置,对于当前s串中的字符c,即便在t串中存在,但是若其在位置 pre 之前,也是不能匹配的。”

但是这个方法cpp要跑100ms

//100ms
    bool isSubsequence(string s, string t) {
        int pre = -1;
        unordered_map<char, vector<int>> pos;
        
        for (int i = 0; i < t.size(); i++) {
            pos[t[i]].push_back(i);
        }
        
        for (int i = 0; i < s.size(); i++) {
            auto it = upper_bound(pos[s[i]].begin(), pos[s[i]].end(), pre);
            if (it == pos[s[i]].end()) {
                return false;
            }
            pre = *it;
        }
        
        return true;
    }
R语言中如何获取前向选择(forward selection)选出的变量 前向选择是一种常用的特征选择方法,可以在给定一个预测模型的情况下,逐步添加变量来确定最佳的预测模型。在R语言中,可以使用适当的函数和库来执行前向选择,并获取选出的变量。以下是一个详细的步骤,展示了如何使用R语言执行前向选择并获得选出的变量。通过按照上述步骤,你可以使用R语言执行前向选择,并获取选出的变量。记住,前向选择是一种特征选择方法,可以根据不同的评估指标选择最佳的预测模型。接下来,准备用于前向选择的数据。使用"summary"函数查看前向选择的摘要,并使用"which"函数获取被选中的变量。 阅读详情

相关推荐

Modelsim安装教程以及管理员身份运行patch_dll.bat时找不到文件mgls.dll的解决

Modesim安装教程 Modesim版本有很多,但是安装方式大都相同,我们以modesim.10.5为例,电脑使用的是win10 64位,讲一下安装过程中的步骤以及出现问题的解决方法 一 安装 1.首先解压安装包,在解压的安装包内选择应用程序,右键选择管理员运行。 2.稍等一会,有的电脑会弹出是否允许modesim进行更改,选择 是 即可,然后出现下面的界面,点击下一步。 3.弹出此界面选择安装路径,选择安装路径,我是在D盘新建的文件夹,直接把文件夹中的路径复制在框内。文件夹名称切记不要带中文 4.

weixin_48564372的博客 1万+

【Leetcode】392. Is Subsequence

【代码】【Leetcode】392. Is Subsequence。

数学、算法爱好者的博客 188

历年CSP-J(NOIP普及组)考点分析与分类汇总(纯干货)

noip/csp-j历年真题考点分类

weixin_42358969的博客 1万+

[leetcode] 392. Is Subsequence 解题报告

题目链接:https://leetcode.com/problems/is-subsequence/ 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

小榕流光的专栏 2537

OpenCL编译环境配置

在 Linux 系统上配置 OpenCL 开发环境需要安装适当的驱动、运行时和开发工具。

深之JohnChen的专栏 2032

msf后渗透之获取登入password、远程控制、调用摄像头

在terminal输入load kili creds_all直接获取密码(不一定成功) 使用kiwi_cmd sekurlsa::logonpasswords获取密码

m0_61506558的博客 2968

【LeetCode】392. Is Subsequence 解题报告(Python)

【LeetCode】392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subsequence/description/ 题目描述: Given a string s and a string t, check if s is subsequence of t. Y...

负雪明烛 3475

向华为学习——解读99页华为战略解码【附全文阅读】

摘要:本方案针对企业战略落地难题,系统介绍华为战略解码方法论。内容涵盖BLM模型等核心工具,聚焦差距分析、市场洞察等关键环节,详细拆解战略到KPI、PBC的落地路径,配套组织保障和执行督导体系。适用于高管及战略规划人员,提供从战略制定到执行的全流程解决方案,帮助企业实现战略与执行的高效协同。

cdfunlove的博客 344

392.leetcode Is Subsequence (medium)[判断一个字符串是否是另一个字符串的子串]

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) strin

qz530308783的专栏 2029

【Halcon-2D测量】add_metrology_object_rectangle2_measure 函数功能(用于向2D计量模型中添加旋转矩形(rectangle2类型)计量对象)

HALCON算子add_metrology_object_rectangle2_measure解析摘要 该算子是HALCON 2D计量模块的核心算子,用于添加旋转矩形计量对象并配置测量参数。主要功能包括:定义旋转矩形几何轮廓(中心坐标、主轴角度、长短半长);生成垂直于矩形边界的测量区域;设置边缘提取预处理参数(高斯平滑、振幅阈值)。关键注意事项:MeasureLength1必须小于矩形长短半长;角度参数Phi需使用弧度单位;多线程需同步模型句柄。该算子适用于方形螺母、PCB板边框等矩形零件的精密测量,需与a

weixin_45590420的博客 1235

【图像超分】论文复现:Pytorch实现SRCNN!保姆级复现教程!提供完整代码和最优权重模型(x2、x3、x4)!科研级论文曲线图!计算平均PSNR和SSIM!代码Windows和Linux都可用!

由于很多同学反应没用过jupyter notebook,更习惯用pycharm项目的形式跑代码。于是,本文将用pycharm项目的形式,重新复现SRCNN!【图像超分】论文复现:新手入门!Pytorch实现SRCNN,数据预处理、模型训练、测试、评估全流程详解,注释详细,简单修改就可以训练你自己的图像数据,有训练好的模型下载地址,随取随用。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 4723

392. Is Subsequence [JavaScript]

一、解题思路   本题要求判断字符串s是否为字符串t的子序列,很简单的思路:起始下标和终止下标向中间扫描。 const isSubsequence = (s, t) =&amp;gt; { if (s.length === 0) { return true } const l1 = s.length - 1 const l2 = t.length - 1 let s1 = 0...

descire 275

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Leetcode 392. Is Subsequence

Leetcode 392. Is Subsequence 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 ...

fengran_mark的博客 176

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode OJ-392.Is Subsequence

LeetCode OJ-392.Is Subsequence题目描述 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

oYinGui1 688

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode: 392. Is Subsequence

题目: LeetCode: 392. Is Subsequence(https://leetcode.com/problems/is-subsequence/description/) class Solution { public boolean isSubsequence(String s, String t) { int currentLoc = 0; byte[] byte...

majinliang1234的博客 200

Leetcode 392 Is Subsequence

Leetcode 392 Is SubsequenceDescriptioniven 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 v

Zeongwan的博客 283

LeetCode //C - 392. Is Subsequence

【代码】LeetCode //C - 392. Is Subsequence。

Made in Code 1025

LeetCode 392. Is Subsequence

392. Is SubsequenceGiven 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 ~=

mingjc的博客 347

LeetCode 392 Is Subsequence

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,

几百个测试用例一致通过 251

[leetcode]392. Is Subsequence

[leetcode]392. Is Subsequence Analysis 早鸭—— [每天刷题并不难0.0] 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...

weixin_32135877的博客 167

【LeetCode】392. Is Subsequence

题目: 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,0

gmr_vhp的博客 348

LeetCode 392. Is Subsequence 解题报告

LeetCode 392. Is Subsequence 解题报告

骆小坑的填坑之旅 1431

LeetCode 392 is subsequence

脑子秀逗了,看过的题目做的时候还是会忘记。晚上笔试的时候遇到判断是否为字符子串的问题。其实自己之前就在LeetCode上做过,但是!!!丝毫!!没有印象!!!而且!!!这道题很简单的啊!小拳拳锤我胸口! 下面进入正题: Given a string s and a string t, check if s is subsequence of t. You may ass...

暗时间 240
上一篇: leetcode 376.Wiggle Subsequence (greedy O(n))
下一篇: leetcode 406. Queue Reconstruction by Height (greedy)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址