【问题标题】:Finding the biggest decimal for each integer in array查找数组中每个整数的最大小数
【发布时间】:2016-09-08 13:19:13
【问题描述】:

我有一个数组,其中包含教程中的每个步骤,它是相应的 git commit sha。我想选择每章中的最后一步,对于以下示例,对应于1.12.53.4。我目前正在使用来自 Typescript 的 let...of 迭代器来分析这些步骤。

[1]     step: '3.4' },
[1]   Commit {
[1]     hash: '52f7762e2a2d901b9d849e70679237fad8f0c05f',
[1]     step: '3.3' },
[1]   Commit {
[1]     hash: '6ef63c21cd7f42fe5f190fb7bd5a7411528b944e',
[1]     step: '3.2' },
[1]   Commit {
[1]     hash: 'fe7bd36851672a24c0cd205f0763d52e6abfa6e1',
[1]     step: '3.1' },
[1]   Commit {
[1]     hash: '13b948cc246b3c9b383c4be24ca0ba0a7c072e67',
[1]     step: '2.5' },
[1]   Commit {
[1]     hash: '679bc61d53a59ad10c0398f2faecd7a4052689dc',
[1]     step: '2.4' },
[1]   Commit {
[1]     hash: '76f57e963afb885fed48b1646fd50025269061ac',
[1]     step: '2.3' },
[1]   Commit {
[1]     hash: 'fd368fed4f47b9686e855b2a76e53dae5880ef69',
[1]     step: '2.2' },
[1]   Commit {
[1]     hash: 'a70e6f556640db53f1ef3acba28c42f582d45890',
[1]     step: '2.1' },
[1]   Commit {
[1]     hash: '95854756de842ff45ebbf9d3703cc7eff1557d5a',
[1]     step: '1.1' },

这样做的巧妙方法是什么?

我当前的方法需要遍历所有提交以查找有多少章,然后使用step = chapter.x 过滤掉所有结果。然后找到该数组中最大的 x。然后重复所有章节。这对于我想要实现的目标来说似乎相当笨拙。

【问题讨论】:

  • 为了清楚起见提出几个问题 - 这是一个提交数组,对吗?假设步骤按降序排序是否安全,如您的示例所示?

标签: javascript arrays typescript


【解决方案1】:

我会看下划线的max() 之类的东西,它允许你传入数组,一个测试最大值的函数,并返回最大值。

如果这是为了一个项目,或者由于某种原因你不能使用库,那么一个简单的 for 循环就足够了。

【讨论】:

    【解决方案2】:

    有很多方法可以解决这个问题。我使用节号来抵消我在数组中的索引。这节省了对数组的迭代(每章一次迭代,而不是每一步迭代),以换取必须对每个元素进行更多操作(拆分步骤字符串并将步骤转换为 int)。你的旅费可能会改变。与其他答案一样,这是一个普通的旧 javascript 解决方案。

    //Only this function is the actual solution
    function findLastSections(list) {
      var chapterEnds = [];
      var currCommit;
      var currIndex = 0;
      
      //Exit clause is when my index goes out-of-bounds.
      while(currIndex < list.length) {
        //I took the goal of trying to only iterate once per chapter.
        console.log('processing element: ' + currIndex);
        //get the last commit for the chapter
        currCommit = list[currIndex];
        //add its step to our return array.
        chapterEnds.unshift(currCommit.step);
        //split the step on the decimal, and parseInt so that I can do arithmetic on it. Add it to the index. This effectively shifts my position in the array by the length of the current chapter.
        currIndex += parseInt(currCommit.step.split('.')[1]);
      }
      console.log('returning: '); 
      console.log(chapterEnds);
      return chapterEnds;
    }
    
    //Everything below this line is just setup stuff to approximate your environment.
    
    //Call to the function.
    findLastSections(getCommitList());
    
     // Constructor function in place of Typescript class
    function Commit(hash, step) {
      this.hash = hash;
      this.step = step;
    }
    
    function getCommitList() {
      //just did this to get it hoisted, so it's not crammed into the top of the snippet.
      return [
          new Commit (
         '52f7362e2a2d901b9d849e70679237fad8f0c05f',
         '3.4'),
          new Commit (
         '52f7762e2a2d901b9d849e70679237fad8f0c05f',
         '3.3'),
       new Commit (
         '6ef63c21cd7f42fe5f190fb7bd5a7411528b944e',
         '3.2'),
       new Commit (
         'fe7bd36851672a24c0cd205f0763d52e6abfa6e1',
         '3.1'),
       new Commit (
         '13b948cc246b3c9b383c4be24ca0ba0a7c072e67',
         '2.5'),
       new Commit (
         '679bc61d53a59ad10c0398f2faecd7a4052689dc',
         '2.4'),
       new Commit (
         '76f57e963afb885fed48b1646fd50025269061ac',
         '2.3'),
       new Commit (
         'fd368fed4f47b9686e855b2a76e53dae5880ef69',
         '2.2'),
       new Commit (
         'a70e6f556640db53f1ef3acba28c42f582d45890',
         '2.1'),
       new Commit (
         '95854756de842ff45ebbf9d3703cc7eff1557d5a',
         '1.1')
    ];
    }

    【讨论】:

      【解决方案3】:

      这具有线性的最坏情况复杂性,所以我能想到的最简单的方法(在纯 javascript 中)是:

      function maxChapters(commits) {
        var result = [];
        var recent = NaN;
        for (var i = 0; i < commits.length; i++) {
          var chapter = Math.floor(commits[i]['step']);
          if (chapter != recent) {
            result.push(commits[i]['step']);
            recent = chapter;
          }
        }
        return result;
      }
      
      var commits = [{
        hash: '52f7762e2a2d901b9d849e70679237fad8f0c05f',
        step: '3.3' 
      }, {
        hash: '6ef63c21cd7f42fe5f190fb7bd5a7411528b944e',
        step: '3.2' 
      }, {
        hash: 'fe7bd36851672a24c0cd205f0763d52e6abfa6e1',
        step: '3.1' 
      }, {
        hash: '13b948cc246b3c9b383c4be24ca0ba0a7c072e67',
        step: '2.5' 
      }, {
        hash: '679bc61d53a59ad10c0398f2faecd7a4052689dc',
        step: '2.4' 
      }, {
        hash: '76f57e963afb885fed48b1646fd50025269061ac',
        step: '2.3' 
      },{
        hash: 'fd368fed4f47b9686e855b2a76e53dae5880ef69',
        step: '2.2' 
      }, {
        hash: 'a70e6f556640db53f1ef3acba28c42f582d45890',
        step: '2.1' 
      }, {
        hash: '95854756de842ff45ebbf9d3703cc7eff1557d5a',
        step: '1.1' 
      }]
      
      console.log(maxChapters(commits));

      【讨论】:

        猜你喜欢
        • 2018-01-08
        • 1970-01-01
        • 2013-06-22
        • 2015-07-04
        • 2015-10-30
        • 2018-08-27
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        相关资源
        最近更新 更多