File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * ํ์ด
3
+ * - stack ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํฉ๋๋ค
4
+ *
5
+ * Big O
6
+ * - N: ์ฃผ์ด์ง ๋ฌธ์์ด s์ ๊ธธ์ด
7
+ *
8
+ * - Time complexity: O(N)
9
+ * - ๋ฌธ์์ด s ์ ์ฒด๋ฅผ ์ํํ ๊ฒฝ์ฐ ์คํ์๊ฐ์ N์ ์ ํ์ ์ผ๋ก ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค
10
+ * - Space complexity: O(N)
11
+ * - "((((((...((((((" ์ ๊ฐ์ ์
๋ ฅ์ ๋ฐ์ผ๋ฉด stack์ ํฌ๊ธฐ๊ฐ ์ต๋ N๊น์ง ์ฆ๊ฐํฉ๋๋ค
12
+ */
13
+
14
+ class Solution {
15
+ public:
16
+ bool isValid (string s) {
17
+ stack<char > st;
18
+ for (char ch : s) {
19
+ if (ch == ' (' || ch == ' {' || ch == ' [' ) {
20
+ st.push (ch);
21
+ } else {
22
+ if (st.empty ()) return false ;
23
+ else if (st.top () == ' (' && ch == ' )' ) st.pop ();
24
+ else if (st.top () == ' {' && ch == ' }' ) st.pop ();
25
+ else if (st.top () == ' [' && ch == ' ]' ) st.pop ();
26
+ else return false ;
27
+ }
28
+ }
29
+ return st.empty ();
30
+ }
31
+ };
You canโt perform that action at this time.
0 commit comments