leetcode Perform String Shifts
Perform String Shifts
You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [direction, amount]:
direction can be 0 (for left shift) or 1 (for right shift).
amount is the amount by which string s is to be shifted.
A left shift by 1 means remove the first character of s and append it to the end.
Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.
Return the final string after all operations.
Example 1:
Input: s = "abc", shift = [[0,1],[1,2]]
Output: "cab"
Explanation:
[0,1] means shift to left by 1. "abc" -> "bca"
[1,2] means shift to right by 2. "bca" -> "cab"
Example 2:
Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]]
Output: "efgabcd"
Explanation:
[1,1] means shift to right by 1. "abcdefg" -> "gabcdef"
[1,1] means shift to right by 1. "gabcdef" -> "fgabcde"
[0,2] means shift to left by 2. "fgabcde" -> "abcdefg"
[1,3] means shift to right by 3. "abcdefg" -> "efgabcd"
Constraints:
1 <= s.length <= 100
s only contains lower case English letters.
1 <= shift.length <= 100
shift[i].length == 2
0 <= shift[i][0] <= 1
0 <= shift[i][1] <= 100
solution
这就是一个循环左移和右移的问题,循环左移时,把最左边一位拿出来,放在末尾即可,循环右移时把最后一位拿出来,放在开头即可
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
lst=list(s)
for i in range(len(shift)):
for j in range(shift[i][1]):
if shift[i][0] == 1:
tmp=lst[len(lst)-1]
lst.pop()
lst.insert(0,tmp)
else:
tmp=lst[0]
lst.remove(lst[0])
lst.append(tmp)
return "".join(lst)
分析:
时间复杂度:O(N);N为移动的次数
空间复杂度:O(1)
参考链接
leetcode Perform String Shifts的更多相关文章
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- [LeetCode] Rotate String 旋转字符串
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [LeetCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
随机推荐
- SOFARPC模式下的Consul注册中心
Consul大家不陌生,就是和Zookeeper.Nacos一伙的,能够作为微服务基础架构的注册中心,算是比较成熟的组件,和Springcloud集成顺滑, 考虑到Eureka已经停止更新,所以有必要 ...
- CMDB_Agent版本
目录 CMDB_Agent版本 CMDB概念 CMDB_Agent介绍 agent方案 ssh类方案 相比较 架构目录 bin-start.py 启动文件 conf-config.py 自定义配置文件 ...
- 第四周java实验
实验四 类与对象的定义及使用 实验时间 2018-9-20 1.实验目的与要求 (1) 理解用户自定义类的定义: 类是具有相同属性和行为的一组对象的集合.java中,用构造器构造并初始化对象. 类是构 ...
- windows10删除用户头像
点击开始菜单,然后这里我们点击最上方的用户,弹出的界面,点击这里的更改帐户设置,大家如图进行操作,点击这里即可. 这里我们通过浏览可以修改自己的账户头像,问题是怎么删除这里使用过的账户头像呢?这里 ...
- Spark在Windows环境下的配置
1.下载 下载地址:http://spark.apache.org/downloads.html. 选择下面版本下载. 2.操作流程:https://blog.csdn.net/nxw_tsp/art ...
- 图像的特征工程:HOG特征描述子的介绍
介绍 在机器学习算法的世界里,特征工程是非常重要的.实际上,作为一名数据科学家,这是我最喜欢的方面之一!从现有特征中设计新特征并改进模型的性能,这就是我们进行最多实验的地方. 世界上一些顶级数据科学家 ...
- C# Protobuf如何做到0分配内存的序列化
题目很简单, 就是IMessage对象怎么变成Byte[] 答案1: msg.ToByteArray() 这肯定不符合我们的要求 答案2: using var memoryStream = new M ...
- 在Centos7下搭建大数据环境,即Zookeeper+Hadoop+HBase
1. 所需软件下载链接(建议直接复制链接到迅雷下载更快): ①hadoop-2.7.6.tar.gz: wget http://mirrors.tuna.tsinghua.edu.cn/apache/ ...
- 不可被忽视的操作系统( FreeRTOS )【1】
把大多数人每个星期的双休过过成了奢侈的节假日放假,把每天23点后定义为自己的自由时间,应该如何去思考这个问题 ? 双休的两天里,不!是放假的两天里,终于有较长的时间好好的学习一下一直断断续续的Free ...
- 图解JVM内存区域划分
图解JVM类加载机制和双亲委派模型一文中讲述了 Java 类加载的过程,它包含加载.验证.准备.解析.初始化.使用.卸载这 7 个步骤.在准备阶段,JVM会将类加载到内存中,为类变量分配内存并赋予初值 ...