溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

leetCode 345. Reverse Vowels of a String 字符串

發(fā)布時(shí)間:2020-05-25 18:21:59 來(lái)源:網(wǎng)絡(luò) 閱讀:402 作者:313119992 欄目:編程語(yǔ)言

345. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

思路:

找到元音字母,標(biāo)記位置,進(jìn)行置換。


代碼如下:

// vowels(元音字母)包括:a,e,i,o,u. 

class Solution {
public:
    bool isVowels(char c)
    {
        char vowels[10] = {'a','e','i','o','u','A','E','I','O','U'};
        for(int i = 0; i < 10; i++)
        {
            if(c == vowels[i])
            {
                return true;
                break;//return 和 break在一起都有反應(yīng)么?
            }
        }
        return false;
    }
    string reverseVowels(string s) {
        vector<int> recordIndex;
        vector<char > str;
        for(int i = 0;i<s.size();i++)
        {
            str.push_back(s.at(i));
        }
        
        for(int i = 0; i < str.size() ; i++)
        {
            if( isVowels(str[i]) )
            {
                recordIndex.push_back(i);
            }
        }
        
        for(int i = 0 ; i < recordIndex.size() / 2 ; i++)
        {
            char left = str[recordIndex[i]];
            char right = str[recordIndex[recordIndex.size() - 1 - i]];
            str[recordIndex[i]] = right;
            str[recordIndex[recordIndex.size() - 1 - i]] = left;
        }
        s = "";
        
        for(int i = 0 ; i < str.size() ; i++)
        {
            s.append(1,str[i]);
        }

        return s;
    }
};

2016-08-08 10:52:10

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI