Baseball Game
2020.12.22
Last updated
2020.12.22
Last updated
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 ith
operation you must apply to the record and is one of the following:
An integer x
- Record a new score of x
.
"+"
- Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
"D"
- Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
"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을 만든다.
2. 반복문 생성 : default문으로 인풋 String의 숫자 원소들은 다 넣는다. 인풋 String이 문자 원소면 그에 해당하는 연산한다. - stack.pop(), stack.peek(), stack.push() 이용
3. Stack의 데이터를 다 더해서 리턴한다.
알고리즘을 Java로 구현
배운 내용 1. switch문과 case문 이용
2. String을 Integer로 바꾸는 방법
3. String 배열의 길이 : StringArray.length