Reverse Vowels of a String

    xiaoxiao2021-03-25  186

    https://leetcode.com/problems/reverse-vowels-of-a-string/?tab=Description

    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".

    bool IsVowels(char ch) { return (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U'); } char* reverseVowels(char* s) { if(!s) return NULL; int len = strlen(s); char swapTmp; int head = 0, tail = len; while(head < tail) { while(head < tail && !IsVowels(s[head])) head++; while(head < tail && !IsVowels(s[tail])) tail--; swapTmp = s[head]; s[head] = s[tail]; s[tail] = swapTmp; head++; tail--; } return s; }
    转载请注明原文地址: https://ju.6miu.com/read-1481.html

    最新回复(0)