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
- SQL
- 코테
- Binary Tree
- 자바
- Stack
- sorting
- geometry
- simulation
- hash table
- java
- two pointers
- bit manipulation
- Binary Search
- Data Structure
- 구현
- array
- dynamic programming
- Matrix
- 코딩테스트
- Math
- implement
- Class
- Number Theory
- database
- 파이썬
- Tree
- string
- greedy
- Method
- Counting
Archives
- Today
- Total
코린이의 소소한 공부노트
[프로그래머스/Lv.0] 컨트롤 제트 본문
1. Input
1) 숫자와 공백, “Z”로 이루어진 문자열 s
2. Output
1) 문자열이 뜻하는 값
2) “Z”가 나오면 바로 앞 숫자를 뺀다는 뜻이다.
3. Constraint
1) 0 < s의 길이 < 1,000
2) -1,000 < s의 원소 중 숫자 < 1,000
3) s에 있는 숫자와 "Z"는 서로 공백으로 구분된다.
4) 연속된 공백은 주어지지 않는다.
5) 0을 제외하고는 0으로 시작하는 숫자는 없다.
6) s의 시작과 끝에는 공백이 없다.
7) 모든 숫자를 지우는 경우는 주어지지 않는다.
8) 지울 숫자가 없는 상태에서 "Z"는 무시한다.
4. Example
Input: s=“1 2 Z 3” -> Output: 4
Input: s=“1 2 Z Z 3” -> Output: 3
설명:
- “Z”를 만나면 “Z” 앞 숫자를 빼고 “1 3”만 남음 -> 4
- “Z”를 만나면 “Z” 앞 숫자를 빼고 “1 Z 3”이 남음 -> 다시 “Z”를 만나 Z” 앞 숫자를 빼면 “3”이 남음 -> 3
5. Code
1) 첫 코드(2022/10/28)
import java.util.ArrayList;
// main()
int answer = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
String[] str = s.split(" ");
// "Z"를 만나면 앞 숫자 제거
for(int i=0 ; i<str.length ; i++){
if(str[i].equals("Z") && list.size()!=0)
list.remove(list.size()-1);
else
list.add(Integer.valueOf(str[i]));
}
// 남은 숫자 더하기
for(int i=0 ; i<list.size() ; i++)
answer += list.get(i);
return answer;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[프로그래머스/Lv.0] 369게임 (0) | 2022.10.28 |
---|---|
[프로그래머스/Lv.0] 가까운 수 (0) | 2022.10.28 |
[프로그래머스/Lv.0] 삼각형의 완성조건 (1) (0) | 2022.10.28 |
[프로그래머스/Lv.0] 중복된 문자 제거 (0) | 2022.10.28 |
[프로그래머스/Lv.0] 배열 원소의 길이 (0) | 2022.10.28 |