Skip to content

Commit 2db19d2

Browse files
committed
refactoring
1 parent 97cf870 commit 2db19d2

File tree

8 files changed

+12
-131
lines changed

8 files changed

+12
-131
lines changed
-71.1 KB
Binary file not shown.

ImplementStrStr/Debug/makefile

Lines changed: 0 additions & 59 deletions
This file was deleted.

ImplementStrStr/Debug/objects.mk

Lines changed: 0 additions & 8 deletions
This file was deleted.

ImplementStrStr/Debug/sources.mk

Lines changed: 0 additions & 27 deletions
This file was deleted.

ImplementStrStr/Debug/src/ImplementStrStr.d

Lines changed: 0 additions & 1 deletion
This file was deleted.
-70.3 KB
Binary file not shown.

ImplementStrStr/Debug/src/subdir.mk

Lines changed: 0 additions & 24 deletions
This file was deleted.

ImplementStrStr/src/ImplementStrStr.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
// or null if needle is not part of haystack.
66
//============================================================================
77

8-
#include <iostream>
9-
using namespace std;
8+
#include <cstring>
109

1110
class Solution {
1211
public:
1312
char *strStr(char *haystack, char *needle) {
14-
if (haystack == NULL || needle == NULL || !*needle) return haystack;
15-
char *p1e = haystack, *p1 = haystack, *p2 = needle;
16-
while (*++p2) p1e++;
17-
while (*p1e) {
18-
char *p1b = p1;
19-
char *p2 = needle;
20-
while (*p1 && *p2 && *p1 == *p2) ++p1, ++p2;
21-
if (!*p2) return p1b;
22-
p1 = p1b + 1;
23-
p1e++;
13+
if (!haystack || !needle) return haystack;
14+
int n = strlen(haystack);
15+
int m = strlen(needle);
16+
int i = 0;
17+
while (i < n-m+1) {
18+
int j = 0;
19+
while (j < m && haystack[i] == needle[j]) {
20+
i++, j++;
21+
}
22+
if (j == m) return haystack+(i-j);
23+
i = i-j+1;
2424
}
2525
return NULL;
2626
}

0 commit comments

Comments
 (0)