Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19-InSange #72

Merged
merged 2 commits into from
Jul 31, 2024
Merged

19-InSange #72

merged 2 commits into from
Jul 31, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Jul 13, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

reverse-substrings-between-each-pair-of-parentheses

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

3์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. s๋Š” ์†Œ๋ฌธ์ž์™€ (, )๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด๋‹ค.
๊ฐ ๊ด„ํ˜ธ๋Š” Layer์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.
์ฒ˜์Œ์—๋Š” ๋ฌธ์ž์—ด์ด ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์˜ค์ง€๋งŒ (๊ฐ€ ์‹œ์ž‘๋˜๋Š” ์ˆœ๊ฐ„ ํ•ด๋‹น Layer์— ์žˆ๋Š” ๋ฌธ์ž์—ด๋“ค์€ ์ˆœ์„œ๊ฐ€ ๋’ค์ง‘ํžŒ๋‹ค.

ํ’€์ด ๋ฐฉ๋ฒ•

  1. )๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์ž๋“ค์„ ์Šคํƒ์—๋‹ค๊ฐ€ ์ง‘์–ด๋„ฃ๋Š”๋‹ค.
  2. )๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ ์Šคํƒ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž๋ฅผ word๋ณ€์ˆ˜์— ๋„ฃ๊ณ  ๋‹ค์‹œ ์Šคํƒ์—๋‹ค๊ฐ€ ์ง‘์–ด ๋„ฃ๋Š”๋‹ค. (=> ๋ฌธ์ž์—ด์„ ํ•œ๋ฒˆ ๋’ค์ง‘์Œ)
  3. ์ •๋‹ต์—๋‹ค๊ฐ€ ์ถ”๊ฐ€ํ•œ๋‹ค.

๋‹จ, ์ฃผ์˜ํ•ด์•ผํ•  ์ 
s = "(abcd)"
stack = a <- b <- c <- d ๋กœ ์ €์žฅ๋œ๋‹ค.
)๋ฅผ ๋งŒ๋‚˜๊ฒŒ๋˜๋ฉด
word = dcba
stack = d<-c<-b<-a๋กœ ์ €์žฅ๋œ๋‹ค.
์ถœ๋ ฅํ• ๋•Œ ํ•œ๋ฒˆ ๋” ๋’ค์ง‘์–ด์ฃผ๋ฉด d c b a๋กœ ์ถœ๋ ฅ์ด ๋˜๊ฒŒ ๋œ๋‹ค! => ans = stack->top() + ans;

#include <iostream>
#include <stack>

using namespace std;

class Solution {
public:
    string reverseParentheses(string s) {
        stack<char> st;

        string ans;
        string word;

        ans = "";

        for (char c : s)
        {
            if (c == ')')
            {
                word = "";
                while (!st.empty() && st.top() != '(') {
                    word += st.top();
                    st.pop();
                }
                if (!st.empty())
                {
                    st.pop();
                }
                for (char w : word)
                {
                    st.push(w);
                }
            }
            else
            {
                st.push(c);
            }
        }

        while (!st.empty())
        {
            ans = st.top() + ans;
            st.pop();
        }


        return ans;
    }
};

image

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์„ธ์ƒ ๊ฟ€ ๋ฌธ์ œ๋ฅผ ํ‘ธ์‹  ์ค„ ์•Œ์•˜๋Š”๋ฐ ๊ด„ํ˜ธ์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌ๊ฐ€ ์กฐ๊ธˆ ๋ณต์žกํ•  ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ์˜€๋„ค์š”.. String ์จ์„œ ์กฐ๊ธˆ ๋ณต์žกํ•œ ํ’€์ด๋ฅผ ์ƒ๊ฐํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์Šคํƒ์„ push, popํ•˜๋ฉด ์ˆœ์„œ๊ฐ€ ๋’ค์ง‘ํžˆ๋Š” ๊ฑธ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ตฐ์š”.. ํ•ญ์ƒ ๋ณด๋ฉด ์ ์žฌ์ ์†Œ์— ์ ์ ˆํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ฐธ ์ž˜ ์“ฐ์‹œ๋Š” ๊ฒƒ ๊ฐ™๋„ค์š” ใ…Žใ…Ž ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค

์˜์–ด ์ฝ๊ธฐ ๊ท€์ฐฎ์€๋ฐ ๋ฐฑ์ค€ ๋ฌธ์ œ๋กœ ๋‹ค์‹œ ๋ณต๊ท€ํ•˜์‹ค ์ƒ๊ฐ์€ ์—†์œผ์‹ ์ง€..?

@InSange
Copy link
Collaborator Author

InSange commented Jul 23, 2024

์„ธ์ƒ ๊ฟ€ ๋ฌธ์ œ๋ฅผ ํ‘ธ์‹  ์ค„ ์•Œ์•˜๋Š”๋ฐ ๊ด„ํ˜ธ์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌ๊ฐ€ ์กฐ๊ธˆ ๋ณต์žกํ•  ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ์˜€๋„ค์š”.. String ์จ์„œ ์กฐ๊ธˆ ๋ณต์žกํ•œ ํ’€์ด๋ฅผ ์ƒ๊ฐํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์Šคํƒ์„ push, popํ•˜๋ฉด ์ˆœ์„œ๊ฐ€ ๋’ค์ง‘ํžˆ๋Š” ๊ฑธ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ตฐ์š”.. ํ•ญ์ƒ ๋ณด๋ฉด ์ ์žฌ์ ์†Œ์— ์ ์ ˆํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ฐธ ์ž˜ ์“ฐ์‹œ๋Š” ๊ฒƒ ๊ฐ™๋„ค์š” ใ…Žใ…Ž ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค

์˜์–ด ์ฝ๊ธฐ ๊ท€์ฐฎ์€๋ฐ ๋ฐฑ์ค€ ๋ฌธ์ œ๋กœ ๋‹ค์‹œ ๋ณต๊ท€ํ•˜์‹ค ์ƒ๊ฐ์€ ์—†์œผ์‹ ์ง€..?

ํ› ํ›  leetcode์™€ ๊ฐ™์€ ์™ธ๊ตญ ์ฝ”๋”ฉ์‚ฌ์ดํŠธ๋Š” ๊ธฐ๋ณธ๊ธฐ๋ฅผ ๋‹จ๋ จํ•˜๊ธฐ์— ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๊ตฌ๊ธ€, ์•„๋งˆ์กด๊ณผ ๊ฐ™์€ ๋จธ๊ธฐ์—…๋“ค๋„ ์ฐธ๊ณ ํ•˜๊ฑฐ๋‚˜ ๋น„์Šทํ•œ ์œ ํ˜•์˜ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€๋‹ค๋ณด๋‹ˆ ์˜์–ด ๊ณต๋ถ€๋„ ํ•  ๊ฒธ ์ฝ”๋”ฉ ์‹ค๋ ฅ๋„ ๋Š˜ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ผ์„์ด์กฐ ์•„๋‹๊นŒ์š”? ๋‹น๋ถ„๊ฐ„์€ ์‰ฌ์—ˆ๋˜๋งŒํผ ์กฐ๊ธˆ ๊ธฐ์ดˆ๋ฅผ ๋‹จ๋ จํ•˜๊ณ  ๋ฐฑ์ค€ ๊ณจ,ํ”Œ ๋ฌธ์ œ๋กœ ๋Œ์•„๊ฐˆ ๊ฒƒ ๊ฐ™์Šด๋‹ค

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ด„ํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์Šคํƒ์— ์ €์žฅํ•˜๊ณ  pop์—ฐ์‚ฐ์œผ๋กœ ์ž˜ ์ฒ˜๋ฆฌํ•˜์…จ๊ตฐ์š”. ์Šคํƒ์˜ "๋‚˜์ค‘์— ๋“ค์–ด๊ฐ„ ๊ฒŒ ์ œ์ผ ์ฒ˜์Œ ๋‚˜์˜ค๋Š”"ํŠน์ง•์„ ๋ฌธ์ œ์— ์ž˜ ํ™œ์šฉํ•˜์‹ ๊ฒŒ ์ธ์ƒ ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค!

@InSange InSange merged commit a59bee6 into main Jul 31, 2024
@InSange InSange deleted the 19-InSange branch July 31, 2024 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants