动态口令
LCR 182. 动态口令
某公司门禁密码使用动态口令技术。初始密码为字符串 password
,密码更新均遵循以下步骤:
- 设定一个正整数目标值
target
- 将
password
前target
个字符按原顺序移动至字符串末尾
请返回更新后的密码字符串。
示例 1:
输入: password = "s3cur1tyC0d3", target = 4
输出: "r1tyC0d3s3cu"
示例 2:
输入: password = "lrloseumgh", target = 6
输出: "umghlrlose"
提示:
1 <= target < password.length <= 10000
调substring API
class Solution {
public String dynamicPassword(String password, int target) {
// 注意 substring 方法是左闭右开的
return password.substring(target, password.length()) + password.substring(0, target);
}
}
- 时间复杂度 O(N): 其中 N 为字符串
password
的长度,字符串切片函数为线性时间复杂度(参考资料)。 - 空间复杂度 O(N) : 两个字符串切片的总长度为 N。
StringBuilder+模运算
class Solution {
public String dynamicPassword(String password, int target) {
StringBuilder res = new StringBuilder();
for(int i = target; i < password.length(); i++)
res.append(password.charAt(i));
for(int i = 0; i < target; i++)
res.append(password.charAt(i));
return res.toString();
}
}
利用求余运算,可以简化代码。
class Solution {
public String dynamicPassword(String password, int target) {
StringBuilder res = new StringBuilder();
// [target, password.length()-1]+[password.length(), target+password.length()-1],
// [target, password.length()-1] % password.length() = [target, password.length()-1],
// [password.length(), target+password.length()-1] % password.length() = [0,target-1]
for(int i = target; i < target + password.length(); i++)
res.append(password.charAt(i % password.length()));
return res.toString();
}
}
- 时间复杂度 O(N) : 线性遍历
password
并添加,使用线性时间。 - 空间复杂度 O(N): 新建的辅助
res
使用 O(N) 大小的额外空间。
不用 StringBuilder,直接用 String 的话
class Solution {
public String dynamicPassword(String password, int target) {
String res = "";
for(int i = target; i < target + password.length(); i++)
res += password.charAt(i % password.length());
return res;
}
}
- 时间复杂度 O(N): 线性遍历
password
并添加,使用线性时间。 - 空间复杂度 O(N): 假设循环过程中内存会被及时回收,内存中至少同时存在长度为 N 和 N-1 的两个字符串(新建长度为 N 的
res
需要使用前一个长度 N-1 的res
),因此至少使用 O(N) 的额外空间。