动态口令【leetcode】

本题选自leetcode图解算法数据结构一书

某公司门禁密码使用动态口令技术。初始密码为字符串 password,密码更新均遵循以下步骤:

设定一个正整数目标值 target

将 password 前 target 个字符按原顺序移动至字符串末尾

请返回更新后的密码字符串。

示例 1:

输入: password = "s3cur1tyC0d3", target = 4

输出: "r1tyC0d3s3cu"

示例 2:

输入: password = "lrloseumgh", target = 6

输出: "umghlrlose"

提示:

1 <= target < password.length <= 10000

法一:利用str的+

python 复制代码
class Solution(object):
    def dynamicPassword(self, password, target):
        """
        :type password: str
        :type target: int
        :rtype: str
        """
        return password[target:]+password[:target]

法二:变成列表直接切片,简单易懂,本来想写循环,感觉麻烦了

python 复制代码
class Solution(object):
    def dynamicPassword(self, password, target):
        """
        :type password: str
        :type target: int
        :rtype: str
        """
        p=list(password)  #字符串无法直接使用append,变为列表再用
        p.append(password[0:target]) #切片,找出需要移动的部分,注意[]的区间是左闭右开
        return "".join(p[target:len(p)+target]) #切片输出,从下标target开始,到右边结束,注意边界右边是开区间,所以取不到
相关推荐
爱上语文3 小时前
Java LeetCode每日一题
java·开发语言·leetcode
大二转专业8 小时前
408算法题leetcode--第24天
考研·算法·leetcode
__AtYou__14 小时前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解
转调15 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
huanxiangcoco16 小时前
152. 乘积最大子数组
python·leetcode
希望有朝一日能如愿以偿18 小时前
力扣题解(飞机座位分配概率)
算法·leetcode·职场和发展
Espresso Macchiato18 小时前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
数据分析螺丝钉19 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
￴ㅤ￴￴ㅤ9527超级帅19 小时前
LeetCode hot100---数组及矩阵专题(C++语言)
c++·leetcode·矩阵
鱼跃鹰飞20 小时前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试