class Solution {
public String solution(int[] numLog) {
String answer = "";
return answer;
}
}
해결법
방법 1
class Solution {
public String solution(int[] numLog) {
String answer = "";
for (int i = 1; i < numLog.length; i++){
if (numLog[i] - numLog[i - 1] == 1) {
answer += 'w';
} else if (numLog[i] - numLog[i - 1] == -1) {
answer += 's';
} else if (numLog[i] - numLog[i - 1] == 10) {
answer += 'd';
} else {
answer += 'a';
}
}
return answer;
}
}
방법 2
class Solution {
public String solution(int[] numLog) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i < numLog.length; i++) {
int diff = numLog[i] - numLog[i - 1];
if (diff == 1) {
sb.append("w");
} else if (diff == -1) {
sb.append("s");
} else if (diff == 10) {
sb.append("d");
} else if (diff == -10) {
sb.append("a");
}
}
return sb.toString();
}
}
수 조작하기 2 Lv. 0