Algorithms review

recording.

Given a two-dimensional nxm array of rows and columns are the order of the write algorithm for Target is in them.

For example, to find such an array of which 7 are in, to talk about ideas and complexity.

[ 
    [2,3,5,9],
    [3,4,7,10],
    [4,5,8,12],
    [6,8,9,15]
]

code:

<?php
 function checkValue(array $data,int $aim){
        $result = false;
        $start = 0;
        $end = count($data[0])-1;
        foreach ($data  as $k=>$v){
            $res = $this->erfenFind($v,$aim,$start,$end);
            if($res['code']==0){
                $result = true;
                break;
            }elseif($res['code']==-1){
                $result = false;
                break;
            }else{
                $start = $res['start'];
                $end = $res['end'];
            }
        }
        return $result;
    }

    function erfenFind( $data,$aim,$start,$end ){
        $len = ($end-$start)+1;
        $len_center =  $start+intval($len/2);

        $result = ['code'=>-2,'start'=>$start,'end'=>$end];

        if($aim < $data[0]){
            return ['code'=>-1,'start'=>'','end'=>''];
        }


        if($aim==$data[$start] || $aim == $data[$end] || $aim==$data[$len_center]){
            $result = ['code'=>0,'start'=>'','end'=>''];
        }


        if($start<$end && $end!=$len_center){

            if( $aim < $data[$start] ){
                return ['code'=>-2,'start'=>$start-1,'end'=>$start];
            }

            if( $aim>$data[$end] ){
                return ['code'=>-2,'start'=>$end,'end'=>$end];
            }

            if( $aim<$data[$len_center] ){
                $result = $this->erfenFind($data,$aim,$start,$len_center);
            }else{
                $result = $this->erfenFind($data,$aim,$len_center,$end);
            }
        }

        return $result;
    }

Given an ordered array, flip operation, will turn the first few elements behind the array, write an algorithm to find the minimum, to talk about ideas and complexity.

The [4,5,6,7,8,9,10,1,2,3]

<?php
  function getMin( array $data,int $start,int $end ){
        $center = $start+intval(($end-$start+1)/2);

        if( $center==$end ){

            return $data[$start]<$data[$end] ?$data[$start]:$data[$end];
        }

        if( $data[$center]>$data[$start]){
            return $this->getMin($data,$center,$end);
        }else{
            return $this->getMin($data,$start,$center);
        }


 }
 
 $data = [4,5,6,7,8,9,10,1,2,3];
 $this->getMin($data,0,count($data)-1);
 

Listed in his given string permutations algorithm.

The string 'abc', the following composition:

a,b,c,ab,bc,ac,abc

<?php
 function get_combinations($str = '', &$comb = array())
    {
        if (trim($str) == '' || ! $str) return false;
        if (strlen($str) <= 1) {
            $comb[] = $str;
        } else {
            $str_first = $str[0];
            $comb_temp = $this->get_combinations(substr($str, 1), $comb);
            $comb[] = $str_first;
            foreach ($comb_temp as $k => $v) {
                $comb[] = $str_first . $v;
            }
        }
        return $comb;
    }

Guess you like

Origin www.cnblogs.com/followyou/p/11444209.html