首页 > 其他 > 详细

Implement strStr()

时间:2014-04-23 10:48:00      阅读:508      评论:0      收藏:0      [点我收藏+]

Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

Solution: Brute force method: check in the haystack one by one. If not equal to needle, reset the pointer.
More solutions can be referred as http://leetcode.com/2010/10/implement-strstr-to-find-substring-in.html
Advanced algorithms such as KMP can be also found via the above link

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     char *strStr(char *haystack, char *needle) {
 4         while(1) {
 5             char* h = haystack, *n = needle;
 6             while(*n != \0 && *h == *n) {
 7                 h++; n++;
 8             }
 9             if(*n == \0) return haystack;
10             if(*h == \0) return NULL;
11             haystack++;
12         }
13     }
14 };
bubuko.com,布布扣

 

Implement strStr(),布布扣,bubuko.com

Implement strStr()

原文:http://www.cnblogs.com/zhengjiankang/p/3682057.html

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