zoukankan      html  css  js  c++  java
  • 二分法

     二分法处理2个json数据, 需要用lodash 里面的_.sortBy 排序 吧rows 和b 变成有序集合,

    然后按照 alltestID 判断大小在去找.

    
    
    let rows =[ { AllTestID: 8000001, CptIDs: [ 138397 ] },
      { AllTestID: 8000004, CptIDs: [ 138397 ] },
      { AllTestID: 8000006, CptIDs: [ 138397 ] },
      { AllTestID: 8000111, CptIDs: [ 138397 ] },
      { AllTestID: 8000117, CptIDs: [ 138397 ] },
      { AllTestID: 8000129, CptIDs: [ 138397 ] },
      { AllTestID: 8000156, CptIDs: [ 22774 ] },
      { AllTestID: 8000159, CptIDs: [ 22833 ] },
      { AllTestID: 8000167, CptIDs: [ 138397 ] },
      { AllTestID: 8000400, CptIDs: [ 23110 ] },
      { AllTestID: 14003858, CptIDs: [ 138397 ] },
      { AllTestID: 14004330, CptIDs: [ 23004 ] },
      { AllTestID: 15000009, CptIDs: [ 22782 ] },
      { AllTestID: 16000500, CptIDs: [ 22840 ] },
      { AllTestID: 16000505, CptIDs: [ 22842 ] },
      { AllTestID: 16000514, CptIDs: [ 23062 ] } ];
    
    
    let b =[ { AllTestID: 8000001,
        ViewCount: -1,
        LastReplyTime: null,
        ChildTableID: -1 },
      { AllTestID: 8000004,
        ViewCount: -1,
        LastReplyTime: null,
        ChildTableID: -1 },
      { AllTestID: 8000006,
        ViewCount: -1,
        LastReplyTime: null,
        ChildTableID: -1 }]
    
    

    for (let j = 0; j < result.length; j++) {
    let obj = result[j];
    let k = await BinarySearch(rows, result[j].AllTestID);
    if (k != -1) {
      let CptIDs = _.uniq(rows[k].CptIDs);
       //去重
       //遍历cptID 处理

      if

       }
    }

    // 二分法处理查找

    function BinarySearch(arr, target) {
        let s = 0;
        let e = arr.length - 1;
        let m = Math.floor((s + e) / 2);
        let sortTag = arr[s].AllTestID <= arr[e].AllTestID;//确定排序顺序
    
        while (s < e && arr[m].AllTestID !== target) {
            if (arr[m].AllTestID > target) {
                sortTag && (e = m - 1);
                !sortTag && (s = m + 1);
            } else {
                !sortTag && (e = m - 1);
                sortTag && (s = m + 1);
            }
            m = Math.floor((s + e) / 2);
        }
        if (arr[m].AllTestID  == target) {
            // console.log('找到了,位置%s', m);
            return m;
        } else {
            console.log('没找到');
            return -1;
        }
    
    }
  • 相关阅读:
    云笔记项目-Spring事务学习-传播Requried
    云笔记项目-Spring事务学习_测试准备
    云笔记项目-AOP知识简单学习
    云笔记项目-过滤器与拦截器学习
    云笔记项目-Java反射知识学习
    云笔记项目-补充JS面向对象编程基础知识
    云笔记项目-移动笔记后高亮显示笔记本和笔记
    算法-第四版-练习1.3.16解答
    Redis相关的内核参数解释与设置
    算法-第四版-练习1.3.17解答
  • 原文地址:https://www.cnblogs.com/yu-hailong/p/11010753.html
Copyright © 2011-2022 走看看