796. 旋转字符串

题目链接:https://leetcode-cn.com/problems/rotate-string/

枚举每个字符

代码

执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:39.2 MB, 在所有 Java 提交中击败了5.08% 的用户
通过测试用例:45 / 45

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public boolean rotateString(String s, String goal) {
int Len = s.length();
int gLen = goal.length();
if (Len == gLen) {
for (int i = 0; i < Len; i++) {
// 在目标串中定位当前扫描的字符
int aim = goal.indexOf(s.charAt(i));
if (aim == -1) {
return false;
}
int cursor = i;
// 取余判断是否相同, 相同则同时移动游标
while (s.charAt(cursor % Len) == goal.charAt(aim % Len)) {
aim++;
// 位移长度等于自身说明已全部扫描
if (aim - i == Len) {
return true;
}
cursor++;
}
}
}
return false;
}
}

796. 旋转字符串
https://pisces34.github.io/2022/02/03/leetcode/796/
发布于
2022年2月3日
许可协议