문제: app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
Brackets coding task - Learn to Code - Codility
Determine whether a given string of parentheses (multiple types) is properly nested.
app.codility.com
char[] ArrayS = S.ToCharArray();
Stack<char> StackS = new Stack<char>();
for (int i = 0; i < ArrayS.Length; i++)
{
if (ArrayS[i].Equals('[') || ArrayS[i].Equals('{') || ArrayS[i].Equals('('))
{
StackS.Push(ArrayS[i]);
}
else
{
if (StackS.Count < 1)
return 0;
switch (ArrayS[i])
{
case ']':
if (StackS.Pop() != '[')
return 0;
break;
case '}':
if (StackS.Pop() != '{')
return 0;
break;
case ')':
if (StackS.Pop() != '(')
return 0;
break;
}
}
}
return StackS.Count == 0 ? 1 : 0;
'공부하자 > Codility' 카테고리의 다른 글
[Codility] Lesson7. Nesting (C#) (0) | 2020.11.20 |
---|---|
[Codility] Lesson7. Fish (C#) (0) | 2020.11.20 |
[Codility] Lesson6. Triangle (C#) (0) | 2020.11.20 |
[Codility] Lesson6. NumberOfDiscIntersections (C#) (0) | 2020.11.20 |
[Codility] Lesson6. MaxProductOfThree (C#) (0) | 2020.11.20 |