본문 바로가기

공부하자/Codility

[Codility] Lesson7. Nesting (C#)

문제: app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/

 

Nesting coding task - Learn to Code - Codility

Determine whether a given string of parentheses (single type) is properly nested.

app.codility.com

 

 

 

            List<char> listS = new List<char>(S);
            Stack<char> stackS = new Stack<char>();
            for (int i = 0; i < listS.Count; i++)
            {
                if (listS[i].Equals('('))
                    stackS.Push(listS[i]);
                else
                {
                    if (stackS.Count > 0)
                    {
                        if (stackS.Peek().Equals('('))
                            stackS.Pop();
                    }
                    else
                        return 0;
                }
            }

            return stackS.Count == 0 ? 1 : 0;

 

'공부하자 > Codility' 카테고리의 다른 글

[Codility] Lesson8. Dominator (C#)  (0) 2020.11.20
[Codility] Lesson7. StoneWall (C#)  (0) 2020.11.20
[Codility] Lesson7. Fish (C#)  (0) 2020.11.20
[Codility] Lesson7. Brackets (C#)  (0) 2020.11.20
[Codility] Lesson6. Triangle (C#)  (0) 2020.11.20