【问题标题】:Finding a value of an array in a circular fashion, when giving a starting index在给出起始索引时以循环方式查找数组的值
【发布时间】:2019-03-11 21:25:43
【问题描述】:

我希望有人可以帮助解决一个涉及以“循环”方式查找​​数组的正确值的小算法问题。我的例子是 javascript,虽然理论上它可以是任何语言。

这是场景:我有一个数字数组,以及一个“当前指针”,它是数组的某个索引值。我想传入一个“差异”整数值,它可能是正数或负数。如果 diff 为负,则指针索引减小,循环回到数组的另一侧。如果为正,则指针索引增加,如果超出范围,则循环回数组的另一端。

我在下面包含了一些示例调用,以指示示例函数调用和预期输出。

var arr = [0, 1, 2, 3, 4];

// starting at current index, increase or
// decrease index of arr by "diff" amount
// and then return the value at that index

function getValue(arr, current, diff) {

    var newIndex;

    if ( diff >= 0 ) {
        // increase current value by diff
        // increments, in a circular fashion
    }
    else if ( diff < 0 ) {
        // decrease current value by diff
        // increments, in a circular fashion
    }

    return arr[newIndex];
}

// sample calls, with expected output

getValue(arr, 0, 2); // 2
getValue(arr, 0, 4); // 4
getValue(arr, 0, 5); // 0
getValue(arr, 0, 12); // 2
getValue(arr, 0, -1); // 4
getValue(arr, 0, -7); // 3

getValue(arr, 3, 2); // 0
getValue(arr, 3, 4); // 2
getValue(arr, 3, 5); // 3
getValue(arr, 3, 12); // 0
getValue(arr, 3, -1); // 2
getValue(arr, 3, -7); // 1

【问题讨论】:

    标签: javascript arrays indexing


    【解决方案1】:

    此时模数的概念很重要:它基本上是除法的余数。你想要的是获得一个元素的匹配索引,不管迭代同一个数组多少次。这有效地找到了数组长度的模数和您想要移动的步数(差异):

    function getValue(arr, current, diff) {
    
        var newIndex = (current + diff) % arr.length;
    
        // If the new index is negative, we just count backwards from the end of the array
        if (newIndex < 0)
          newIndex = arr.length + newIndex;
    
        return arr[newIndex];
    }
    

    上面的逻辑是这样的:

    1. 它决定了它应该在阵列前后移动多远。我们只需将currentdiff 相加
    2. 使用模数运算符% 来确定我们在遍历数组后的最终索引,直到没有余数为止
    3. 如果索引为负数(如果我们向后移动,这是可能的),那么我们只是将其视为“从数组末尾向后计数”

    请参阅下面的概念验证:

    var arr = [0, 1, 2, 3, 4];
    
    function getValue(arr, current, diff) {
    
        var newIndex = (current + diff) % arr.length;
        
        // If the new index is negative, we just count backwards from the end of the array
        if (newIndex < 0)
          newIndex = arr.length + newIndex;
          
        console.log(newIndex);
        return arr[newIndex];
    }
    
    // sample calls, with expected output
    
    getValue(arr, 0, 2); // 2
    getValue(arr, 0, 4); // 4
    getValue(arr, 0, 5); // 0
    getValue(arr, 0, 12); // 2
    getValue(arr, 0, -1); // 4
    getValue(arr, 0, -7); // 3
    
    getValue(arr, 3, 2); // 0
    getValue(arr, 3, 4); // 2
    getValue(arr, 3, 5); // 3
    getValue(arr, 3, 12); // 0
    getValue(arr, 3, -1); // 2
    getValue(arr, 3, -7); // 1

    【讨论】:

    • 因为这不是 Java,所以您不需要 if
    • @JonasWilms 是的,你会这样做,否则你会得到一个返回未定义的负索引:如果你尝试删除if 语句并尝试getValue(arr, 3, -7);,你会得到-4 这是一个无效索引。
    • 哎呀,用其他语言搞砸了,对不起
    【解决方案2】:
    var arr = [0, 1, 2, 3, 4];
    function getValue(arr, current, diff) {
        if (diff >=0) {
            return arr[(current+diff) % arr.length];
        }
        else {
            return arr[(arr.length + ((current+diff) % arr.length)) % arr.length];
        }
    }
    
    console.log(getValue(arr, 0, 2)); // 2
    console.log(getValue(arr, 0, 4)); // 4
    console.log(getValue(arr, 0, 5)); // 0
    console.log(getValue(arr, 0, 12)); // 2
    console.log(getValue(arr, 0, -1)); // 4
    console.log(getValue(arr, 0, -7)); // 3
    
    console.log(getValue(arr, 3, 2)); // 0
    console.log(getValue(arr, 3, 4)); // 2
    console.log(getValue(arr, 3, 5)); // 3
    console.log(getValue(arr, 3, 12)); // 0
    console.log(getValue(arr, 3, -1)); // 2
    console.log(getValue(arr, 3, -7)); // 1
    

    var arr = [0, 1, 2, 3, 4];
    function getValue(arr, current, diff) {
        if (diff >=0) {
            return arr[(current+diff) % arr.length];
        }
        else {
            return arr[(arr.length + ((current+diff) % arr.length)) % arr.length];
        }
    }
    
    console.log(getValue(arr, 0, 2)); // 2
    console.log(getValue(arr, 0, 4)); // 4
    console.log(getValue(arr, 0, 5)); // 0
    console.log(getValue(arr, 0, 12)); // 2
    console.log(getValue(arr, 0, -1)); // 4
    console.log(getValue(arr, 0, -7)); // 3
    
    console.log(getValue(arr, 3, 2)); // 0
    console.log(getValue(arr, 3, 4)); // 2
    console.log(getValue(arr, 3, 5)); // 3
    console.log(getValue(arr, 3, 12)); // 0
    console.log(getValue(arr, 3, -1)); // 2
    console.log(getValue(arr, 3, -7)); // 1

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2016-10-28
      • 1970-01-01
      • 2021-04-06
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      相关资源
      最近更新 更多