내 답안
class Solution {
public String solution(String code) {
String answer = "";
int mode = 0;
for (int i = 0; i < code.length(); i++) {
if (code.charAt(i) == '1') mode = 1 - mode;
else if (i % 2 == mode) answer += code.charAt(i);
}
return "".equals(answer) ? "EMPTY" : answer;
}
}
Java
복사
다른 사람 풀이법
class Solution {
public String solution(String code) {
StringBuilder answer = new StringBuilder();
int mode = 0;
for (int i = 0; i < code.length(); i++) {
char current = code.charAt(i);
if (current == '1') {
mode = mode == 0 ? 1 : 0;
continue;
}
if (i % 2 == mode) {
answer.append(current);
}
}
return answer.length() == 0 ? "EMPTY" : answer.toString();
}
}
Java
복사
class Solution {
public String solution(String code) {
int mode = 0;
String ret = "";
for (int idx = 0; idx < code.length(); idx++) {
if (code.charAt(idx) == '1') {
mode = 1 - mode;
} else if (mode == 0 && idx % 2 == 0) {
ret += code.charAt(idx);
} else if (mode == 1 && idx % 2 == 1) {
ret += code.charAt(idx);
}
}
if (ret.equals("")) {
return "EMPTY";
} else {
return ret;
}
}
}
Java
복사