1 题目:
某公司门禁密码使用动态口令技术。初始密码为字符串 password
,密码更新均遵循以下步骤:
- 设定一个正整数目标值
target
- 将
password
前target
个字符按原顺序移动至字符串末尾
请返回更新后的密码字符串。
2 参考 :
3 代码:
C++
class Solution {
public:
string reverseLeftWords(string s, int n) {
reverse(s.begin(), s.begin() + n);
reverse(s.begin() + n, s.end());
reverse(s.begin(), s.end());
return s;
}
};
Python
# 1 切片
class Solution:
def dynamicPassword(self, password: str, target: int) -> str:
return password[target:] + password[:target]
# 2 拼接
class Solution:
def dynamicPassword(self, password: str, target: int) -> str:
res = []
for i in range(target, len(password)):
res.append(password[i])
for i in range(target):
res.append(password[i])
return ''.join(res)
4 思路: