Skip to content

Commit bab8f4c

Browse files
committed
update
1 parent 03fddb9 commit bab8f4c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// AC
2+
class Solution {
3+
public int arraySign(int[] nums) {
4+
int negativeCount = 0;
5+
for(int i = 0; i < nums.length; i++) {
6+
if (nums[i] == 0) {
7+
return 0;
8+
}
9+
if (nums[i] < 0) {
10+
negativeCount++;
11+
}
12+
}
13+
14+
return negativeCount % 2 == 0 ? 1 : -1;
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// AC: 模拟
2+
// Runtime: 198 ms, faster than 33.33% of Java online submissions for Find the Winner of the Circular Game.
3+
// Memory Usage: 38.3 MB, less than 33.33% of Java online submissions for Find the Winner of the Circular Game.
4+
class Solution {
5+
public int findTheWinner(int n, int k) {
6+
if (n == 1 || k == 1) {
7+
return n;
8+
}
9+
10+
int[] record = new int[n + 1];
11+
for(int i = 0; i < n + 1; i++) {
12+
record[i] = 0;
13+
}
14+
Stack<Integer> losed = new Stack<>();
15+
int pos = 1;
16+
boolean startFirstLoop = false;
17+
while(losed.size() < n) {
18+
int tempK = k;
19+
if (!startFirstLoop) {
20+
tempK--;
21+
pos = (pos + 1) % (n + 1);
22+
startFirstLoop = true;
23+
}
24+
while (true) {
25+
if (pos != 0 && record[pos] != 1) {
26+
if (tempK > 0) {
27+
tempK--;
28+
}
29+
if (tempK == 0) {
30+
break;
31+
}
32+
}
33+
pos = (pos + 1) % (n + 1);
34+
}
35+
record[pos] = 1;
36+
losed.add(pos);
37+
}
38+
return losed.peek();
39+
}
40+
}

0 commit comments

Comments
 (0)