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 ...
随机推荐
- pytorch RNN层api的几个参数说明
classtorch.nn.RNN(*args, **kwargs) input_size – The number of expected features in the input x hidde ...
- quagga/zebra - 交叉编译(cross)和本地编译(native)
https://github.com/dramalife/note.git AUTHOR : Dramalife@live.com Init : 2020.03.19 Update : source ...
- 关于CORS(跨域资源共享)的几个http请求头小实验
对几种与跨域相关的请求头做一个总结 关于跨域可以看:9 种常见的前端跨域解决方案(详解) 看完后可以配合我的代码做些实验,看看注释掉某个响应头会发生什么,整体代码会在最后贴出 跨域简单请求 需要在服务 ...
- Web_Servlet—— Servlet生命周期
第4章 Servlet生命周期(重要) 4.1 Servlet生命周期概述 1,应用程序中的对象不仅在空间上有层次结构的关系,在时间上也会因为处于程序运行过程中的不同阶段而表现出不同的状态和不同的行为 ...
- android 练习效果(界面一)
- HTML中的IE条件注释,让低版本IE也能正常运行HTML5+CSS3网站的3种解决方案
最近的项目中,因为需要兼容IE7,IE8,IE9,解研究了IE的条件注释,顺手写下来备忘. HTML中的IE条件注释 IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解. ...
- 初识socket之TCP协议
TCP服务端.客户端(基础版本) # 这是服务端import socket server = socket.socket() # 买手机server.bind(('127.0.0.1', 8080 ...
- hdu1541树状数组(降维打击)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1541/ 题意是:在二维图上有一系列坐标,其中坐标给出的顺序是:按照y升序排序,如果y值相同则按照x升序排序.这个 ...
- Mac下 eclipse target runtime com.genuitec.runtime 解决方法
Mac下 eclipse target runtime com.genuitec.runtime 解决方法 解决步骤如下: 首先是找到工程项目一个名叫.settings的文件夹,里面有个叫 org.e ...
- Nuget多项目批量打包上传服务器的简明教程
本篇不会介绍Nuget是什么,如何打包上传Nuget包,怎么搭建私有Nuget服务器.这些问题园子里都有相应的文章分享,这里不做过多阐述.另外本文假设你已经下载了Nuget.exe,并且已经设置好了环 ...