4ms solution C


  • 1
    S
    char* reverseVowels(char* s) {
    //"aeiouAEIOU" counts
    int len = 0;
    int i = 0;
    char temp =0;
    // record the index  when "aeiouAEIOU" appear
    int* array = malloc( strlen(s)*sizeof(int) );
    
    for( i= 0; *(s+i) !=0 ; ++i) {
    		switch( *(s+i) ) {
    			case 'a':
    			case 'e':
    			case 'i':
    			case 'o':
    			case 'A':
    			case 'E':
    			case 'I':
    			case 'O':
    			case 'U':
    			case 'u':array[len++]=i;
    				break;
    		}
    }
    // only 1 or none  "aeiouAEIOU" 
    if( len < 2 )
    	return s;
    len--;
    //reverse the "aeiouAEIOU"  index of  the string
    for( i = 0 ; i<=len/2 ; ++i) {
    	temp = *(s+array[i]);
    	*(s+array[i])= *(s+array[len-i]);
    	*(s+array[len-i]) = temp;
    }
    
    free(array);  
    array=NULL;
    return s;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.