首页 > 其他 > 详细

[NCH 1]

时间:2015-06-12 23:41:28      阅读:275      评论:0      收藏:0      [点我收藏+]

Preview:

1. Implement strStr()

O(m*n):

技术分享
 1 class Solution
 2 {
 3 public:
 4     int strStr(string haystack,string needle)
 5     {
 6         for(int i=0;i<=int(haystack.size()-needle.size());i++)
 7         {
 8             int j;
 9             for(j=0;j<needle.size();j++)
10             {
11                 if(haystack[i+j]!=needle[j])
12                     break;
13             }
14             if(j==needle.size())
15                 return i;
16         }
17         return -1;
18     }
19 };
View Code

注意:size()函数返回值是size_t类型,是unsigned的,所以假如有可能为负数的话就会出问题。haystack.size()是有可能小于needle.size()的,有可能为负,因此要加个int强制转换。

O(m+n):

ref: soul

2. 

 

[NCH 1]

原文:http://www.cnblogs.com/forcheryl/p/4572769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!