@@ -6,19 +6,19 @@ enum Turn {
6
6
}
7
7
8
8
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 {
11
11
return 0 ;
12
12
}
13
13
let next_turn = match turn {
14
14
Turn :: AliceTurn => Turn :: BobTurn ,
15
15
Turn :: BobTurn => Turn :: AliceTurn ,
16
16
} ;
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) ; // 如果移动最左边的石头,后续产生的差异
19
19
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) ;
22
22
let score_removing_rightmost = stones_without_rightmost. iter ( ) . sum :: < i32 > ( ) ;
23
23
match turn {
24
24
Turn :: AliceTurn => ( difference_removing_leftmost + score_removing_leftmost)
@@ -29,7 +29,7 @@ impl Solution {
29
29
}
30
30
31
31
pub fn stone_game_vii ( stones : Vec < i32 > ) -> i32 {
32
- Self :: operate ( & stones, & Turn :: AliceTurn )
32
+ Self :: operate ( & stones, 0 , stones . len ( ) , & Turn :: AliceTurn )
33
33
}
34
34
}
35
35
0 commit comments