使用Javascript查找数组是否包含5个连续数字
我有一个包含数字的排序数组。我希望能够检查这个数组(或类似的数组)是否包含5个连续的数字 注意:数组可能包含重复的和两位数的数字 我正在尝试,但失败了使用Javascript查找数组是否包含5个连续数字,javascript,arrays,Javascript,Arrays,我有一个包含数字的排序数组。我希望能够检查这个数组(或类似的数组)是否包含5个连续的数字 注意:数组可能包含重复的和两位数的数字 我正在尝试,但失败了 var array = [1,3,5,7,7,8,9,10,11] var current = null; var cnt = 0; for (var i = 0; i < array.length; i++) { if (array[i] != current) { if (cnt > 4) {
var array = [1,3,5,7,7,8,9,10,11]
var current = null;
var cnt = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] != current) {
if (cnt > 4) {
return true;
}
current = array[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 4) {
return true;
}
}
var数组=[1,3,5,7,7,8,9,10,11]
无功电流=零;
var-cnt=0;
对于(var i=0;i4){
返回true;
}
电流=阵列[i];
cnt=1;
}否则{
cnt++;
}
}
如果(cnt>4){
返回true;
}
}
cnt
在达到两个7时只会增加一次
将递增行置于truthy条件下,将重置行置于else语句中
//放入函数中进行测试。
函数foo(){
变量数组=[1,3,5,7,7,8,9,10,11]
无功电流=零;
var-cnt=0;
对于(var i=0;i4){
返回true;
}
电流=阵列[i];
cnt++;
}否则{
cnt=1;
}
}
如果(cnt>4){
返回true;
}否则{
返回false;
}
};
//调用函数。
警报(foo())代码>一种迭代、直接的方法是:
var should_be_true = [1,3,5,7,7,8,9,10,11];
var should_be_false = [1,3,5,7,9,11,13,15,17];
var testArray = function(array) {
var conseq = 1;
for (var idx = 1; idx < array.length ; idx++) {
if (array[idx] == array[idx-1] + 1)
conseq++;
else
conseq = 1;
if (conseq == 5)
return true;
}
return false;
}
console.log(testArray(should_be_true)); //true
console.log(testArray(should_be_false)); //false
函数fiveStraight(){
var数组=[1,3,5,7,7,8,9,10,12];
var-prev=数组[0];
var numberOfStraight=1;
对于(变量i=1;i
.功能性方法是
function fiveInARow(array) {
// compare if one element is greater than or equal to the previous one
function compare(elt, i, arr) { return !i || elt >= arr[i-1]; });
// check if at a given position, every one of the last five comparisons is true
function check (_, i, greaters) {
return i >= 4 && greaters.slice(i-4, i) . every(Boolean);
}
return array . map(compare) . some(check);
}
这里的逻辑是首先使用map
创建一个布尔数组,显示每个元素是否大于或等于前一个元素。这将生成一个数组,如[true,true,true,false,true]
some
部分询问,对于任何元素,该元素和前面四个元素中的每一个都是真的吗?如果是,则返回true
递归解
递归解决方案可能更容易阅读
function fiveInARow(array) {
return function _five(array, prev, n) {
if (n >= 5) return true;
if (!array.length) return false;
var next = array.shift();
return _five(array, next, next === prev ? n : next >= prev ? n+1 : 0);
}(array, -999999, 5);
}
以下是我发现的最直接的方法。降序值和升序值都是连续的(如果不是这样的话,请使用Math.abs()
call)
使用此代码,您可以找到给定数字的最大连续数,或者通常可以找到最大连续数
var findMaxConsecutiveOnes = function (arr, number) {
//check for boundries
if(!number || !arr.length) return;
// maximum number of consectuives
let max = 0;
// count homy many consecutives before it ends (why it's 1 ? because a lonely number is still counted)
let counter = 1;
// you can ignore the next 2 variable if you want to use for loop instead of while
let length = arr.length;
let i = 1; // counting from index 1 because we are checking against index-1
while (i < length) {
if (arr[i] == arr[i - 1]) {
// boom, we have a consecutive, count it now
counter++;
} else {
// rest to 1
counter = 1;
}
// always update the max variable to the new max value
max = Math.max(counter, max);
//for the sake of iteration
i++;
}
return max== number;
};
console.log(findMaxConsecutiveOnes([5, 5, 5, 1, 1, 1, 1, 1]));
var findMaxConcertiveONES=函数(arr,编号){
//检查边界
如果(!number | |!arr.length)返回;
//最大连续人数
设max=0;
//在结束之前数一数homy的许多连续数字(为什么是1?因为一个孤独的数字仍然被计算)
设计数器=1;
//如果要使用for循环而不是while,可以忽略下一个2变量
let length=arr.length;
设i=1;//从索引1开始计数,因为我们正在检查索引1
while(i
表示您可以给出示例输入和预期答案。您尝试过什么?我的方法大致如下:遍历数组,存储当前值,如果下一个值与当前值相同,则增加一个计数器。如果它们不同,请将计数器设置回0。查找本文中的内容:这是在函数中定义的吗?不确定return true
将转到何处,或者您将如何看到结果。另外,cnt
只会增加一次,当它通过“连续”点击两个7
sBy时,你是什么意思?在您的样本数据中,无论这些数字是否“连续”,它们只是单调递增的。重复项是在一行的五个索引中计数,还是为了计数而跳过?在进行连续顺序检查之前,在每个索引前面检查5+是否更有效?我不这么认为。最坏情况=遍历整个阵列。最好的情况是条纹落在第一个数字上。我从头开始,一直走到最后。如果我发现一条条纹,我会返回真的。如果不是,我到了最后,我返回false。我不认为你能改进这种方法。事实上,这是一个有趣的问题。只需每隔一秒读取一次位置(将索引增加2而不是1),就可以将搜索效率提高一倍。如果两个值相差2,请返回并检查中间的值。这样可以避免一些不必要的读取…如果没有序列,则为n/2。对不起,这是我的第一个答案:D将编辑它
var array = [1,3,5,7,7,8,9,10,11];
if(checkCons(array)){
//do what you want with it
}
function fiveStraight() {
var array = [1, 3, 5, 7, 7, 8, 9, 10, 12];
var prev = array[0];
var numberOfStraight = 1;
for (var i = 1; i < array.length; i++) {
numberOfStraight = array[i] === prev + 1 ? numberOfStraight + 1 : 1;
prev = array[i];
if (numberOfStraight === 5) return true;
}
return false;
}
function fiveInARow(array) {
// compare if one element is greater than or equal to the previous one
function compare(elt, i, arr) { return !i || elt >= arr[i-1]; });
// check if at a given position, every one of the last five comparisons is true
function check (_, i, greaters) {
return i >= 4 && greaters.slice(i-4, i) . every(Boolean);
}
return array . map(compare) . some(check);
}
function fiveInARow(array) {
return function _five(array, prev, n) {
if (n >= 5) return true;
if (!array.length) return false;
var next = array.shift();
return _five(array, next, next === prev ? n : next >= prev ? n+1 : 0);
}(array, -999999, 5);
}
function consecutiveValuesCount(array) {
// sort the values if not pre-sorted
var sortedValues = array.sort();
// the result variable / how many consecutive numbers did we find?
// there's always atleast 1 consecutive digit ...
var count = 1;
for (var i = 0; i < sortedValues.length - 1; i++) {
// both descending and ascending orders count as we are using Math.abs()
if (Math.abs(sortedValues[i] - sortedValues[i+1]) == 1) {
++count;
}
}
return count;
}
//input
var array = [1,2,4,5,3];
// output
5
var findMaxConsecutiveOnes = function (arr, number) {
//check for boundries
if(!number || !arr.length) return;
// maximum number of consectuives
let max = 0;
// count homy many consecutives before it ends (why it's 1 ? because a lonely number is still counted)
let counter = 1;
// you can ignore the next 2 variable if you want to use for loop instead of while
let length = arr.length;
let i = 1; // counting from index 1 because we are checking against index-1
while (i < length) {
if (arr[i] == arr[i - 1]) {
// boom, we have a consecutive, count it now
counter++;
} else {
// rest to 1
counter = 1;
}
// always update the max variable to the new max value
max = Math.max(counter, max);
//for the sake of iteration
i++;
}
return max== number;
};
console.log(findMaxConsecutiveOnes([5, 5, 5, 1, 1, 1, 1, 1]));