Skip to content

Commit 01381fc

Browse files
author
EternalChildren
committed
increase three problem
1 parent f90aca0 commit 01381fc

File tree

9 files changed

+190
-0
lines changed

9 files changed

+190
-0
lines changed

isLongPressedName/rust/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

isLongPressedName/rust/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
authors = ["EternalChildren <colin.zcn@outlook.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

isLongPressedName/rust/src/lib.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 4ms 2.1MB
2+
pub fn is_long_pressed_name(name: String, typed: String) -> bool {
3+
let vec = name.as_bytes();
4+
let mut index = 0;
5+
6+
for k in typed.bytes() {
7+
println!("index is {}", index);
8+
if k == vec[index] {
9+
if index < vec.len() - 1 {
10+
index += 1;
11+
}
12+
} else {
13+
if index == 0 || vec[index - 1] != k {
14+
return false;
15+
}
16+
}
17+
}
18+
if vec[index] != typed.bytes().last().unwrap() {
19+
return false;
20+
}
21+
22+
true
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn it_works() {
31+
let name = String::from("alex");
32+
let typed = String::from("aaleex");
33+
assert_eq!(is_long_pressed_name(name, typed), true);
34+
35+
let name1 = String::from("saeed");
36+
let typed1 = String::from("ssaaedd");
37+
assert_eq!(is_long_pressed_name(name1, typed1), false);
38+
39+
let name2 = String::from("leelee");
40+
let typed2 = String::from("lleeelee");
41+
assert_eq!(is_long_pressed_name(name2, typed2), true);
42+
43+
let name3 = String::from("laiden");
44+
let typed3 = String::from("laiden");
45+
assert_eq!(is_long_pressed_name(name3, typed3), true);
46+
47+
let name4 = String::from("vtkgn");
48+
let typed4 = String::from("vttkgnn");
49+
assert_eq!(is_long_pressed_name(name4, typed4), true);
50+
51+
let name4 = String::from("vtkgn");
52+
let typed4 = String::from("vttkgnn");
53+
assert_eq!(is_long_pressed_name(name4, typed4), true);
54+
55+
let name5 = String::from("dfuyalc");
56+
let typed5 = String::from("fuuyallc");
57+
assert_eq!(is_long_pressed_name(name5, typed5), false);
58+
}
59+
}

uniquePaths/rust/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

uniquePaths/rust/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
authors = ["EternalChildren <colin.zcn@outlook.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

uniquePaths/rust/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 0ms 1.9MB
2+
pub fn unique_paths_combination(m: i32, n: i32) -> i32 {
3+
let b = m + n - 2;
4+
let t = if m < n { m - 1 } else { n - 1 };
5+
let mut ans: i64 = 1;
6+
for i in 1..=t {
7+
ans = ans * (b - i + 1) as i64 / i as i64;
8+
}
9+
ans as i32
10+
}
11+
12+
#[cfg(test)]
13+
mod tests {
14+
use super::*;
15+
16+
#[test]
17+
fn it_works() {
18+
assert_eq!(unique_paths_combination(3, 2), 3);
19+
assert_eq!(unique_paths_combination(7, 3), 28);
20+
assert_eq!(unique_paths_combination(51, 9), 1916797311);
21+
}
22+
}

validMountainArray/rust/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

validMountainArray/rust/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
authors = ["EternalChildren <colin.zcn@outlook.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

validMountainArray/rust/src/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// 4ms 2.2MB
2+
pub fn valid_mountain_array(a: Vec<i32>) -> bool {
3+
if a.len() < 3 {
4+
return false;
5+
}
6+
let mut sign = false;
7+
let mut num = -1;
8+
9+
for (i, v) in a.iter().enumerate() {
10+
println!("sign is {}", sign);
11+
println!("num is {}", num);
12+
if !sign {
13+
if v > &num {
14+
num = *v;
15+
} else if v < &num {
16+
if i == 1 {
17+
return false;
18+
}
19+
sign = true;
20+
num = *v;
21+
} else {
22+
return false;
23+
}
24+
} else {
25+
if v < &num {
26+
num = *v;
27+
} else {
28+
return false;
29+
}
30+
}
31+
}
32+
33+
if !sign {
34+
return false;
35+
}
36+
37+
true
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::*;
43+
44+
#[test]
45+
fn it_works() {
46+
let v = vec![2, 1];
47+
assert_eq!(valid_mountain_array(v), false);
48+
49+
let v1 = vec![3, 5, 5];
50+
assert_eq!(valid_mountain_array(v1), false);
51+
52+
let v2 = vec![0, 3, 2, 1];
53+
assert_eq!(valid_mountain_array(v2), true);
54+
}
55+
56+
#[test]
57+
fn special() {
58+
let v3 = vec![9, 8, 7, 6, 0];
59+
assert_eq!(valid_mountain_array(v3), false);
60+
61+
let v4 = vec![0, 7, 8, 9];
62+
assert_eq!(valid_mountain_array(v4), false);
63+
}
64+
65+
#[test]
66+
fn last() {
67+
let v = vec![3, 7, 20, 14, 15, 14, 10, 8, 2, 1];
68+
assert_eq!(valid_mountain_array(v), false);
69+
}
70+
}

0 commit comments

Comments
 (0)