본문 바로가기

Java/Java 알고리즘 LeetCode

[LeetCode- Ch6. 큐와 스택] 1. 야구게임 ## (+ Switch, Integer.parseInt, Integer.valueOf)

반응형

 

https://leetcode.com/problems/baseball-game/

 

Baseball Game - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1. 정규표현식을 이용해 음수 / 양수 포함한 숫자 체크

: 음수와 소수까지 처리 가능. 

public static boolean isNumeric(String s) {
        return s != null && s.matches("[-+]?\\d*\\.?\\d+");
    }

 

: 음수와 양수만 처리가능

  public static boolean isNumeric(String s) {
    return s != null && s.matches("[-+]?\\d*\\d+");
}

 

2. 스택에 넣기

class Solution {
    
      public static boolean isNumeric(String s) {
        return s != null && s.matches("[-+]?\\d*\\.?\\d+");
    }
    public int calPoints(String[] arr) {
        Stack<Integer> stack = new Stack<>();
        int answer=0;
        int n=arr.length;
        for(int i=0;i<n;i++){
            if(isNumeric(arr[i])){
                stack.push(Integer.parseInt(arr[i]));
                answer+=Integer.parseInt(arr[i]);
                System.out.println("int"+stack.peek()+" "+answer);
            }else{
                if(arr[i].equals("C")){
                    System.out.println("C "+stack.peek()+" "+answer);
                    answer-=stack.peek();
                    stack.pop();
                }
                else if(arr[i].equals("D")){
                    int tmp=stack.peek();
                    stack.push(tmp*2);
                    answer+=stack.peek();
                    System.out.println("D "+stack.peek()+" "+answer);
                }
                else if(arr[i].equals("+")){
                    int tmp1=stack.pop();
                    System.out.println(tmp1);
                    int tmp2=stack.pop();
                    stack.push(tmp2);
                    stack.push(tmp1);
                    stack.push(tmp1+tmp2);
                    
                    System.out.println("tmp1 "+tmp1+" tmp2 "+tmp2);
                    answer+=tmp1+tmp2;
                    System.out.println("answer "+answer);
                }
            }
            
        }
        return answer;
    }
}

+) 세련된 풀이

#String으로 처리해서 -> 숫자는 마지막에 Integer.valueOf() or Integer.parseInt()로 변환

 

1. 값들을 스택에 넣는다.

3. 연산을 수행한 후 스택에 넣는다.

2. 스택에 있는 값들을 모두 더한다.

 

1. while문 이용

2. switch문 이용

 

1. while문 이용

class Solution {
    public int calPoints(String[] operations) {
        int answer=0;
        int tmp=0;
        Stack<Integer> stack = new Stack<>();
        for(String s:operations){
            if(s.equals("C")){
                stack.pop();
            }else if(s.equals("D")){
                stack.push(stack.peek()*2);
            }else if(s.equals("+")){
                int x=stack.pop();
                int y=stack.pop();
                stack.push(y);
                stack.push(x);
                stack.push(x+y);
            }else{
                stack.push(Integer.valueOf(s));
            }
        }
        while(!stack.isEmpty()) tmp+=stack.pop();
        return tmp;
    }
}

 

2. switch문 이용

for(String s: String[]){

    switch(s){

        case "+": break;

         default:

 }

}

 

for(int i: int[]){

    switch(i){

        case 1: break;

         default:

 }

}

 

class Solution {
        public static int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack<>();
        for (String op : ops) {
            switch (op) {
                case "+":
                    int x = stack.pop();
                    int y = stack.pop();
                    stack.push(y);
                    stack.push(x);
                    stack.push(x + y);
                    break;
                case "D":
                    stack.push(stack.peek() * 2);
                    break;
                case "C":
                    stack.pop();
                    break;
                default:
                    stack.push(Integer.valueOf(op));
            }
        }

        int sum = 0;
        while (!stack.isEmpty()) {
            sum += stack.pop();
        }
        return sum;
    }
}
반응형