forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplement_strStr.cpp
46 lines (42 loc) · 894 Bytes
/
Implement_strStr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jun 28, 2012
Problem: Implement strStr()
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if
needle is not part of haystack.
Solution:
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
char *strStr(char *haystack, char *needle) {
int len1 = strlen(haystack);
int len2 = strlen(needle);
bool flag;
for (int i = 0; i < len1 - len2 + 1; i++) {
flag = true;
for (int j = 0; j < len2; j++) {
if (haystack[i + j] != needle[j]) {
flag = false;
break;
}
}
if (flag) {
return haystack + i;
}
}
return NULL;
}
};