Leetcode PHP Problem--D10 942. DI String Match

Keywords: PHP

942. DI String Match

Title Link

942. DI String Match

Title Analysis

Given a string S containing only I and D, an array is returned.
This number satisfies the following conditions:
When S[i] is I, A [i] < A [i + 1]. The latter number is larger than the former.
When S[i] is D, A [i] > A [i + 1]. The number in front is bigger than that in the back.

thinking

Loop through a given string.
When I is encountered, the subscript of the current letter is inserted directly behind the array.
When D is encountered, the current subscript is inserted before the current subscript position of the array.

Final code

<?php
class Solution {
    function diStringMatch($S) {
            $S = str_split($S);
                    $n = range(0,count($S));
                            $nums = [array_shift($n)];
                                    $currentPosition = 0;
                                            foreach($S as $s){
                                                        if($s == 'I'){
                                                                        $nums[] = array_shift($n);
                                                                                    }
                                                                                                else{
                                                                                                                $left = ($currentPosition>=0 ?array_slice($nums,0,$currentPosition):[]);
                                                                                                                                $right = ($currentPosition<count($nums)?array_slice($nums,$currentPosition):[]);
                                                                                                                                                $middle = [array_pop($n)];
                                                                                                                                                                $nums = array_merge($left,$middle,$right);
                                                                                                                                                                            }
                                                                                                                                                                                        $currentPosition++;
                                                                                                                                                                                                }
                                                                                                                                                                                                       return $nums;
                                                                                                                                                                                                           }
                                                                                                                                                                                                           }
                                                                                                                                                                                                       
                                                                                                                                                                                                       Personally, I don't think this is a very good description. When you have time, you will try to describe the problem clearly.
                                                                                                                                                                                                       
                                                                                                                                                                                                       If you think this article is useful to you, you are welcome to use the support of Love Generation (https://afdian.net/@skys215).
                                                                                                                                                                                                       
                                                                                                                                                                                                       

Posted by Dave Liebman on Wed, 20 Mar 2019 07:30:53 -0700