코딩테스트/프로그래머스 Lv. 0

[프로그래머스] Lv. 0 수 조작하기 1 JAVA

촙오 개발자 2025. 2. 3. 22:05
반응형

수 조작하기

 

문제 설명

요구사항

  • 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;
    }
}

 

결과

반응형