File tree Expand file tree Collapse file tree 4 files changed +125
-0
lines changed Expand file tree Collapse file tree 4 files changed +125
-0
lines changed Original file line number Diff line number Diff line change
1
+ fn is_palindrome_valid ( s : & str ) -> bool {
2
+ let chars: Vec < _ > = s. chars ( ) . collect ( ) ;
3
+ let mut left = 0 ;
4
+ let mut right = s. len ( ) - 1 ;
5
+ while left < right {
6
+ // Skip non-alphanumeric characters from the left.
7
+ while left < right && !chars[ left] . is_alphanumeric ( ) {
8
+ left += 1
9
+ }
10
+ // Skip non-alphanumeric characters from the right.
11
+ while left < right && !chars[ right] . is_alphanumeric ( ) {
12
+ right -= 1
13
+ }
14
+ // If the characters at the left and right pointers don't
15
+ // match, the string is not a palindrome.
16
+ if chars[ left] != chars[ right] {
17
+ return false ;
18
+ }
19
+ left += 1 ;
20
+ right -= 1 ;
21
+ }
22
+ true
23
+ }
Original file line number Diff line number Diff line change
1
+ use std:: {
2
+ cmp:: { max, min}
3
+ } ;
4
+
5
+ fn largest_container ( heights : Vec < i32 > ) -> i32 {
6
+ let mut max_water = 0 ;
7
+ let mut left = 0 ;
8
+ let mut right = heights. len ( ) - 1 ;
9
+
10
+ while left < right {
11
+ // Calculate the water contained between the current pair of lines.
12
+ let water = min ( heights[ left] , heights[ right] )
13
+ * ( right as i32 - left as i32 ) ;
14
+ max_water = max ( max_water, water) ;
15
+ // Move the pointers inward, always moving the pointer at the
16
+ // shorter line. If both lines have the same height, move both
17
+ // pointers inward.
18
+ if heights[ left] < heights[ right] {
19
+ left += 1 ;
20
+ } else if heights[ left] > heights[ right] {
21
+ right -= 1 ;
22
+ } else {
23
+ left += 1 ;
24
+ right -= 1 ;
25
+ }
26
+ }
27
+ max_water
28
+ }
Original file line number Diff line number Diff line change
1
+ fn pair_sum_sorted ( nums : & [ i32 ] , target : i32 ) -> Vec < usize > {
2
+ let mut left = 0 ;
3
+ let mut right = nums. len ( ) - 1 ;
4
+ while left < right {
5
+ let sum = nums[ left] + nums[ right] ;
6
+ // If the sum is smaller, increment the left pointer, aiming
7
+ // to increase the sum toward the target value.
8
+ if sum < target {
9
+ left += 1 ;
10
+ } else if sum > target {
11
+ // If the sum is larger, decrement the right pointer, aiming
12
+ // to decrease the sum toward the target value.
13
+ right -= 1 ;
14
+ } else {
15
+ // If the target pair is found, return its indexes.
16
+ return vec ! [ left, right] ;
17
+ }
18
+ }
19
+ vec ! [ ]
20
+ }
Original file line number Diff line number Diff line change
1
+ fn triplet_sum ( mut nums : Vec < i32 > ) -> Vec < Vec < i32 > > {
2
+ let mut triplets: Vec < Vec < i32 > > = vec ! [ ] ;
3
+ nums. sort ( ) ;
4
+ let len = nums. len ( ) ;
5
+
6
+ for i in 0 ..len {
7
+ // Optimization: triplets consisting of only positive numbers
8
+ // will never sum to 0.
9
+ if nums[ i] > 0 {
10
+ break ;
11
+ }
12
+
13
+ // To avoid duplicate triplets, skip 'a' if it's the same as
14
+ // the previous number.
15
+ if i > 0 && nums[ i] == nums[ i - 1 ] {
16
+ continue ;
17
+ }
18
+
19
+ // Find all pairs that sum to a target of '-a' (-nums[i]).
20
+ let pairs = pair_sum_sorted_all_pairs ( & nums, i + 1 , -nums[ i] ) ;
21
+ for pair in pairs {
22
+ triplets. push ( vec ! [ nums[ i] , pair[ 0 ] , pair[ 1 ] ] ) ;
23
+ }
24
+ }
25
+
26
+ triplets
27
+ }
28
+
29
+ fn pair_sum_sorted_all_pairs ( nums : & Vec < i32 > , start : usize , target : i32 ) -> Vec < Vec < i32 > > {
30
+ let mut pairs = Vec :: new ( ) ;
31
+ let mut left = start;
32
+ let mut right = nums. len ( ) - 1 ;
33
+
34
+ while left < right {
35
+ let sum = nums[ left] + nums[ right] ;
36
+
37
+ if sum == target {
38
+ pairs. push ( vec ! [ nums[ left] , nums[ right] ] ) ;
39
+ left += 1 ;
40
+
41
+ // To avoid duplicate '[b, c]' pairs, skip 'b' if it's the
42
+ // same as the previous number.
43
+ while left < right && nums[ left] == nums[ left - 1 ] {
44
+ left += 1 ;
45
+ }
46
+ } else if sum < target {
47
+ left += 1 ;
48
+ } else {
49
+ right -= 1 ;
50
+ }
51
+ }
52
+
53
+ pairs
54
+ }
You can’t perform that action at this time.
0 commit comments