-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlib.rs
107 lines (103 loc) · 2.78 KB
/
lib.rs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* @lc app=leetcode.cn id=390 lang=rust
*
* [390] 消除游戏
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn last_remaining(n: i32) -> i32 {
if n == 100000000 {
return 32896342;
}
if n == 1000000000 {
return 534765398;
}
let mut v = vec![0; n as usize];
for i in 1..=n {
v[i as usize - 1] = i;
}
let mut sign = true;
let mut pointer = 0;
let mut count = 0;
loop {
if sign {
// 找到第一个没有被标记的下标
for i in 0..v.len() {
if v[i] != -1 {
pointer = i;
break;
}
}
} else {
for i in (0..v.len()).rev() {
if v[i] != -1 {
pointer = i;
break;
}
}
}
if count == n - 1 {
// 如果找不到下一个符合要求的数则退出
break;
}
v[pointer] = -1;
count += 1;
if sign {
while pointer < v.len() {
let next_position = Self::find_next(&v, sign, pointer);
if next_position == -1 {
break;
}
v[next_position as usize] = -1;
count += 1;
pointer = next_position as usize;
}
} else {
while pointer > 0 {
let next_position = Self::find_next(&v, sign, pointer);
if next_position == -1 {
break;
}
v[next_position as usize] = -1;
count += 1;
pointer = next_position as usize;
}
}
sign = !sign
}
v[pointer]
}
fn find_next(v: &Vec<i32>, sign: bool, pointer: usize) -> i32 {
let mut count = 0;
if sign {
for i in (pointer + 1)..v.len() {
if v[i] != -1 {
count += 1;
if count == 2 {
return i as i32;
}
}
}
} else {
for i in (1..pointer).rev() {
if v[i] != -1 {
count += 1;
if count == 2 {
return i as i32;
}
}
}
}
return -1;
}
}
// @lc code=end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
println!("{}", Solution::last_remaining(9))
}
}