-
Notifications
You must be signed in to change notification settings - Fork 152
/
PermCheck.java
33 lines (26 loc) · 1.01 KB
/
PermCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package PermCheck;
import java.util.*;
// note: remember to import (when using some data structures)
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
// to check "permutation"
// the main idea is as follows:
// 1. use set to remember which elements have appeared
// 2. use "for loop" to check if all the elements from "1 to A.length" appeared
// If all the elements have appeared, then "yes".
// Otherwise, "no".
Set<Integer> set = new HashSet<Integer>();
for(int i=0; i < A.length; i++){
set.add(A[i]);
}
// check if "all" the elements from "1 to A.length" appeared
for(int i=1; i<= A.length; i++){
if( set.contains(i) == false )
return 0; // not a permutation (A[i] is missing)
}
// if it contains all the elements (from "1 to A.length")
// then, "yes"
return 1;
}
}