标签:mil ret ring data problems code href 题意 strong
【 声明:版权全部,转载请标明出处,请勿用于商业用途。 联系信箱:libin493073668@sina.com】
题目链接:https://leetcode.com/problems/implement-strstr/
题意:
给定两个串,判定needle串是否haystack串的子串,假设是,返回匹配的起始位置。假设不是,则返回-1
思路:
直接两个循环暴力解决
class Solution { public: int strStr(string haystack, string needle) { int len1 = haystack.length(),len2 = needle.length(); int i,j; if(len2>len1) return -1; if(len1 == len2 && haystack!=needle) return -1; for(i = 0; i<=len1-len2; i++) { for(j = 0; j<len2; j++) { if(haystack[i+j]!=needle[j]) break; } if(j == len2) return i; } return -1; } };
[LeedCode OJ]#28 Implement strStr()
标签:mil ret ring data problems code href 题意 strong
原文地址:http://www.cnblogs.com/lytwajue/p/7191214.html