277 Find the Celebrity
Problem:
Solutions:
/* The knows API is defined in the parent class Relation.
boolean knows(int a, int b); */
public class Solution extends Relation {
public int findCelebrity(int n) {
boolean[] normal = new boolean[n];
for (int i = 0; i < n; i ++) {
if (normal[i] == true) {
continue;
}
boolean cele = true;
for (int j = 0; j < n; j ++) {
if (j == i) {
continue;
}
if (knows(j, i)) {
normal[j] = true;
}
if (!knows(j, i) || knows(i, j)) {
cele = false;
break;
}
}
if (cele == true) {
return i;
}
}
return -1;
}
}Last updated