如何确定 Stack<int> 中是否存在重复项实例? [英] How to determine if there are duplicates in a Stack<int> instance?
问题描述
我需要检查堆栈中是否有重复项(仅在基本方法 pop、push、top、isempty 中使用)如果堆栈没有重复,则返回 true,如果堆栈为空,则返回 true如果堆栈有重复,则返回 false
public static bool CheckStack(Stack s, int x){堆栈s1 = new Stack();布尔标志 = 假;如果 (s.IsEmpty())返回假;while (!s.IsEmpty() && ! 标志){int temp = s.Pop();s1.Push(温度);如果(温度== x)标志 = 真;}s = s1;返回标志;}public static bool SpecialStack(Stack s){int temp = s.Pop();while(!s.IsEmpty() && !CheckStack(s,temp)){温度 = s.Pop();}如果 (s.IsEmpty())返回真;返回假;}public static void Main(){堆栈st3 = new Stack();st3.Push(7);st3.Push(2);st3.Push(1);st3.Push(8);st3.Push(7);Console.WriteLine(SpecialStack(st3));}
如何在不使用其他函数的情况下做到这一点,并且解决方案需要使用基本方法而不使用内置函数的 c# 的最短方法
问题 #1:堆栈被清除
关于代码本身,乍一看我们需要:
bool CheckStack (ref Stack s, int x)
要恢复堆栈,否则需要重新考虑设计,因为方法结束时原始为空,返回后也是如此.
问题 2:堆栈被还原
在浅拷贝中也通过 pop 和 push 还原原始堆栈.
问题 3:特殊堆栈
我对这种方法持怀疑态度,除非目标是一次清空堆栈中的一项,并在每次迭代时调用 CheckStack,直到没有更多项为止.
简单的 Linq 解决方案
我们可以使用 Linq Distinct 并比较每个 <代码>计数代码>:
Console.WriteLine(st3.Distinct().Count() != st3.Count);
因此,如果计数匹配,则没有重复,否则会有一些.
通用扩展方法改进
静态公共类 StackHelper{static public bool HasDuplicates(这个 Stack 栈){返回 stack.Distinct().Count() != stack.Count;}}Console.WriteLine(st3.HasDuplicates());//真的
除按要求弹出、推送、包含和计数外,不使用任何其他方法的解决方案
static public bool HasDuplicates(这个栈栈){//检查重复项bool nodup = true;var temp = new Stack();while ( stack.Count != 0 ){var item = stack.Pop();if ( nodup && temp.Contains(item) ) nodup = false;temp.Push(item);}//恢复原栈while ( temp.Count != 0 ){stack.Push(temp.Pop());}返回 !nodup;}
注意:T
可以替换为int
以去除通用性,方法可以根据需要声明,扩展与否,静态或实例...>
i need to check if there is duplicates in stack (use only in the basic method pop,push,top,isempty) and return true if the stack have not duplicate or return true if the stack is empty and return false if the stack have duplicates
public static bool CheckStack(Stack<int> s, int x)
{
Stack<int> s1 = new Stack<int>();
bool flag = false;
if (s.IsEmpty())
return false;
while (!s.IsEmpty() && ! flag)
{
int temp = s.Pop();
s1.Push(temp);
if (temp == x)
flag = true;
}
s = s1;
return flag;
}
public static bool SpecialStack(Stack<int> s)
{
int temp = s.Pop();
while(!s.IsEmpty() && !CheckStack(s,temp))
{
temp = s.Pop();
}
if (s.IsEmpty())
return true;
return false;
}
public static void Main()
{
Stack<int> st3 = new Stack<int>();
st3.Push(7);
st3.Push(2);
st3.Push(1);
st3.Push(8);
st3.Push(7);
Console.WriteLine(SpecialStack(st3));
}
how can i do that without use another function and the shortest way the solution needs to be with basic method without use build in function of c#
Problem #1: Stack is cleared
Regarding the code itself, at first glance we need :
bool CheckStack (ref Stack<int> s, int x)
To restore the stack, otherwise rethink the design is needed because the original is empty at the end of the method, and so after the return.
Passing Objects By Reference or Value in C#
Passing Reference Types by Reference (C# Programming Guide)
Problem #2: Stack is reverted
Also by pop and push the original stack is reverted in the shallow copy.
Problem #3: SpecialStack
I have doubts about this method unless the goal is to empty the stack one item at a time and call CheckStack on each iteration until there is no more items.
Simple Linq solution
We can use Linq Distinct and compare each Count
:
Console.WriteLine(st3.Distinct().Count() != st3.Count);
Thus if the counts match there is no duplicates, else there are some.
Generic extension method improvement
static public class StackHelper
{
static public bool HasDuplicates<T>(this Stack<T> stack)
{
return stack.Distinct().Count() != stack.Count;
}
}
Console.WriteLine(st3.HasDuplicates()); // True
Solution without using any other method than Pop, Push, Contains and Count as requested
static public bool HasDuplicates<T>(this Stack<T> stack)
{
// Check for duplicates
bool nodup = true;
var temp = new Stack<T>();
while ( stack.Count != 0 )
{
var item = stack.Pop();
if ( nodup && temp.Contains(item) ) nodup = false;
temp.Push(item);
}
// Restore original stack
while ( temp.Count != 0 )
{
stack.Push(temp.Pop());
}
return !nodup;
}
Note: T
can be replaced by int
to remove the genericity and the method can be declared as needed, extension or not, static or instance...
这篇关于如何确定 Stack<int> 中是否存在重复项实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!