[Algorithms] Array & Linked List

反转链表(简单)

问题链接:reverse-linked-list

  • 非递归方式
public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) {
      return head;
    }
    ListNode pre = null;
    ListNode curr = head;
    while (curr != null) {
      ListNode next = curr.next;
      curr.next = pre;
      pre = curr;
      curr = next;
    }
    return pre;
}
  • 递归方式
public ListNode reverseList(ListNode head) {
  if(head == null || head.next == null){
    return head;
  }
  ListNode newNode = reverseList(head.next);
  newNode.next = head;
  head.next = null;
  return newNode;
}

两两交换链表中的节点(中等)

问题链接:swap-nodes-in-pairs

  • 非递归方式
public ListNode swapPairs(ListNode head) {
  if (head == null || head.next == null) {
    return head;
  }
  ListNode dummy = new ListNode(0);
  ListNode prev = dummy;
  dummy.next = head;
  ListNode curr = head;
  while (curr != null) {
    ListNode next = curr.next;
    if (next == null) {
      break;
    }
    prev.next = next;
    curr.next = next.next;
    next.next = curr;
    prev = curr;
    curr = curr.next;
  }
  return dummy.next;
}
  • 递归方式
public ListNode swapPairs(ListNode head) {
  if (head == null || head.next == null) {
    return head;
  }
  ListNode next = head.next;
  head.next = swapPairs(next.next);
  next.next = head;
  return next;
}

环形链表(简单)

问题链接:linked-list-cycle

public boolean hasCycle(ListNode head) {
  ListNode slow = head;
  ListNode fast = head;
  while (fast != null && fast.next != null) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow == fast) {
      return true;
    }
  }
  return false;
}

环形链表ii(中等)

问题链接:linked-list-cycle-ii

public ListNode detectCycle(ListNode head) {
  ListNode tortoise = head;
  ListNode hare = head;
  while (hare != null && hare.next != null) {
    tortoise = tortoise.next;
    hare = hare.next.next;
    if (tortoise == hare) {
      tortoise = head;
      while (tortoise != hare) {
        tortoise = tortoise.next;
        hare = hare.next;
      }
      return tortoise;
    }
  }
  return null;
}

K 个一组翻转链表(困难)

问题链接:reverse-nodes-in-k-group

  • 顺序执行的方式,分组反转,然后合并结果
public ListNode reverseKGroup(ListNode head, int k) {
  ListNode dummy = new ListNode(0);
  dummy.next = head;

  ListNode prev = dummy;
  ListNode end = dummy;

  while (end.next != null) {
    ListNode start = prev.next;
    for (int i = 0; i < k && end != null; i++) {
      end = end.next;
    }
    if (end == null) {
      break;
    }

    ListNode next = end.next;
    end.next = null;
    prev.next = reverse(start);
    start.next = next;

    prev = start;
    end = prev;
  }

  return dummy.next;
}

public ListNode reverse(ListNode head) {
  if (head == null || head.next == null) {
    return head;
  }

  ListNode pre = null;
  ListNode curr = head;
  while (curr != null) {
    ListNode next = curr.next;
    curr.next = pre;
    pre = curr;
    curr = next;
  }
  return pre;
}

转载于:https://www.cnblogs.com/NotNil/p/11395672.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: "list of synthesis algorithms"的意思是“合成算法列表”,通常指音频合成方面的算法列表。这些算法可以用来生成声音、音乐、语音等。常见的合成算法包括频率调制合成(FM Synthesis)、加法合成(Additive Synthesis)、物理建模合成(Physical Modeling Synthesis)、渐变波表合成(Wavetable Synthesis)等等。 ### 回答2: "list of synthesis algorithms"的意思是合成算法列表。在计算机科学和工程领域,算法是一组有序的、逐步规定的操作步骤,用于解决特定问题或完成特定任务。合成算法是一类特定的算法,用于将给定的输入转换为所需的输出。 合成算法可以应用于各种领域,例如音频合成、图像合成、语音合成等。音频合成算法可以生成各种类型的声音,包括音乐、人声、环境音效等。图像合成算法可以根据给定的参数和规则生成新的图像,如计算机生成的图像、特效等。语音合成算法可以将文字或符号转化为能够听到的声音。 "list of synthesis algorithms"通常指的是列举出不同类型的合成算法,并可能提供相关的详细描述、参数和应用示例。这个列表可以帮助研究人员、开发人员或艺术家选择适合他们需求的合成算法,并了解每种算法的特点和用途。 总之,“list of synthesis algorithms”指的是一个包含各种类型合成算法的列表,用于生成各种类型的输出,包括音频、图像和语音等,以解决特定问题或实现特定任务。 ### 回答3: "List of synthesis algorithms" 意思是“合成算法列表”。在计算机科学和信号处理领域,合成(synthesis)是指通过组合不同的元素或步骤来生成新的实体或数据。因此,合成算法是一系列用于生成特定实体(如音频、图像或文本)的算法的集合。 合成算法的目的是将多个输入元素(如音频信号的频率、幅度和相位)合并起来,生成一个新的输出(如混合音频)。这些算法通常具有特定的数学和计算机操作,以执行合成过程。合成算法可以根据应用程序的需求和目标进行选择和应用。 合成算法可以应用于多个领域,例如音乐合成、语音合成、图像生成和文本生成。在音乐合成中,合成算法可以根据用户的输入和参数自动生成音乐片段。在语音合成中,合成算法可以根据文本输入生成人工语音。在图像生成中,合成算法可以使用输入的特征和参数生成新的图像。在文本生成中,合成算法可以根据给定的文本生成相关的内容。 合成算法的选择取决于所需生成实体的类型和目标。不同的算法可以应用不同的技术,例如加法合成、FM合成、渐进合成等。通过使用合成算法,我们能够以自动化的方式生成新的数据和实体,从而实现各种应用和创意的需要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值