当前位置: 首页 > news >正文

LeetCode 2807. 在链表中插入最大公约数【链表,迭代,递归】1279

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

给你一个链表的头 head ,每个结点包含一个整数值。

在相邻结点之间,请你插入一个新的结点,结点值为这两个相邻结点值的 最大公约数

请你返回插入之后的链表。

两个数的 最大公约数 是可以被两个数字整除的最大正整数。

示例 1:

输入:head = [18,6,10,3]
输出:[18,6,6,2,10,1,3]
解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
- 186 的最大公约数为 6 ,插入第一和第二个结点之间。
- 610 的最大公约数为 2 ,插入第二和第三个结点之间。
- 103 的最大公约数为 1 ,插入第三和第四个结点之间。
所有相邻结点之间都插入完毕,返回链表。

示例 2:

输入:head = [7]
输出:[7]
解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
没有相邻结点,所以返回初始链表。

提示:

  • 链表中结点数目在 [1, 5000] 之间。
  • 1 <= Node.val <= 1000

解法 迭代

遍历链表,在当前节点 cur \textit{cur} cur 后面插入 g c d gcd gcd 节点,同时 gcd \textit{gcd} gcd 节点指向 cur \textit{cur} cur 的下一个节点。插入后, cur \textit{cur} cur 更新为 cur . next . next \textit{cur}.\textit{next}.\textit{next} cur.next.next ,也就是 c u r cur cur 原来的下一个节点,开始下一轮循环。循环直到 c u r cur cur 没有下一个节点为止。

// cpp
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;}
};
// java
class Solution {public ListNode insertGreatestCommonDivisors(ListNode head) {for (ListNode cur = head; cur.next != null; cur = cur.next.next) {cur.next = new ListNode(gcd(cur.val, cur.next.val), cur.next);}return head;}private int gcd(int a, int b) { while (a != 0) {int t = a;a = b % a;b = t;}return b;}
}
// python
class Solution:def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]:cur = headwhile cur.next:cur.next = ListNode(gcd(cur.val, cur.next.val), cur.next)cur = cur.next.nextreturn head
// go
/*** Definition for singly-linked list.* type ListNode struct {*     Val int*     Next *ListNode* }*/
func insertGreatestCommonDivisors(head *ListNode) *ListNode {for cur := head; cur.Next != nil; cur = cur.Next.Next {cur.Next = &ListNode{gcd(cur.Val, cur.Next.Val), cur.Next}}return head
}
func gcd(a, b int) int {for a != 0 {a, b = b % a, a}return b
}
// rust
// 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 {pub fn insert_greatest_common_divisors(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {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}fn gcd(mut a: i32, mut b: i32) -> i32 {while a != 0 {(a, b) = (b % a, a);}b}
}

复杂度分析:

  • 时间复杂度: O ( n log ⁡ ⁡ U ) \mathcal{O}(n\log⁡U) O(nlogU) ,其中 n n n 为链表长度, U U U 为节点值的最大值。每次计算 g c d gcd gcd 需要 O ( log ⁡ ⁡ U ) \mathcal{O}(\log⁡U) O(logU) 的时间。
  • 空间复杂度: O ( 1 ) \mathcal{O}(1) O(1) 。返回值的空间不计入。
http://www.lryc.cn/news/277421.html

相关文章:

  • Hive之set参数大全-3
  • Golang拼接字符串性能对比
  • 【问题解决】web页面html锚点定位后内容被遮挡问题解决【暗锚】
  • easyui datagrid无数据时显示无数据
  • 动态规划python简单例子-斐波那契数列
  • 免 费 搭 建 多模式商城:b2b2c、o2o、直播带货一网打尽
  • Python AttributeError: ‘NoneType‘ object has no attribute ‘shape‘如何解决
  • vue3自定义确认密码匹配验证规则
  • 岗位所处定位,岗位职责
  • 2024阿里云服务器配置推荐方案
  • OceanBase原生分布式数据库
  • 首次使用go-admin
  • 软件工程概论---内聚性和耦合性
  • 纯血鸿蒙「扩圈」100天,酝酿已久的突围
  • UICollection Compositional Layout全详解
  • 单例模式的模板
  • C#基础-空处理
  • 测试平台开发vue组件化重构前端代码
  • 龍运当头--html做一个中国火龙祝大家龙年大吉
  • Dockerfile语法和简单镜像构建
  • uniapp使用wxml-to-canvas开发小程序保存canvas图片
  • 关于数据库切换的麻烦
  • Qt/QML编程学习之心得:Linux下读写文件File(24)
  • 【Vue2+3入门到实战】(22)VUE3之组合式API - setup、reactive和ref函数、computed、watch、生命周期函数详细讲解
  • 如何在互联网上找到你想要的数据?
  • 揭秘淘宝商品详情API如何助力电商创新发展
  • vue element plus Space 间距
  • 【驱动序列】C#获取电脑硬件之CPU信息,以及它都有那些品牌
  • 目标检测-One Stage-YOLO v3
  • 安泰ATA-4014高压功率放大器在传感器脉冲涡流检测中的应用