Leetcode moderate four Z-transform

Z-transform:

php:

44ms, the string operation is completed, the main equation: n + (k - i * 2) and n + i * 2

class Solution {

    /**
     * @param String $s
     * @param Integer $numRows
     * @return String
     */
    function convert($s, $numRows) {
        $res = '';
        $curr = '';
        $k = ($numRows - 1)*2;
        if($numRows == 1) return $s;
        for($i = 0;$i < $numRows;$i++){
            $n = $i;
            for($j = 0;$j < strlen($s);$j++){
                if($j == 0){
                    $res .= $s[$n];
                }
                if(($j % 2 == 0) && ($j != 0)){
                    if($n == $n+2*$i){
                        continue;
                    }
                    $n = $n+2*$i;
                    if($n > strlen($s)-1){
                        break;
                    }
                    $res .= $s[$n];
                }
                if($j % 2 != 0){
                    if($n == $n + ($k - $i*2)){
                        continue;
                    }
                    $n = $n + ($k - $i*2);
                    if($n > strlen($s)-1){
                        break;
                    }
                    $res .= $s[$n];
                }
            }
        }
        return $res;
    }
}

 

Guess you like

Origin blog.csdn.net/qq_36688622/article/details/89225673