java codility training Genomic-range-query

后端 未结 30 2318
悲哀的现实
悲哀的现实 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:39

    simple php 100/100 solution

    function solution($S, $P, $Q) {
        $result = array();
        for ($i = 0; $i < count($P); $i++) {
            $from = $P[$i];
            $to = $Q[$i];
            $length = $from >= $to ? $from - $to + 1 : $to - $from + 1;
            $new = substr($S, $from, $length);
    
            if (strpos($new, 'A') !== false) {
                $result[$i] = 1;
            } else {
                if (strpos($new, 'C') !== false) {
                    $result[$i] = 2;
                } else {
                    if (strpos($new, 'G') !== false) {
                        $result[$i] = 3;
                    } else {
                       $result[$i] = 4;
                    }
                }
            }
        }
        return $result;
    }
    

提交回复
热议问题