java - 所以我对以下方法有疑问,不知道为什么它不适用于 9 个案例中的 3 个?
问题描述
编写一个名为 isConsecutive 的方法,该方法接受整数的 PriorityQueue 作为参数,如果队列包含从队列前面开始的连续整数序列,则返回 true。连续整数是一个接一个的整数,如 5、6、7、8、9 等,因此如果队列存储 [7、8、9、10、11],您的方法应该返回 true。(如果传递了一个空队列,也返回 true。)如果您的方法在计算过程中修改了队列的状态,它应该在返回之前恢复队列。您可以使用一个堆栈或队列作为辅助存储。
public boolean isConsecutive(PriorityQueue<Integer> q){
if(q.isEmpty()){return true;}
boolean checker = true;
Object[] other = q.toArray();
for(int i = 0; i < other.length-1; i++){
if (checker == false) return checker;
checker = (int) other[i] < (int) other[i+1];
}
return checker;
}
解决方案
推荐阅读
- r - 如何从生存数据中绘制风险函数?
- swift - 使用 AVSpeechSynthesizer 进行中文拼音
- python-3.x - 试图进行二等分搜索,但
- angular - 启用 AoT 时,ActivatedRoute.snapshot 中的 Angular 路由“数据”为空
- angular - 'ISortPriority 类型的参数
[]' 不可分配给类型参数 - java - 使用元素 id 的 Thymeleaf 参考
- c# - MVVM 在保持 View 和 View Model 解耦的同时打开窗口
- javascript - 如何更改选定的反应选择输入颜色?
- algorithm - 如何在由数字数组组成的数组中“打开括号”?
- vba - 在 Outlook 对象模型中哪里可以找到 mailitem 对象的属性值“电子邮件帐户”?