题目
判断子序列_牛客题霸_牛客网 (nowcoder.com)
Python
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param S string字符串
# @param T string字符串
# @return bool布尔型
#
class Solution:def isSubsequence(self , S: str, T: str) -> bool:# write code hereif len(S)>len(T):return Falseps=pt=0while ps<len(S) and pt<len(T):if S[ps]==T[pt]:ps+=1pt+=1return ps==len(S)
C++
class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param S string字符串 * @param T string字符串 * @return bool布尔型*/bool isSubsequence(string S, string T) {// write code hereif(S.size()>T.size()) return false;int ps=0,pt=0;while(ps<S.size() && pt<T.size()){if(S[ps]==T[pt]) ps++;pt++;}return ps==S.size();}
};
C语言
/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param S string字符串 * @param T string字符串 * @return bool布尔型*/#include <string.h>
bool isSubsequence(char* S, char* T )
{// write code hereif(strlen(S)>strlen(T)) return 0;int ps=0,pt=0;while(ps<strlen(S) && pt<strlen(T)){if(S[ps]==T[pt]) ps++;pt++;}return ps==strlen(S);
}