내 답안
class Solution {
public int solution(int n, String control) {
for(int i=0; i<control.length(); i++){
switch(control.charAt(i)){
case 'w':
n += 1;
break;
case 's':
n -= 1;
break;
case 'd':
n += 10;
break;
case 'a':
n -= 10;
break;
}
}
return n;
}
}
Java
복사
다른 사람 풀이법
class Solution {
public int solution(int n, String control) {
int answer = n;
for(char ch : control.toCharArray()) {
switch(ch) {
case 'w': answer += 1; break;
case 's': answer -= 1; break;
case 'd': answer += 10; break;
case 'a': answer -= 10; break;
default:break;
}
}
return answer;
}
}
Java
복사
class Solution {
public int solution(int n, String control) {
for(char c : control.toCharArray()){
n += c == 'w' ? 1 : c == 's' ? -1 : c == 'd' ? 10 : -10;
}
return n;
}
}
Java
복사