Runtime 4ms with C solution


  • 0
    N
    unsigned char isVowels(char c)
    {
    	switch(c)
    	{
    		case 'a':
    		case 'o':
    		case 'e':
    		case 'i':
    		case 'u':
    		case 'A':
    		case 'O':
    		case 'E':
    		case 'I':
    		case 'U':
    			return 1;
    		default:
    			return 0;
    	}
    	return 0;
    }
    
    void swap(char* c1,char* c2)
    {
    	char tmp = 0;
    	tmp = *c1;
    	*c1 = *c2;
    	*c2 = tmp;
    }
    
    char* reverseVowels(char* s) {
    	size_t i = 0, j = 0;
    
    	if (NULL == s)
    	{
    		return s;
    	}
    
    	for (i = 0, j = strlen(s); i != j; ++i)
    	{
    		if (isVowels(*(s+i)))
    		{
    			while(i != --j)
    			{
    				if (isVowels(*(s+j)))
    				{
    					swap(s+i,s+j);
    					break;
    				}
    			}
    			if (i == j)
    			{
    				break;
    			}
    		}
    	}
    	return s;
    }
    
    

Log in to reply
 

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