Leetcode

来源:互联网 发布:淘宝卖家信誉怎么算 编辑:程序博客网 时间:2024/06/07 11:54
  1. IsSubsequence
    Python

    class Solution(object):
    def isSubsequence(self, s, t):
    """
    :type s: str
    :type t: str
    :rtype: bool
    """
    for i, ele in enumerate(s):
    if ele in t:
    pos = t.index(ele)
    t = t[pos+1:]
    else:
    return False
    return True

    C

    bool isSubsequence(char* s, char* t) {
    int ls, lt,i,j,start=0;
    ls = strlen(s);
    lt = strlen(t);
    for (i = 0; i < ls; i++) {
    if (start >= lt)
    return false;
    for (j = start; j < lt; j++) {
    if (s[i] == t[j]) {
    start = j + 1;
    break;
    }
    else if (j >= lt - 1)
    return false;
    }
    }
    return true;
    }

    大神就是大神
bool isSubsequence(char* s, char* t) {    while (*t)        s += *s == *t++;    return !*s;}

如果s与t匹配,则*s==*t++为1,所以s的指针加1。主要思路就是遍历t与s中字符匹配,匹配到则指针加1,当t遍历完,检查s是否还剩有元素。对c的指针用法还是不熟练

0 0
原创粉丝点击