阿里笔试 使用 JavaScript 实现一个 getIntersection 函数,可获取多个区间的交集

标签:   

阿里笔试 使用 JavaScript 实现一个 getIntersection 函数,可获取多个区间的交集

示例:

getIntersection([1, 4], [3, 5]); // return [ 3, 4 ]
getIntersection([5, 2], [4, 9], [3, 6]); // return [ 4, 5 ]
getIntersection([1, 7], [8, 9]); // return null
getIntersection(['x', 7], [4, 9]); // return null
getIntersection([1, 2]); // return [ 1, 2 ]
getIntersection([1, 2], [2, 3]); // return [ 2, 2 ]

对于这一题,,,,表示不会实现函数接收不定参数,于是乎只做到了完成两个参数的

还是有太多东西不会啊

我的提交:

function getIntersection(args1 ,args2 ) {
        args1="'"+args1+"'";
        args1a=args1.charAt(1);
        args1b=args1.charAt(3);
        args2="'"+args2+"'";
        args2a=args2.charAt(1);
        args2b=args2.charAt(3);     
        if(isNaN(args1a) || isNaN(args1b) || isNaN(args2a) || isNaN(args2b)  )
                    return null;
        if(args1a>args1b){
            chang=args1a;
            args1a=args1b;
            args1b=chang;
        }       
        if(args2a>args2b){
            change=args2a;
            args2a=args2b;
            args2b=change;
        }       
        if(args1a<args2a){
            if(args1b<args2b){ 
                result2=args1b;
                result1=args2a;
            }else{
                result2=args2b;
                result1=args2a;
            }
        }else{
            changea=args1a;
            changeb=args1b;
            args1a=args2a;
            args1b=args2b;
            args2a=changea;
            args2b=changeb;
            if(args1b<args2b){
                result2=args1b;
                result1=args2a;
            }else{
                result2=args2b;
                result1=args2a;
            }
        }
        result="["+result1+","+result2+"]";
        if(args1b<args2a)
            return null;        
        return result;
}

发表评论 登录

目前评论:3

  • avatar showtan 回复 2019-09-09 12:51:51
    getIntersection(a, b){
            for (let val of arguments) { val.sort(); };
            let arr0 = [], arr1 = []; 
            for (let val of arguments) { arr0.push(val[0]); arr1.push(val[1]); };
            let arr0Max = Math.max.apply(null, arr0), arr1Max = Math.min.apply(null, arr1);
            // console.log(arr0Max &gt; arr1Max ? null : arr0Max === arr1Max ? [arr0Max] : [arr0Max, arr1Max]);
            return arr0Max &gt; arr1Max ? null : arr0Max === arr1Max ? [arr0Max] : [arr0Max, arr1Max];
    }

    应该可以

    • avatar 忆云竹 回复 2019-09-24 10:48:59

      受教了

  • avatar sirius 回复 2021-03-14 13:18:46

    实现getIntersection,可接收多个区间,并返回所有区间的交集(用区间表示),如空集用null表示

    区间用长度为2的数字数组表示,如[2, 5]表示区间2到5(包括2和5);

    区间不限定方向,如[5, 2]等同于[2, 5];

    【示例】

    // getIntersection([5, 2], [4, 9], [3, 6]); // [4, 5]
    // getIntersection([1, 7], [8, 9]); // null
    function getIntersection(...args: number[][]): string | null {
      //因为最终只是一个交集,所以可以采取递归,两个数组求交集然后又跟后面的求交集
      //求交集方法,某一个数组的右边大于另一个数组的左边,并且这个数组的左边小于另一个的右边
      //左边右边当然是要先内部排序才行
      let args1: any = args[0];
      let args2: any = args[1];
      console.log("args1:" + args1);
      console.log("args2:" + args2);
      args1 = args1.toString();
      let args1a = args1.charAt(0);
      let args1b = args1.charAt(2);
      args2 = args2.toString();
      let args2a = args2.charAt(0);
      let args2b = args2.charAt(2);
    
      if (isNaN(args1a) || isNaN(args1b) || isNaN(args2a) || isNaN(args2b))
        return null;
      if (args1a &gt; args1b) {
        let change = args1a;
        args1a = args1b;
        args1b = change;
      }
      if (args2a &gt; args2b) {
        let change = args2a;
        args2a = args2b;
        args2b = change;
      }
      let result1, result2;
      if (args1a &lt; args2a) {
        if (args1b &lt; args2b) {
          result2 = args1b;
          result1 = args2a;
        } else {
          result2 = args2b;
          result1 = args2a;
        }
      } else {
        let changea = args1a;
        let changeb = args1b;
        args1a = args2a;
        args1b = args2b;
        args2a = changea;
        args2b = changeb;
        if (args1b &lt; args2b) {
          result2 = args1b;
          result1 = args2a;
        } else {
          result2 = args2b;
          result1 = args2a;
        }
      }
      if (args1b  2) {
        let restArgs: any = args.slice(2);
        let string2Array: number[] = JSON.parse("[[[" + result + "]]]");
        string2Array = result.slice(1, -1).split(",").map(Number);
        console.log("string2Array:" + string2Array);
        return getIntersection(string2Array, restArgs);
      }
    
      return result;
    }
    
    console.log(getIntersection([5, 2], [4, 9], [3, 6]));