问题描述:
[let code]28.实现strstr()
问题分析:

1.如果s2为空,根据题目提示,返回0下标;
2.遍历被检测字符串s1,每次拿出与检测字符串长度相同长度的子串出来比较;
3.当当前下标之后的长度小于子串,肯定不匹配,返回-1未找到标志;

完整代码:

    int strStr(string s1, string s2) 
    {
        if (s2.size() == 0) return 0;
	    else
	    {
		    string temp;
		    for (int i = 0; i < s1.size(); i++)
		    {
			    if (i + s2.size() > s1.size())
				    return -1;
			    else
			    {
				    temp = s1.substr(i, s2.size());
				    if (temp == s2)
					    return i;
			    }
		    }	
	    }	
	    	
	    return -1;
    }

结果展示:
[let code]28.实现strstr()

相关文章:

  • 2021-08-10
  • 2021-09-15
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2021-08-26
  • 2021-12-18
  • 2021-05-28
猜你喜欢
  • 2021-10-18
  • 2021-04-27
  • 2021-06-24
  • 2021-07-25
  • 2021-12-25
  • 2021-11-28
相关资源
相似解决方案