LoginSignup
0
0

More than 1 year has passed since last update.

LeetCode No.1

Posted at

344. Reverse String

使用言語:PHP
逆さまにする関数を知っていたので、イチコロでした〜!

class Solution {

    /**
     * @param String[] $s
     * @return NULL
     */
    function reverseString(&$s) {
     $s = array_reverse($s);
    }
}

別の解

while文やfor文でもできるみたいですね。
while文にあまり馴染みないので、while文で解いてみました。

class Solution {

    /**
     * @param String[] $s
     * @return NULL
     */
    function reverseString(&$s) {
        $left = 0;
        $right = count($s) - 1;
        while($left < $right){
            $tmp = $s[$left];
            $s[$left] = $s[$right];
            $s[$right] = $tmp;
            $left++;
            $right--;
        }
        return $s;
    }
}


0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0