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 ...
随机推荐
- [剑指offer]25.合并两个排序的链表(迭代+递归)
25.合并两个排序的链表 题目 输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的. 示例1: 输入:1->2->4, 1->3->4 输出:1-> ...
- SpringBoot(三)SpringBoot自动配置
我们都知道SpringBoot帮助我们集成了许多组件和配置,那么SpringBoot是如何集成这些配置并在启动是自动进行配置呢.说到这就不得又需要回过头来看一下@SpringBootApplicati ...
- 加油站问题 Gas Station
2019-06-01 17:09:30 问题描述: 问题求解: 其实本题本质上是一个数学题. [定理] 对于一个循环数组,如果这个数组整体和 SUM >= 0,那么必然可以在数组中找到这么一个元 ...
- linggle使用技巧
Linggle 搜索引擎是一个可用于英语写作的语法.句子工具,可帮助学习者分析更准确的英文写作建议,能够根据词性来推测短句和句子,可精准的分享出完整英文句子如何撰写. Linggle 是台湾学术团队研 ...
- html之样式
HTML 样式 1. font字体 font-family 字体样式 比如:微软雅黑.Serif 字体.Sans-serif 字体.Monospace 字体.Cursive 字体.Fantasy 字体 ...
- Tensorboard 详解(上篇)
花间提壶华小厨 1. Tensorboard简介 对大部分人而言,深度神经网络就像一个黑盒子,其内部的组织.结构.以及其训练过程很难理清楚,这给深度神经网络原理的理解和工程化带来了很大的挑战.为了解决 ...
- PHP7内核(八):深入理解字符串的实现
在前面大致预览了常用变量的结构之后,我们今天来仔细的剖析一下字符串的具体实现. 一.字符串的结构 struct _zend_string { zend_refcounted_h gc; /* 字符串类 ...
- CodeForces - 1006F (深搜从两端向中间搜,省时)
题意:输入n,m,k,给出一个n*m的图,里面有权值,从1,1一路异或到n,m,只能向右或向下走,等于k有多少种路径. 思路:一开始就是直接暴力写个深搜,稳稳的超时,分析一下时间复杂度.每个点有两个方 ...
- python文件封装成*.exe
python文件封装成*.exe文件(单文件和多文件) 环境:win10 64位 python3.7 原文: https://www.cnblogs.com/jackzz/p/9431923.html ...
- SpringBoot常见注解的解释
@Component 这个注解类似SSM中的Controller和Service注解 ,将加了这个注解的类装配到Sping容器内,这样就可以在其他类用@Autowired注解实现依赖注入. @Conf ...