给定一个排序的不同节点的双向链表(没有两个节点具有相同的数据)和一个值 x。任务是计算列表中乘积达到给定值 x 的三元组。
例子:
Input: list = 1->2->4->5->6->8->9, x = 8
Output: 1
Triplet is (1, 2, 4)
Input: list = 1->2->4->5->6->8->9, x = 120
Output: 1
Triplet is (4, 5, 6)
朴素的方法:使用三个嵌套循环生成所有三元组并检查三元组中的元素是否达到 x。
下面是上述方法的实现:
C++
// C++ implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
#include
using namespace std;
// structure of node of doubly linked list
struct Node {
int data;
struct Node *next, *prev;
};
// function to count triplets in a sorted doubly linked list
// whose product is equal to a given value 'x'
int countTriplets(struct Node* head, int x)
{
struct Node *ptr1, *ptr2, *ptr3;
int count = 0;
// generate all possible triplets
for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
for (ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next)
for (ptr3 = ptr2->next; ptr3 != NULL; ptr3 = ptr3->next)
// if elements in the current triplet product up to 'x'
if ((ptr1->data * ptr2->data * ptr3->data) == x)
// increment count
count++;
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct Node** head, int data)
{
// allocate node
struct Node* temp = new Node();
// put in the data
temp->data = data;
temp->next = temp->prev = NULL;
if ((*head) == NULL)
(*head) = temp;
else {
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
// Driver program to test above
int main()
{
// start with an empty doubly linked list
struct Node* head = NULL;
// insert values in sorted order
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 8;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
Java
// Java implementation to count triplets
// in a sorted doubly linked list
// whose sum is equal to a given value 'x'
import java.util.*;
// Represents node of a doubly linked list
class Node
{
public int data;
public Node prev, next;
public Node(int val)
{
data = val;
prev = null;
next = null;
}
}
class GFG
{
// function to count triplets in
// a sorted doubly linked list
// whose sum is equal to a given value 'x'
static int countTriplets(Node head, int x)
{
Node ptr1, ptr2, ptr3;
int count = 0;
// generate all possible triplets
for (ptr1 = head; ptr1 != null; ptr1 = ptr1.next)
for (ptr2 = ptr1.next; ptr2 != null; ptr2 = ptr2.next)
for (ptr3 = ptr2.next; ptr3 != null; ptr3 = ptr3.next)
// if elements in the current triplet sum up to 'x'
if ((ptr1.data * ptr2.data * ptr3.data) == x)
// increment count
count++;
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
static Node insert(Node head, int val)
{
// allocate node
Node temp = new Node(val);
if (head == null)
head = temp;
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
// Driver code
public static void main(String []args)
{
// start with an empty doubly linked list
Node head = null;
// insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
System.out.println("count = " + countTriplets(head, x));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to count triplets
# in a sorted doubly linked list
# whose sum is equal to a given value 'x'
# Represents node of a doubly linked list
class Node:
data = None
prev = None
next_ = None
def __init__(self, val):
self.data = val
self.prev = None
self.next_ = None
# function to count triplets in
# a sorted doubly linked list
# whose sum is equal to a given value 'x'
def countTriplets(head, x):
ptr1, ptr2, ptr3 = Node(0), Node(0), Node(0)
count = 0
# generate all possible triplets
ptr1 = head
while ptr1 is not None:
ptr2 = ptr1.next_
while ptr2 is not None:
ptr3 = ptr2.next_
while ptr3 is not None:
# if elements in the current
# triplet sum up to 'x'
if ptr1.data * ptr2.data * ptr3.data == x:
# increment count
count += 1
ptr3 = ptr3.next_
ptr2 = ptr2.next_
ptr1 = ptr1.next_
# required count of triplets
return count
# A utility function to insert a new node at the
# beginning of doubly linked list
def insert(head, val):
# allocate node
temp = Node(val)
if head is None:
head = temp
else:
temp.next_ = head
head.prev = temp
head = temp
return head
# Driver Code
if __name__ == "__main__":
# start with an empty doubly linked list
head = Node(0)
# insert values in sorted order
head = insert(head, 9)
head = insert(head, 8)
head = insert(head, 6)
head = insert(head, 5)
head = insert(head, 4)
head = insert(head, 2)
head = insert(head, 1)
x = 8
print("count =", countTriplets(head, x))
# This code is contributed by
# sanjeev2552
C#
// C# implementation to count triplets
// in a sorted doubly linked list
// whose sum is equal to a given value 'x'
using System;
// Represents node of a doubly linked list
public class Node
{
public int data;
public Node prev, next;
public Node(int val)
{
data = val;
prev = null;
next = null;
}
}
class GFG
{
// function to count triplets in
// a sorted doubly linked list
// whose sum is equal to a given value 'x'
static int countTriplets(Node head, int x)
{
Node ptr1, ptr2, ptr3;
int count = 0;
// generate all possible triplets
for (ptr1 = head; ptr1 != null; ptr1 = ptr1.next)
for (ptr2 = ptr1.next; ptr2 != null; ptr2 = ptr2.next)
for (ptr3 = ptr2.next; ptr3 != null; ptr3 = ptr3.next)
// if elements in the current triplet sum up to 'x'
if ((ptr1.data * ptr2.data * ptr3.data) == x)
// increment count
count++;
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
static Node insert(Node head, int val)
{
// allocate node
Node temp = new Node(val);
if (head == null)
head = temp;
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
// Driver code
public static void Main(String []args)
{
// start with an empty doubly linked list
Node head = null;
// insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
Console.WriteLine("count = " + countTriplets(head, x));
}
}
// This code is contributed by Arnab Kundu
Javascript
C++
// C++ implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
#include
using namespace std;
// structure of node of doubly linked list
struct Node {
int data;
struct Node *next, *prev;
};
// function to count triplets in a sorted doubly linked list
// whose product is equal to a given value 'x'
int countTriplets(struct Node* head, int x)
{
struct Node *ptr, *ptr1, *ptr2;
int count = 0;
// unordered_map 'um' implemented as hash table
unordered_map um;
// insert the tuple in 'um'
for (ptr = head; ptr != NULL; ptr = ptr->next)
um[ptr->data] = ptr;
// generate all possible pairs
for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
for (ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next) {
// p_product = product of elements in the current pair
int p_product = (ptr1->data * ptr2->data);
// if 'x/p_product' is present in 'um' and
// either of the two nodes
// are not equal to the 'um[x/p_product]' node
if (um.find(x / p_product) != um.end() && um[x / p_product] != ptr1
&& um[x / p_product] != ptr2)
// increment count
count++;
}
// required count of triplets
// division by 3 as each triplet is counted 3 times
return (count / 3);
}
// A utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct Node** head, int data)
{
// allocate node
struct Node* temp = new Node();
// put in the data
temp->data = data;
temp->next = temp->prev = NULL;
if ((*head) == NULL)
(*head) = temp;
else {
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
// Driver program to test above functions
int main()
{
// start with an empty doubly linked list
struct Node* head = NULL;
// insert values in sorted order
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 8;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
Java
// Java implementation to count triplets
// in a sorted doubly linked list whose
// product is equal to a given value 'x'
import java.io.*;
import java.util.*;
// Structure of node of doubly linked list
class Node
{
int data;
Node next, prev;
}
class GFG{
// Function to count triplets in a sorted
// doubly linked list whose product is
// equal to a given value 'x'
static int countTriplets(Node head, int x)
{
Node ptr, ptr1, ptr2;
int count = 0;
// Unordered_map 'um' implemented
// as hash table
Map um = new HashMap();
// Insert the
// tuple in 'um'
for(ptr = head; ptr != null; ptr = ptr.next)
{
um.put(ptr.data, ptr);
}
// Generate all possible pairs
for(ptr1 = head;
ptr1 != null;
ptr1 = ptr1.next)
{
for(ptr2 = ptr1.next;
ptr2 != null;
ptr2 = ptr2.next)
{
// p_product = product of elements
// in the current pair
int p_product = (ptr1.data * ptr2.data);
// If 'x/p_product' is present in 'um' and
// either of the two nodes are not equal
// to the 'um[x/p_product]' node
if (um.containsKey(x / p_product) &&
um.get(x / p_product) != ptr1 &&
um.get(x / p_product) != ptr2)
{
// Increment count
count++;
}
}
}
// Required count of triplets
// division by 3 as each triplet
// is counted 3 times
return (count / 3);
}
// A utility function to insert a new
// node at the beginning of doubly linked list
static Node insert(Node head, int data)
{
// Allocate node
Node temp = new Node();
// Put in the data
temp.data = data;
temp.next = temp.prev = null;
if (head == null)
{
head = temp;
}
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
// Driver code
public static void main(String[] args)
{
// Start with an empty doubly linked list
Node head = null;
// Insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
System.out.println("Count = " +
countTriplets(head, x));
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 implementation to
# count triplets in a sorted
# doubly linked list whose
# product is equal to a given
# value 'x'
from math import ceil
# structure of node of doubly
# linked list
class Node:
def __init__(self, x):
self.data = x
self.next = None
self.prev = None
# function to count triplets
# in a sorted doubly linked
# list whose product is equal
# to a given value 'x'
def countTriplets(head, x):
ptr, ptr1, ptr2 = None, None, None
count = 0
# unordered_map 'um' implemented
# as hash table
um = {}
# insert the tuple in 'um'
ptr = head
while ptr != None:
um[ptr.data] = ptr
ptr = ptr.next
# generate all possible pairs
ptr1 = head
while ptr1 != None:
ptr2 = ptr1.next
while ptr2 != None:
# p_product = product of
# elements in the current
# pair
p_product = (ptr1.data *
ptr2.data)
# if 'x/p_product' is present
# in 'um' and either of the
# two nodes are not equal to
# the 'um[x/p_product]' node
if ((x / p_product) in um and
(x / p_product) != ptr1 and
um[x / p_product] != ptr2):
# increment count
count += 1
ptr2 = ptr2.next
ptr1 = ptr1.next
# required count of triplets
# division by 3 as each triplet
# is counted 3 times
return (count // 3)
# A utility function to insert a
# new node at the beginning of
# doubly linked list
def insert(head, data):
# allocate node
temp = Node(data)
# put in the data
temp.data = data
temp.next = temp.prev = None
if (head == None):
head = temp
else:
temp.next = head
head.prev = temp
head = temp
return head
# Driver code
if __name__ == '__main__':
# start with an empty
# doubly linked list
head = None
# insert values in sorted
# order
head = insert(head, 9)
head = insert(head, 8)
head = insert(head, 6)
head = insert(head, 5)
head = insert(head, 4)
head = insert(head, 2)
head = insert(head, 1)
x = 8
print("Count =",
countTriplets(head, x))
# This code is contributed by Mohit Kumar 29
C#
// C# implementation to count triplets
// in a sorted doubly linked list whose
// product is equal to a given value 'x'
using System;
using System.Collections.Generic;
// Structure of node of doubly linked list
class Node
{
public int data;
public Node next, prev;
}
class GFG
{
// Function to count triplets in a sorted
// doubly linked list whose product is
// equal to a given value 'x'
static int countTriplets(Node head, int x)
{
Node ptr, ptr1, ptr2;
int count = 0;
// Unordered_map 'um' implemented
// as hash table
Dictionary um = new Dictionary();
// Insert the
// tuple in 'um'
for(ptr = head; ptr != null; ptr = ptr.next)
{
um.Add(ptr.data, ptr);
}
// Generate all possible pairs
for(ptr1 = head;
ptr1 != null;
ptr1 = ptr1.next)
{
for(ptr2 = ptr1.next;
ptr2 != null;
ptr2 = ptr2.next)
{
// p_product = product of elements
// in the current pair
int p_product = (ptr1.data * ptr2.data);
// If 'x/p_product' is present in 'um' and
// either of the two nodes are not equal
// to the 'um[x/p_product]' node
if (um.ContainsKey(x / p_product) &&
um[x / p_product] != ptr1 &&
um[x / p_product] != ptr2)
{
// Increment count
count++;
}
}
}
// Required count of triplets
// division by 3 as each triplet
// is counted 3 times
return (count / 3);
}
// A utility function to insert a new
// node at the beginning of doubly linked list
static Node insert(Node head, int data)
{
// Allocate node
Node temp = new Node();
// Put in the data
temp.data = data;
temp.next = temp.prev = null;
if (head == null)
{
head = temp;
}
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
// Driver code
static public void Main ()
{
// Start with an empty doubly linked list
Node head = null;
// Insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
Console.WriteLine("Count = " + countTriplets(head, x));
}
}
// This code is contributed by rag2127
Javascript
C++
// C++ implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
#include
using namespace std;
// structure of node of doubly linked list
struct Node {
int data;
struct Node *next, *prev;
};
// function to count pairs whose product equal to given 'value'
int countPairs(struct Node* first, struct Node* second, int value)
{
int count = 0;
// The loop terminates when either of two pointers
// become NULL, or they cross each other (second->next
// == first), or they become same (first == second)
while (first != NULL && second != NULL && first != second
&& second->next != first) {
// pair found
if ((first->data * second->data) == value) {
// increment count
count++;
// move first in forward direction
first = first->next;
// move second in backward direction
second = second->prev;
}
// if product is greater than 'value'
// move second in backward direction
else if ((first->data * second->data) > value)
second = second->prev;
// else move first in forward direction
else
first = first->next;
}
// required count of pairs
return count;
}
// function to count triplets in a sorted doubly linked list
// whose product is equal to a given value 'x'
int countTriplets(struct Node* head, int x)
{
// if list is empty
if (head == NULL)
return 0;
struct Node *current, *first, *last;
int count = 0;
// get pointer to the last node of
// the doubly linked list
last = head;
while (last->next != NULL)
last = last->next;
// traversing the doubly linked list
for (current = head; current != NULL; current = current->next) {
// for each current node
first = current->next;
// count pairs with product(x / current->data) in the range
// first to last and add it to the 'count' of triplets
count += countPairs(first, last, x / current->data);
}
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct Node** head, int data)
{
// allocate node
struct Node* temp = new Node();
// put in the data
temp->data = data;
temp->next = temp->prev = NULL;
if ((*head) == NULL)
(*head) = temp;
else {
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
// Driver program to test above
int main()
{
// start with an empty doubly linked list
struct Node* head = NULL;
// insert values in sorted order
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 8;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
Java
// Java implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
import java.util.*;
class GFG
{
// structure of node of doubly linked list
static class Node
{
int data;
Node next, prev;
};
// function to count pairs whose product
// equal to given 'value'
static int countPairs(Node first, Node second,
int value)
{
int count = 0;
// The loop terminates when either of two pointers
// become null, or they cross each other (second.next
// == first), or they become same (first == second)
while (first != null && second != null &&
first != second && second.next != first)
{
// pair found
if ((first.data * second.data) == value)
{
// increment count
count++;
// move first in forward direction
first = first.next;
// move second in backward direction
second = second.prev;
}
// if product is greater than 'value'
// move second in backward direction
else if ((first.data * second.data) > value)
second = second.prev;
// else move first in forward direction
else
first = first.next;
}
// required count of pairs
return count;
}
// function to count triplets in
// a sorted doubly linked list
// whose product is equal to a given value 'x'
static int countTriplets(Node head, int x)
{
// if list is empty
if (head == null)
return 0;
Node current, first, last;
int count = 0;
// get pointer to the last node of
// the doubly linked list
last = head;
while (last.next != null)
last = last.next;
// traversing the doubly linked list
for (current = head; current != null;
current = current.next)
{
// for each current node
first = current.next;
// count pairs with product(x / current.data)
// in the range first to last and
// add it to the 'count' of triplets
count += countPairs(first, last, x / current.data);
}
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
static Node insert(Node head, int data)
{
// allocate node
Node temp = new Node();
// put in the data
temp.data = data;
temp.next = temp.prev = null;
if ((head) == null)
(head) = temp;
else
{
temp.next = head;
(head).prev = temp;
(head) = temp;
}
return head;
}
// Driver code
public static void main(String args[])
{
// start with an empty doubly linked list
Node head = null;
// insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
System.out.println( "Count = "+ countTriplets(head, x));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation to count triplets
# in a sorted doubly linked list whose
# product is equal to a given value 'x'
# Structure of node of doubly linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Function to count pairs whose product
# equal to given 'value'
def countPairs(first, second, value):
count = 0
# The loop terminates when either of two pointers
# become None, or they cross each other (second.next
# == first), or they become same (first == second)
while (first != None and second != None and
first != second and second.next != first):
# Pair found
if ((first.data * second.data) == value):
# Increment count
count += 1
# Move first in forward direction
first = first.next
# Move second in backward direction
second = second.prev
# If product is greater than 'value'
# move second in backward direction
elif ((first.data * second.data) > value):
second = second.prev
# Else move first in forward direction
else:
first = first.next
# Required count of pairs
return count
# Function to count triplets in a sorted
# doubly linked list whose product is
# equal to a given value 'x'
def countTriplets(head, x):
# If list is empty
if (head == None):
return 0
count = 0
# Get pointer to the last node of
# the doubly linked list
last = head
while (last.next != None):
last = last.next
current = head
# Traversing the doubly linked list
while current != None:
# For each current node
first = current.next
# Count pairs with product(x / current.data)
# in the range first to last and
# add it to the 'count' of triplets
count += countPairs(first, last,
x // current.data)
current = current.next
# Required count of triplets
return count
# A utility function to insert a new node
# at the beginning of doubly linked list
def insert(head, data):
# Allocate node
temp = Node(data)
# Put in the data
temp.data = data
temp.next = temp.prev = None
if ((head) == None):
(head) = temp
else:
temp.next = head
(head).prev = temp
(head) = temp
return head
# Driver code
if __name__=='__main__':
# Start with an empty doubly linked list
head = None
# Insert values in sorted order
head = insert(head, 9)
head = insert(head, 8)
head = insert(head, 6)
head = insert(head, 5)
head = insert(head, 4)
head = insert(head, 2)
head = insert(head, 1)
x = 8
print( "Count = " + str(countTriplets(head, x)))
# This code is contributed by rutvik_56
C#
// C# implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
using System;
class GFG
{
// structure of node of doubly linked list
class Node
{
public int data;
public Node next, prev;
};
// function to count pairs whose product
// equal to given 'value'
static int countPairs(Node first, Node second,
int value)
{
int count = 0;
// The loop terminates when either of two pointers
// become null, or they cross each other (second.next
// == first), or they become same (first == second)
while (first != null && second != null &&
first != second && second.next != first)
{
// pair found
if ((first.data * second.data) == value)
{
// increment count
count++;
// move first in forward direction
first = first.next;
// move second in backward direction
second = second.prev;
}
// if product is greater than 'value'
// move second in backward direction
else if ((first.data * second.data) > value)
second = second.prev;
// else move first in forward direction
else
first = first.next;
}
// required count of pairs
return count;
}
// function to count triplets in
// a sorted doubly linked list
// whose product is equal to a given value 'x'
static int countTriplets(Node head, int x)
{
// if list is empty
if (head == null)
return 0;
Node current, first, last;
int count = 0;
// get pointer to the last node of
// the doubly linked list
last = head;
while (last.next != null)
last = last.next;
// traversing the doubly linked list
for (current = head; current != null;
current = current.next)
{
// for each current node
first = current.next;
// count pairs with product(x / current.data)
// in the range first to last and
// add it to the 'count' of triplets
count += countPairs(first, last, x / current.data);
}
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
static Node insert(Node head, int data)
{
// allocate node
Node temp = new Node();
// put in the data
temp.data = data;
temp.next = temp.prev = null;
if ((head) == null)
(head) = temp;
else
{
temp.next = head;
(head).prev = temp;
(head) = temp;
}
return head;
}
// Driver code
public static void Main(String []args)
{
// start with an empty doubly linked list
Node head = null;
// insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
Console.WriteLine( "Count = "+ countTriplets(head, x));
}
}
// This code is contributed by Rajput-Ji
Javascript
Count = 1
时间复杂度: O(n^3)
辅助空间: O(1)
方法 2(哈希):创建一个哈希表,其中(键、值)元组表示为(节点数据、节点指针)元组。遍历双向链表并将每个节点的数据及其指针对(元组)存储在哈希表中。现在,生成每个可能的节点对。对于每对节点,计算p_product(两个节点中数据的乘积)并检查(x/p_product)是否存在于哈希表中。如果存在,则还要验证该对中的两个节点与哈希表中与 (x/p_product) 关联的节点不同,最后增加计数。返回 (count / 3) 因为每个三元组在上述过程中被计数 3 次。
下面是上述方法的实现:
C++
// C++ implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
#include
using namespace std;
// structure of node of doubly linked list
struct Node {
int data;
struct Node *next, *prev;
};
// function to count triplets in a sorted doubly linked list
// whose product is equal to a given value 'x'
int countTriplets(struct Node* head, int x)
{
struct Node *ptr, *ptr1, *ptr2;
int count = 0;
// unordered_map 'um' implemented as hash table
unordered_map um;
// insert the tuple in 'um'
for (ptr = head; ptr != NULL; ptr = ptr->next)
um[ptr->data] = ptr;
// generate all possible pairs
for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
for (ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next) {
// p_product = product of elements in the current pair
int p_product = (ptr1->data * ptr2->data);
// if 'x/p_product' is present in 'um' and
// either of the two nodes
// are not equal to the 'um[x/p_product]' node
if (um.find(x / p_product) != um.end() && um[x / p_product] != ptr1
&& um[x / p_product] != ptr2)
// increment count
count++;
}
// required count of triplets
// division by 3 as each triplet is counted 3 times
return (count / 3);
}
// A utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct Node** head, int data)
{
// allocate node
struct Node* temp = new Node();
// put in the data
temp->data = data;
temp->next = temp->prev = NULL;
if ((*head) == NULL)
(*head) = temp;
else {
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
// Driver program to test above functions
int main()
{
// start with an empty doubly linked list
struct Node* head = NULL;
// insert values in sorted order
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 8;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
Java
// Java implementation to count triplets
// in a sorted doubly linked list whose
// product is equal to a given value 'x'
import java.io.*;
import java.util.*;
// Structure of node of doubly linked list
class Node
{
int data;
Node next, prev;
}
class GFG{
// Function to count triplets in a sorted
// doubly linked list whose product is
// equal to a given value 'x'
static int countTriplets(Node head, int x)
{
Node ptr, ptr1, ptr2;
int count = 0;
// Unordered_map 'um' implemented
// as hash table
Map um = new HashMap();
// Insert the
// tuple in 'um'
for(ptr = head; ptr != null; ptr = ptr.next)
{
um.put(ptr.data, ptr);
}
// Generate all possible pairs
for(ptr1 = head;
ptr1 != null;
ptr1 = ptr1.next)
{
for(ptr2 = ptr1.next;
ptr2 != null;
ptr2 = ptr2.next)
{
// p_product = product of elements
// in the current pair
int p_product = (ptr1.data * ptr2.data);
// If 'x/p_product' is present in 'um' and
// either of the two nodes are not equal
// to the 'um[x/p_product]' node
if (um.containsKey(x / p_product) &&
um.get(x / p_product) != ptr1 &&
um.get(x / p_product) != ptr2)
{
// Increment count
count++;
}
}
}
// Required count of triplets
// division by 3 as each triplet
// is counted 3 times
return (count / 3);
}
// A utility function to insert a new
// node at the beginning of doubly linked list
static Node insert(Node head, int data)
{
// Allocate node
Node temp = new Node();
// Put in the data
temp.data = data;
temp.next = temp.prev = null;
if (head == null)
{
head = temp;
}
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
// Driver code
public static void main(String[] args)
{
// Start with an empty doubly linked list
Node head = null;
// Insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
System.out.println("Count = " +
countTriplets(head, x));
}
}
// This code is contributed by avanitrachhadiya2155
蟒蛇3
# Python3 implementation to
# count triplets in a sorted
# doubly linked list whose
# product is equal to a given
# value 'x'
from math import ceil
# structure of node of doubly
# linked list
class Node:
def __init__(self, x):
self.data = x
self.next = None
self.prev = None
# function to count triplets
# in a sorted doubly linked
# list whose product is equal
# to a given value 'x'
def countTriplets(head, x):
ptr, ptr1, ptr2 = None, None, None
count = 0
# unordered_map 'um' implemented
# as hash table
um = {}
# insert the tuple in 'um'
ptr = head
while ptr != None:
um[ptr.data] = ptr
ptr = ptr.next
# generate all possible pairs
ptr1 = head
while ptr1 != None:
ptr2 = ptr1.next
while ptr2 != None:
# p_product = product of
# elements in the current
# pair
p_product = (ptr1.data *
ptr2.data)
# if 'x/p_product' is present
# in 'um' and either of the
# two nodes are not equal to
# the 'um[x/p_product]' node
if ((x / p_product) in um and
(x / p_product) != ptr1 and
um[x / p_product] != ptr2):
# increment count
count += 1
ptr2 = ptr2.next
ptr1 = ptr1.next
# required count of triplets
# division by 3 as each triplet
# is counted 3 times
return (count // 3)
# A utility function to insert a
# new node at the beginning of
# doubly linked list
def insert(head, data):
# allocate node
temp = Node(data)
# put in the data
temp.data = data
temp.next = temp.prev = None
if (head == None):
head = temp
else:
temp.next = head
head.prev = temp
head = temp
return head
# Driver code
if __name__ == '__main__':
# start with an empty
# doubly linked list
head = None
# insert values in sorted
# order
head = insert(head, 9)
head = insert(head, 8)
head = insert(head, 6)
head = insert(head, 5)
head = insert(head, 4)
head = insert(head, 2)
head = insert(head, 1)
x = 8
print("Count =",
countTriplets(head, x))
# This code is contributed by Mohit Kumar 29
C#
// C# implementation to count triplets
// in a sorted doubly linked list whose
// product is equal to a given value 'x'
using System;
using System.Collections.Generic;
// Structure of node of doubly linked list
class Node
{
public int data;
public Node next, prev;
}
class GFG
{
// Function to count triplets in a sorted
// doubly linked list whose product is
// equal to a given value 'x'
static int countTriplets(Node head, int x)
{
Node ptr, ptr1, ptr2;
int count = 0;
// Unordered_map 'um' implemented
// as hash table
Dictionary um = new Dictionary();
// Insert the
// tuple in 'um'
for(ptr = head; ptr != null; ptr = ptr.next)
{
um.Add(ptr.data, ptr);
}
// Generate all possible pairs
for(ptr1 = head;
ptr1 != null;
ptr1 = ptr1.next)
{
for(ptr2 = ptr1.next;
ptr2 != null;
ptr2 = ptr2.next)
{
// p_product = product of elements
// in the current pair
int p_product = (ptr1.data * ptr2.data);
// If 'x/p_product' is present in 'um' and
// either of the two nodes are not equal
// to the 'um[x/p_product]' node
if (um.ContainsKey(x / p_product) &&
um[x / p_product] != ptr1 &&
um[x / p_product] != ptr2)
{
// Increment count
count++;
}
}
}
// Required count of triplets
// division by 3 as each triplet
// is counted 3 times
return (count / 3);
}
// A utility function to insert a new
// node at the beginning of doubly linked list
static Node insert(Node head, int data)
{
// Allocate node
Node temp = new Node();
// Put in the data
temp.data = data;
temp.next = temp.prev = null;
if (head == null)
{
head = temp;
}
else
{
temp.next = head;
head.prev = temp;
head = temp;
}
return head;
}
// Driver code
static public void Main ()
{
// Start with an empty doubly linked list
Node head = null;
// Insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
Console.WriteLine("Count = " + countTriplets(head, x));
}
}
// This code is contributed by rag2127
Javascript
Count = 1
时间复杂度: O(n^2)
辅助空间: O(n)
方法三(使用两个指针):从左到右遍历双向链表。对于遍历过程中的每个当前节点,初始化两个指针 first = 指向当前节点旁边节点的指针和 last = 指向列表最后一个节点的指针。现在,计算列表中从第一个到最后一个指针的对数,该指针乘积到值(x / 当前节点的数据)(本文中描述的算法)。将此计数添加到三元组的 total_count 中。指向最后一个节点的指针只能在开头找到一次。
下面是上述方法的实现:
C++
// C++ implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
#include
using namespace std;
// structure of node of doubly linked list
struct Node {
int data;
struct Node *next, *prev;
};
// function to count pairs whose product equal to given 'value'
int countPairs(struct Node* first, struct Node* second, int value)
{
int count = 0;
// The loop terminates when either of two pointers
// become NULL, or they cross each other (second->next
// == first), or they become same (first == second)
while (first != NULL && second != NULL && first != second
&& second->next != first) {
// pair found
if ((first->data * second->data) == value) {
// increment count
count++;
// move first in forward direction
first = first->next;
// move second in backward direction
second = second->prev;
}
// if product is greater than 'value'
// move second in backward direction
else if ((first->data * second->data) > value)
second = second->prev;
// else move first in forward direction
else
first = first->next;
}
// required count of pairs
return count;
}
// function to count triplets in a sorted doubly linked list
// whose product is equal to a given value 'x'
int countTriplets(struct Node* head, int x)
{
// if list is empty
if (head == NULL)
return 0;
struct Node *current, *first, *last;
int count = 0;
// get pointer to the last node of
// the doubly linked list
last = head;
while (last->next != NULL)
last = last->next;
// traversing the doubly linked list
for (current = head; current != NULL; current = current->next) {
// for each current node
first = current->next;
// count pairs with product(x / current->data) in the range
// first to last and add it to the 'count' of triplets
count += countPairs(first, last, x / current->data);
}
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct Node** head, int data)
{
// allocate node
struct Node* temp = new Node();
// put in the data
temp->data = data;
temp->next = temp->prev = NULL;
if ((*head) == NULL)
(*head) = temp;
else {
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
// Driver program to test above
int main()
{
// start with an empty doubly linked list
struct Node* head = NULL;
// insert values in sorted order
insert(&head, 9);
insert(&head, 8);
insert(&head, 6);
insert(&head, 5);
insert(&head, 4);
insert(&head, 2);
insert(&head, 1);
int x = 8;
cout << "Count = "
<< countTriplets(head, x);
return 0;
}
Java
// Java implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
import java.util.*;
class GFG
{
// structure of node of doubly linked list
static class Node
{
int data;
Node next, prev;
};
// function to count pairs whose product
// equal to given 'value'
static int countPairs(Node first, Node second,
int value)
{
int count = 0;
// The loop terminates when either of two pointers
// become null, or they cross each other (second.next
// == first), or they become same (first == second)
while (first != null && second != null &&
first != second && second.next != first)
{
// pair found
if ((first.data * second.data) == value)
{
// increment count
count++;
// move first in forward direction
first = first.next;
// move second in backward direction
second = second.prev;
}
// if product is greater than 'value'
// move second in backward direction
else if ((first.data * second.data) > value)
second = second.prev;
// else move first in forward direction
else
first = first.next;
}
// required count of pairs
return count;
}
// function to count triplets in
// a sorted doubly linked list
// whose product is equal to a given value 'x'
static int countTriplets(Node head, int x)
{
// if list is empty
if (head == null)
return 0;
Node current, first, last;
int count = 0;
// get pointer to the last node of
// the doubly linked list
last = head;
while (last.next != null)
last = last.next;
// traversing the doubly linked list
for (current = head; current != null;
current = current.next)
{
// for each current node
first = current.next;
// count pairs with product(x / current.data)
// in the range first to last and
// add it to the 'count' of triplets
count += countPairs(first, last, x / current.data);
}
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
static Node insert(Node head, int data)
{
// allocate node
Node temp = new Node();
// put in the data
temp.data = data;
temp.next = temp.prev = null;
if ((head) == null)
(head) = temp;
else
{
temp.next = head;
(head).prev = temp;
(head) = temp;
}
return head;
}
// Driver code
public static void main(String args[])
{
// start with an empty doubly linked list
Node head = null;
// insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
System.out.println( "Count = "+ countTriplets(head, x));
}
}
// This code is contributed by Arnab Kundu
蟒蛇3
# Python3 implementation to count triplets
# in a sorted doubly linked list whose
# product is equal to a given value 'x'
# Structure of node of doubly linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Function to count pairs whose product
# equal to given 'value'
def countPairs(first, second, value):
count = 0
# The loop terminates when either of two pointers
# become None, or they cross each other (second.next
# == first), or they become same (first == second)
while (first != None and second != None and
first != second and second.next != first):
# Pair found
if ((first.data * second.data) == value):
# Increment count
count += 1
# Move first in forward direction
first = first.next
# Move second in backward direction
second = second.prev
# If product is greater than 'value'
# move second in backward direction
elif ((first.data * second.data) > value):
second = second.prev
# Else move first in forward direction
else:
first = first.next
# Required count of pairs
return count
# Function to count triplets in a sorted
# doubly linked list whose product is
# equal to a given value 'x'
def countTriplets(head, x):
# If list is empty
if (head == None):
return 0
count = 0
# Get pointer to the last node of
# the doubly linked list
last = head
while (last.next != None):
last = last.next
current = head
# Traversing the doubly linked list
while current != None:
# For each current node
first = current.next
# Count pairs with product(x / current.data)
# in the range first to last and
# add it to the 'count' of triplets
count += countPairs(first, last,
x // current.data)
current = current.next
# Required count of triplets
return count
# A utility function to insert a new node
# at the beginning of doubly linked list
def insert(head, data):
# Allocate node
temp = Node(data)
# Put in the data
temp.data = data
temp.next = temp.prev = None
if ((head) == None):
(head) = temp
else:
temp.next = head
(head).prev = temp
(head) = temp
return head
# Driver code
if __name__=='__main__':
# Start with an empty doubly linked list
head = None
# Insert values in sorted order
head = insert(head, 9)
head = insert(head, 8)
head = insert(head, 6)
head = insert(head, 5)
head = insert(head, 4)
head = insert(head, 2)
head = insert(head, 1)
x = 8
print( "Count = " + str(countTriplets(head, x)))
# This code is contributed by rutvik_56
C#
// C# implementation to count triplets
// in a sorted doubly linked list
// whose product is equal to a given value 'x'
using System;
class GFG
{
// structure of node of doubly linked list
class Node
{
public int data;
public Node next, prev;
};
// function to count pairs whose product
// equal to given 'value'
static int countPairs(Node first, Node second,
int value)
{
int count = 0;
// The loop terminates when either of two pointers
// become null, or they cross each other (second.next
// == first), or they become same (first == second)
while (first != null && second != null &&
first != second && second.next != first)
{
// pair found
if ((first.data * second.data) == value)
{
// increment count
count++;
// move first in forward direction
first = first.next;
// move second in backward direction
second = second.prev;
}
// if product is greater than 'value'
// move second in backward direction
else if ((first.data * second.data) > value)
second = second.prev;
// else move first in forward direction
else
first = first.next;
}
// required count of pairs
return count;
}
// function to count triplets in
// a sorted doubly linked list
// whose product is equal to a given value 'x'
static int countTriplets(Node head, int x)
{
// if list is empty
if (head == null)
return 0;
Node current, first, last;
int count = 0;
// get pointer to the last node of
// the doubly linked list
last = head;
while (last.next != null)
last = last.next;
// traversing the doubly linked list
for (current = head; current != null;
current = current.next)
{
// for each current node
first = current.next;
// count pairs with product(x / current.data)
// in the range first to last and
// add it to the 'count' of triplets
count += countPairs(first, last, x / current.data);
}
// required count of triplets
return count;
}
// A utility function to insert a new node at the
// beginning of doubly linked list
static Node insert(Node head, int data)
{
// allocate node
Node temp = new Node();
// put in the data
temp.data = data;
temp.next = temp.prev = null;
if ((head) == null)
(head) = temp;
else
{
temp.next = head;
(head).prev = temp;
(head) = temp;
}
return head;
}
// Driver code
public static void Main(String []args)
{
// start with an empty doubly linked list
Node head = null;
// insert values in sorted order
head = insert(head, 9);
head = insert(head, 8);
head = insert(head, 6);
head = insert(head, 5);
head = insert(head, 4);
head = insert(head, 2);
head = insert(head, 1);
int x = 8;
Console.WriteLine( "Count = "+ countTriplets(head, x));
}
}
// This code is contributed by Rajput-Ji
Javascript
Count = 1
时间复杂度: O(n^2)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。