llllmz

导航

28. 找出字符串中第一个匹配项的下标

 

决定转前端了。先用c++刷题刷着先

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(haystack.size() < needle.size()) return -1;
        for(int i = 0; i <= haystack.size() - needle.size(); ++i){
            int j = 0;
            while(j < needle.size()){
                if(haystack[i + j] == needle[j]){
                    ++j;
                }else{
                    break;
                }
            }
            if(j == needle.size()) return i; 
        }
        return -1;
    }
};

 

posted on 2024-10-19 18:20  神奇的萝卜丝  阅读(5)  评论(0编辑  收藏  举报