zoukankan      html  css  js  c++  java
  • js 取到相同的字符串 返回对应的下标

    ["aaa","aaa","","ddd","eee","eee","eee","eee","eee","eee","yyy","zzz","ggg","ggg"]
    
      有这么一个数组,中间有一段字符串是相同的,我们不知道是什么字符串,现在,请求出中间相同一段的起止下标以及内容
    
    

    var rest=[];
    var array=["aaa","aaa","","ddd","eee","eee","eee","eee","eee","eee","yyy","zzz","ggg","ggg"];
    array.forEach(function (v,i) {
    var sameInLeft=array[i]==array[i-1]?1:0;
    var sameInRight=array[i]=array[i+1]?1:0;
    if(sameInLeft+sameInRight==1){
    rest.push(i);
    }

    });
    for(var i=0;i<rest.length/2;i++){
    var indexArr=[rest[i*2],rest[i*2+1]];
    //排除收尾连续
    if(indexArr[0]!=0&& indexArr[1]!=(array.length-1)){
    console.log(indexArr,array.slice(indexArr[0],indexArr[1]+1))

    }

    }

     

     ——》

    [4, 5] [1, 1]
     [6, 7] [1, 1]
     [8, 9] [1, 1]
     [10, 11] [1, 1]
     [12, undefined] []

  • 相关阅读:
    经典入门_排序
    经典入门_贪心算法
    经典入门_Hash的应用
    经典入门_排序
    uva 839
    uva 327
    uva 699
    uva 712
    uva 297
    uva 548
  • 原文地址:https://www.cnblogs.com/zxyun/p/5717250.html
Copyright © 2011-2022 走看看