java codility training Genomic-range-query

后端 未结 30 2269
悲哀的现实
悲哀的现实 2021-02-01 12:47

The task is:

A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper-case English letters A, C, G, T.

<
30条回答
  •  一向
    一向 (楼主)
    2021-02-01 13:40

    The php 100/100 solution:

    function solution($S, $P, $Q) {
        $S      = str_split($S);
        $len    = count($S);
        $lep    = count($P);
        $arr    = array();
        $result = array();
        $clone  = array_fill(0, 4, 0);
        for($i = 0; $i < $len; $i++){
            $arr[$i] = $clone;
            switch($S[$i]){
                case 'A':
                    $arr[$i][0] = 1;
                    break;
                case 'C':
                    $arr[$i][1] = 1;
                    break;
                case 'G':
                    $arr[$i][2] = 1;
                    break;
                default:
                    $arr[$i][3] = 1;
                    break;
            }
        }
        for($i = 1; $i < $len; $i++){
            for($j = 0; $j < 4; $j++){
                $arr[$i][$j] += $arr[$i - 1][$j];
            }
        }
        for($i = 0; $i < $lep; $i++){
            $x = $P[$i];
            $y = $Q[$i];
            for($a = 0; $a < 4; $a++){
                $sub = 0;
                if($x - 1 >= 0){
                    $sub = $arr[$x - 1][$a];
                }
                if($arr[$y][$a] - $sub > 0){
                    $result[$i] = $a + 1;
                    break;
                }
            }
        }
        return $result;
    }
    

提交回复
热议问题