Skip to content
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

21.06.26 - [PG] 비밀지도 #97

Closed
suhyunsim opened this issue Jun 27, 2021 · 0 comments
Closed

21.06.26 - [PG] 비밀지도 #97

suhyunsim opened this issue Jun 27, 2021 · 0 comments
Assignees
Labels
lv.1 프로그래머스 - level 1 문자열 문자열 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jun 27, 2021

문제

핵심 아이디어

  • Integer.toBinaryString()
  • String.format("%" + n + "s", str)
    • %s: 문자열 형식, n개 만큼

어려운 점, 실수

  • 처음에 스택과 큐로 풀이하려다가 너무 복잡해져서 시간이 오래걸림
  • 이진수 변환하는 것, String format 사용하는걸 놓침

풀이

package main.java.com.poogle.PG.Q17681;

import java.util.Arrays;

public class Solution {
    public static String[] solution(int n, int[] arr1, int[] arr2) {
        String[] answer = new String[n];
        for (int i = 0; i < n; i++) {
            String str = Integer.toBinaryString(arr1[i] | arr2[i]);
            //앞에 채워서 최대 5자로 만들기 (문자열 형식 %s에 개수 n을 붙인 상태)
            str = String.format("%" + n + "s", str);
            str = str.replaceAll("1", "#");
            str = str.replaceAll("0", " ");
            answer[i] = str;
        }
        return answer;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(solution(5, new int[]{1, 20, 28, 18, 11}, new int[]{2, 1, 21, 17, 28})));
    }
}
@suhyunsim suhyunsim self-assigned this Jun 27, 2021
@suhyunsim suhyunsim added lv.1 프로그래머스 - level 1 문자열 문자열 실패 시도했지만 맞지 못한 문제 labels Jun 28, 2021
@suhyunsim suhyunsim added this to the 6월 4주 차 milestone Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.1 프로그래머스 - level 1 문자열 문자열 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant