Skip to content

Commit 4920d8b

Browse files
committed
feat: use anchor
1 parent cc3d268 commit 4920d8b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

stone_game_vii/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ enum Turn {
66
}
77

88
impl Solution {
9-
fn operate(stones: &[i32], turn: &Turn) -> i32 {
10-
if stones.len() == 0 {
9+
fn operate(stones: &Vec<i32>, i: usize, j: usize, turn: &Turn) -> i32 {
10+
if i + 1 == j {
1111
return 0;
1212
}
1313
let next_turn = match turn {
1414
Turn::AliceTurn => Turn::BobTurn,
1515
Turn::BobTurn => Turn::AliceTurn,
1616
};
17-
let stones_without_leftmost = &stones[1..];
18-
let difference_removing_leftmost = Self::operate(stones_without_leftmost, &next_turn); // 如果移动最左边的石头,后续产生的差异
17+
let stones_without_leftmost = &stones[i + 1..j];
18+
let difference_removing_leftmost = Self::operate(stones, i + 1, j, &next_turn); // 如果移动最左边的石头,后续产生的差异
1919
let score_removing_leftmost = stones_without_leftmost.iter().sum::<i32>();
20-
let stones_without_rightmost = &stones[..stones.len() - 1];
21-
let difference_removing_rightmost = Self::operate(stones_without_rightmost, &next_turn);
20+
let stones_without_rightmost = &stones[i..j - 1];
21+
let difference_removing_rightmost = Self::operate(stones, i, j - 1, &next_turn);
2222
let score_removing_rightmost = stones_without_rightmost.iter().sum::<i32>();
2323
match turn {
2424
Turn::AliceTurn => (difference_removing_leftmost + score_removing_leftmost)
@@ -29,7 +29,7 @@ impl Solution {
2929
}
3030

3131
pub fn stone_game_vii(stones: Vec<i32>) -> i32 {
32-
Self::operate(&stones, &Turn::AliceTurn)
32+
Self::operate(&stones, 0, stones.len(), &Turn::AliceTurn)
3333
}
3434
}
3535

0 commit comments

Comments
 (0)