最长无重复子数组

<?php


/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 
 * @param arr int整型一维数组 the array
 * @return int整型
 */
function maxLength( $arr )
{
    // write code here
        $temp_arr = [];    //存储临时数组
    $max      = 0;     //存储最大值
    // write code here
    for($i = 0; $i<count($arr); $i++) {
 
 
        if (in_array($arr[$i], $temp_arr)) {
            //删除前置元素
            $temp_arr = array_slice($temp_arr , array_search($arr[$i],$temp_arr) + 1);
        }
 
        array_push($temp_arr , $arr[$i]);
 
        //统计最大值
        if(count($temp_arr) > $max) {
            $max = count($temp_arr);
        }
    }
 
    return $max;
}

猜你喜欢

转载自blog.csdn.net/qq_40192867/article/details/123205307
今日推荐