常见算法

  function getRelativePath($a, $b) {
        $relativePath = "";
        $pathA = explode('/', dirname($a));
        $pathB = explode('/', dirname($b));
        $n = 0;
        $len = count($pathB) > count($pathA) ? count($pathA) : count($pathB);
        for ($n = 0; $n < $len; $n++) {
            if ( $n >= $len || $pathA[$n] != $pathB[$n] ) {
                echo $n ;
                break;
            }
        }
        $relativePath .= str_repeat('../', count($pathB) - $n);
        $relativePath .= implode('/', array_splice($pathA, $n));
        return $relativePath;
    }


    function StrToInt($str)
    {
        // write code here
        $tem = array('1','2','3','4','5','6','7','8','9','0');
        $len = strlen($str);
        if($len == 0)return 0;
        $arr = str_split($str);
        $sum = 0;
        for ($i =0; $i 
  1. //二分查找递归实现  
  2. function binSearch2($arr,$low,$height,$k){  
  3.     if($low<=$height){  
  4.         $mid=floor(($low+$height)/2);//获取中间数  
  5.         if($arr[$mid]==$k){  
  6.             return $mid;  
  7.         }elseif($arr[$mid]<$k){  
  8.             return binSearch2($arr,$mid+1,$height,$k);  
  9.         }elseif($arr[$mid]>$k){  
  10.             return binSearch2($arr,$low,$mid-1,$k);  
  11.         }  
  12.     }  
  13.     return -1;  

    function string_fz($string){
        $len = strlen($string);
        $mid = floor($len/2);
        for($i=0; $i<$mid; $i++){
            $temp =  $string[$i] ;
            $string[$i] = $string[$len-$i-1];
            $string[$len-$i-1] =$temp;
        }
         return $string;
    }




    //指定数组以$keys键值排序
    function array_sort1($array,$keys,$type='asc'){
        $keysvalue = $new_array = array();
        foreach ($array as $k=>$v){
            $keysvalue[$k] = $v[$keys];
        }
        if($type == 'asc'){
            asort($keysvalue);
        }else{
            arsort($keysvalue);
        }
        reset($keysvalue);
        foreach ($keysvalue as $k=>$v){
            $new_array[$k] = $array[$k];
        }
        return $new_array;
    }

你可能感兴趣的:(数据结构与算法)