码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode5 Implement strstr() 实现strstr函数功能

时间:2016-08-06 09:58:19      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

  Implement strstr() 实现strstr函数功能

    whowhoha@outlook.com

Question:

Implement strstr(). Returns the index of the first occurrence of needle in haystack, or –1

if needle is not part of haystack.

 

int strStr(string haystack, string needle)

{

        for (int i = 0; ; i++) {

                 for (int j = 0; ; j++) {

                         if (j == needle.length()) {

                                  return i;

                         }

                         if (i + j == haystack.length()) {

                                  return -1;

                         }

                         if (needle[j] != haystack[i + j]) {

                                  break;

                         }

                 }

        }

}

leetcode5 Implement strstr() 实现strstr函数功能

标签:

原文地址:http://www.cnblogs.com/whowhoha/p/5743287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!