Skip to content

Commit c16f184

Browse files
committed
Merge branch 'master' of github.com:cch123/leetcode-rust
2 parents f060016 + 71a0fdc commit c16f184

File tree

15 files changed

+280
-0
lines changed

15 files changed

+280
-0
lines changed

S0789-escape-the-ghosts/Cargo.lock

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

S0789-escape-the-ghosts/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "S0789-escape-the-ghosts"
3+
version = "0.1.0"
4+
authors = ["xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
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 escape_ghosts(ghosts: Vec<Vec<i32>>, target: Vec<i32>) -> bool {
5+
// 只要本体和目标的距离 < 任何一个 ghost 到目标的距离,就可以获胜
6+
// 否则的话 ghost 可以选择行动模式:
7+
// 1.先不管本体,直接走到终点
8+
// 2.这样不管本体怎么走,最终都没法获胜了
9+
let target_dist = target[0].abs() + target[1].abs();
10+
let mut res = true;
11+
ghosts.iter().for_each(|g|{
12+
if (g[0]-target[0]).abs() + (g[1]-target[1]).abs() < target_dist {
13+
res = false;
14+
}
15+
});
16+
17+
res
18+
}
19+
}
20+
21+
fn main() {
22+
}

S0817-linked-list-components/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "S0817-linked-list-components"
3+
version = "0.1.0"
4+
authors = ["xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
struct Solution;
2+
// Definition for singly-linked list.
3+
#[derive(PartialEq, Eq, Debug)]
4+
pub struct ListNode {
5+
pub val: i32,
6+
pub next: Option<Box<ListNode>>
7+
}
8+
9+
impl ListNode {
10+
#[inline]
11+
fn new(val: i32) -> Self {
12+
ListNode {
13+
next: None,
14+
val
15+
}
16+
}
17+
}
18+
use std::collections::HashSet;
19+
impl Solution {
20+
pub fn num_components(head: Option<Box<ListNode>>, g: Vec<i32>) -> i32 {
21+
let mut cur = head;
22+
let mut passing_component = false;
23+
let set = g.iter().collect::<HashSet<_>>();
24+
let mut res = 0;
25+
while cur.is_some() {
26+
if passing_component == false {
27+
if set.contains(&cur.as_ref().unwrap().val) {
28+
passing_component = true;
29+
}
30+
} else {
31+
if !set.contains(&cur.as_ref().unwrap().val) {
32+
passing_component = false;
33+
res += 1;
34+
}
35+
}
36+
cur = cur.unwrap().next;
37+
}
38+
39+
if passing_component == true {
40+
return res + 1;
41+
}
42+
res
43+
}
44+
}
45+
46+
fn main() {
47+
println!("Hello, world!");
48+
}

S0977-squares-of-a-sorted-array/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "S0977-squares-of-a-sorted-array"
3+
version = "0.1.0"
4+
authors = ["xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct Solution;
2+
impl Solution {
3+
pub fn sorted_squares(a: Vec<i32>) -> Vec<i32> {
4+
let mut res = a.iter().map(|x|(*x)*(*x)).collect::<Vec<_>>();
5+
res.sort();
6+
res
7+
}
8+
}
9+
10+
fn main() {
11+
println!("Hello, world!");
12+
}

S0978-longest-turbulent-subarray/Cargo.lock

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

0 commit comments

Comments
 (0)