给定一个包含N 个正整数的数组arr ,任务是检查给定的数组 arr 是否表示一个排列。
A sequence of N integers is called a permutation if it contains all integers from 1 to N exactly once.
例子:
Input: arr[] = {1, 2, 5, 3, 2}
Output: No
Explanation:
The given array contains 2 twice, and 4 is missing for the array to represent a permutation of length 5.
Input: arr[] = {1, 2, 5, 3, 4}
Output: Yes
Explanation:
The given array contains all integers from 1 to 5 exactly once. Hence, it represents a permutation of length 5.
天真的方法:在 O(N 2 ) 时间内
这里提到了这种方法
另一种方法:在 O(N) 时间和 O(N) 空间中
这里提到了这种方法。
高效方法:使用HashTable
- 创建一个 N 大小的 HashTable 来存储从 1 到 N 的每个数字的频率计数
- 遍历给定的数组并将每个数字的频率存储在 HashTable 中。
- 然后遍历 HashTable 并检查从 1 到 N 的所有数字的频率是否为 1。
- 如果上述条件为真,则打印“是”,否则打印“否”。
下面是上述方法的实现:
CPP
// C++ program to decide if an array
// represents a permutation or not
#include
using namespace std;
// Function to check if an
// array represents a permutation or not
string permutation(int arr[], int N)
{
int hash[N + 1] = { 0 };
// Counting the frequency
for (int i = 0; i < N; i++) {
hash[arr[i]]++;
}
// Check if each frequency is 1 only
for (int i = 1; i <= N; i++) {
if (hash[i] != 1)
return "No";
}
return "Yes";
}
// Driver code
int main()
{
int arr[] = { 1, 1, 5, 5, 3 };
int n = sizeof(arr) / sizeof(int);
cout << permutation(arr, n) << endl;
return 0;
}
Java
// Java program to decide if an array
// represents a permutation or not
class GFG{
// Function to check if an
// array represents a permutation or not
static String permutation(int arr[], int N)
{
int []hash = new int[N + 1];
// Counting the frequency
for (int i = 0; i < N; i++) {
hash[arr[i]]++;
}
// Check if each frequency is 1 only
for (int i = 1; i <= N; i++) {
if (hash[i] != 1)
return "No";
}
return "Yes";
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 1, 5, 5, 3 };
int n = arr.length;
System.out.print(permutation(arr, n) +"\n");
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to decide if an array
# represents a permutation or not
# Function to check if an
# array represents a permutation or not
def permutation(arr, N) :
hash = [0]*(N + 1);
# Counting the frequency
for i in range(N) :
hash[arr[i]] += 1;
# Check if each frequency is 1 only
for i in range(1, N + 1) :
if (hash[i] != 1) :
return "No";
return "Yes";
# Driver code
if __name__ == "__main__" :
arr = [ 1, 1, 5, 5, 3 ];
n = len(arr);
print(permutation(arr, n));
# This code is contributed by Yash_R
C#
// C# program to decide if an array
// represents a permutation or not
using System;
class GFG{
// Function to check if an
// array represents a permutation or not
static string permutation(int []arr, int N)
{
int []hash = new int[N + 1];
// Counting the frequency
for (int i = 0; i < N; i++) {
hash[arr[i]]++;
}
// Check if each frequency is 1 only
for (int i = 1; i <= N; i++) {
if (hash[i] != 1)
return "No";
}
return "Yes";
}
// Driver code
public static void Main(string[] args)
{
int []arr = { 1, 1, 5, 5, 3 };
int n = arr.Length;
Console.Write(permutation(arr, n) +"\n");
}
}
// This code is contributed by Yash_R
Javascript
No
时间复杂度: O(N)
辅助空间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。