[프로그래머스/Java] Lv.0 특별한 이차원 배열 1

febCho·2024년 3월 21일
0

코딩테스트

목록 보기
120/253
post-thumbnail

문제

정수 n이 매개변수로 주어질 때, 다음과 같은 n × n 크기의 이차원 배열 arr를 return 하는 solution 함수를 작성해 주세요.

  • arr[i][j] (0 ≤ i, j < n)의 값은 i = j라면 1, 아니라면 0입니다.

- 제한사항

  • 1 ≤ n ≤ 100

풀이

class Solution {
    public int[][] solution(int n) {
        int[][] answer = new int[n][n];
        
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(i == j){
                    answer[i][j] = 1;
                }else{
                    answer[i][j] = 0;
                }
            }
        }
        
        return answer;
    }
}

결과

profile
Done is better than perfect.

0개의 댓글