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 ...
随机推荐
- C语言之歌词解析
0x00 脚下的路 不知道为啥要写这个小标题,可能是年轻的心想体验一下苍老的感觉,抑或是少年的一阵迷茫.混沌的四年,终究还是入了这一行.从初时的不知,到现在的刚开始,中间的间隔竟是四年之久,想起了陈奕 ...
- npm git 常用命令行 记录
1. 推出node命令行: 两次ctrl+C或者一次ctrl+D 退出终端:exit; 2.npm 常用 npm install <name> 安装包 npm install &l ...
- NLPer,你知道最近很火的自然语言处理库么?
介绍 "NLP's ImageNet moment has arrived." 想象一下我们有能力构建支持谷歌翻译的自然语言处理(NLP)模型,并且在Python中仅需几行代码来完 ...
- lvs + keepalived + gninx 高性能负载均衡
1,nginx 作为负载均衡器,在应用层实现了负载均衡和反向代理,在分布式集群中,能够有效的去处理大数据量,高访问的应用.但是,如果nginx 服务挂了怎么办? 为此,可以实现两台nginx或者多台n ...
- 《java编程思想》操作符
1. 自动递增和递减 递增和递减运算是两种相当不错的快捷运算,递减操作符是 "--",意为减少一个单位,递增操作符是 "++",意为增加一个单位.这两个操作符各 ...
- 403 Invalid CORS request 跨域问题解决
这里使用springMVC自带的CORS解决跨域问题 什么是跨域问题 1.请求地址与当前地址不相同 2.端口号不相同 技术有限端口号不同还未发现 3.二级域名不相同 出现这种问题如何解决有很多种方法, ...
- POI2014 FAR-FarmCraft 树形DP+贪心
题目链接 https://www.luogu.org/problem/P3574 题意 翻译其实已经很明确了 分析 这题一眼就是贪心啊,但贪心的方法要思索一下,首先是考虑先走时间多的子树,但不太现实, ...
- 原来rollup这么简单之 tree shaking篇
大家好,我是小雨小雨,致力于分享有趣的.实用的技术文章. 内容分为翻译和原创,如果有问题,欢迎随时评论或私信,希望和大家一起进步. 分享不易,希望能够得到大家的支持和关注. 计划 rollup系列打算 ...
- Java Web项目bug经验202002112049
运行程序后,如果配置有问题,可能不会进代码,而直接报错.
- STL(六)——map、multimap
STL--map.multimap 文章目录 STL--map.multimap 关联容器与map的介绍 map与set的异同 map与multimap的异同 map类对象的构造 map添加元素 ma ...