Golang每日一练(leetDay0070) 移除链表元素、计数质数

简介: Golang每日一练(leetDay0070) 移除链表元素、计数质数

203. 移除链表元素  Remove Linked-list Elements


给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点


示例 1:

d25595fdbfc9fca0f45590162d1b3c27.jpeg


输入:head = [1,2,6,3,4,5,6], val = 6

输出:[1,2,3,4,5]


示例 2:

输入:head = [], val = 1

输出:[]


示例 3:

输入:head = [7,7,7,7], val = 7

输出:[]

 

提示:

   列表中的节点数目在范围 [0, 104] 内

   1 <= Node.val <= 50

   0 <= val <= 50

代码:

package main
import "fmt"
type ListNode struct {
  Val  int
  Next *ListNode
}
func removeElements(head *ListNode, val int) *ListNode {
  top := &ListNode{Val: 0}
  top.Next = head
  pre := top
  temp := head
  for temp != nil {
    if temp.Val == val {
      pre.Next = temp.Next
    } else {
      pre = temp
    }
    temp = temp.Next
  }
  return top.Next
}
func createLinkedList(nums []int) *ListNode {
  if len(nums) == 0 {
    return nil
  }
  head := &ListNode{Val: nums[0]}
  cur := head
  for i := 1; i < len(nums); i++ {
    cur.Next = &ListNode{Val: nums[i]}
    cur = cur.Next
  }
  return head
}
func printLinkedList(head *ListNode) {
  cur := head
  for cur != nil {
    fmt.Print(cur.Val, "->")
    cur = cur.Next
  }
  fmt.Println("nil")
}
func main() {
  nums := []int{1, 2, 6, 3, 4, 5, 6}
  head := createLinkedList(nums)
  printLinkedList(head)
  head = removeElements(head, 6)
  printLinkedList(head)
  nums2 := []int{}
  head = createLinkedList(nums2)
  printLinkedList(head)
  head = removeElements(head, 1)
  printLinkedList(head)
  nums3 := []int{7, 7, 7, 7}
  head = createLinkedList(nums3)
  printLinkedList(head)
  head = removeElements(head, 7)
  printLinkedList(head)
}

输出:

1->2->6->3->4->5->6->nil

1->2->3->4->5->nil

nil

nil

7->7->7->7->nil

nil

递归法:

package main
import "fmt"
type ListNode struct {
  Val  int
  Next *ListNode
}
func removeElements(head *ListNode, val int) *ListNode {
  if head == nil {
    return nil
  }
  head.Next = removeElements(head.Next, val)
  if head.Val == val {
    return head.Next
  }
  return head
}
func createLinkedList(nums []int) *ListNode {
  if len(nums) == 0 {
    return nil
  }
  head := &ListNode{Val: nums[0]}
  cur := head
  for i := 1; i < len(nums); i++ {
    cur.Next = &ListNode{Val: nums[i]}
    cur = cur.Next
  }
  return head
}
func printLinkedList(head *ListNode) {
  cur := head
  for cur != nil {
    fmt.Print(cur.Val, "->")
    cur = cur.Next
  }
  fmt.Println("nil")
}
func main() {
  nums := []int{1, 2, 6, 3, 4, 5, 6}
  head := createLinkedList(nums)
  printLinkedList(head)
  head = removeElements(head, 6)
  printLinkedList(head)
}



204. 计数质数 Count Primes


给定整数 n ,返回 所有小于非负整数 n 的质数的数量


示例 1:

输入:n = 10

输出:4

解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。


示例 2:

输入:n = 0

输出:0


示例 3:

输入:n = 1

输出:0


提示:

  • 0 <= n <= 5 * 10^6

代码:

package main
import (
  "fmt"
  "math"
)
func countPrimes(n int) int {
  if n <= 2 {
    return 0
  }
  isPrime := make([]bool, n)
  for i := 0; i < n; i++ {
    isPrime[i] = true
  }
  isPrime[0] = false
  isPrime[1] = false
  for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
    if isPrime[i] {
      for j := i * i; j < n; j += i {
        isPrime[j] = false
      }
    }
  }
  count := 0
  for i := 0; i < n; i++ {
    if isPrime[i] {
      count++
    }
  }
  return count
}
func main() {
  fmt.Println(countPrimes(10))
  fmt.Println(countPrimes(0))
  fmt.Println(countPrimes(1))
}


输出:

4

0

0

目录
相关文章
|
12月前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
125 1
|
4月前
|
存储
203. 移除链表元素,707.设计链表,206. 反转链表
链表是数据结构中的重要概念,包含单链表、双链表和循环链表。单链表每个节点存储数据与下一节点指针;双链表增加上一节点指针;循环链表首尾相连。 **例题解析:** 1. **203. 移除链表元素**:通过遍历链表删除指定值节点,注意处理头节点特殊情况。 2. **707. 设计链表**:实现链表的增删查操作,需理解指针操作逻辑,避免直接修改目标节点。 3. **206. 反转链表**:采用双指针或递归方法改变节点指向,完成链表反转。 以上题目涵盖链表核心操作,掌握后可灵活应对相关问题。
|
12月前
【LeetCode 06】203.移除链表元素
【LeetCode 06】203.移除链表元素
84 0
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
101 0
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
132 2
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点.
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点
168 1
[leetcode 链表] 反转链表 vs 链表相交
[leetcode 链表] 反转链表 vs 链表相交