Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package coding;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Check whether array A is a permutation.
- * @author Seok Kyun. Choi. 최석균 (Syaku)
- * @since 2018. 5. 22.
- */
- public class PermCheck {
- public static void main(String[] args) {
- Solution solution = new Solution();
- System.out.println(solution.solution(new int[]{4}));
- System.out.println(solution.solution(new int[]{3,1,2}));
- System.out.println(solution.solution(new int[]{1,2,3}));
- System.out.println(solution.solution(new int[]{4,2}));
- System.out.println(solution.solution(new int[]{10,11}));
- System.out.println(solution.solution(new int[]{4,1,3,2}));
- System.out.println(solution.solution(new int[]{4,1,3,1000000000}));
- System.out.println("----------------------");
- System.out.println(solution.solution(new int[]{1,2,4,4,3}));
- System.out.println(solution.solution(new int[]{1,2,4,4}));
- }
- /**
- * 조건
- * N = 1..100000 정수
- * A[] = 1.. 1000000000 정수
- * 순열이면 1, 아니면 0 반환
- * 순열은 1에서 N까지의 각 요소를 한 번만 포함하는 시퀀스입니다.
- *
- */
- static class Solution {
- public Set<Integer> distinct(int[] A) {
- Set<Integer> result = new HashSet<>();
- for (int a : A) {
- result.add(a);
- }
- return result;
- }
- public int solution(int[] A) {
- // 중복 제거
- Set<Integer> store = distinct(A);
- // 1부터 시작되는 수
- // 1부터 존재유무 확인
- // 배열은 0부터 시작되니 마지막까지 확인하기 위한 <= 표
- for (int i = 1; i <= A.length; i++) {
- if (!store.contains(i)) {
- return 0;
- }
- }
- return 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment