【发布时间】:2021-04-25 21:22:00
【问题描述】:
我想在队列中找到不同的数字,但除了数组等队列之外我不能使用任何东西。
这是我的代码:
Queue distinct = new Queue(10);
Queue distincttemp1 = new Queue(10);
int count=0;
Random rnd = new Random();
boolean flag=true;
for (int i = 0; i < 10; i++) {
int x = rnd.nextInt(9);
System.out.print(x + " ");
distinct.enqueue(x);
}
System.out.println("");
除了将每个数字添加到队列中之外,我找不到找到重复多少数字的解决方案。它应该很简单,只需几个循环。你能帮我找到一个算法吗?
输入输出应该是这样的:
队列:4 8 5 8 4 3 2 8
输出:5
【问题讨论】: