给定一个包含N 个节点的单向链表,任务是从列表中找到所有不同节点的平均值,这些节点的数据值为奇数斐波那契数。
例子:
Input: LL = 5 -> 21 -> 8 ->12-> 3 -> 13 ->144 -> 6
Output 10.5
Explanation:
Fibonacci Nodes present in the Linked List are {5, 21, 8, 3, 13, 144}
Odd Fibonacci Nodes present in the List are {5, 21, 3, 13}
Count of Odd Fibonacci Nodes is 4
Therefore , Mean of Odd Fibonacci Node Values = (5 + 21 + 3 + 13) / 4 = 10.5
Input: LL = 55 -> 3 -> 91 -> 89 -> 76 -> 233 -> 34 -> 87 -> 5 -> 100
Output:77
Explanation:
Fibonacci Nodes present in the Linked List are {55, 3, 89, 233, 34, 5}
Odd Fibonacci Nodes present in the Linked List are {55, 3, 89, 233, 5}
Count of Odd Fibonacci Nodes is 5
Therefore , Mean of Odd Fibonacci Node Values = (55 + 5 + 3 + 89 + 233) / 5 = 77
方法:这个想法是使用散列来预先计算和存储所有斐波那契数直到链表中的最大元素。
请按照以下步骤解决问题:
- 初始化两个变量,比如cnt , sum分别存储奇数斐波纳契节点的数量和所有奇数斐波纳契节点的总和。
- 遍历单向链表并存储链表的最大元素,例如Max 。
- 创建一个集合,比如hashmap来存储所有 Fibonacci 数直到Max 。
- 遍历链表并检查当前节点是否为奇数和斐波那契数。如果发现为真,则增加cnt的值并将当前节点的数据值相加并从Hashmap 中删除该节点。
- 最后,将(sum / cnt)的值打印为所需答案。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Structure of a
// singly Linked List
struct Node {
// Stores data value
// of a Node
int data;
// Stores pointer
// to next Node
Node* next;
};
// Function to insert a node at the
// beginning of the singly Linked List
void push(Node** head_ref, int new_data)
{
// Create a new Node
Node* new_node = new Node;
// Insert the data into
// the Node
new_node->data = new_data;
// Insert pointer to
// the next Node
new_node->next = (*head_ref);
// Update head_ref
(*head_ref) = new_node;
}
// Function to find the largest
// element from the linked list
int largestElement(struct Node* head_ref)
{
// Stores the largest element
// in the linked list
int Max = INT_MIN;
Node* head = head_ref;
// Iterate over the linked list
while (head != NULL) {
// If max is less than
// head->data
if (Max < head->data) {
// Update max
Max = head->data;
}
// Update head
head = head->next;
}
return Max;
}
// Function to store all Fibonacci numbers
// up to the largest element of the list
set createHashMap(int Max)
{
// Store all Fibonacci numbers
// up to Max
set hashmap;
// Stores first element of
// Fibonacci number
int prev = 0;
// Stores second element of
// Fibonacci number
int curr = 1;
// Insert prev into hashmap
hashmap.insert(prev);
// Insert curr into hashmap
hashmap.insert(curr);
// Insert all elements of
// Fibonacci numbers up to Max
while (curr <= Max) {
// Stores current fibonacci number
int temp = curr + prev;
// Insert temp into hashmap
hashmap.insert(temp);
// Update prev
prev = curr;
// Update curr
curr = temp;
}
return hashmap;
}
// Function to find the mean
// of odd Fibonacci nodes
double meanofnodes(struct Node* head)
{
// Stores the largest element
// in the linked list
int Max = largestElement(head);
// Stores all fibonacci numbers
// up to Max
set hashmap
= createHashMap(Max);
// Stores current node
// of linked list
Node* curr = head;
// Stores count of
// odd Fibonacci nodes
int cnt = 0;
// Stores sum of all
// odd fibonacci nodes
double sum = 0.0;
// Traverse the linked list
while (curr != NULL) {
// if the data value of
// current node is an odd number
if ((curr->data) & 1){
// if data value of the node
// is present in hashmap
if (hashmap.count(curr->data)) {
// Update cnt
cnt++;
// Update sum
sum += curr->data;
// Remove current fibonacci number
// from hashmap so that duplicate
// elements can't be counted
hashmap.erase(curr->data);
}
}
// Update curr
curr = curr->next;
}
// Return the required mean
return (sum / cnt);
}
// Driver Code
int main()
{
// Stores head node of
// the linked list
Node* head = NULL;
// Insert all data values
// in the linked list
push(&head, 5);
push(&head, 21);
push(&head, 8);
push(&head, 12);
push(&head, 3);
push(&head, 13);
push(&head, 144);
push(&head, 6);
cout<
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Structure of a
// singly Linked List
static class Node
{
// Stores data value
// of a Node
int data;
// Stores pointer
// to next Node
Node next;
};
static Node head;
// Function to insert a
// node at the beginning
// of the singly Linked List
static Node push(Node head_ref,
int new_data)
{
// Create a new Node
Node new_node = new Node();
// Insert the data into
// the Node
new_node.data = new_data;
// Insert pointer to
// the next Node
new_node.next = head_ref;
// Update head_ref
head_ref = new_node;
return head_ref;
}
// Function to find the largest
// element from the linked list
static int largestElement(Node head_ref)
{
// Stores the largest element
// in the linked list
int Max = Integer.MIN_VALUE;
Node head = head_ref;
// Iterate over the
// linked list
while (head != null)
{
// If max is less than
// head.data
if (Max < head.data)
{
// Update max
Max = head.data;
}
// Update head
head = head.next;
}
return Max;
}
// Function to store all
// Fibonacci numbers up
// to the largest element
// of the list
static HashSet
createHashMap(int Max)
{
// Store all Fibonacci
// numbers up to Max
HashSet hashmap =
new HashSet<>();
// Stores first element of
// Fibonacci number
int prev = 0;
// Stores second element of
// Fibonacci number
int curr = 1;
// Insert prev into hashmap
hashmap.add(prev);
// Insert curr into hashmap
hashmap.add(curr);
// Insert all elements of
// Fibonacci numbers up
// to Max
while (curr <= Max)
{
// Stores current fibonacci
// number
int temp = curr + prev;
// Insert temp into hashmap
hashmap.add(temp);
// Update prev
prev = curr;
// Update curr
curr = temp;
}
return hashmap;
}
// Function to find the mean
// of odd Fibonacci nodes
static double meanofnodes()
{
// Stores the largest element
// in the linked list
int Max = largestElement(head);
// Stores all fibonacci numbers
// up to Max
HashSet hashmap =
createHashMap(Max);
// Stores current node
// of linked list
Node curr = head;
// Stores count of
// odd Fibonacci nodes
int cnt = 0;
// Stores sum of all
// odd fibonacci nodes
double sum = 0.0;
// Traverse the linked list
while (curr != null)
{
// if the data value of
// current node is an
// odd number
if ((curr.data) %2== 1)
{
// if data value of the node
// is present in hashmap
if (hashmap.contains(curr.data))
{
// Update cnt
cnt++;
// Update sum
sum += curr.data;
// Remove current fibonacci
// number from hashmap so that
// duplicate elements can't be
// counted
hashmap.remove(curr.data);
}
}
// Update curr
curr = curr.next;
}
// Return the required mean
return (sum / cnt);
}
// Driver Code
public static void main(String[] args)
{
// Stores head node of
// the linked list
head = null;
// Insert all data values
// in the linked list
head = push(head, 5);
head = push(head, 21);
head = push(head, 8);
head = push(head, 12);
head = push(head, 3);
head = push(head, 13);
head = push(head, 144);
head = push(head, 6);
System.out.print(meanofnodes());
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Structure of a
# singly Linked List
class Node:
def __init__(self):
# Stores data value
# of a Node
self.data = 0
# Stores pointer
# to next Node
self.next = None
# Function to add a node at the
# beginning of the singly Linked List
def push( head_ref, new_data):
# Create a new Node
new_node = Node()
# Insert the data into
# the Node
new_node.data = new_data;
# Insert pointer to
# the next Node
new_node.next = head_ref
# Update head_ref
head_ref = new_node;
return head_ref
# Function to find the largest
# element from the linked list
def largestElement(head_ref):
# Stores the largest element
# in the linked list
Max = -10000000
head = head_ref;
# Iterate over the linked list
while (head != None):
# If max is less than
# head.data
if (Max < head.data):
# Update max
Max = head.data;
# Update head
head = head.next;
return Max;
# Function to store all Fibonacci numbers
# up to the largest element of the list
def createHashMap(Max):
# Store all Fibonacci numbers
# up to Max
hashmap = set()
# Stores first element of
# Fibonacci number
prev = 0;
# Stores second element of
# Fibonacci number
curr = 1;
# Insert prev into hashmap
hashmap.add(prev);
# Insert curr into hashmap
hashmap.add(curr);
# Insert all elements of
# Fibonacci numbers up to Max
while (curr <= Max):
# Stores current fibonacci number
temp = curr + prev;
# Insert temp into hashmap
hashmap.add(temp);
# Update prev
prev = curr;
# Update curr
curr = temp;
return hashmap;
# Function to find the mean
# of odd Fibonacci nodes
def meanofnodes(head):
# Stores the largest element
# in the linked list
Max = largestElement(head);
# Stores all fibonacci numbers
# up to Max
hashmap = createHashMap(Max);
# Stores current node
# of linked list
curr = head;
# Stores count of
# odd Fibonacci nodes
cnt = 0;
# Stores sum of all
# odd fibonacci nodes
sum = 0.0;
# Traverse the linked list
while (curr != None):
# if the data value of
# current node is an odd number
if ((curr.data) % 2 == 1):
# if data value of the node
# is present in hashmap
if (curr.data in hashmap):
# Update cnt
cnt += 1
# Update sum
sum += curr.data;
# Remove current fibonacci number
# from hashmap so that duplicate
# elements can't be counted
hashmap.remove(curr.data);
# Update curr
curr = curr.next;
# Return the required mean
return (sum / cnt);
# Driver Code
if __name__=='__main__':
# Stores head node of
# the linked list
head = None;
# Insert all data values
# in the linked list
head = push(head, 5);
head = push(head, 21);
head = push(head, 8);
head = push(head, 12);
head = push(head, 3);
head = push(head, 13);
head = push(head, 144);
head = push(head, 6);
print(meanofnodes(head))
# This code is contributed by rutvik_56
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Structure of a
// singly Linked List
public class Node
{
// Stores data value
// of a Node
public int data;
// Stores pointer
// to next Node
public Node next;
};
static Node head;
// Function to insert a
// node at the beginning
// of the singly Linked List
static Node push(Node head_ref,
int new_data)
{
// Create a new Node
Node new_node = new Node();
// Insert the data into
// the Node
new_node.data = new_data;
// Insert pointer to
// the next Node
new_node.next = head_ref;
// Update head_ref
head_ref = new_node;
return head_ref;
}
// Function to find the largest
// element from the linked list
static int largestElement(Node head_ref)
{
// Stores the largest element
// in the linked list
int Max = int.MinValue;
Node head = head_ref;
// Iterate over the
// linked list
while (head != null)
{
// If max is less than
// head.data
if (Max < head.data)
{
// Update max
Max = head.data;
}
// Update head
head = head.next;
}
return Max;
}
// Function to store all
// Fibonacci numbers up
// to the largest element
// of the list
static HashSet createDictionary(int Max)
{
// Store all Fibonacci
// numbers up to Max
HashSet hashmap = new HashSet();
// Stores first element of
// Fibonacci number
int prev = 0;
// Stores second element of
// Fibonacci number
int curr = 1;
// Insert prev into hashmap
hashmap.Add(prev);
// Insert curr into hashmap
hashmap.Add(curr);
// Insert all elements of
// Fibonacci numbers up
// to Max
while (curr <= Max)
{
// Stores current fibonacci
// number
int temp = curr + prev;
// Insert temp into hashmap
hashmap.Add(temp);
// Update prev
prev = curr;
// Update curr
curr = temp;
}
return hashmap;
}
// Function to find the mean
// of odd Fibonacci nodes
static double meanofnodes()
{
// Stores the largest element
// in the linked list
int Max = largestElement(head);
// Stores all fibonacci numbers
// up to Max
HashSet hashmap = createDictionary(Max);
// Stores current node
// of linked list
Node curr = head;
// Stores count of
// odd Fibonacci nodes
int cnt = 0;
// Stores sum of all
// odd fibonacci nodes
double sum = 0.0;
// Traverse the linked list
while (curr != null)
{
// if the data value of
// current node is an
// odd number
if ((curr.data) % 2 == 1)
{
// if data value of the node
// is present in hashmap
if (hashmap.Contains(curr.data))
{
// Update cnt
cnt++;
// Update sum
sum += curr.data;
// Remove current fibonacci
// number from hashmap so that
// duplicate elements can't be
// counted
hashmap.Remove(curr.data);
}
}
// Update curr
curr = curr.next;
}
// Return the required mean
return (sum / cnt);
}
// Driver Code
public static void Main(String[] args)
{
// Stores head node of
// the linked list
head = null;
// Insert all data values
// in the linked list
head = push(head, 5);
head = push(head, 21);
head = push(head, 8);
head = push(head, 12);
head = push(head, 3);
head = push(head, 13);
head = push(head, 144);
head = push(head, 6);
Console.Write(meanofnodes());
}
}
// This code is contributed by Amit Katiyar
Javascript
10.5
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。