当前位置: 首页 > 知识库问答 >
问题:

检查数字范围内的数字而不重复的最有效方法

贡斌
2023-03-14

给定一个数n,最小数最小值,最大数最大值,什么是最有效的方法来确定

>

  • n 是否在范围内,包括 、 min - max

    数字n包含或不包含重复的数字

    这里的效率意味着方法或方法集需要最少的计算资源,并在最短的时间内返回 truefalse

    上下文:在< code>for循环中< code>if处的条件,可能需要数千到数十万次迭代才能返回结果;对于< code>Number检查,返回< code>true或< code>false所需的毫秒数可能会影响性能

    在< code>DevTools上的< code>Profiles面板中,对于迭代的< code>71,3307项的集合,下面的< code>RegExp被列为使用总共< code > 1097.3ms毫秒中的< code > 27.2ms毫秒来完成循环。在< code>836,7628项的集合中,下面迭代的< code>RegExp在总共< code > 11285.3ms毫秒中使用了< code > 193.5ms毫秒。

    要求:在最少的时间内返回给定上述参数布尔truefalse的最有效方法。

    注意:解决方案不必限于RegExp;下面用作模式返回预期结果。

    当前使用 RegExp reRegExp.protype.test()js

    var min = 2
    , max = 7
    , re = new RegExp("[" + min + "-" + max + "](.)(?!=\1)", "g")
    , arr = [81, 35, 22, 45, 49];
    
    for (var i = 0; i < arr.length; i++) {
      console.log(re.test(arr[i]), i, arr[i])
        /*
          false 0 81 
          true 1 35
          false 2 22
          true 3 45
          false 4 49 
        */
    }
  • 共有2个答案

    阎慈
    2023-03-14

    解决方案1

    使用正则表达式进行测试

    var min = 2;
    var max = 7;
    res = "";
    arr = [81, 35, 22, 45, 49]
    arr.push("");
    regex=new RegExp("[" + min + "-" + max + "](.)(?!=\1)", "g")
    var result = arr.reduce(function(a, b) {
      if (regex.test(a)) {
        res = res + a + " is true\n"
      } else {
        res = res + a + " is false\n"
      };
      return b
    });
    console.log(res)
    

    reduce方法的不同之处在于,它类似于python中的生成器函数(动态生成输出)

    它使用回调函数简单地循环数组中的每个元素。我不能说reduce函数有多有效。

    尽管如此,请考虑数组中的两个元素

    81                             35          
    ^
    take this value            take the result
    and do something           from the previous 
                               element and add it
                               to the result computed
                               for this element  
    

    更多信息https://msdn . Microsoft . com/en-us/library/ff 679975(v = vs . 94)。aspx

    解决方案2

    使用list存储值及其布尔值

    var min = 2;
    var max = 7;
    res = [""];
    arr = [81, 35, 22, 45, 49]
    arr.push("");
    regex=new RegExp("[" + min + "-" + max + "](.)(?!=\1)", "g")
    var result = arr.reduce(function(a, b) {
      if (regex.test(a)) {
        res.push([a,true])
      } else {
        res.push([a,false])
      };
      return b
    });
    console.log(res)
    
    党宇定
    2023-03-14

    这样做的优点是易于理解。

    function checkDigits(min, max, n) {
        var digits = Array(10);                   // Declare the length of the array (the 10 digits) to avoid any further memory allocation
        while (n) {
            d = (n % 10);                         // Get last digit
            n = n / 10 >>0;                       // Remove it from our number (the >>0 bit is equivalent to compose(Math.floor, Math.abs))
            if (d < min || d > max || digits[d])  // Test if "d" is outside the range or if it has been checked in the "digits" array
                return false;
            else
                digits[d] = true;                 // Mark the digit as existing
        }
    }
    
    js prettyprint-override">var min = 2
    , max = 7
    , arr = [81, 35, 22, 45, 49];
    
    function checkDigits(min, max, n) {
        var digits = Array(10);                   // Declare the length of the array (the 10 digits) to avoid any further memory allocation
        while (n) {
            d = (n % 10);                         // Get last digit
            n = n / 10 >>0;                       // Remove it from our number (the >>0 bit is equivalent to compose(Math.floor, Math.abs))
            if (d < min || d > max || digits[d])  // Test if "d" is outside the range or if it has been checked in the "digits" array
                return false;
            else
                digits[d] = true;                 // Mark the digit as existing
        }
        return true;
    }
    
    for (var i = 0; i < arr.length; i++) {
      console.log(checkDigits(min, max, arr[i]), i, arr[i])
    }
     类似资料:
    • 以下代码根据变量所处的范围,为变量指定特定值。 这工作得很好,但是我想知道是否有一种更Pythonic的方法来编写下面的条件块。

    • 有没有办法确定一个数字是否在两个特定数字的范围内,如果这些数字正在变化?例如: 确定 num3 是否介于 num1 和 num2 之间是相当容易的。但是,假设 num1 和 num2 在程序运行期间动态变化: 其他一切都保持不变。现在,与以前相同的算法将不再有效。有没有一种优雅的方法来检查数字是否使用动态变化的最大值和最小值的范围?

    • 因此,我必须编写一个程序,使用numDigits方法找到一个范围内的所有回文数,该方法取一个int数并返回该数的位数,使用isPalindrome方法取一个int数并返回一个布尔值true或false。这是在爪哇。 我有一个numDigits方法编码,工作很好,但我不知道如何获得它的输出,并使用它找到一个范围内的所有回文 null

    • 本文向大家介绍JavaScript查找范围内所有数字的总和,包括了JavaScript查找范围内所有数字的总和的使用技巧和注意事项,需要的朋友参考一下 问题 我们需要编写一个JavaScript函数,该函数接受一个指定范围的数组。 我们的函数应该找到并返回落在该范围内的所有自然数之和,包括范围数。 示例 以下是代码- 输出结果 以下是控制台输出-

    • 我有N个数字,让我们说。现在我想找出在给定范围内有多少对数字。(L和R给定)。数字对=两个数字相同。我的方法:

    • 问题内容: 听起来很简单…但是我一直在努力解决这个问题,试图找到一个解决方案。 对于一个数字范围,例如 1-12 ,我想在该范围内生成一个随机序列, 并 包括 1 和 12 。 不过我不想重复数字 。 所以我想要这样的东西-3,1,8,6,5,4 ..依此类推,每个数字从1到12。 然后,我想将这些随机数放入,并使用该数组“随机”选择并在jsp页面上显示一些项目(例如从数据库中提取的清单)。 到目