【 关关的刷题日记50】 Leetcode 345. Reverse Vowels of a String

时间:2022-05-08
本文章向大家介绍【 关关的刷题日记50】 Leetcode 345. Reverse Vowels of a String,主要内容包括题目、思路、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

关关的刷题日记50 – 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"..

题目的意思是给定一个字符串,要求翻转它的元音字母。

思路

思路:设置头尾双重指针,从两头往中间查找元音字母,遇到元音字母进行字符交换。

class Solution {
public:
    string reverseVowels(string s) {
        if(s.empty() || s.size()==1)
            return s;
        for(int i=0, j=s.size()-1; i<j;)
        {
            while(!(tolower(s[i])=='a' || tolower(s[i])=='e' || tolower(s[i])=='i' || tolower(s[i])=='o' || tolower(s[i])=='u'))
                i++;
            while(!(tolower(s[j])=='a' || tolower(s[j])=='e' || tolower(s[j])=='i' || tolower(s[j])=='o' || tolower(s[j])=='u'))
                j--;
            if(i<j)
            {
                swap(s[i],s[j]);
                i++;
                j--;
            }
        }
        return s;
    }
};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。