반응형
수 조작하기
문제 설명


요구사항
- control에 따라 1, -1, 10, -10을 n에 더하여 리턴
테스트
package lv0;
import java.util.HashMap;
import java.util.Map;
public class 수_조작하기_1 {
public int solution(int n, String control) {
int answer = n;
Map<Character, Integer> map = new HashMap<Character, Integer>();
//문자마다 가중치
map.put('w', 1);
map.put('s', -1);
map.put('d', 10);
map.put('a', -10);
//한글자씩 읽어 가중치 더하기
for(int i = 0; i < control.length(); i++) {
answer += map.get(control.charAt(i));
}
return answer;
}
}
프로그래머스
import java.util.HashMap;
import java.util.Map;
class Solution {
public int solution(int n, String control) {
int answer = n;
Map<Character, Integer> map = new HashMap<Character, Integer>();
map.put('w', 1);
map.put('s', -1);
map.put('d', 10);
map.put('a', -10);
for(int i = 0; i < control.length(); i++) {
answer += map.get(control.charAt(i));
}
return answer;
}
}
결과

반응형
'코딩테스트 > 프로그래머스 Lv. 0' 카테고리의 다른 글
| [프로그래머스] Lv. 0 수열과 구간 쿼리 3 JAVA (0) | 2025.02.03 |
|---|---|
| [프로그래머스] Lv. 0 수 조작하기 2 JAVA (0) | 2025.02.03 |
| [프로그래머스] Lv. 0 마지막 두 원소 JAVA (0) | 2025.02.03 |
| [프로그래머스] Lv. 0 이어 붙인 수 JAVA (1) | 2025.02.02 |
| [프로그래머스] Lv. 0 원소들의 곱과 합 JAVA (0) | 2025.02.02 |