给定一个整数堆栈S和一个整数数组arr[] ,任务是检查堆栈中是否存在所有数组元素。
例子:
Input: S = {10, 20, 30, 40, 50}, arr[] = {20, 30}
Output: Yes
Explanation:
Elements 20 and 30 are present in the stack.
Input: S = {50, 60}, arr[] = {60, 50}
Output: Yes
Explanation:
Elements 50 and 60 are present in the stack.
做法:思路是在Hashmap中保持数组元素的出现频率。现在,当堆栈不为空时,继续从堆栈中弹出元素并减少Hashmap中元素的频率。最后,当堆栈为空时,检查哈希映射中每个元素的频率是否为零。如果发现为真,则打印 Yes。否则,打印编号。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to check if all array
// elements is present in the stack
bool checkArrInStack(stacks, int arr[],
int n)
{
mapfreq;
// Store the frequency
// of array elements
for(int i = 0; i < n; i++)
freq[arr[i]]++;
// Loop while the elements in the
// stack is not empty
while (!s.empty())
{
int poppedEle = s.top();
s.pop();
// Condition to check if the
// element is present in the stack
if (freq[poppedEle])
freq[poppedEle] -= 1;
}
if (freq.size() == 0)
return 0;
return 1;
}
// Driver code
int main()
{
stacks;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
int arr[] = {20, 30};
int n = sizeof arr / sizeof arr[0];
if (checkArrInStack(s, arr, n))
cout << "YES\n";
else
cout << "NO\n";
}
// This code is contributed by Stream_Cipher
Java
// Java program of
// the above approach
import java.util.*;
class GFG{
// Function to check if all array
// elements is present in the stack
static boolean checkArrInStack(Stacks,
int arr[], int n)
{
HashMapfreq = new HashMap();
// Store the frequency
// of array elements
for(int i = 0; i < n; i++)
if(freq.containsKey(arr[i]))
freq.put(arr[i], freq.get(arr[i]) + 1);
else
freq.put(arr[i], 1);
// Loop while the elements in the
// stack is not empty
while (!s.isEmpty())
{
int poppedEle = s.peek();
s.pop();
// Condition to check if the
// element is present in the stack
if (freq.containsKey(poppedEle))
freq.put(poppedEle, freq.get(poppedEle) - 1);
}
if (freq.size() == 0)
return false;
return true;
}
// Driver code
public static void main(String[] args)
{
Stack s = new Stack();
s.add(10);
s.add(20);
s.add(30);
s.add(40);
s.add(50);
int arr[] = {20, 30};
int n = arr.length;
if (checkArrInStack(s, arr, n))
System.out.print("YES\n");
else
System.out.print("NO\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program of
# the above approach
# Function to check if all array
# elements is present in the stack
def checkArrInStack(s, arr):
freq = {}
# Store the frequency
# of array elements
for ele in arr:
freq[ele] = freq.get(ele, 0) + 1
# Loop while the elements in the
# stack is not empty
while s:
poppedEle = s.pop()
# Condition to check if the
# element is present in the stack
if poppedEle in freq:
freq[poppedEle] -= 1
if not freq[poppedEle]:
del freq[poppedEle]
if not freq:
return True
return False
# Driver Code
if __name__ == "__main__":
s = [10, 20, 30, 40, 50]
arr = [20, 30]
if checkArrInStack(s, arr):
print("YES")
else:
print("NO")
C#
// C# program of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if all array
// elements is present in the stack
static bool checkArrInStack(Stacks,
int []arr, int n)
{
Dictionaryfreq = new Dictionary();
// Store the frequency
// of array elements
for(int i = 0; i < n; i++)
if(freq.ContainsKey(arr[i]))
freq[arr[i]] = freq[arr[i]] + 1;
else
freq.Add(arr[i], 1);
// Loop while the elements in the
// stack is not empty
while (s.Count != 0)
{
int poppedEle = s.Peek();
s.Pop();
// Condition to check if the
// element is present in the stack
if (freq.ContainsKey(poppedEle))
freq[poppedEle] = freq[poppedEle] - 1;
}
if (freq.Count == 0)
return false;
return true;
}
// Driver code
public static void Main(String[] args)
{
Stack s = new Stack();
s.Push(10);
s.Push(20);
s.Push(30);
s.Push(40);
s.Push(50);
int []arr = {20, 30};
int n = arr.Length;
if (checkArrInStack(s, arr, n))
Console.Write("YES\n");
else
Console.Write("NO\n");
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
YES
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。