获得最小和最高的数字左右我的价值

时间:2014-03-22 15:07:02

标签: javascript arrays algorithm

请告诉我是否有更快,更优雅的方式来获得此结果:
我有一个数字数组,一旦设置了一个值,我应该在右边和左边得到最小和最高的数字。

例如,如果我有:[1,2,3,4,6,7,8,9]且值为5,则 我的号码是:1,4,6,9
如果值是4
我的数字将是1,4,4,9
我可怕的代码是:

var arr = [1, 8, 2, 3, 9, 5, 4, 6, 7];
var result1 = [];
var result2 = [];
var goal = 5;
for (a = 0; a < arr.length; a++) {
    if (arr[a] < goal) {
        result1.push(arr[a])
    } else if (arr[a] === goal) {
        result1.push(arr[a]);
        result2.push(arr[a]);
    } else {
        result2.push(arr[a]);
    }
};
var count1 = result1[0];
for (x = 0; x < result1.length; x++) {
    if (result1[x] < count1) {
        count1 = result1[x]
    }
};
var count11 = result1[0];
for (xx = 0; xx < result1.length; xx++) {
    if (result1[xx] > count11) {
        count11 = result1[xx]
    }
};
var count2 = result2[0];
for (y = 0; y < result2.length; y++) {
    if (result2[y] > count2) {
        count2 = result2[y]
    }
};
var count22 = result2[0];
for (yy = 0; yy < result2.length; yy++) {
    if (result2[yy] < count22) {
        count22 = result2[yy]
    }
};
console.log(count1 + ' ' + count11 + ' ' + count22 + ' ' + count2)

4 个答案:

答案 0 :(得分:1)

您可以使用一些强大的方法大量简化代码:Array::filterMath.min / max

var arr = [1, 8, 2, 3, 9, 5, 4, 6, 7];
var goal = 5;

var result1 = arr.filter(function(x) { return x <= goal });
var result2 = arr.filter(function(x) { return x >= goal });
var count1  = Math.min.apply(Math, result1);
var count11 = Math.max.apply(Math, result1);
var count2  = Math.min.apply(Math, result2);
var count22 = Math.max.apply(Math, result2);

答案 1 :(得分:0)

假设数组总是排序,

var array = [1, 2, 3, 4, 6, 7, 8, 9],
    number = 5,
    pivot, small, large;

for (var i = 0, len = array.length; i < len; i += 1) {
    if (array[i] >= number) {
        pivot = i;
        break;
    }
}

if (pivot > 0) {
    small = [array[0], array[pivot - 1]];
    large = [array[pivot], array[len - 1]];
    console.log(small, large);
} else {
    console.log("Not possible");
}

答案 2 :(得分:0)

修改

虽然我意识到问题已得到解答并且选择了另一个回复,但我至少想将我的代码更新为经过测试的版本。 FWIW,它有效,并且不依赖于也不依赖于排序数组。

function minmax(valueArray, targetValue) {

var i = 0;
var minBelow;
var maxBelow;
var minAbove;
var maxAbove;

while (i < valueArray.length) {
    var currentValue = valueArray[i];

    if (currentValue < targetValue) {
        if (currentValue < minBelow || !minBelow) {
            minBelow = currentValue;
        }
        if (currentValue > maxBelow || !maxBelow) {
            maxBelow = currentValue;
        }
    }
    if (currentValue > targetValue) {
        if (currentValue < minAbove || !minAbove) {
            minAbove = currentValue;
        }
        if (currentValue > maxAbove || !maxAbove) {
            maxAbove = currentValue;
        }

    }
    i++;
}

return {
    minBelow: minBelow,
    maxBelow: maxBelow,
    minAbove: minAbove,
    maxAbove: maxAbove
};

}

function test() {
    alert('In test');
    var foo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var bar = minmax(foo, 5);
    alert(bar.minBelow + ","+ bar.maxBelow + "," + bar.minAbove + "," + bar.maxAbove);
}

答案 3 :(得分:0)

我想出了以下解决方案。考虑到数组总是排序

function GetVals(arr, pivot){
    return arr.reduce(function(t,v,i,arr){ 
        if (v < pivot) {
            if (typeof t.LMin == "undefined" || t.LMin > v)
                t.LMin = v;
            if (typeof t.LMax == "undefined" || t.LMax < v)
                t.LMax = v;
        } else if (v > pivot) {
            if (typeof t.RMin == "undefined" || t.RMin > v)
                t.RMin = v;
            if (typeof t.RMax == "undefined" || t.RMax < v)
                t.RMax = v;
        }
        return t;
    }, {});
}

返回的结果将包含4个属性,左{min}和LMin LMax分别为最小和RMin RMax

如果LMinLMax相等,则表示左侧只有1个值(右侧的规则相同)

如果LMinLMax未定义,则表示左侧没有值小于pivot。