成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 917. Reverse Only Letters

superw / 2906人閱讀

Problem

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 <= 100
33 <= S[i].ASCIIcode <= 122 
S doesn"t contain  or "
Solution
class Solution {
    public String reverseOnlyLetters(String S) {
        char[] str = S.toCharArray();
        int i = 0, j = str.length-1;
        while (i < j) {
            while (i < j && !Character.isLetter(str[i])) i++;
            while (i < j && !Character.isLetter(str[j])) j--;
            swap(str, i++, j--);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        return sb.toString();
    }
    private void swap(char[] str, int i, int j) {
        char temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/72095.html

相關(guān)文章

  • Leetcode PHP題解--D63 917. Reverse Only Letters

    摘要:題目鏈接題目分析給定一個(gè)包含符號(hào)的字符串,僅倒轉(zhuǎn)字母的出現(xiàn)順序,不改變符號(hào)的出現(xiàn)位置。思路先把字符串分成字母和符號(hào)兩部分,保留下標(biāo)。抽離字母數(shù)組的鍵和值,對(duì)值部分進(jìn)行倒轉(zhuǎn),合并到鍵數(shù)組中。最終代碼若覺得本文章對(duì)你有用,歡迎用愛發(fā)電資助。 D63 917. Reverse Only Letters 題目鏈接 917. Reverse Only Letters 題目分析 給定一個(gè)包含符號(hào)的...

    binaryTree 評(píng)論0 收藏0
  • LeetCode[316] Remove Duplicate Letters

    摘要:復(fù)雜度思路用一個(gè)每次考慮當(dāng)前的字符大小和的頂端字符的大小,如果當(dāng)前字符比較小的話,則可以出頂端的字符,將當(dāng)前的字符放進(jìn)中。需要維持了一個(gè)判斷當(dāng)前字符在剩余字符串中的出現(xiàn)次數(shù),考慮能否將這個(gè)字符從棧中彈出。 LeetCode[316] Remove Duplicate Letters Given a string which contains only lowercase letter...

    tomorrowwu 評(píng)論0 收藏0
  • [LeetCode]Remove Duplicate Letters

    摘要:首先要求就是得保證在原來的字符串中存在當(dāng)前字符的順序,即要保證每次的字符的次數(shù)大于。讀字符的過程中,把字符存到里,當(dāng)發(fā)現(xiàn)之前存的字符中比當(dāng)前字符大而且頻率還大于就可以把那個(gè)字符出去?;舅枷刖褪窃谝欢ǖ南拗茥l件下出比當(dāng)前選擇差的元素。 Remove Duplicate Letters Given a string which contains only lowercase lette...

    gaosboy 評(píng)論0 收藏0
  • 翻轉(zhuǎn)字符串的相關(guān)題目

    摘要:一題目描述空格分隔,逐個(gè)反轉(zhuǎn)二題目描述三題目描述當(dāng)然也可以用的做,不過用雙指針更快。 LeetCode: 557. Reverse Words in a String III 一、LeetCode: 557. Reverse Words in a String III 題目描述 Given a string, you need to reverse the order of chara...

    lykops 評(píng)論0 收藏0
  • [LeetCode] 482. License Key Formatting

    Problem You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes. Given a number K, we would wan...

    codercao 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<