Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- hash table
- SQL
- Matrix
- bit manipulation
- Method
- Number Theory
- geometry
- string
- 코딩테스트
- two pointers
- sorting
- Counting
- 자바
- dynamic programming
- implement
- 코테
- database
- 파이썬
- Class
- Data Structure
- Stack
- greedy
- array
- Binary Tree
- simulation
- Math
- Binary Search
- java
- 구현
- Tree
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 682. Baseball Game 본문
1. Input
1) String[] operations
2. Output
1) 다음 규칙에 따라 계산한 결과를 int로 반환
// operations[i]가
- 정수: 해당 점수를 기록한다.
- +: 이전의 두 점수를 더한 값을 기록한다.
- C: 마지막 점수 기록을 없앤다.
- D: 마지막 점수의 2배를 기록한다.
3. Constraint
1) 1 <= operations.length <= 1000
2) operations[i]는 "C", "D", "+"와 [- 3*10^4, 3* 10^4]범위의 정수를 나타내는 문자열로 이루어져 있다.
3) “+" 앞에는 적어도 점수가 2개 있다.
4) "C"와 "D" 앞에는 적어도 점수가 1개 있다.
4. Example
Input: operations = ["5","2","C","D","+"] -> Output: 30
설명:
- "5": 5점 추가. 점수판=[5]
- "2": 2점 추가. 점수판=[5, 2]
- "C": 마지막 점수 제거. 점수판=[5]
- "D": 마지막 점수 2배. 점수판=[5, 10]
- "+": 이전 두 점수 더한 값을 기록. 점수판=[5, 10, 15]
- 따라서 5 + 10 + 15 = 30을 반환한다.
5. Code
1) 첫 코드(2023/03/16)
ArrayList<Integer> list = new ArrayList<Integer>();
for(String s : operations){
switch(s){
case "C": list.remove(list.size()-1); break;
case "D": list.add(list.get(list.size()-1)*2); break;
case "+": list.add(list.get(list.size()-1)+list.get(list.size()-2)); break;
default: list.add(Integer.valueOf(s));
}
}
int answer = 0;
for(int i=0 ; i<list.size() ; i++)
answer += list.get(i);
return answer;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 27866. 문자와 문자열 (0) | 2023.03.17 |
---|---|
[LeetCode/Easy] 2529. Maximum Count of Positive Integer and Negative Integer (0) | 2023.03.16 |
[LeetCode/Easy] 2535. Difference Between Element Sum and Digit Sum of an Array (0) | 2023.03.13 |
[LeetCode/Medium] 677. Map Sum Pairs (0) | 2023.03.13 |
[LeetCode/Easy] 674. Longest Continuous Increasing Subsequence (0) | 2023.03.13 |