Skip to content

Commit 5d33263

Browse files
author
atwood
committed
owe 11, Fxxk
1 parent 3d2feff commit 5d33263

File tree

2 files changed

+126
-7
lines changed

2 files changed

+126
-7
lines changed

No20_Stack.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode_20
6+
{
7+
//static void Main(string[] args)
8+
//{
9+
// var solution = new Solution();
10+
// while (true)
11+
// {
12+
// //int input = int.Parse(Console.ReadLine());
13+
// //int input2 = int.Parse(Console.ReadLine());
14+
// //int input3 = int.Parse(Console.ReadLine());
15+
// string input = Console.ReadLine();
16+
// ////string input2 = Console.ReadLine();
17+
// //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
18+
// //int input2 = int.Parse(Console.ReadLine());
19+
// //int[] intArr = new int[] { 1, 3, 2 };
20+
// //int[] intArr = new int[] { 4, 2, 1, 3, 2, 6, 3 };
21+
// //int[] intArr2 = new int[] { 4, 2, 1, 3, 2, 6, 3 };
22+
// var res = solution.IsValid(input);
23+
// ConsoleX.WriteLine(res);
24+
// }
25+
//}
26+
27+
public class Solution
28+
{
29+
/// <summary>
30+
/// 犹记得以前大学的时候用的string做出来的,哈哈,也不知是怎么写的
31+
/// 时间复杂度:O(n)
32+
/// 空间复杂度:O(n)
33+
/// </summary>
34+
/// <param name="s"></param>
35+
/// <returns></returns>
36+
public bool IsValid(string s)
37+
{
38+
Stack<char> stack = new Stack<char>();
39+
for (int i = 0; i < s.Length; i++)
40+
{
41+
switch (s[i])
42+
{
43+
case '(':
44+
case '[':
45+
case '{': stack.Push(s[i]); break;
46+
case ')':
47+
case ']':
48+
case '}':
49+
if (stack.Count == 0)
50+
return false;
51+
char c = stack.Pop();
52+
if ((c == '(' && s[i] == ')') || (c == '[' && s[i] == ']') || (c == '{' && s[i] == '}'))
53+
break;
54+
else
55+
return false;
56+
default: break;
57+
58+
}
59+
}
60+
return stack.Count == 0;
61+
}
62+
}
63+
}

Program.cs

+63-7
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,80 @@ static void Main(string[] args)
1717
//int input = int.Parse(Console.ReadLine());
1818
//int input2 = int.Parse(Console.ReadLine());
1919
//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();
2222
//int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
2323
//int input2 = int.Parse(Console.ReadLine());
2424
//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);
2929
}
3030
}
3131

3232
public class Solution
3333
{
34-
public double FindMedianSortedArrays(int[] nums1, int[] nums2)
34+
public int StrStr(string haystack, string needle)
3535
{
36+
if (needle == string.Empty)
37+
return 0;
3638

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;
3763
}
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+
//}
3894
}
3995
}
4096
}

0 commit comments

Comments
 (0)