Print
카테고리: [ Algorithm ]
조회수: 7350

1. 문제

길이가 n인 배열에 1부터 n까지 숫자가 중복 없이 한 번씩 들어 있는지를 확인하려고 합니다.

1부터 n까지 숫자가 중복 없이 한 번씩 들어 있는 경우 true를, 아닌 경우 false를 반환하도록 함수를 완성해주세요.


2. 코드

public class NumberArrayCheck {
        public static void main(String[] args) {
                NumberArrayCheck nac = new NumberArrayCheck();
                System.out.println(nac.check(new int[]{1,2,3,4,5,6,7,8,9}));
                System.out.println(nac.check(new int[]{1,2,3,3,5,6}));
                System.out.println(nac.check(new int[]{1,2,3,4,5,10}));
        }
 
        private boolean check(int[] numArray) {
                boolean result = true;
 
                boolean[] checkArray = new boolean[numArray.length+1];
 
                for ( int inx=0; inx<numArray.length; inx++ ) {
                        if ( numArray[inx] <= numArray.length &amp;&amp; !checkArray[numArray[inx]] ) {
                                checkArray[numArray[inx]] = true;
                        } else {
                                result = false;
                                break;
                        }
                }
 
                return result;
        }
}