Skip to content

Commit

Permalink
add: 在链表中插入最大公约数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 6, 2024
1 parent 339fdff commit 7c6a503
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 链表

- [在链表中插入最大公约数](src/list/insert_greatest_common_divisors_in_linked_list.rs) [链表, 数学, 数论]

- LeetCode 2807. 在链表中插入最大公约数 <https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list>

- [从链表中删去总和值为零的连续节点](src/list/remove_zero_sum_consecutive_nodes_from_linked_list.rs) [哈希表, 链表]

- LeetCode 1171. 从链表中删去总和值为零的连续节点 <https://leetcode.cn/problems/remove-zero-sum-consecutive-nodes-from-linked-list>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions src/list/insert_greatest_common_divisors_in_linked_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 在链表中插入最大公约数
// https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list
// INLINE ../../images/list/insert_greatest_common_divisors_in_linked_list.jpeg

use crate::libs::list_node::ListNode;

pub struct Solution;

// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
fn gcd(mut a: i32, mut b: i32) -> i32 {
// 如果 a 或 b 是负数,那么取绝对值
a = a.abs();
b = b.abs();
// 如果 a 或 b 是 0,那么返回另一个数
if a == 0 {
return b;
}
if b == 0 {
return a;
}
while b != 0 {
let tmp = b;
b = a % b;
a = tmp;
}
a
}

pub fn insert_greatest_common_divisors(
mut head: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
if head.is_none() {
return None;
}

let mut cur = &mut head;
while cur.as_ref().unwrap().next.is_some() {
let x = cur.as_mut().unwrap();
let next = x.next.take();
x.next = Some(Box::new(ListNode {
val: Self::gcd(x.val, next.as_ref().unwrap().val),
next,
}));
cur = &mut cur.as_mut().unwrap().next.as_mut().unwrap().next;
}
head
}
}
1 change: 1 addition & 0 deletions src/list/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod insert_greatest_common_divisors_in_linked_list;
pub mod middle_of_the_linked_list;
pub mod remove_nth_node_from_end_of_list;
pub mod remove_zero_sum_consecutive_nodes_from_linked_list;
30 changes: 30 additions & 0 deletions tests/list/insert_greatest_common_divisors_in_linked_list_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use rust_practice::libs::list_node::vec_to_list_node;
use rust_practice::list::insert_greatest_common_divisors_in_linked_list::Solution;

#[test]
fn insert_greatest_common_divisors() {
// 示例 1:
// 输入:head = [18,6,10,3]
// 输出:[18,6,6,2,10,1,3]
// 解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
// - 18 和 6 的最大公约数为 6 ,插入第一和第二个结点之间。
// - 6 和 10 的最大公约数为 2 ,插入第二和第三个结点之间。
// - 10 和 3 的最大公约数为 1 ,插入第三和第四个结点之间。
// 所有相邻结点之间都插入完毕,返回链表。
let arr = vec![18, 6, 10, 3];
let head = vec_to_list_node(&arr);
let res = vec![18, 6, 6, 2, 10, 1, 3];
let res = vec_to_list_node(&res);
assert_eq!(Solution::insert_greatest_common_divisors(head), res);

// 示例 2:
// 输入:head = [7]
// 输出:[7]
// 解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
// 没有相邻结点,所以返回初始链表。
let arr = vec![7];
let head = vec_to_list_node(&arr);
let res = vec![7];
let res = vec_to_list_node(&res);
assert_eq!(Solution::insert_greatest_common_divisors(head), res);
}
1 change: 1 addition & 0 deletions tests/list/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod middle_of_the_linked_list_test;
pub mod remove_nth_node_from_end_of_list_test;
pub mod remove_zero_sum_consecutive_nodes_from_linked_list_test;
pub mod insert_greatest_common_divisors_in_linked_list_test;

0 comments on commit 7c6a503

Please sign in to comment.