使用JS找出数组中的最长递增子串

1、数组中的最长连续递增子串

 function findL(arr){
    let arr_act=[];
    let arr_pre=[];
    let arr_max=[];

    for(var i=0;i<arr.length;i++){
      let val=arr[i]
      if(arr_act.length==0){
        arr_act.push(val)
      }else if(val<arr_act[arr_act.length-1]){
        arr_pre=arr_act;
        arr_act=[];
        arr_act.push(val)
      }else {
        arr_act.push(val)
      }

      if(arr_pre.length>arr_max.length){
        arr_max=arr_pre;
      }

    }
    return arr_max.length>arr_act.length?arr_max:arr_act
  }

2、数组中最长的递增子串(不要求连续)

function findJ(arr){
    let arr_max=[];
    let cache=[];
    for(var i=0;i<arr.length;i++){
      cache.push([])
    }
    for(let i=0;i<arr.length;i++){
      for(let j=0;j<i;j++){
        if(arr[j]<arr[i]){
          if(cache[i].length<cache[j].length+1){
            cache[i]=[].concat(cache[j])
          }
          
        }
      }
      cache[i].push(arr[i]);
      if(cache[i].length>arr_max.length){
        arr_max=[].concat(cache[i])
      }
    }
    return arr_max
  }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值