LeetCode笔记: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".

大意:

写一个函数,输入一个字符串然后翻转里面的元音字母。
例1:
给出 s = "hello",返回"holle"。
例2:
给出 s = "leetcode",返回"leotcede"。
注意:
元音不包括字母“y”。

思路:

首先想到的一个思路是遍历字符串中每个字母,遇到元音字母就记录下字母和所在的位置。遍历完后,对着记录下来的元音字母,将字符串中的元音按照反序替换一遍就好了,这种做法也做出来了,但是结果非常耗时,花了200多ms。
后来想到了第二种方法,在字符串的头和尾都放一个指针进行遍历,两端向中间去遍历,当两端都遇到元音字母后,就对换。直到两个指针碰头为止。这个方法就快多了,同时优化一下检查是否是元音字母的方法,只需要几ms就搞定了。
需要注意的是题目中并没有说字符串是纯大写或者小写,所以大小写都要考虑,这个容易忽略。

代码1(Java):

public class Solution {
    public String reverseVowels(String s) {
        int[] vowelIndex = new int[s.length()];
        char[] vowelChar = new char[s.length()];
        int index = 0;// 标记上面两个数组记录的位置
        // 记录元音字母及出现的位置
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u' || s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O' || s.charAt(i) == 'U') {
                vowelChar[index] = s.charAt(i);
                vowelIndex[index] = i;
                index++;
            }
        }
        // 替换元音字母位置
        if (index == 0) return s;
        else {
            StringBuffer buffer = new StringBuffer(s);
            for (int i = 0; i < index; i++) {
                buffer.replace(vowelIndex[i], vowelIndex[i]+1, String.valueOf(vowelChar[index-i-1]));
            }
            return buffer.toString();
        }
    }
}

代码2(Java):

public class Solution {
    static final String vowels = "aeiouAEIOU";
    public String reverseVowels(String s) {
        int first = 0, last = s.length() - 1;
        char[] array = s.toCharArray();
        while(first < last){
            // 正向找元音
            while(first < last && vowels.indexOf(array[first]) == -1){
                first++;
            }
            // 逆向找元音
            while(first < last && vowels.indexOf(array[last]) == -1){
                last--;
            }
            // 对换
            char temp = array[first];
            array[first] = array[last];
            array[last] = temp;
            first++;
            last--;
        }
        return new String(array);
    }
}

代码(C++)

class Solution {
public:
    string reverseVowels(string s) {
        vector<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        int len = s.length();
        bool i_found = false;
        bool j_found = false;
        for (int i = 0, j = 1; i + j < len;) {
            if (find(vowels.begin(), vowels.end(), s[i]) == vowels.end()) {
                i++;
                continue;
            } else {
                i_found = true;
                if (j_found) {
                    char temp = s[i];
                    s[i] = s[len-j];
                    s[len-j] = temp;
                    i_found = false;
                    j_found = false;
                    i++;
                    j++;
                    continue;
                }
            }
            if (find(vowels.begin(), vowels.end(), s[len-j]) == vowels.end()) {
                j++;
                continue;
            } else {
                j_found = true;
                if (i_found) {
                    char temp = s[i];
                    s[i] = s[len-j];
                    s[len-j] = temp;
                    i_found = false;
                    j_found = false;
                    i++;
                    j++;
                    continue;
                }
            }
        }
        return s;
    }
};

合集:https://github.com/Cloudox/LeetCode-Record


查看作者首页

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容