[leetcode-917-Reverse Only Letters]

Posted hellowOOOrld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode-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:

  1. S.length <= 100
  2. 33 <= S[i].ASCIIcode <= 122 
  3. S doesn‘t contain  or "

思路:

两个标记,分别从前和从后往中间方向遍历,遇到字母交换,否则游标继续往下走。

bool isLetter(char ch)
{
    return ((ch >= a && ch <= z) || (ch >= A && ch <= Z));
}
string reverseOnlyLetters(string S)
{
    string ret = S;
    int n = S.length();
    int start = 0;
    int end = n - 1;
    while(start <= n-1 && end >= 0)
    {
        if(!isLetter(S[end])){end--;continue;}
        if(!isLetter(ret[start])){start++;continue;}
        ret[start++] = S[end--];
    } 
    return ret;       
}

 

以上是关于[leetcode-917-Reverse Only Letters]的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode-917 Reverse Only Letters(仅仅反转字母)

LeetCode 917. Reverse Only Letters (仅仅反转字母)

NOS列表(picos/SnapRoute/ONL)

白盒交换机NOS列表(picos/SnapRoute/ONL)

temp

Postgres 没有表现