[LeetCode&Python] Problem 917. Reverse Only Letters
Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.
Example 1:
Input: "ab-cd"
Output: "dc-ba"
Example 2:
Input: "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
Example 3:
Input: "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"
Note:
S.length <= 10033 <= S[i].ASCIIcode <= 122Sdoesn't contain\or"
class Solution:
def reverseOnlyLetters(self, S):
"""
:type S: str
:rtype: str
""" n=len(S)
s=list(S)
i=0
j=n-1
while i<j:
while i<j and not s[i].isalpha():i+=1
while i<j and not s[j].isalpha():j-=1
s[i],s[j]=s[j],s[i]
i+=1
j-=1
return ''.join(s)
[LeetCode&Python] Problem 917. Reverse Only Letters的更多相关文章
- [LeetCode&Python] Problem 541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- [LeetCode&Python] Problem 557. Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- [LeetCode&Python] Problem 206. Reverse Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- 【Leetcode_easy】917. Reverse Only Letters
problem 917. Reverse Only Letters solution: class Solution { public: string reverseOnlyLetters(strin ...
- 【LeetCode】917. Reverse Only Letters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 单指针 双指针 日期 题目地址: https:/ ...
- [LeetCode] 917. Reverse Only Letters 只翻转字母
Given a string S, return the "reversed" string where all characters that are not a letter ...
- LeetCode 917 Reverse Only Letters 解题报告
题目要求 Given a string S, return the "reversed" string where all characters that are not a le ...
- #Leetcode# 917. Reverse Only Letters
https://leetcode.com/problems/reverse-only-letters/ Given a string S, return the "reversed" ...
- 【leetcode】917. Reverse Only Letters(双指针)
Given a string s, reverse the string according to the following rules: All the characters that are n ...
随机推荐
- Ubuntu 18 开机启动慢
1.通过指令分析 # sudo systemd-analyze blame 39.607s mysql.service 25.194s systemd-journal-flush.service 23 ...
- deque_01
A.头尾 添加/移除 A.1.deque::push_back(elemValue); A.2.deque::pop_back(); A.3.deque::push_front(elemValue); ...
- java中的值传递和引用传递用法详解
值传递:方法调用时,实际参数把它的值传递给对应的形式参数,方法执行中形式参数值的改变不影响实际参 数的值. 引用传递:也称为传地址.方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方法中相对 ...
- angular5 组件之间监听传值变化
http://www.cnblogs.com/SLchuck/p/5904000.html https://i.cnblogs.com/EditPosts.aspx?postid=7995179&am ...
- English trip -- VC(情景课)2 A At school
xu言: Sometimes, I feel very confused. However, there will always be a weak light in a corner to ligh ...
- Confluence 6 导入 Active Directory 服务器证书 - UNIX
为了让你的应用服务器能够信任你的目录服务器.你目录服务器上导出的证书需要导入到你应用服务器的 Java 运行环境中.JDK 存储了信任的证书,这个存储信任证书的文件称为一个 keystore.默认的 ...
- 『cs231n』作业1选讲_通过代码理解KNN&交叉验证&SVM
通过K近邻算法探究numpy向量运算提速 茴香豆的“茴”字有... ... 使用三种计算图片距离的方式实现K近邻算法: 1.最为基础的双循环 2.利用numpy的broadca机制实现单循环 3.利用 ...
- $LANG、$NLS_LANG 记录一下
环境:linux $LANG 为linux termal终端环境下的 语言环境 $NLS_LANG 为oracle数据库中 会话中的语言环境. 个人理解,望大家补充
- 46. 47. Permutations
求全排列. 1. 无重复元素 Given a collection of distinct numbers, return all possible permutations. For example ...
- en_o out1
1● o əʊ ɒ ə ɔː ʌ ʊ: 2● oor ʊə ɔː 3● oo u u: 4● or ɜː ə ɔː 5● oar ore ɔː 6● ow əʊ aʊ ...