

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String str = sc.next();
System.out.println(solution(n, str));
sc.close();
}
private static String solution(int n, String str) {
String answer = "";
for(int i = 0; i<n; i++) {
String tmp = str.substring(0,7).replace('#', '1').replace('*', '0');
int num = Integer.parseInt(tmp, 2);
answer += (char)num;
str = str.substring(7);
}
return answer;
}
문제풀이
1. 문자의 개수와 문자열을 solution의 매개변수로 넘겨준다.
2. answer라는 문자열을 만든다.
3. 임시변수 tmp에 str을 0~6번 배열까지 자르고 이를 replace '#'은 '1'로 '*'은 '0'으로
4. int num에 Integer.parseInt를 사용하여 tmp가 2진수를 10진수로 바꿔준다.
5. answer에 (char)num으로 문자로 바꿔서 answer에 더해준다.
6. str의 앞 부분을 잘라준다.
7. (반복)
'코딩테스트 문제풀이' 카테고리의 다른 글
| 전화번호 목록 (0) | 2021.10.31 |
|---|---|
| 가장 큰 수 (0) | 2021.10.31 |
| 문자열 압축 (0) | 2021.10.24 |
| 가장 짧은 문자거리 (0) | 2021.10.24 |
| 숫자만 추출 (0) | 2021.10.17 |