스택 문제풀이

강영우·2024년 2월 20일
0

문제1

문자열 뒤집기

입출력 예시

입력: "Hello"
출력: "OlleH"

입력: 1 3 5 7 9
출력: 9 7 5 3 1

public static String reverseString(String str) {
    Stack stack = new Stack();
    for (int i=0; i<str.length(); i++){
        stack.push(str.charAt(i));
    }
    StringBuilder sb= new StringBuilder();
    while(!stack.isEmpty()) {
        sb.append(stack.pop());
    }
    return sb.toString();
}

public static void main(String[] args) {
    // Test code
    String result = reverseString("Hello");
    System.out.println("result = " + result);

    result = reverseString("1 3 5 7 9");
    System.out.println("result = " + result);
}

문제2

괄호 짝 검사

입출력 예시

입력: "("
출력: Fail

입력: ")"
출력: Fail

입력: "()"
출력: Pass

public static void checkParenthesis(String str) {
    Stack stack = new Stack();
    boolean checkFlag = true;
    for(String s: str.split("")){
        if(s.equals("(")){
            stack.push(s);
        } else if(s.equals(")")){
            if(!stack.isEmpty()) {
                stack.pop();
            } else {
                checkFlag = false;
            }
        }
    }
    if(checkFlag && stack.isEmpty()){
        System.out.println("PASS");
    } else {
        System.out.println("FAIL");
    }

}

public static void main(String[] args) {
    // Test code
    checkParenthesis("(");          // FAIL!
    checkParenthesis(")");          // FAIL!
    checkParenthesis("()");         // PASS!
    checkParenthesis("()()()");     // PASS!
    checkParenthesis("(())()");     // PASS!
    checkParenthesis("(((()))");    // FAIL!
}

문제 3

후위표기법 연산
숫자두개를 쓰고 연산자를 쓰는 표기법일때 계산기를 만드시오

입출력예시

입력: "2 2 +"
출력: 4

입력: "2 2 -"
출력: 0

public static double calculate(String string) {
    Stack<Double> stack = new Stack();
    for(String s: string.split(" ")){
        if(s.equals("+")){
            stack.push(stack.pop() + stack.pop());
        } else if (s.equals("-")){
            stack.push(- stack.pop() + stack.pop());
        } else if (s.equals("*")){
            stack.push(stack.pop() * stack.pop());
        } else if (s.equals("/")){
            stack.push(1 / stack.pop() * stack.pop());
        } else{
            stack.push(Double.parseDouble(s));
        }
    }
    return stack.pop();
}

public static void main(String[] args) {
    // Test code
    System.out.println(calculate("2 2 +"));    // 4
    System.out.println(calculate("2 2 -"));    // 0
    System.out.println(calculate("2 2 *"));    // 4
    System.out.println(calculate("2 2 /"));    // 1

    System.out.println(calculate("1 1 + 2 * 3 * 2 / 5 -"));    // 1
    System.out.println(calculate("5 2 * 3 - 8 * 4 /"));        // 14

}

문제 4

두 문자열 비교
단, #은 backspace 로 바로 이전의 문자를 삭제하는 기능이라고 가정

입출력 예시

입력: s1 = "tree", s2 = "th#ree"
출력: true

입력: s1 = "ab#a", s2 = "aab#"
출력: true

입력: s1 = "wo#w", s2 = "ww#o"
출력: false

public static boolean stringCompare(String s1, String s2) {
    Stack<String> stack1 = new Stack<>();
    Stack<String> stack2 = new Stack<>();
    for(String s: s1.split("")){
        if(s.equals("#")){
            stack1.pop();
        } else {
            stack1.push(s);
        }
    }
    for (String s: s2.split("")){
        if(s.equals("#")){
            stack2.pop();
        } else {
            stack2.push(s);
        }
    }
    return stack1.toString().equals(stack2.toString());
}

public static void main(String[] args) {
    // Test code
    String s1 = "tree";
    String s2 = "th#ree";
    System.out.println(stringCompare(s1, s2));

    s1 = "ab#a";
    s2 = "aab#";
    System.out.println(stringCompare(s1, s2));

    s1 = "wo#w";
    s2 = "ww#o";
    System.out.println(stringCompare(s1, s2));
}
profile
배움의 연속을 매순간 저장하는

0개의 댓글