We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个字符串 s 和一个整数 k 。你可以从 s 的前 k 个字母中选择一个,并把它加到字符串的末尾。
返回 在应用上述步骤的任意数量的移动后,字典上最小的字符串 。
输入:s = "cba", k = 1 输出:"acb" 解释: 在第一步中,我们将第一个字符(“c”)移动到最后,获得字符串 “bac”。 在第二步中,我们将第一个字符(“b”)移动到最后,获得最终结果 “acb”。
输入:s = "baaca", k = 3 输出:"aaabc" 解释: 在第一步中,我们将第一个字符(“b”)移动到最后,获得字符串 “aacab”。 在第二步中,我们将第三个字符(“c”)移动到最后,获得最终结果 “aaabc”。
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/orderly-queue 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
The text was updated successfully, but these errors were encountered:
题目看似是一个困难题,但分析之后会发现很简单 对 key 的值分情况讨论
class Solution { /** * @param String $s * @param Integer $k * @return String */ function orderlyQueue($s, $k) { if ($k > 1) { $arr = str_split($s, 1); // 不能使用 explode 函数,因为 explode 的第一个参数不能时空 sort($arr); return implode('', $arr); } $result = $s; for ($i = 0; $i < strlen($s); $i++) { $s = substr($s, 1) . $s[0]; if (strcasecmp($s, $result) < 0) { $result = $s; } } return $result; } }
Sorry, something went wrong.
No branches or pull requests
给定一个字符串 s 和一个整数 k 。你可以从 s 的前 k 个字母中选择一个,并把它加到字符串的末尾。
返回 在应用上述步骤的任意数量的移动后,字典上最小的字符串 。
示例 1:
示例 2:
提示:
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/orderly-queue
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
The text was updated successfully, but these errors were encountered: