- Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathImplementstrStr.cs
More file actions
Latest commit
executable file
·43 lines (40 loc) · 2.11 KB
/
Copy pathImplementstrStr.cs
File metadata and controls
executable file
·43 lines (40 loc) · 2.11 KB
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
// Source : https://leetcode.com/problems/implement-strstr/
// Author : codeyu
// Date : Thursday, October 13, 2016 11:07:52 PM
/**********************************************************************************
*
*
* Implement strStr().
*
*
* Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
*
*
**********************************************************************************/
usingSystem;
usingSystem.Collections.Generic;
usingAlgorithms.Utils;
namespaceAlgorithms
{
publicclassSolution028
{
publicstaticintStrStr(stringhaystack,stringneedle)
{
inti,j;
for(i=j=0;i<haystack.Length&&j<needle.Length;)
{
if(haystack[i]==needle[j])
{
i++;
j++;
}
else
{
i-=j-1;
j=0;
}
}
returnj!=needle.Length?-1:i-j;
}
}
}