
✨ 문제
Baseball Games
You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.
You are given a list of strings operations, where operations[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.
- 'D'
- Record a new score that is the double of the previous socre.
- 'C'
- Invalidate the previous score, removing it from the record.
Return the sum of all the scores on the record after applying all the operations.
The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.
https://leetcode.com/problems/baseball-game/description/
✨ 최종코드
class Solution {
public int calPoints(String[] operations) {
Stack<Integer> records = new Stack<>();
int sum = 0;
for (String str : operations) {
if (str.equals("C"))
records.pop();
else if (str.equals("D"))
records.push(records.peek() * 2);
else if (str.equals("+")) {
int tmp1 = records.pop().intValue();
int tmp2 = records.peek() + tmp1;
records.push(tmp1);
records.push(tmp2);
}
else records.push(Integer.parseInt(str));
}
while (!records.isEmpty())
sum += records.pop();
return sum;
}
}
최근 숫자를 사용해야하는 것 같아 스택을 사용해서 풀었다. 점수를 저장할 스택 records와 총점을 저장할 정수형 변수 sum을 선언했다.
상향된 for문으로 operations의 값을 순환하여 문제의 조건에 맞게 코드를 작성하였다. "C"인 경우 가장 최근에 저장된 숫자 제거, "D"인 경우 가장 최근에 저장된 수의 * 2 점, "+"인 경우 가장 최근에 저장된 숫자 2개의 합을 저장하도록 하였고, 그 외는 숫자이므로 바로 push하도록 했다.
foreach문이 끝나면 while로 records의 모든 값을 pop하여 sum에 저장해주었고 마지막으로 stack의 모든 요소가 더해진 값 sum을 반환하도록 하였다.
풀만한 문제가 나와서 조앗따 ^^
'알고리즘 > 99클럽' 카테고리의 다른 글

✨ 문제
Baseball Games
You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.
You are given a list of strings operations, where operations[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.
- 'D'
- Record a new score that is the double of the previous socre.
- 'C'
- Invalidate the previous score, removing it from the record.
Return the sum of all the scores on the record after applying all the operations.
The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.
https://leetcode.com/problems/baseball-game/description/
✨ 최종코드
class Solution { public int calPoints(String[] operations) { Stack<Integer> records = new Stack<>(); int sum = 0; for (String str : operations) { if (str.equals("C")) records.pop(); else if (str.equals("D")) records.push(records.peek() * 2); else if (str.equals("+")) { int tmp1 = records.pop().intValue(); int tmp2 = records.peek() + tmp1; records.push(tmp1); records.push(tmp2); } else records.push(Integer.parseInt(str)); } while (!records.isEmpty()) sum += records.pop(); return sum; } }
최근 숫자를 사용해야하는 것 같아 스택을 사용해서 풀었다. 점수를 저장할 스택 records와 총점을 저장할 정수형 변수 sum을 선언했다.
상향된 for문으로 operations의 값을 순환하여 문제의 조건에 맞게 코드를 작성하였다. "C"인 경우 가장 최근에 저장된 숫자 제거, "D"인 경우 가장 최근에 저장된 수의 * 2 점, "+"인 경우 가장 최근에 저장된 숫자 2개의 합을 저장하도록 하였고, 그 외는 숫자이므로 바로 push하도록 했다.
foreach문이 끝나면 while로 records의 모든 값을 pop하여 sum에 저장해주었고 마지막으로 stack의 모든 요소가 더해진 값 sum을 반환하도록 하였다.
풀만한 문제가 나와서 조앗따 ^^