Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Rstudio
- Q타입클래스
- RProgramming
- 알고리즘
- 이클립스
- 프로그래머스
- R명령어
- cor()
- R프로그래밍
- git
- r
- stepfilter
- 자바
- 한글깨지는문제
- str()
- querydsl적용하기
- 이중배열
- 머신러닝
- summary()
- JPA
- java
- Spring
- LIKE검색
- core.autocrlf
- queryDSL
- 머신러닝프로세스
- DTO사용이유
- git오류
- Eclipse
- programmers
Archives
- Today
- Total
놀고 싶어요
[Programmers/Java] 프로그래머스 비밀지도 문제풀이 본문
public class 비밀지도 {
public static void main(String[] args) {
System.out.println(Arrays.toString(solution(5, new int[]{9, 20, 28, 18, 11}, new int[]{30, 1, 21, 17, 28})));
}
public static String[] solution(int n, int[] arr1, int[] arr2) {
char[][] map1 = changeTwoArray(n, arr1);
char[][] map2 = changeTwoArray(n, arr2);
char[][] newMap = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newMap[i][j] = (char) (map1[i][j] | map2[i][j]);
}
}
String[] answer = new String[n];
for (int i = 0; i < n; i++) {
StringBuilder temp = new StringBuilder();
for (int j = 0; j < n; j++) {
// 1일 때에는 #
if(newMap[i][j]=='1') {
temp.append("#");
} else {
temp.append(" ");
}
}
answer[i] = temp.toString();
// System.out.println(temp);
}
return answer;
}
// 2차 char 배열로 변경
private static char[][] changeTwoArray(int n, int[] arr) {
char[][] map = new char[n][n];
for (int i = 0; i < arr.length; i++) {
String temp = "";
while(arr[i]>0) {
temp = (arr[i]%2) + temp;
arr[i] = arr[i]/2;
}
// temp = String.format("%0"+n+"d", Integer.parseInt(temp)); // 런타임에러발생 (숫자 범위 초과 문제 인 듯..)
temp = String.format("%"+n+"s", temp); // 0말고 그냥 빈칸으로 해준다.
map[i] = temp.toCharArray();
// System.out.println(Arrays.toString(map[i]));
}
return map;
}
}

풀이를 완료하고 보니 Integer.toBinaryString를 이용하여 푼 풀이가 있었다.
public class 비밀지도2 {
public static void main(String[] args) {
System.out.println(Arrays.toString(solution(5, new int[]{9, 20, 28, 18, 11}, new int[]{30, 1, 21, 17, 28})));
}
public static String[] solution(int n, int[] arr1, int[] arr2) {
String[] answer = new String[n];
for (int i = 0; i < n; i++) {
answer[i] = Integer.toBinaryString(arr1[i]|arr2[i]);
answer[i] = String.format("%"+n+"s", answer[i]);
answer[i] = answer[i].replaceAll("1", "#");
answer[i] = answer[i].replaceAll("0", " ");
}
return answer;
}
}

'Algorithm > Programmers' 카테고리의 다른 글
| [Programmers/Java] 프로그래머스 타겟 넘버 문제풀이 (0) | 2021.10.17 |
|---|---|
| [Programmers/Java] 프로그래머스 폰켓몬 문제풀이 (0) | 2021.09.14 |