expression expand(Stack)

Given an expression s includes numbers, letters and brackets. Number represents the number of repetitions inside of the brackets(can be a string or another expression). Please expand expression to be a string.

Example

s = abc2[d] return abcdd
s = 3[2[az]3[k]]xyz return azazkkkazazkkkazazkkkxyz
//solution 1
//use stack and an Element class.
class Element {
    public String str;
    public int num;
    public Element(String str) {
        this.str = str;
    }
    public Element(int num) {
        this.num = num;
    }
}
public class Solution {
    public String expressionExpand(String s) {
        Stack stack = new Stack<>();
        int number = 0;
        
        for (char c : s.toCharArray()) {
            if (Character.isDigit(c)) {
                number = number * 10 + c - '0';
            } else if (c == '[') {
                stack.push(new Element(number));
                number = 0;
            } else if (c == ']') {
                String newStr = popStack(stack);
                Element elem = stack.pop();
                for (int i = 0; i < elem.num; i++) {
                    stack.push(new Element(newStr));
                }
            } else {
                stack.push(new Element(String.valueOf(c)));
            }
        }
        
        return popStack(stack);
    }
    
    //pop stack untill get a number of empty
    private String popStack(Stack stack) {
        Stack buffer = new Stack<>();
        while (!stack.isEmpty() && stack.peek().str != null) {
            buffer.push(stack.pop().str);
        }
        
        StringBuilder sb = new StringBuilder();
        while (!buffer.isEmpty()) {
            sb.append(buffer.pop());
        }
        
        return sb.toString();
    }
}
//solution 2
//use object stack => Stack
public String expressionExpand(String s) {
    Stack stack = new Stack<>();
    char[] arr = s.toCharArray();
    
    int num = 0;
    for(char c : arr){
       if(Character.isDigit(c)){
           num = num * 10 + c - '0';
       }
       else if(c == '['){
           stack.push(Integer.valueOf(num));
           num = 0;
       }
       else if(c == ']'){
           popStack(stack);
       }
       else{
           stack.push(c);
       }
    }
    popStack(stack);
    return stack.pop().toString();
}
private void popStack(Stack stack){
    StringBuilder add = new StringBuilder();
    int count;
    Stack buffer = new Stack();
    while(!stack.isEmpty() && stack.peek() instanceof Integer == false){
        buffer.push(stack.pop());
    }
    while(!buffer.isEmpty()){
        add.append(buffer.pop());
    }
    
    count = stack.isEmpty()? 1 : (Integer) stack.pop();
    StringBuilder part = new StringBuilder(add);
    if(count > 0){
        for(int i = 0; i < count - 1; i++)
            add.append(part);
        stack.push(add);// reput
    }
}
 

                            
                        
                    
                    
                    

你可能感兴趣的:(expression expand(Stack))