You are given a string s
of lowercase English letters and a 2D integer array shifts
where shifts[i] = [starti, endi, directioni]
.
(资料图片仅供参考)
For every i
, shift the characters in s
from the index starti
to the index endi
(inclusive) forward if directioni = 1
,
or shift the characters backward if directioni = 0
.
Shifting a character forward means replacing it with the next letter in the alphabet
(wrapping around so that 'z'
becomes 'a'
).
Similarly, shifting a character backward means replacing it with the previous letter in the alphabet (wrapping around so that 'a'
becomes 'z'
).
Return the final string after all such shifts to s
are applied.
Example 1:
Input: s = "abc", shifts = [[0,1,0],[1,2,1],[0,2,1]]
Output: "ace"
Explanation: Firstly, shift the characters from index 0 to index 1 backward.
Now s = "zac".Secondly, shift the characters from index 1 to index 2 forward.
Now s = "zbd".Finally, shift the characters from index 0 to index 2 forward. Now s = "ace".
Example 2:
Input: s = "dztz", shifts = [[0,0,0],[1,1,1]]
Output: "catz"
Explanation: Firstly, shift the characters from index 0 to index 0 backward.
Now s = "cztz".Finally, shift the characters from index 1 to index 1 forward.
Now s = "catz".
Constraints:
1 <= s.length, shifts.length <= 5 * 104
shifts[i].length == 3
0 <= starti <= endi < s.length
0 <= directioni <= 1
s
consists of lowercase English letters.
印象深刻的一道题啊,就是将更改from -to的位置记下来,向前则from++,to--(to--一直没想明白,--就是抵消前面++的次数),然后一个变量累加,类似于前缀和,然后去遍历,
中间出过一次问题,就是对数据%26之后还要加26再%26,就是避免溢出错误(还是别的负数的错误)我理解的是负数的错误。
Runtime: 10 ms, faster than 34.15% of Java online submissions for Shifting Letters II.
Memory Usage: 83.4 MB, less than 34.15% of Java online submissions for Shifting Letters II.
5月14日,郑州经开区第五大街一地铁口,一个核酸采样舱已布点就位,市民有序进行核酸采样。郑州经开区宣传部供图近日,河南省发布《河南省
稳就业保民生。由人力资源和社会保障部主办,河南省人力资源和社会保障厅承办的河南省2022年百日千万网络招聘专项行动将于5月16日启动,持
政策内容由河南省(郑州市)12320卫生健康热线整理(截至2022年5月15日上午9时)01进入郑州市市域外入(返)郑人员须查验两码一证,即健康
5月15日是全国公安机关5 15打击和防范经济犯罪宣传日,河南省公安厅公布一批经济犯罪案件,作案手法曝光。信阳宋某等人组织领导传销活动案2
给独居老人发放蔬菜包,骑着三轮车为居民送快递,把多余的蔬菜和可乐送给邻居……上海本轮疫情中,河南籍29岁的刘鹏程在小区主动当起了志愿
X 关闭
X 关闭