Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc87ddd7

Browse files
add a solution for 277
1 parent4b09485 commitc87ddd7

File tree

1 file changed

+34
-1
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+34
-1
lines changed

‎src/main/java/com/fishercoder/solutions/_277.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,40 @@ public int findCelebrity(int n) {
1919
}
2020

2121
//this is a mock-up method to make IDE happy.s
22-
privatebooleanknows(inti,intcandidate) {
22+
booleanknows(inti,intcandidate) {
23+
returnfalse;
24+
}
25+
}
26+
27+
publicstaticclassSolution2 {
28+
/**
29+
* My completely original solution on 10/21/2021, which turns out to match https://leetcode.com/problems/find-the-celebrity/solution/ Solution 1.
30+
* Time: O(n^2)
31+
* Space: O(1)
32+
*/
33+
publicintfindCelebrity(intn) {
34+
for (inti =0;i <n;i++) {
35+
//check if i is the celebrity
36+
intj =0;
37+
for (;j <n;j++) {
38+
if (i !=j) {
39+
if (knows(i,j)) {
40+
break;
41+
}
42+
if (!knows(j,i)) {
43+
break;
44+
}
45+
}
46+
}
47+
if (j ==n) {
48+
returni;
49+
}
50+
}
51+
return -1;
52+
}
53+
54+
//this is a mock-up method to make IDE happy.s
55+
booleanknows(inti,intcandidate) {
2356
returnfalse;
2457
}
2558
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp