[White] Day28 road brush title leetcode394. Decoded string (matching brackets)

Given a coded string, it returns the decoded string.

Encoding rule is: k [encoded_string], wherein the inside square brackets represents encoded_string exactly repeated k times. Note k ensure a positive integer.

You can think of the input string is always valid; the input string no extra spaces, and enter the square brackets always conform to the required format.

Further, the raw data that you can not contain numbers, all figures only represent the number of repetitions k, 3a such as an input or as 2 [4] does not occur.

Example:

S = ". 3 [A] 2 [BC]", Back "aaabcbc."
S = ". 3 [A2 [C]]", Back "accaccacc."
S = "2 [ABC]. 3 [CD] EF" return "abcabccdcdcdef".

source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/decode-string
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

The first submission of code:

class Solution {
public:
    string decodeString(string s) {
        stack<char> stk;
        string res;
        int i=0;
        
        while (i<s.size()){
            if (s[i] == '['){
                ++i;
                
                while(s[i] != ']'){
                    stk.push(s[i]);
                    ++i;
                }
                
                string tmp;
                while (stk.top() != '['){
                    tmp = tmp.insert(0, 1, stk.pop());
                }
            
                int n = int(stk.pop());
                for (int j=0; j<n; ++j)
                    res = res + tmp;
                continue;
            }
            
            if (s[i]>='0' && s[i]<='9'){
                stk.push(s[i]);
                ++i;
                continue;
            }
                
            res = res + s[i];        
        }
        
        return res;        
    }
};

Question 1 templates are not familiar with, there must be many mistakes

2 without problem for nesting processing logic error.

 

The key is not familiar with C ++ template programming, lost the motivation to continue to think, I began to look at other people's code for AC

Reference @ YouLookDeliciousC code and submit their own codes:

class Solution {
public:
    string decodeString(string s) {
        stack<string> str_stack;
        stack<int>  num_stack;
        string tmp;
        int i=0, count = 0;;
        
        while (i<s.size()){
            if (s[i]>='0' && s[i]<='9'){
                count = count * 10 + (s[i]-'0');
                ++i;
                continue;
            }
            
            if (s[i] == '['){
                num_stack.push(count);
                count = 0;
                
                //str_stack.top() = str_stack.top() + tmp;
                str_stack.push(tmp);
                tmp = "";
                
                ++i;
                continue;               
            }
            
            if (s[i] == ']'){
                for (int j=0; j<num_stack.top(); ++j){
                    str_stack.top() = str_stack.top() + tmp;
                }
                tmp = str_stack.top();
                
                num_stack.pop();
                str_stack.pop();                
                
                ++i;
                continue;
            }
                
            tmp = tmp + s[i];
            ++i;        
        }
        
        return tmp;        
    }
};

By before, there is a bug:

if (s[i]>='0' && s[i]<='9'){
   count = count * 10 + int(s[i]);

Python probably write more, resulting in the syntax, debugging results come out a lot "aaaaaaa ......", with the vs2012 debugging found that "3" to the 51, it turned out to be '3' asc code direct conversion.

 

while-continue miscellaneous code is again Comparative @ YouLookDeliciousC use for loop control code.

class Solution {
public:
    string decodeString(string s) {
        stack<string> str_stack;
        stack<int>  num_stack;
        string tmp;
        int count = 0;
        
        for (int i=0; i<s.size(); ++i){
            if (s[i]>='0' && s[i]<='9'){
                count = count * 10 + (s[i]-'0');
            }
            else if (s[i] == '['){
                num_stack.push(count);
                str_stack.push(tmp);
                
                count = 0;                
                tmp = "";               
            }
            else if (s[i] == ']'){
                for (int j=0; j<num_stack.top(); ++j){
                    str_stack.top() Str_stack.top = () + tmp; 
                } 
                tmp = str_stack.top (); 
                
                num_stack.pop (); 
                str_stack.pop (); 
            } 
            the else 
                tmp = tmp + S [I]; 
        } 
        
        return tmp;         
    } 
}; 
// I think that this code has been streamlined to a simple no Jane.

Because they do not know the character is not the whole letter, so if-else at the end.

 

to sum up:

1, this problem belongs to the classic problem of stack data structure, the basic problem;

2. Harvest:

  • char type character turn int:
int count = 0;
for (int i=0; i<string.size(); ++i)
    if (s[i]>=0 && s[i]<=9)
        count = count * 10 + (s[i] - '0');

Found a bit much to explain the operation of the library functions, and then have the opportunity to see;

  • C ++ STL stack in use

stack<string> str_stack;

 

stack.push()

stack.pop (); Note stack.pop () does not return any value.

stack.top()

 

Guess you like

Origin www.cnblogs.com/ACStrive/p/11599709.html