0
点赞
收藏
分享

微信扫一扫

LeetCode Algorithm 2000. 反转单词前缀

​​2000. 反转单词前缀​​

Ideas

Python可以直接通过index定位ch的索引,然后前半部分通过切片和[::-1]进行翻转,后半部分通过切片直接拼接。

Code

Python

class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
return word if ch not in word else word[:word.index(ch) + 1][::-1] + word[word.index(ch) + 1:]


举报

相关推荐

0 条评论