728x90
반응형
return = [동쪽으로 떨어진 거리, 북쪽으로 떨어진 거리]
자바
class Solution{
public int[] solution(String route){
int east = 0;
int north = 0;
int[] answer = new int [2];
for(int i=0; i<route.length(); i++){
switch(route.charAt(i)){
case 'N':
north++;
break;
case 'S':
north--;
break;
case 'E':
east++;
break;
case 'W':
east--;
break;
}
}
answer[0] = east;
answer[1] = north;
return answer;
}
}
파이썬
def solution(route):
east = 0
north = 0
for i in route:
if i == "N":
north += 1
elif i == "S":
north -= 1
elif i == "E":
east += 1
elif i == "W":
east -= 1
return [east, north]
728x90
반응형
'취준 > 코딩테스트' 카테고리의 다른 글
[프로그래머스] 대소문자 바꿔서 출력하기 (lv.0) (0) | 2024.01.30 |
---|---|
[프로그래머스] 문자열 반복해서 출력하기 (lv.0) (0) | 2024.01.30 |
[프로그래머스] a와 b 출력하기 (lv.0) (0) | 2024.01.30 |
[프로그래머스] 가채점 (lv.0) (0) | 2024.01.22 |