本文主要是介绍CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Eliminate all ‘b’ and ‘ac’ in an array of characters, you have to replace them in-place, and you are only allowed to iterate over the char array once.
Examples:
abc -> ac
ac->''
rbact->rt
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The key point is in-place and iteration once.
int eliminate( char* p)
{int deleted = 0;if (! p )return deleted;while (*p){if (*p == 'b')deleted++;else if ( ( *p == 'a' ) && ( *(p+1) == 'c')){deleted += 2;p++;} else if ( deleted > 0 )*(p-deleted) = *p;p++;}*(p-deleted) = '\0';return deleted;
}
这篇关于CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!