Skip to content

Commit 7fb4275

Browse files
committed
2020-6-18
1 parent 539d997 commit 7fb4275

8 files changed

+236
-9
lines changed

src/_0007_reverse_integer.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn reverse(x: i32) -> i32 {
5+
let sign = match x < 0 {
6+
true => -1,
7+
false => 1,
8+
};
9+
let mut result: i32 = 0;
10+
let mut number: i32 = x.abs();
11+
12+
while number != 0 {
13+
match result.checked_mul(10) {
14+
Some(v) => result = v,
15+
None => return 0,
16+
}
17+
match result.checked_add(number % 10) {
18+
Some(v) => result = v,
19+
None => return 0,
20+
}
21+
number /= 10;
22+
}
23+
24+
result * sign
25+
}
26+
}
27+
28+
#[test]
29+
fn test() {
30+
assert_eq!(Solution::reverse(123), 321);
31+
}

src/_0020_valid_parentheses.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ struct Solution;
22

33
impl Solution {
44
pub fn is_valid(s: String) -> bool {
5-
let mut stack: Vec<char> = vec![];
5+
let mut char_stack: Vec<char> = vec![];
66

77
for c in s.chars() {
88
match c {
9-
'(' | '[' | '{' => stack.push(c),
10-
')' | ']' | '}' => match stack.pop() {
11-
Some(p) => {
12-
if !((p == '(' && c == ')')
13-
|| (p == '[' && c == ']')
14-
|| (p == '{' && c == '}'))
9+
'(' | '{' | '[' => char_stack.push(c),
10+
')' | '}' | ']' => match char_stack.pop() {
11+
Some(l) => {
12+
if !((c == ')' && l == '(')
13+
| (c == '}' && l == '{')
14+
| (c == ']' && l == '['))
1515
{
1616
return false;
1717
}
@@ -21,7 +21,8 @@ impl Solution {
2121
_ => {}
2222
}
2323
}
24-
stack.is_empty()
24+
25+
char_stack.is_empty()
2526
}
2627
}
2728

src/_0053_maximum_subarray.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
5+
let mut current_max = nums[0];
6+
let mut overall_max = nums[0];
7+
8+
for i in 1..nums.len() {
9+
current_max = nums[i].max(current_max + nums[i]);
10+
overall_max = overall_max.max(current_max);
11+
}
12+
overall_max
13+
}
14+
}
15+
16+
#[test]
17+
fn test() {
18+
assert_eq!(
19+
Solution::max_sub_array(vec![-2, 1, -3, 4, -1, 2, 1, -5, 4]),
20+
6
21+
);
22+
}

src/_0088_merge_sorted_array.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn merge_sorted_array(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
5+
let mut i = m - 1;
6+
let mut j = n - 1;
7+
let mut k = m + n - 1;
8+
9+
while k >= 0 {
10+
if i >= 0 && (j < 0 || nums1[i as usize] >= nums2[j as usize]) {
11+
nums1[k as usize] = nums1[i as usize];
12+
i -= 1;
13+
} else {
14+
nums1[k as usize] = nums2[j as usize];
15+
j -= 1;
16+
}
17+
k -= 1;
18+
}
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
let mut arr1 = vec![1, 2, 3, 0, 0, 0];
25+
let mut arr2 = vec![2, 5, 6];
26+
let m: i32 = 3;
27+
let n: i32 = 3;
28+
let answer = vec![1, 2, 2, 3, 5, 6];
29+
Solution::merge_sorted_array(&mut arr1, m, &mut arr2, n);
30+
assert_eq!(arr1, answer);
31+
}

src/_0415_add_strings.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn add_strings(num1: String, num2: String) -> String {
5+
use std::{char, iter};
6+
// prepare and create result with capacity
7+
let len1 = num1.len();
8+
let len2 = num2.len();
9+
let mut carry = 0;
10+
let mut result = String::with_capacity(len1.max(len2) + 1);
11+
12+
// choose num1 to be the shorter one, num2 the longer one
13+
let (num1, num2) = if len1 < len2 {
14+
(num1, num2)
15+
} else {
16+
(num2, num1)
17+
};
18+
19+
// create character tuple (c1,c2) from num1 and num2
20+
// fill empty digits with 1
21+
for (c1, c2) in num1
22+
.chars()
23+
.rev()
24+
.chain(iter::repeat('0'))
25+
.zip(num2.chars().rev())
26+
{
27+
let n1 = c1.to_digit(10).unwrap();
28+
let n2 = c2.to_digit(10).unwrap();
29+
let mut sum = n1 + n2 + carry;
30+
31+
if sum < 10 {
32+
carry = 0;
33+
} else {
34+
carry = 1;
35+
sum -= 10;
36+
}
37+
38+
result.push(char::from_digit(sum, 10).unwrap());
39+
}
40+
41+
// handle the last digit with carry
42+
if carry == 1 {
43+
result.push('1');
44+
} else if result.is_empty() {
45+
result.push('0')
46+
}
47+
48+
// convert result to reverse string and return
49+
result.chars().rev().collect::<String>()
50+
}
51+
}
52+
53+
#[test]
54+
fn test() {
55+
let a = "0".to_string();
56+
let b = "0".to_string();
57+
let c = "0".to_string();
58+
assert_eq!(Solution::add_strings(a, b), c);
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn is_alien_sorted(words: Vec<String>, order: String) -> bool {
5+
let mut dictionary: Vec<char> = vec!['a'; 256];
6+
for (i, v) in order.chars().enumerate() {
7+
dictionary[v as usize] = (i as u8 + 'a' as u8) as char;
8+
}
9+
10+
let words: Vec<String> = words
11+
.into_iter()
12+
.map(|word| Solution::translate(&dictionary, word))
13+
.collect();
14+
let mut words_sort: Vec<String> = words.to_vec();
15+
words_sort.sort();
16+
words == words_sort
17+
}
18+
19+
fn translate(dictionary: &Vec<char>, word: String) -> String {
20+
word.chars().map(|c| dictionary[c as usize]).collect()
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
let words: Vec<String> = vec!["hello".to_string(), "leetcode".to_string()];
27+
let order: String = "hlabcdefgijkmnopqrstuvwxyz".to_string();
28+
assert_eq!(Solution::is_alien_sorted(words, order), true);
29+
}

src/k.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
fn valid_utf8(data: Vec<i32>) -> bool {
5+
let mut count = 0;
6+
for x in data {
7+
if count == 0 {
8+
if x >> 3 == 0b11110 {
9+
count += 3;
10+
continue;
11+
}
12+
if x >> 4 == 0b1110 {
13+
count += 2;
14+
continue;
15+
}
16+
if x >> 5 == 0b110 {
17+
count += 1;
18+
continue;
19+
}
20+
if x >> 7 == 0 {
21+
continue;
22+
}
23+
return false;
24+
} else {
25+
if x >> 6 == 0b10 {
26+
count -= 1;
27+
continue;
28+
}
29+
return false;
30+
}
31+
}
32+
count == 0
33+
}
34+
}
35+
36+
#[test]
37+
fn test() {
38+
let data = vec![197, 130, 1];
39+
assert_eq!(Solution::valid_utf8(data), true);
40+
let data = vec![235, 140, 4];
41+
assert_eq!(Solution::valid_utf8(data), false);
42+
}

src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1-
#![deny(clippy::all)]
1+
// #![deny(clippy::all)]
22
#![allow(dead_code)]
33
#![allow(clippy::many_single_char_names)]
44
#![allow(clippy::collapsible_if)]
55
#![allow(clippy::needless_range_loop)]
66
#![allow(clippy::float_cmp)]
77
#![allow(clippy::cast_lossless)]
88

9+
//
10+
mod k;
911
//
1012
mod util;
1113
//
1214
mod _0001_two_sum;
1315
//
16+
mod _0007_reverse_integer;
17+
//
1418
mod _0020_valid_parentheses;
1519
//
1620
mod _0021_merge_two_sorted_lists;
1721
//
22+
mod _0053_maximum_subarray;
23+
//
24+
mod _0088_merge_sorted_array;
25+
//
1826
mod _0202_happy_number;
1927
//
28+
mod _0415_add_strings;
29+
//
30+
mod _0953_verifying_an_alien_dictionary;
31+
//
2032
mod _1281_subtract_the_product_and_sum_of_digits_of_an_integer;
2133
//
2234
mod _1287_element_appearing_more_than_25_in_sorted_array;

0 commit comments

Comments
 (0)