确定数组中的数字和任何其他两个数字是否形成连续数字的三个

时间:2017-04-10 10:37:36

标签: javascript arrays

写一个这样的函数:

function canFormContinuosNums(num, array);
//num is the target number, 
//array is the source array which contain some int number:

例如:

num = 1, 
array =[ 2,3,4,5,7,8,9,0];

函数必须测试天气数组包含两个数字,如2,3或0,2,可以形成' 012'或者' 123'。

该函数必须返回false或true;

如果为true则返回两个数组,如[2,3]或[0,2];

我尝试了很多,但都没有完美的工作。非常感谢你的帮助。

3 个答案:

答案 0 :(得分:0)

function canFormContinuousNums(num, array){
    // array contains the 2 numbers before num
    if(array.indexOf(num-2)!=-1 && array.indexOf(num-1)!=-1){
        return [num-2,num-1];
    }
    // array contains the number before num AND the number after num
    else if(array.indexOf(num-1)!=-1 && array.indexOf(num+1)!=-1){
        return [num-1,num+1];
    }
    // array contains the 2 numbers after num
    else if(array.indexOf(num+1)!=-1 && array.indexOf(num+2)!=-1){
        return [num+1,num+2];
    }

    return false;
}

这应该让你的数组包含两个数字,例如你可以形成一个连续的三个数,如果可以的话,将返回包含前两个数字的数组,或者包含前一个和后一个,或包含下两个数字的那个。

您还可以使用以下代码进行组合:

0 -> no combination
1 -> the two previous numbers
2 -> the one before & the one after
4 -> the two next numbers

然后:

3 will give you both 1 & 2
5 will give you both 1 & 4 // note that 1 & 4 is impossible without 2
6 will give you both 2 & 4
7 will give you all three combinations.

如下:



function canFormContinuousNums(num, array){
    var result = 0;
    // array contains the 2 numbers before num
    if(array.indexOf(num-2)!=-1 && array.indexOf(num-1)!=-1){
        result += 1;
    }
    // array contains the number before num AND the number after num
    if(array.indexOf(num-1)!=-1 && array.indexOf(num+1)!=-1){
        result += 2;
    }
    // array contains the 2 numbers after num
    if(array.indexOf(num+1)!=-1 && array.indexOf(num+2)!=-1){
        result += 4;
    }

    return result;
}

var array = [0,1,4,6,8,9,11,14,15,17,18];
console.log(canFormContinuousNums(20, array)); // no combination
console.log(canFormContinuousNums(2, array)); // [0,1]
console.log(canFormContinuousNums(5, array)); // [4,6]
console.log(canFormContinuousNums(10, array)); // [8,9] & [9,11]
console.log(canFormContinuousNums(13, array)); // [14,15]
console.log(canFormContinuousNums(7, array)); // [6,8] & [8,9]
console.log(canFormContinuousNums(16, array)); // [14,15] & [15,17] & [17,18]

// test function to display all the combinations switch the number and the result given by canFormContinuousNums
function displayResult(number,result){
  if(result & 1){
    console.log("["+(number-2)+","+(number-1)+"]");
  }
  if(result & 2){
    console.log("["+(number-1)+","+(number+1)+"]");
  }
  if(result & 4){
    console.log("["+(number+1)+","+(number+2)+"]");
  }
}

console.log("Test displayResult(16,7)");
displayResult(16,canFormContinuousNums(16,array));




编辑: 如果您希望获得所有组合,以下内容应该有效:



function canFormContinuousNums(num, array){
  var flag = false;
  var result = [[0,0],[0,0],[0,0]];
  // array contains the 2 numbers before num
  if(array.indexOf(num-2)!=-1 && array.indexOf(num-1)!=-1){
    flag = true;
    result[0][0] = num-2;
    result[0][1] = num-1;
  }
  else{
    result[0] = false;
  }
  // array contains the number before num AND the number after num
  if(array.indexOf(num-1)!=-1 && array.indexOf(num+1)!=-1){
    flag = true;
    result[1][0] = num-1;
    result[1][1] = num+1;
  }
  else{
    result[1] = false;
  }
  // array contains the 2 numbers after num
  if(array.indexOf(num+1)!=-1 && array.indexOf(num+2)!=-1){
    flag = true;
    result[2][0] = num+1;
    result[2][1] = num+2;
  }
  else{
    result[2] = false;
  }

  if(flag == true){
    return result;
  }
  else{
    return false;
  }
}

var array2 = [0,1,2];
console.log(canFormContinuousNums(1,array2));
console.log(canFormContinuousNums(4,array2));




答案 1 :(得分:0)

您可以尝试这样的事情:

逻辑:

  • 找到要插入数字的索引。
  • 现在循环超出部分范围:
  • 0index-2开始捕捉以前的元素。
  • index + 2停止循环。这可能导致超出界限。
  • 现在验证,如果值存在且next和current元素之间的差异为1增量count
  • 现在比较如果count大于等于您所需的连续长​​度(在您的情况下 2 ),并相应地返回。

function canMakeContinuous(arr, num) {
  if(arr.indexOf(num) > -1) return false
  var copy = arr.slice();
  var index = 0;
  arr.some(function(item, i) {
    index = i;
    if (item > num) {
      return true;
    }
  });
  copy.splice(index, 0, num)
  var count = 0;
  for (var i = Math.max(index - 2, 0); i < index + 3; i++) {
    if (copy[i + 1] !== undefined && copy[i + 1] - copy[i] === 1) {
      count++;
    } else if (count < 2) {
      count = 0;
    }
  }
  return count > 1
}


var array = [2, 3, 4, 7, 8, 9, 0];

canMakeContinuous(array, 1)
canMakeContinuous(array, 6)
canMakeContinuous(array, 5)
canMakeContinuous(array, 9)

答案 2 :(得分:0)

这个怎么样?

&#13;
&#13;
function canFormContinuosNums(num, array) {
var tempArray = new Array();
var outputArray = new Array();
  for (var i = 0; i < 3; i++) {
   tempArray[i] =  [num+i-2, num+i-1, num+i];
   tempArray[i].splice(tempArray[i].indexOf(num), 1);
   var check = 0;
   for (var k = 0; k < tempArray[i].length; k++) {
    if (array.includes(tempArray[i][k])) {
      check += 1;
    }
   }
   if (check == 2) {
    outputArray.push(tempArray[i]);
   }
  }
  console.log(outputArray);
  
};
num = 4, 
array =[2,3,4,5,7,8,9,0];
canFormContinuosNums(num, array);
&#13;
&#13;
&#13;