Guest User

Untitled

a guest
May 27th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package coding;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5.  
  6. /**
  7. * Check whether array A is a permutation.
  8. * @author Seok Kyun. Choi. 최석균 (Syaku)
  9. * @since 2018. 5. 22.
  10. */
  11. public class PermCheck {
  12. public static void main(String[] args) {
  13. Solution solution = new Solution();
  14. System.out.println(solution.solution(new int[]{4}));
  15. System.out.println(solution.solution(new int[]{3,1,2}));
  16. System.out.println(solution.solution(new int[]{1,2,3}));
  17. System.out.println(solution.solution(new int[]{4,2}));
  18. System.out.println(solution.solution(new int[]{10,11}));
  19. System.out.println(solution.solution(new int[]{4,1,3,2}));
  20. System.out.println(solution.solution(new int[]{4,1,3,1000000000}));
  21. System.out.println("----------------------");
  22. System.out.println(solution.solution(new int[]{1,2,4,4,3}));
  23. System.out.println(solution.solution(new int[]{1,2,4,4}));
  24. }
  25.  
  26. /**
  27. * 조건
  28. * N = 1..100000 정수
  29. * A[] = 1.. 1000000000 정수
  30. * 순열이면 1, 아니면 0 반환
  31. * 순열은 1에서 N까지의 각 요소를 한 번만 포함하는 시퀀스입니다.
  32. *
  33. */
  34. static class Solution {
  35.  
  36. public Set<Integer> distinct(int[] A) {
  37. Set<Integer> result = new HashSet<>();
  38. for (int a : A) {
  39. result.add(a);
  40. }
  41. return result;
  42. }
  43. public int solution(int[] A) {
  44.  
  45. // 중복 제거
  46. Set<Integer> store = distinct(A);
  47.  
  48. // 1부터 시작되는 수
  49. // 1부터 존재유무 확인
  50. // 배열은 0부터 시작되니 마지막까지 확인하기 위한 <= 표
  51. for (int i = 1; i <= A.length; i++) {
  52. if (!store.contains(i)) {
  53. return 0;
  54. }
  55. }
  56. return 1;
  57. }
  58. }
  59.  
  60. }
Add Comment
Please, Sign In to add comment