Post

leetcode(리트코드)416-Partition Equal Subset Sum

leetcode 416 - Partition Equal Subset Sum 문제입니다.

1. 문제

https://leetcode.com/problems/decode-string/


2. Input , Output


3. 분류 및 난이도

Medium 난이도 문제입니다.
leetcode Top 100 Liked 문제입니다.


4. 문제 해석

  • string이 들어옵니다. 숫자만큼 ‘[]’ 에 있는 것들을 반복해야합니다.
  • 결과를 리턴합니다.
  • discuss 참조했습니다.

5. code

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
    string decodeString(string s) {
        stack<string> chars;
        stack<int> nums;
        int num = 0;
        string res = "";
        
        for(char c : s){
            if(isdigit(c)){
                num = num * 10 + (c-'0');
            }
            else if(isalpha(c)){
                res.push_back(c);
            }
            else if(c =='['){
                chars.push(res);
                nums.push(num);
                num = 0;
                res = "";
            }
            else if(c==']'){
                string tmp = res;
                for(int i =0;i<nums.top()-1; ++i)
                    res += tmp;
                res = chars.top() + res;
                chars.pop(), nums.pop();
            }

        }
        return res;
    }
};


6. 결과 및 후기, 개선점

c++ 100%

This post is licensed under CC BY 4.0 by the author.