地址:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
<?php /** 字符串的左旋轉操作是把字符串前面的若干個字符轉移到字符串的尾部。請定義一個函數實現字符串左旋轉操作的功能。比如,輸入字符串"abcdefg"和數字2,該函數將返回左旋轉兩位得到的結果"cdefgab"。 示例 1: 輸入: s = "abcdefg", k = 2 輸出: "cdefgab" 示例 2: 輸入: s = "lrloseumgh", k = 6 輸出: "umghlrlose" 限制: 1 <= k < s.length <= 10000 來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。 */ class Solution { /** * @param String $s * @param Integer $n * @return String */ function reverseLeftWords($s, $n) { #1 // $index = substr($s,0,$n); // $end = substr($s,$n); // return $end.$index; #2 $arr =[]; for($i = $n;$i <$n+strlen($s);$i++){ $m = $i % (strlen($s)); $arr[] = $s[$m]; } return implode('',$arr); } }