-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0028-Implement-strStr.cs
More file actions
53 lines (46 loc) · 1.45 KB
/
0028-Implement-strStr.cs
File metadata and controls
53 lines (46 loc) · 1.45 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
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0028.Implement_strStr
{
public class _0028_Implement_strStr
{
public int StrStr(string haystack, string needle)
{
// Solution 3
// Implement the StrStr method without using the implemented method.
if (needle.Length == 0) return 0;
if (needle.Length > haystack.Length) return -1;
int i = 0, j = 0, index = 0;
while (i < haystack.Length)
{
if (haystack[i] == needle[j])
{
// accumulate
i++;
j++;
if (j == needle.Length) return index;
}
else
{
// recalculate
index++;
i = index;
j = 0;
}
}
return -1;
// Solution 2
// 直接使用 IndexOf 方法回傳結果
// By default, it return -1 if nothing is found.
//return haystack.IndexOf(needle);
// Solution 1
//if (string.IsNullOrWhiteSpace(haystack) && string.IsNullOrWhiteSpace(needle))
// return 0;
//if (haystack.Contains(needle))
// return haystack.IndexOf(needle);
//else
// return -1;
}
}
}