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 6b6f3ef commit 6498944
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

### 链表

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

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

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

- 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.
25 changes: 25 additions & 0 deletions src/list/insert_greatest_common_divisors_in_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 在链表中插入最大公约数
// https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list
// INLINE ../../images/list/insert_greatest_common_divisors_in_linked_list.jpeg

#include <headers.hpp>

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *insertGreatestCommonDivisors(ListNode *head) {
for (auto cur = head; cur->next; cur = cur->next->next) {
cur->next = new ListNode(gcd(cur->val, cur->next->val), cur->next);
}
return head;
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2024-01-05 14:42:19
// 执行编译时间:2024-01-06 17:10:44
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down
41 changes: 41 additions & 0 deletions test/list/insert_greatest_common_divisors_in_linked_list_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <list/insert_greatest_common_divisors_in_linked_list.cpp>

TEST(在链表中插入最大公约数, ListNode) {
Solution solution;
// 示例 1:
// 输入:head = [18,6,10,3]
// 输出:[18,6,6,2,10,1,3]
// 解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
// - 18 和 6 的最大公约数为 6 ,插入第一和第二个结点之间。
// - 6 和 10 的最大公约数为 2 ,插入第二和第三个结点之间。
// - 10 和 3 的最大公约数为 1 ,插入第三和第四个结点之间。
// 所有相邻结点之间都插入完毕,返回链表。
int list[] = {18, 6, 10, 3};
int n = sizeof(list) / sizeof(int);
ListNode *head = createLinkedList(list, n);
ListNode *result = solution.insertGreatestCommonDivisors(head);
int expected[] = {18, 6, 6, 2, 10, 1, 3};
int m = sizeof(expected) / sizeof(int);
ListNode *expected_result = createLinkedList(expected, m);
EXPECT_STREQ(listNodeToString(head).c_str(),
listNodeToString(expected_result).c_str());
deleteLinkedList(head);
deleteLinkedList(expected_result);

// 示例 2:
// 输入:head = [7]
// 输出:[7]
// 解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
// 没有相邻结点,所以返回初始链表。
int list2[] = {7};
int n2 = sizeof(list2) / sizeof(int);
ListNode *head2 = createLinkedList(list2, n2);
ListNode *result2 = solution.insertGreatestCommonDivisors(head2);
int expected2[] = {7};
int m2 = sizeof(expected2) / sizeof(int);
ListNode *expected_result2 = createLinkedList(expected2, m2);
EXPECT_STREQ(listNodeToString(head2).c_str(),
listNodeToString(expected_result2).c_str());
deleteLinkedList(head2);
deleteLinkedList(expected_result2);
}

0 comments on commit 6498944

Please sign in to comment.