4ms, C, O(n), Easy to understand


  • 0
    S
       bool isPalindrome(char* s) {
        	int i;
        	int str_len;
        	const char* p_head;
        	const char* p_tail;
        	int head_lower_case_value;
        	int tail_lower_case_value;
        
        	/* Get string length */
        	for (i = 0; s[i] != '\0'; i++){}
        	str_len = i;
        
            /* Return true for NULL string */
        	if (str_len == 0){ return true; }
        
        	/* init p_head, p_tail */
        	p_head = &s[0];
        	p_tail = &s[str_len - 1];
        
        	while (1)
        	{
        		/* Get a valid p_head and its lowercase value */
        		while (p_head < p_tail)
        		{
        			if ('A' <= *p_head && *p_head <= 'Z')
        			{
        				head_lower_case_value = *p_head + 32;
        				break;
        			}
        			else if ('a' <= *p_head && *p_head <= 'z')
        			{
        				head_lower_case_value = *p_head;
        				break;
        			}
        		    else if ('0' <= *p_head && *p_head <= '9')
        			{
        				head_lower_case_value = *p_head;
        				break;
        			}
        			else
        			{
        				p_head++;
        			}
        		}
        
        		/* Get a valid p_tail and its lowercase value */
        		while (p_tail > p_head)
        		{
        			if ('A' <= *p_tail && *p_tail <= 'Z')
        			{
        				tail_lower_case_value = *p_tail + 32;
        				break;
        			}
        			else if ('a' <= *p_tail && *p_tail <= 'z')
        			{
        				tail_lower_case_value = *p_tail;
        				break;
        			}
        			else if ('0' <= *p_tail && *p_tail <= '9')
        			{
        				tail_lower_case_value = *p_tail;
        				break;
        			}
        			else
        			{
        				p_tail--;
        			}
        		}
        		
        		/* Return true if p_head exceeds p_tail*/
        		if (p_head >= p_tail)
        		{ 
        		    return true; 
        		}
        		
        		/* Compare head value and tail value */
        		if (head_lower_case_value == tail_lower_case_value)
        		{
        			p_head++;
        			p_tail--;
        		}
        		else
        		{
        			return false;
        		}
        
        	}
           
        }

Log in to reply
 

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