@@ -17,24 +17,80 @@ static void Main(string[] args)
17
17
//int input = int.Parse(Console.ReadLine());
18
18
//int input2 = int.Parse(Console.ReadLine());
19
19
//int input3 = int.Parse(Console.ReadLine());
20
- // string input = Console.ReadLine();
21
- //// string input2 = Console.ReadLine();
20
+ string input = Console . ReadLine ( ) ;
21
+ string input2 = Console . ReadLine ( ) ;
22
22
//int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
23
23
//int input2 = int.Parse(Console.ReadLine());
24
24
//int[] intArr = new int[] { 1, 3, 2 };
25
- int [ ] intArr = new int [ ] { 4 , 2 , 1 , 3 , 2 , 6 , 3 } ;
26
- int [ ] intArr2 = new int [ ] { 4 , 2 , 1 , 3 , 2 , 6 , 3 } ;
27
- solution . FindMedianSortedArrays ( intArr , intArr2 ) ;
28
- // ConsoleX.WriteLine(res);
25
+ // int[] intArr = new int[] { 4, 2, 1, 3, 2, 6, 3 };
26
+ // int[] intArr2 = new int[] { 4, 2, 1, 3, 2, 6, 3 };
27
+ var res = solution . StrStr ( input , input2 ) ;
28
+ ConsoleX . WriteLine ( res ) ;
29
29
}
30
30
}
31
31
32
32
public class Solution
33
33
{
34
- public double FindMedianSortedArrays ( int [ ] nums1 , int [ ] nums2 )
34
+ public int StrStr ( string haystack , string needle )
35
35
{
36
+ if ( needle == string . Empty )
37
+ return 0 ;
36
38
39
+ int ans = - 1 ;
40
+ for ( int i = 0 ; i <= haystack . Length - needle . Length ; i ++ )
41
+ {
42
+ int j = 0 ;
43
+ for ( ; j < needle . Length ; j ++ )
44
+ {
45
+ if ( haystack [ i + j ] != needle [ j ] )
46
+ {
47
+ //sunday算法:不匹配,则查看 待匹配字符串 的后一位字符 c:1.若c存在于Pattern中,则 idx = idx + 偏移表[c] 2.否则,idx = idx + len(pattern)
48
+ int nextStartIndex = needle . IndexOf ( haystack [ i + j ] ) ;
49
+ if ( nextStartIndex == - 1 )
50
+ i = i + needle . Length ;
51
+ else
52
+ i = i + nextStartIndex ;
53
+ break ;
54
+ }
55
+ }
56
+ if ( j == needle . Length )
57
+ {
58
+ ans = i ;
59
+ break ;
60
+ }
61
+ }
62
+ return ans ;
37
63
}
64
+
65
+ /// <summary>
66
+ /// 时间复杂度:O(n),可能这种比较好算的就要算最优和最差了吧,最优时间是O(n),最差时间是O((n-l)n)
67
+ /// 空间复杂度:O(1)
68
+ /// </summary>
69
+ /// <param name="haystack"></param>
70
+ /// <param name="needle"></param>
71
+ /// <returns></returns>
72
+ //public int StrStr(string haystack, string needle)
73
+ //{
74
+ // if (needle == string.Empty)
75
+ // return 0;
76
+
77
+ // int ans = -1;
78
+ // for (int i = 0; i <= haystack.Length - needle.Length; i++)
79
+ // {
80
+ // int j = 0;
81
+ // for (; j < needle.Length; j++)
82
+ // {
83
+ // if (haystack[i + j] != needle[j])
84
+ // break;
85
+ // }
86
+ // if (j == needle.Length)
87
+ // {
88
+ // ans = i;
89
+ // break;
90
+ // }
91
+ // }
92
+ // return ans;
93
+ //}
38
94
}
39
95
}
40
96
}
0 commit comments