zoukankan      html  css  js  c++  java
  • 28. Implement strStr() (String)

    Implement strStr().

    Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int firstPos = 0;
            int i, j;
            int haySize = haystack.length();
            int needleSize = needle.length();
            
            if(needleSize == 0) return 0;
            
            for(i = 0; i < haySize; firstPos++){
                i = firstPos;
                for(j = 0; j < needleSize && i < haySize; j++, i++){
                    if(needle[j]!=haystack[i]) break;
                }
                if(j == needleSize) return firstPos;
            }
            return -1;
        }
    };
  • 相关阅读:
    10.7
    10.5
    周六
    周五
    周四
    周三
    四则运算
    zabbix——yum安装
    Stirling's Formula
    CONTRASTIVE REPRESENTATION DISTILLATION
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/4812134.html
Copyright © 2011-2022 走看看