Skip to content

Commit 83c856c

Browse files
committed
12/20/2020
1 parent fa627e4 commit 83c856c

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,5 @@ leetcode practise solution in rust
167167
|1680|[Concatenation of Consecutive Binary Numbers](src/_1680_concatenation_of_consecutive_binary_numbers.rs)|
168168
|1684|[Count the Number of Consistent Strings](src/_1684_count_the_number_of_consistent_strings.rs)|
169169
|1685|[Sum of Absolute Differences in a Sorted Array](src/_1685_sum_of_absolute_differences_in_a_sorted_array.rs)|
170+
|1694|[Reformat Phone Number](src/_1694_reformat_phone_number.rs)|
171+
|1695|[Maximum Erasure Value](src/_1695_maximum_erasure_value.rs)|

src/_1680_concatenation_of_consecutive_binary_numbers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl Solution {
1111
result as i32
1212
}
1313

14-
fn binary_digits(mut decimal: usize) -> usize {
14+
fn binary_digits(decimal: usize) -> usize {
1515
let mut bits = 0;
1616
while decimal > 0 {
1717
bits += 1;

src/_1694_reformat_phone_number.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn reformat_number(number: String) -> String {
5+
let nums: Vec<char> = number.chars().filter(|&c| c >= '0' && c <= '9').collect();
6+
let n = nums.len();
7+
let tail = n % 3;
8+
9+
let mut result = "".to_string();
10+
for i in 0..n {
11+
if (tail == 1 && i == n - 2) || (i % 3 == 0 && i != 0) {
12+
result.push('-');
13+
}
14+
result.push(nums[i]);
15+
if tail == 1 && i == n - 2 {
16+
result.push(nums[i + 1]);
17+
break;
18+
}
19+
}
20+
result
21+
}
22+
}
23+
#[test]
24+
fn test() {
25+
assert_eq!(
26+
Solution::reformat_number("1-23-45 6".to_string()),
27+
"123-456".to_string()
28+
);
29+
assert_eq!(
30+
Solution::reformat_number("123 4-567".to_string()),
31+
"123-45-67".to_string()
32+
);
33+
assert_eq!(
34+
Solution::reformat_number("123 4-5678".to_string()),
35+
"123-456-78".to_string()
36+
);
37+
assert_eq!(
38+
Solution::reformat_number("12".to_string()),
39+
"12".to_string()
40+
);
41+
assert_eq!(
42+
Solution::reformat_number("--17-5 229 35-39475 ".to_string()),
43+
"175-229-353-94-75".to_string()
44+
);
45+
}

src/_1695_maximum_erasure_value.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::collections::HashSet;
2+
struct Solution;
3+
4+
impl Solution {
5+
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
6+
let mut result = 0;
7+
let mut sum = 0;
8+
let mut set: HashSet<i32> = HashSet::new();
9+
let mut left: usize = 0;
10+
let mut right: usize = 0;
11+
12+
while right < nums.len() {
13+
let cur = nums[right];
14+
if set.contains(&cur) {
15+
while nums[left] != cur {
16+
set.remove(&nums[left]);
17+
sum -= nums[left];
18+
left += 1;
19+
}
20+
left += 1;
21+
} else {
22+
set.insert(cur);
23+
sum += cur;
24+
}
25+
result = result.max(sum);
26+
right += 1;
27+
}
28+
result as i32
29+
}
30+
}
31+
32+
#[test]
33+
fn test() {
34+
assert_eq!(Solution::maximum_unique_subarray(vec![4, 2, 4, 5, 6]), 17);
35+
assert_eq!(
36+
Solution::maximum_unique_subarray(vec![5, 2, 1, 2, 5, 2, 1, 2, 5]),
37+
8
38+
);
39+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,8 @@ mod _1684_count_the_number_of_consistent_strings;
342342
//
343343
mod _1685_sum_of_absolute_differences_in_a_sorted_array;
344344
//
345+
mod _1694_reformat_phone_number;
346+
//
347+
mod _1695_maximum_erasure_value;
348+
//
345349
mod test;

0 commit comments

Comments
 (0)