快资讯:Leetcode 2381. Shifting Letters II

来源:哔哩哔哩 时间:2023-03-17 12:06:19

You are given a string sof lowercase English letters and a 2D integer array shiftswhere shifts[i] = [starti, endi, directioni]


(资料图片仅供参考)

For every i, shift the characters in sfrom the index startito 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 sare 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

sconsists 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.

X 关闭

快资讯:Leetcode 2381. Shifting Letters II

YouaregivenastringsoflowercaseEnglishlettersanda2Dintegerarrayshiftswhereshifts[i]=[starti,endi,directioni] Foreveryi,shiftthecharact

2023-03-17

学校应对心理危机的培训和演练方案_应对心理危机时 最常见的资源类型有哪些A精神资源B社群资源C

1、应对心理危机时。2、最常见的资源类型有(ABCD)  A精神资源   B社群资源   C内在资源   D外在资I源选ABCD 即:应对心理危机时。3、最常见

2023-03-17

紫金银行03月16日获沪股通增持352.72万股_天天视讯

03月16日,紫金银行获沪股通增持352 72万股,最新持股量为1301 66万股,占公司A股总股本的0 36%。

2023-03-17

12年义务教育什么时候开始?

1、南岸区的12年义务教育今年秋季开始,正好我同事的女儿今年初中毕业,但是她没遇上,她妈妈让她重读一个初三。2、我同事因为操心女儿读书的

2023-03-17

全球动态:2023年3月16日江苏省工业白油价格最新行情预测

据中国报告大厅对2023年3月16日江苏省工业白油价格最新走势监测显示:2023年3月16日江苏省工业白油(密度:0 8

2023-03-16

全球微头条丨点赞!关键时刻 他们合力救起落水男子

点赞!关键时刻他们合力救起落水男子

2023-03-16

集线器是什么

1、集线器是指在同一物理介质下连接多根双绞线以太网线或光纤的设备。集线器是在OSI模型中运行的物理层。可以看作是多端口中

2023-03-16

想让孩子长得更高,家长需要知道这些 全球今亮点

除去先天因素及基因突变、疾病造成的影响,营养、睡眠、运动等后天因素也对身高起着至关重要的作用。专家提醒,家长们应该重视这些后天因素对

2023-03-16

今头条!晶雪节能董秘回复:为保证投资者获得信息的公平性,公司会在定期报告中披露股东户数

晶雪节能(301010)03月16日在投资者关系平台上答复了投资者关心的问题。

2023-03-16

比比例除法的区别和联系_比比例

1、比例的基本性质:在比例里,两个外项的积等于两个内项的积解比例的基本性质:根据比例的基本性质,如果已知比例中的任何三个

2023-03-16

Copyright ?  2015-2022 海峡科技网版权所有  备案号:皖ICP备2022009963号-10   联系邮箱:396 029 142 @qq.com