[LintCode]strStr(C++)

来源:互联网 发布:米惠淘宝返利网 编辑:程序博客网 时间:2024/06/07 06:10
class Solution {public:    /**     * Returns a index to the first occurrence of target in source,     * or -1  if target is not part of source.     * @param source string to be scanned.     * @param target string containing the sequence of characters to match.     */    int strStr(const char *source, const char *target) {        // write your code here        int i = 0;        int j = 0;                if(source == NULL || target == NULL) return -1;                int lens = strlen(source);        int lent = strlen(target);        for (i = 0; i < lens - lent + 1; i++) {            for (j = 0; j < lent; j++) {                if (source[i + j] != target[j]) {                    break;                }            }            if (j == lent) {                return i;            }        }        return -1;    }};

原创粉丝点击