-
Notifications
You must be signed in to change notification settings - Fork 1
[PG_17681] 비밀지도 풀이 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| class Solution { | ||
|
|
||
| // 정수 값을 이진수 배열로 변환하는 함수 | ||
| // value: 변환할 정수 | ||
| // length: 결과 이진수 배열의 길이 (지도 가로 길이) | ||
| private static int[] toBinary(int value, int length) { | ||
| int[] answer = new int[length]; | ||
|
|
||
| // 이진수는 뒤에서부터 채워야 하므로, 배열의 끝에서부터 채움 | ||
| for (int i = 0; i < length; i++) { | ||
| answer[length - i - 1] = value % 2; // 2로 나눈 나머지를 저장 (0 또는 1) | ||
| value /= 2; // 다음 비트를 위해 2로 나눔 | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| // 이진수 배열을 문자열로 변환하는 함수 | ||
| // 0은 공백 ' ', 1은 벽 '#' | ||
| private static String toString(int[] binary) { | ||
| StringBuilder answer = new StringBuilder(); | ||
| final char[] TILES = {' ', '#'}; // 매핑 테이블: 0 -> ' ', 1 -> '#' | ||
|
|
||
| for (int bit : binary) { | ||
| answer.append(TILES[bit]); // bit 값에 따라 문자 추가 | ||
| } | ||
|
|
||
| return answer.toString(); // 최종 문자열 반환 | ||
| } | ||
|
|
||
| // 메인 로직 함수 | ||
| // n: 지도의 크기 (정사각형이므로 n x n) | ||
| // arr1, arr2: 두 장의 지도를 나타내는 정수 배열 | ||
| public String[] solution(int n, int[] arr1, int[] arr2) { | ||
| String[] answer = new String[n]; // 정답 배열 (각 행의 문자열 저장) | ||
|
|
||
| for (int row = 0; row < n; row++) { | ||
| // 두 지도의 같은 위치를 OR 연산하여 겹치는 벽 찾기 | ||
| int mergedRow = arr1[row] | arr2[row]; // 비트 OR 연산 | ||
|
|
||
| // OR 연산 결과를 이진수 배열로 변환 후, 문자열로 변환 | ||
| answer[row] = toString(toBinary(mergedRow, n)); | ||
| } | ||
|
|
||
| return answer; // 최종 지도 문자열 배열 반환 | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저라면 귀찮아서 toBinaryString 써서 풀었을 것 같은데.. 대단하십니다 .. 직접 구현하는 방법 알고 갑니다!