Set 中所有成对连续元素的绝对差
给定一组包含N个元素的整数。任务是打印集合中所有成对连续元素的绝对差。使用迭代器访问一组大小为 N 的成对连续对。
例子:
Input: s = {8, 5, 4, 3, 15, 20}
Output: 1 1 3 7 5
Explanation:
The set is : 3 4 5 8 15 20
The difference between 4 and 3 is 1
The difference between 5 and 4 is 1
The difference between 8 and 5 is 3
The difference between 15 and 8 is 7
The difference between 20 and 15 is 5
Input: s = {5, 10, 15, 20}
Output: 5 5 5
Explanation:
The set is : 5 10 15 20
The difference between 10 and 5 is 5
The difference between 15 and 10 is 5
The difference between 20 and 15 is 5
数组中所有成对连续元素的绝对差一文介绍了查找数组中所有成对连续元素的绝对差的方法。
方法:这个问题可以用两个指针算法来解决。我们将使用迭代器作为两个指针来迭代集合并检查给定的条件。按照以下步骤来了解上述问题的解决方法:
- 声明两个迭代器itr1 和 itr2 ,它们都指向集合的开始元素。
- 在循环开始时递增itr2即itr2++ 。
- 减去 itr1 和 itr2 指向的值,即*itr2 – *itr1 。
- 在循环结束时增加 itr1,这意味着*itr1++。
- 如果 itr2 到达集合的末尾,则中断循环并退出。
在 C++ 中,集合元素在存储到内存之前进行排序并删除重复项。因此,在下面的 C++ 程序中,成对连续元素之间的差异是在已排序集上计算的,如上述示例中所述。
下面是上述方法的C++程序实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to calculate the
// difference of consecutive pairwise
// elements in a set
void display_difference(set s)
{
// Declaring the set iterator
set::iterator itr;
// Printing difference between
// consecutive elements in a set
set::iterator itr1 = s.begin();
set::iterator itr2 = s.begin();
while (1) {
itr2++;
if (itr2 == s.end())
break;
cout << (*itr2 - *itr1) << " ";
itr1++;
}
}
// Driver code
int main()
{
// Declaring the set
set s{ 8, 5, 4, 3, 15, 20 };
// Invoking the display_difference()
// function
display_difference(s);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;
// Function to calculate the
// difference of consecutive pairwise
// elements in a set
class GFG {
static void display_difference(HashSet S) {
// Printing difference between
// consecutive elements in a set
TreeSet s = new TreeSet(S);
int itr1 = 0;
int itr2 = 0;
while (true) {
itr2 += 1;
;
if (itr2 >= s.size()) {
break;
}
List temp = new ArrayList();
temp.addAll(s);
System.out.print((temp.get(itr2) - temp.get(itr1)) + " ");
itr1 += 1;
}
}
// Driver code
public static void main(String args[])
{
// Declaring the set
HashSet s = new HashSet();
s.add(8);
s.add(5);
s.add(4);
s.add(3);
s.add(15);
s.add(20);
// Invoking the display_difference()
// function
display_difference(s);
}
}
// This code is contributed by gfgking
Python3
# Python 3 program to implement
# the above approach
# Function to calculate the
# difference of consecutive pairwise
# elements in a set
def display_difference(s):
# Printing difference between
# consecutive elements in a set
itr1 = 0
itr2 = 0
while (1):
itr2 += 1
if (itr2 >= len(s)):
break
print((list(s)[itr2] - list(s)[itr1]), end=" ")
itr1 += 1
# Driver code
if __name__ == "__main__":
# Declaring the set
s = set([8, 5, 4, 3, 15, 20])
# Invoking the display_difference()
# function
display_difference(s)
# This code is contributed by ukasp.
C#
// C# program to implement
// the above approach
// Function to calculate the
// difference of consecutive pairwise
// elements in a set
using System;
using System.Collections.Generic;
public class GFG
{
static void display_difference(HashSet S)
{
// Printing difference between
// consecutive elements in a set
SortedSet s = new SortedSet(S);
int itr1 = 0;
int itr2 = 0;
while (true) {
itr2 += 1;
;
if (itr2 >= s.Count) {
break;
}
List temp = new List();
temp.AddRange(s);
Console.Write((temp[itr2] - temp[itr1]) + " ");
itr1 += 1;
}
}
// Driver code
public static void Main(String []args)
{
// Declaring the set
HashSet s = new HashSet();
s.Add(8);
s.Add(5);
s.Add(4);
s.Add(3);
s.Add(15);
s.Add(20);
// Invoking the display_difference()
// function
display_difference(s);
}
}
// This code is contributed by Rajput-Ji.
Javascript
输出:
1 1 3 7 5
时间复杂度: O(n)
辅助空间: O(n)