@@ -70,11 +70,11 @@ pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
7070 let mut nums = nums;
7171 let n = n as usize ;
7272 for i in 0 ..n {
73- nums[ 2 * i] = nums [ 2 * i ] | ( ( nums[ i] & 1023 ) << 10 ) ;
74- nums[ 2 * i + 1 ] = nums [ 2 * i + 1 ] | ( ( nums[ i + n] & 1023 ) << 10 ) ;
73+ nums[ 2 * i] |= ( nums[ i] & 1023 ) << 10 ;
74+ nums[ 2 * i + 1 ] |= ( nums[ i + n] & 1023 ) << 10 ;
7575 }
7676 nums. iter_mut ( ) . for_each ( |num| {
77- * num = * num >> 10 ;
77+ * num >>= 10 ;
7878 } ) ;
7979 nums
8080}
@@ -101,7 +101,7 @@ pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {
101101 return false ;
102102 }
103103 }
104- return true ;
104+ true
105105 }
106106
107107 let mut cnt = 0 ;
@@ -150,7 +150,6 @@ pub fn construct_array(n: i32, k: i32) -> Vec<i32> {
150150/// [412. Fizz Buzz](https://leetcode.cn/problems/fizz-buzz/)
151151pub fn fizz_buzz ( n : i32 ) -> Vec < String > {
152152 ( 1 ..=n)
153- . into_iter ( )
154153 . map ( |num| {
155154 if num % 3 == 0 && num % 5 == 0 {
156155 "FizzBuzz" . to_string ( )
@@ -194,7 +193,7 @@ pub fn fizz_buzz(n: i32) -> Vec<String> {
194193/// 注意: 如果原本为0, 这时 `31 - (leading_zero)`可能有溢出
195194///
196195pub fn number_of_steps ( num : i32 ) -> i32 {
197- ( num. count_ones ( ) + 31u32 . checked_sub ( num. leading_zeros ( ) ) . unwrap_or ( 0 ) ) as i32
196+ ( num. count_ones ( ) + 31u32 . saturating_sub ( num. leading_zeros ( ) ) ) as i32
198197}
199198
200199/// [829. 连续整数求和](https://leetcode.cn/problems/consecutive-numbers-sum/)
@@ -329,15 +328,15 @@ pub fn unique_letter_string(s: String) -> i32 {
329328 let pos = pos as i32 ;
330329 if curr_pos[ i] > -1 {
331330 // 这个字符之前出现过, 这时的pos对应上述推导中的后一个
332- ans = ans + ( pos - curr_pos[ i] ) * ( curr_pos[ i] - last_pos[ i] ) ;
331+ ans += ( pos - curr_pos[ i] ) * ( curr_pos[ i] - last_pos[ i] ) ;
333332 }
334333 last_pos[ i] = curr_pos[ i] ;
335334 curr_pos[ i] = pos;
336335 }
337336 // 对于只出现过一次的字符, 上面循环统计不到, 即等效 后一个 的位置为字符结尾
338337 for ( last, curr) in last_pos. into_iter ( ) . zip ( curr_pos. into_iter ( ) ) {
339338 if curr > -1 {
340- ans = ans + ( curr - last) * ( s. len ( ) as i32 - curr) ;
339+ ans += ( curr - last) * ( s. len ( ) as i32 - curr) ;
341340 }
342341 }
343342 ans
@@ -363,7 +362,7 @@ pub fn largest_overlap(img1: Vec<Vec<i32>>, img2: Vec<Vec<i32>>) -> i32 {
363362 }
364363 }
365364 }
366- delta. values ( ) . into_iter ( ) . max ( ) . copied ( ) . unwrap_or ( 0 )
365+ delta. values ( ) . max ( ) . copied ( ) . unwrap_or ( 0 )
367366}
368367
369368/// [836. 矩形重叠](https://leetcode.cn/problems/rectangle-overlap/)
@@ -488,15 +487,15 @@ pub fn mirror_reflection(p: i32, q: i32) -> i32 {
488487 } else if ( d / q) % 2 == 0 {
489488 return 2 ;
490489 }
491- return 1 ;
490+ 1
492491}
493492
494493fn gcd ( a : i32 , b : i32 ) -> i32 {
495494 let ( m, n) = ( a. max ( b) , a. min ( b) ) ;
496495 if n == 0 {
497496 return m;
498497 }
499- return gcd ( n, m % n) ;
498+ gcd ( n, m % n)
500499}
501500fn lcm ( a : i32 , b : i32 ) -> i32 {
502501 a * b / gcd ( a, b)
@@ -511,7 +510,7 @@ pub fn transpose(matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
511510 ret[ j] [ i] = matrix[ i] [ j] ;
512511 }
513512 }
514- return ret;
513+ ret
515514}
516515
517516/// [LCP 06. 拿硬币](https://leetcode.cn/problems/na-ying-bi)
@@ -882,8 +881,7 @@ mod tests {
882881 expect : & ' static [ i32 ] ,
883882 }
884883
885- vec ! [
886- TestCase {
884+ [ TestCase {
887885 name : "basic 1" ,
888886 n : 3 ,
889887 k : 1 ,
@@ -894,8 +892,7 @@ mod tests {
894892 n : 3 ,
895893 k : 2 ,
896894 expect : & [ 1 , 3 , 2 ] ,
897- } ,
898- ]
895+ } ]
899896 . iter ( )
900897 . for_each ( |testcase| {
901898 let acutal = construct_array ( testcase. n , testcase. k ) ;
@@ -911,8 +908,7 @@ mod tests {
911908 expect : i32 ,
912909 }
913910
914- vec ! [
915- TestCase {
911+ [ TestCase {
916912 name : "basic 1" ,
917913 mat : & [ & [ 1 , 0 , 0 ] , & [ 0 , 0 , 1 ] , & [ 1 , 0 , 0 ] ] ,
918914 expect : 1 ,
@@ -926,8 +922,7 @@ mod tests {
926922 name : "basic 3" ,
927923 mat : & [ & [ 0 , 0 , 0 , 1 ] , & [ 1 , 0 , 0 , 0 ] , & [ 0 , 1 , 1 , 0 ] , & [ 0 , 0 , 0 , 0 ] ] ,
928924 expect : 2 ,
929- } ,
930- ]
925+ } ]
931926 . iter ( )
932927 . for_each ( |testcase| {
933928 let mat = testcase. mat . iter ( ) . map ( |line| line. to_vec ( ) ) . collect ( ) ;
@@ -945,8 +940,7 @@ mod tests {
945940 expect : & ' static [ i32 ] ,
946941 }
947942
948- vec ! [
949- TestCase {
943+ [ TestCase {
950944 name : "basic 1" ,
951945 nums : & [ 2 , 5 , 1 , 3 , 4 , 7 ] ,
952946 n : 3 ,
@@ -963,8 +957,7 @@ mod tests {
963957 nums : & [ 1 , 1 , 2 , 2 ] ,
964958 n : 2 ,
965959 expect : & [ 1 , 2 , 1 , 2 ] ,
966- } ,
967- ]
960+ } ]
968961 . iter ( )
969962 . for_each ( |testcase| {
970963 let actual = shuffle ( testcase. nums . to_vec ( ) , testcase. n ) ;
@@ -980,8 +973,7 @@ mod tests {
980973 expect : i32 ,
981974 }
982975
983- vec ! [
984- TestCase {
976+ [ TestCase {
985977 name : "basic" ,
986978 nums : & [ 1 , 2 , 3 ] ,
987979 expect : 2 ,
@@ -1000,8 +992,7 @@ mod tests {
1000992 name : "fix 1" ,
1001993 nums : & [ 1 ] ,
1002994 expect : 0 ,
1003- } ,
1004- ]
995+ } ]
1005996 . iter ( )
1006997 . for_each ( |testcase| {
1007998 let actual = min_moves2 ( testcase. nums . to_vec ( ) ) ;
0 commit comments