์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿˆ

๋ฌธ์ œ ํ’€์ด[Stack] - ๊ด„ํ˜ธ ๋ฌธ์ œ, ๊ณ„์‚ฐ๊ธฐ ๋ฌธ์ œ, ๋ธŒ๋ผ์šฐ์ € ๊ตฌํ˜„

Jeein0313 2023. 7. 12. 23:46

๊ด„ํ˜ธ ๋ฌธ์ œ

 

public static boolean isBalanced(String str){
    Stack<Character> stack = new Stack<>();

    for(int i=0;i<str.length();i++){
        char c = str.charAt(i);
        if(c=='('||c=='{'||c=='['){
            stack.push(c);
        }else if(c==')'||c=='}'||c==']'){
            if(stack.isEmpty()){
                return false;
            }
            char top = stack.pop();
            if(((c==')')&&top!='(') || ((c=='}')&&top!='{') || ((c==']')&&top!='[')){
                return false;
            }
        }
    }
    return stack.isEmpty();
}

 

 

๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„(๊ด„ํ˜ธ ์žˆ๋Š” ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ ๋ฌธ์ œ)

 

์ค‘์œ„ ์ˆ˜์‹์„ ํ›„์œ„ ์ˆ˜์‹์œผ๋กœ ๋ฐ”๊พธ๋ฉด์„œ ํ›„์œ„ ์ˆ˜์‹์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ์Œ.

 

public static double calculate(String str){
    Stack<Double> numbers = new Stack<>();
    Stack<Character> operators = new Stack<>();

    int len = str.length();
    for(int i=0;i<len;i++){
        char ch = str.charAt(i);
        if(Character.isDigit(ch)){
            double num = ch - '0';
            while(i+1<len && Character.isDigit(str.charAt(i+1))){
                num = num * 10 + (str.charAt(i + 1) - '0');
                i++;
            }
            numbers.push(num);
        } else if (ch == '(') {
            operators.push(ch);
        } else if (ch ==')'){
            while(operators.peek()!='('){
                double result = applyOperation(operators.pop(), numbers.pop(), numbers.pop());
                numbers.push(result);
            }
            operators.pop();
        }else if(ch == '+'|| ch == '-' || ch=='*' || ch=='/'){
            while(!operators.isEmpty() && hasPrecedence(ch, operators.peek())){
                double result = applyOperation(operators.pop(), numbers.pop(), numbers.pop());
                numbers.push(result);
            }
            operators.push(ch);
        }
        while(!operators.isEmpty()){
            double result = applyOperation(operators.pop(), numbers.pop(), numbers.pop());
            numbers.push(result);
        }
    }
    return numbers.pop();
}

public static double applyOperation(char operator, double b, double a){
    switch(operator){
        case '+':
            return a+b;
        case '-':
            return a-b;
        case '*':
            return a*b;
        case '/':
            if (b==0){
                throw new UnsupportedOperationException("Cannot divide by zero.");
            }
            return a / b;
    }
    return 0;
}

public static boolean hasPrecedence(char op1, char op2){
    if(op2 == '(' || op2 == ')'){
        return false;
    }
    if((op1 == '*' || op1 == '/') && (op2 == '+' || op2 =='-') ){
        return false;
    }
    return true;
}

 

 

๋ธŒ๋ผ์šฐ์ € ๊ตฌํ˜„ํ•˜๊ธฐ

 

๋ธŒ๋ผ์šฐ์ € ์•ž์œผ๋กœ ๊ฐ€๊ธฐ, ๋’ค๋กœ ๊ฐ€๊ธฐ, ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ์ด๋™์„ ์Šคํƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด๋ณด๋Š” ๋ฌธ์ œ

 

public static ArrayList<Stack> browserStack(String[] actions, String start){
    Stack<String> prev = new Stack<>();
    Stack<String> curr = new Stack<>();
    Stack<String> next = new Stack<>();

    ArrayList<Stack> result = new ArrayList<>();

    curr.push(start);

    for(int i=0;i<actions.length;i++){
        try{
            int b = Integer.parseInt(actions[i]);
            if(b==-1 && !prev.isEmpty()){
                next.push(curr.pop());
                curr.push(prev.pop());
            }else if(b==1 && !next.isEmpty()){
                prev.push(curr.pop());
                curr.push(next.pop());
            }
        }catch (NumberFormatException e){
            prev.push(curr.pop());
            curr.push(actions[i]);
            if(!next.isEmpty())
                next.clear();
        }
    }

    result.add(prev);
    result.add(curr);
    result.add(next);

    return result;
}