leetCode345.ReverseVowelsofaString字符串

345. Reverse Vowels of a String

創(chuàng)新互聯(lián)是專業(yè)的湘東網(wǎng)站建設(shè)公司,湘東接單;提供網(wǎng)站設(shè)計(jì)、做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行湘東網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

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

文章名稱:leetCode345.ReverseVowelsofaString字符串
文章出自:http://muchs.cn/article44/ijohee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、Google、手機(jī)網(wǎng)站建設(shè)、企業(yè)建站、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)