Baseball Game

2020.12.22

You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.

At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ithoperation you must apply to the record and is one of the following:

  1. An integer x - Record a new score of x.

  2. "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.

  3. "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.

  4. "C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.

Return the sum of all the scores on the record.์ž๋ฃŒ๊ตฌ์กฐ : Stack

์•Œ๊ณ ๋ฆฌ์ฆ˜ 1. ์ •์ˆ˜ํ˜• Stack์„ ๋งŒ๋“ ๋‹ค.

Stack<Integer> stack = new Stack<>();

2. ๋ฐ˜๋ณต๋ฌธ ์ƒ์„ฑ : default๋ฌธ์œผ๋กœ ์ธํ’‹ String์˜ ์ˆซ์ž ์›์†Œ๋“ค์€ ๋‹ค ๋„ฃ๋Š”๋‹ค. ์ธํ’‹ String์ด ๋ฌธ์ž ์›์†Œ๋ฉด ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์—ฐ์‚ฐํ•œ๋‹ค. - stack.pop(), stack.peek(), stack.push() ์ด์šฉ

for(String str : ops) {
    if(str == "C") {//์•ž์— ์›์†Œ ์‚ญ์ œ}
    if(str == "D") {//์•ž์— ์›์†Œ 2๋ฐฐ}
    if(str == "+") {//์•ž์— ์›์†Œ 2๊ฐœ ๋ง์…ˆ}
    stack.push(Integer.valueOf(str));
}

3. Stack์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ๋”ํ•ด์„œ ๋ฆฌํ„ดํ•œ๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ Java๋กœ ๊ตฌํ˜„

public class BaseballGame {

	
	public static void main(String[] args) {
		String[] strs = {"5","-2","4","C","D","9","+","+"};
		System.out.println(calPoints(strs));
	}

	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;
    }
}

๋ฐฐ์šด ๋‚ด์šฉ 1. switch๋ฌธ๊ณผ case๋ฌธ ์ด์šฉ

for(String str : ops) {
    switch(str) {
        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.pop()*2);
            break;
        case "C" :
            stack.pop();
            break;
        dafualt:
            stack.push(Integer.valueOf(str));
}

2. String์„ Integer๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•

Integer.valueOf(String);
Integer.parseInt(String);

3. String ๋ฐฐ์—ด์˜ ๊ธธ์ด : StringArray.length

Last updated

Was this helpful?